Geklust aan Client en toebehoren, belangrijk: activiteit van global router ook ergens controleren!

This commit is contained in:
2011-06-10 18:44:03 +00:00
parent c092e737b0
commit 0d5b999cc6
10 changed files with 119 additions and 49 deletions

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

View File

@@ -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);
}
} }
} }

View File

@@ -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);
} }

View File

@@ -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;

View File

@@ -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();
}
} }
} }

View File

@@ -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();

View File

@@ -4,55 +4,96 @@ 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 {
socket = new Socket(ip, port); client = new Client(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);
} catch (IOException e) { } catch (IOException e) {
log.error(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) { public void event(Event event) {
try { try {
objectOutputStream.writeObject(event); client.send(event);
} catch (IOException e) { } catch (IOException e) {
log.error(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);
}
}
} }

View File

@@ -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) {

View 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);
}
}