Geklust aan Client en toebehoren, belangrijk: activiteit van global router ook ergens controleren!
This commit is contained in:
BIN
java/resource/M.png
Normal file
BIN
java/resource/M.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 9.3 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 3.2 MiB |
@@ -10,13 +10,14 @@ import mimis.device.wiimote.WiimoteDevice;
|
|||||||
import mimis.event.EventRouter;
|
import mimis.event.EventRouter;
|
||||||
import mimis.event.router.GlobalRouter;
|
import mimis.event.router.GlobalRouter;
|
||||||
import mimis.exception.event.router.GlobalRouterException;
|
import mimis.exception.event.router.GlobalRouterException;
|
||||||
|
import mimis.exception.worker.ActivateException;
|
||||||
|
import mimis.util.swing.Dialog;
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
|
||||||
public class Client {
|
public class Client {
|
||||||
protected Log log = LogFactory.getLog(getClass());
|
protected static Log log = LogFactory.getLog(Client.class);
|
||||||
|
|
||||||
public static final String IP = "127.0.0.1";
|
public static final String IP = "127.0.0.1";
|
||||||
public static final int PORT = 6789;
|
public static final int PORT = 6789;
|
||||||
@@ -43,10 +44,20 @@ public class Client {
|
|||||||
public void start() {
|
public void start() {
|
||||||
log.debug("Client");
|
log.debug("Client");
|
||||||
Mimis mimis = new Mimis(eventRouter, deviceArray);
|
Mimis mimis = new Mimis(eventRouter, deviceArray);
|
||||||
mimis.start();
|
try {
|
||||||
|
mimis.activate();
|
||||||
|
} catch (ActivateException e) {
|
||||||
|
log.fatal(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Main().start();
|
try {
|
||||||
|
String ip = Dialog.question("Server IP:", IP);
|
||||||
|
int port = Integer.valueOf(Dialog.question("Server Port:", PORT));
|
||||||
|
new Client(ip, port).start();
|
||||||
|
} catch (GlobalRouterException e) {
|
||||||
|
log.fatal(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ public class GUI extends JFrame {
|
|||||||
public GUI(Mimis mimis, Manager<Application> applicationManager, Manager<Device> deviceManager) {
|
public GUI(Mimis mimis, Manager<Application> applicationManager, Manager<Device> deviceManager) {
|
||||||
super(TITLE);
|
super(TITLE);
|
||||||
this.mimis = mimis;
|
this.mimis = mimis;
|
||||||
setIconImage(Swing.getImage("kop.png"));
|
setIconImage(Swing.getImage(Mimis.ICON));
|
||||||
createFrame(applicationManager, deviceManager);
|
createFrame(applicationManager, deviceManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import org.apache.commons.logging.LogFactory;
|
|||||||
|
|
||||||
public class Mimis extends EventHandler {
|
public class Mimis extends EventHandler {
|
||||||
protected Log log = LogFactory.getLog(getClass());
|
protected Log log = LogFactory.getLog(getClass());
|
||||||
|
public static final String ICON = "M.png";
|
||||||
|
|
||||||
protected ArrayCycle<Application> applicationCycle;
|
protected ArrayCycle<Application> applicationCycle;
|
||||||
protected Device[] deviceArray;
|
protected Device[] deviceArray;
|
||||||
|
|||||||
@@ -73,8 +73,10 @@ public class NetworkDevice extends Device {
|
|||||||
Socket socket = serverSocket.accept();
|
Socket socket = serverSocket.accept();
|
||||||
Client client = new Client(socket);
|
Client client = new Client(socket);
|
||||||
client.start();
|
client.start();
|
||||||
log.trace("Client connected");
|
log.debug("Client connected");
|
||||||
} catch (IOException e) {}
|
} catch (IOException e) {
|
||||||
|
log.error(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void stop() throws DeactivateException {
|
public void stop() throws DeactivateException {
|
||||||
@@ -116,7 +118,9 @@ public class NetworkDevice extends Device {
|
|||||||
|
|
||||||
public void stop() {
|
public void stop() {
|
||||||
try {
|
try {
|
||||||
disconnect();
|
objectInputStream.close();
|
||||||
|
objectOutputStream.close();
|
||||||
|
socket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e);
|
log.error(e);
|
||||||
} finally {
|
} finally {
|
||||||
@@ -127,11 +131,5 @@ public class NetworkDevice extends Device {
|
|||||||
public void send(Object object) throws IOException {
|
public void send(Object object) throws IOException {
|
||||||
objectOutputStream.writeObject(object);
|
objectOutputStream.writeObject(object);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void disconnect() throws IOException {
|
|
||||||
objectInputStream.close();
|
|
||||||
objectOutputStream.close();
|
|
||||||
socket.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import javax.swing.JFrame;
|
|||||||
import javax.swing.JPanel;
|
import javax.swing.JPanel;
|
||||||
import javax.swing.WindowConstants;
|
import javax.swing.WindowConstants;
|
||||||
|
|
||||||
|
import mimis.Mimis;
|
||||||
import mimis.exception.worker.DeactivateException;
|
import mimis.exception.worker.DeactivateException;
|
||||||
import mimis.util.Swing;
|
import mimis.util.Swing;
|
||||||
import mimis.util.swing.HoldButton;
|
import mimis.util.swing.HoldButton;
|
||||||
@@ -42,7 +43,7 @@ public class Panel extends JFrame implements HoldButtonListener {
|
|||||||
Panel(PanelDevice panelDevice) {
|
Panel(PanelDevice panelDevice) {
|
||||||
super(TITLE);
|
super(TITLE);
|
||||||
this.panelDevice = panelDevice;
|
this.panelDevice = panelDevice;
|
||||||
setIconImage(Swing.getImage("kop.png"));
|
setIconImage(Swing.getImage(Mimis.ICON));
|
||||||
createControls();
|
createControls();
|
||||||
layoutControls();
|
layoutControls();
|
||||||
pack();
|
pack();
|
||||||
|
|||||||
@@ -4,25 +4,64 @@ import java.io.IOException;
|
|||||||
import java.io.ObjectInputStream;
|
import java.io.ObjectInputStream;
|
||||||
import java.io.ObjectOutputStream;
|
import java.io.ObjectOutputStream;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
|
||||||
|
|
||||||
import mimis.Event;
|
import mimis.Event;
|
||||||
import mimis.Worker;
|
import mimis.Worker;
|
||||||
import mimis.event.EventRouter;
|
import mimis.event.EventRouter;
|
||||||
import mimis.event.Feedback;
|
import mimis.event.Feedback;
|
||||||
import mimis.exception.event.router.GlobalRouterException;
|
import mimis.exception.worker.ActivateException;
|
||||||
|
import mimis.exception.worker.DeactivateException;
|
||||||
|
|
||||||
public class GlobalRouter extends EventRouter {
|
public class GlobalRouter extends EventRouter {
|
||||||
protected Socket socket;
|
protected String ip;
|
||||||
protected ObjectOutputStream objectOutputStream;
|
protected int port;
|
||||||
protected ObjectInputStream objectInputStream;
|
protected Client client;
|
||||||
|
|
||||||
public GlobalRouter(String ip, int port) throws GlobalRouterException {
|
public GlobalRouter(String ip, int port) {
|
||||||
|
this.ip = ip;
|
||||||
|
this.port = port;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void activate() throws ActivateException {
|
||||||
try {
|
try {
|
||||||
|
client = new Client(ip, port);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(e);
|
||||||
|
throw new ActivateException();
|
||||||
|
}
|
||||||
|
super.activate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean active() {
|
||||||
|
if (active && client.active()) {
|
||||||
|
active = false;
|
||||||
|
}
|
||||||
|
return active;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void deactivate() throws DeactivateException {
|
||||||
|
client.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void event(Event event) {
|
||||||
|
try {
|
||||||
|
client.send(event);
|
||||||
|
} catch (IOException e) {
|
||||||
|
log.error(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Client extends Worker {
|
||||||
|
protected Socket socket;
|
||||||
|
protected ObjectInputStream objectInputStream;
|
||||||
|
protected ObjectOutputStream objectOutputStream;
|
||||||
|
|
||||||
|
public Client(String ip, int port) throws IOException {
|
||||||
socket = new Socket(ip, port);
|
socket = new Socket(ip, port);
|
||||||
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
|
|
||||||
objectInputStream = new ObjectInputStream(socket.getInputStream());
|
objectInputStream = new ObjectInputStream(socket.getInputStream());
|
||||||
new Worker() {
|
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||||
|
}
|
||||||
|
|
||||||
public void work() {
|
public void work() {
|
||||||
try {
|
try {
|
||||||
Object object;
|
Object object;
|
||||||
@@ -38,21 +77,23 @@ public class GlobalRouter extends EventRouter {
|
|||||||
log.error(e);
|
log.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
return;
|
public boolean active() {
|
||||||
} catch (UnknownHostException e) {
|
return active = socket.isConnected();
|
||||||
log.error(e);
|
|
||||||
} catch (IOException e) {
|
|
||||||
log.error(e);
|
|
||||||
}
|
|
||||||
throw new GlobalRouterException();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void event(Event event) {
|
public void stop() {
|
||||||
try {
|
try {
|
||||||
objectOutputStream.writeObject(event);
|
objectInputStream.close();
|
||||||
|
objectOutputStream.close();
|
||||||
|
socket.close();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
log.error(e);
|
log.error(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void send(Object object) throws IOException {
|
||||||
|
objectOutputStream.writeObject(object);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class Swing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Image getImage(String name) {
|
public static Image getImage(String name) {
|
||||||
return toolkit.getImage((getResource(name)));
|
return toolkit.getImage(getResource(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ImageIcon getImageIcon(String name) {
|
public static ImageIcon getImageIcon(String name) {
|
||||||
|
|||||||
18
java/src/mimis/util/swing/Dialog.java
Normal file
18
java/src/mimis/util/swing/Dialog.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package mimis.util.swing;
|
||||||
|
|
||||||
|
import javax.swing.JOptionPane;
|
||||||
|
|
||||||
|
public class Dialog {
|
||||||
|
public static final String TITLE = "MIMIS Dialog";
|
||||||
|
|
||||||
|
public static String question(String message, Object initial) {
|
||||||
|
return question(TITLE, message, initial);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String question(String title, String message, Object initial) {
|
||||||
|
return (String) JOptionPane.showInputDialog(
|
||||||
|
null, message, title,
|
||||||
|
JOptionPane.QUESTION_MESSAGE,
|
||||||
|
null, null, initial);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user