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.router.GlobalRouter;
|
||||
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.LogFactory;
|
||||
|
||||
|
||||
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 int PORT = 6789;
|
||||
@@ -43,10 +44,20 @@ public class Client {
|
||||
public void start() {
|
||||
log.debug("Client");
|
||||
Mimis mimis = new Mimis(eventRouter, deviceArray);
|
||||
mimis.start();
|
||||
try {
|
||||
mimis.activate();
|
||||
} catch (ActivateException e) {
|
||||
log.fatal(e);
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,14 +24,14 @@ public class GUI extends JFrame {
|
||||
protected static final String TITLE = "MIMIS Manager";
|
||||
protected static final String APPLICATION_TITLE = "Applications";
|
||||
protected static final String DEVICE_TITLE = "Devices";
|
||||
|
||||
|
||||
protected Mimis mimis;
|
||||
protected TextArea textArea;
|
||||
|
||||
public GUI(Mimis mimis, Manager<Application> applicationManager, Manager<Device> deviceManager) {
|
||||
super(TITLE);
|
||||
this.mimis = mimis;
|
||||
setIconImage(Swing.getImage("kop.png"));
|
||||
setIconImage(Swing.getImage(Mimis.ICON));
|
||||
createFrame(applicationManager, deviceManager);
|
||||
}
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ import org.apache.commons.logging.LogFactory;
|
||||
|
||||
public class Mimis extends EventHandler {
|
||||
protected Log log = LogFactory.getLog(getClass());
|
||||
public static final String ICON = "M.png";
|
||||
|
||||
protected ArrayCycle<Application> applicationCycle;
|
||||
protected Device[] deviceArray;
|
||||
|
||||
@@ -73,10 +73,12 @@ public class NetworkDevice extends Device {
|
||||
Socket socket = serverSocket.accept();
|
||||
Client client = new Client(socket);
|
||||
client.start();
|
||||
log.trace("Client connected");
|
||||
} catch (IOException e) {}
|
||||
log.debug("Client connected");
|
||||
} catch (IOException e) {
|
||||
log.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void stop() throws DeactivateException {
|
||||
super.stop();
|
||||
for (Client client : clientList) {
|
||||
@@ -116,7 +118,9 @@ public class NetworkDevice extends Device {
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
disconnect();
|
||||
objectInputStream.close();
|
||||
objectOutputStream.close();
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
log.error(e);
|
||||
} finally {
|
||||
@@ -127,11 +131,5 @@ public class NetworkDevice extends Device {
|
||||
public void send(Object object) throws IOException {
|
||||
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.WindowConstants;
|
||||
|
||||
import mimis.Mimis;
|
||||
import mimis.exception.worker.DeactivateException;
|
||||
import mimis.util.Swing;
|
||||
import mimis.util.swing.HoldButton;
|
||||
@@ -42,7 +43,7 @@ public class Panel extends JFrame implements HoldButtonListener {
|
||||
Panel(PanelDevice panelDevice) {
|
||||
super(TITLE);
|
||||
this.panelDevice = panelDevice;
|
||||
setIconImage(Swing.getImage("kop.png"));
|
||||
setIconImage(Swing.getImage(Mimis.ICON));
|
||||
createControls();
|
||||
layoutControls();
|
||||
pack();
|
||||
|
||||
@@ -4,55 +4,96 @@ import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.net.Socket;
|
||||
import java.net.UnknownHostException;
|
||||
|
||||
import mimis.Event;
|
||||
import mimis.Worker;
|
||||
import mimis.event.EventRouter;
|
||||
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 {
|
||||
protected Socket socket;
|
||||
protected ObjectOutputStream objectOutputStream;
|
||||
protected ObjectInputStream objectInputStream;
|
||||
protected String ip;
|
||||
protected int port;
|
||||
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 {
|
||||
socket = new Socket(ip, port);
|
||||
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
objectInputStream = new ObjectInputStream(socket.getInputStream());
|
||||
new Worker() {
|
||||
public void work() {
|
||||
try {
|
||||
Object object;
|
||||
do {
|
||||
object = objectInputStream.readObject();
|
||||
if (object instanceof Feedback) {
|
||||
add((Feedback) object);
|
||||
}
|
||||
} while (object != null);
|
||||
} catch (IOException e) {
|
||||
log.error(e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
return;
|
||||
} catch (UnknownHostException e) {
|
||||
log.error(e);
|
||||
client = new Client(ip, port);
|
||||
} catch (IOException e) {
|
||||
log.error(e);
|
||||
throw new ActivateException();
|
||||
}
|
||||
throw new GlobalRouterException();
|
||||
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 {
|
||||
objectOutputStream.writeObject(event);
|
||||
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);
|
||||
objectInputStream = new ObjectInputStream(socket.getInputStream());
|
||||
objectOutputStream = new ObjectOutputStream(socket.getOutputStream());
|
||||
}
|
||||
|
||||
public void work() {
|
||||
try {
|
||||
Object object;
|
||||
do {
|
||||
object = objectInputStream.readObject();
|
||||
if (object instanceof Feedback) {
|
||||
add((Feedback) object);
|
||||
}
|
||||
} while (object != null);
|
||||
} catch (IOException e) {
|
||||
log.error(e);
|
||||
} catch (ClassNotFoundException e) {
|
||||
log.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean active() {
|
||||
return active = socket.isConnected();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
objectInputStream.close();
|
||||
objectOutputStream.close();
|
||||
socket.close();
|
||||
} catch (IOException 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) {
|
||||
return toolkit.getImage((getResource(name)));
|
||||
return toolkit.getImage(getResource(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