Skip to content
Browse files

Refactored the input server code.

  • Loading branch information...
1 parent eff8f6c commit 7f7e965facd14e43254f9e5e3860a1832dcdcb81 @ushadow committed May 24, 2012
View
98 server/src/edu/mit/yingyin/websocket/HandInputListener.java
@@ -0,0 +1,98 @@
+package edu.mit.yingyin.websocket;
+
+import java.io.IOException;
+
+import org.OpenNI.ActiveHandEventArgs;
+import org.OpenNI.GestureRecognizedEventArgs;
+import org.OpenNI.IObservable;
+import org.OpenNI.IObserver;
+import org.OpenNI.Point3D;
+import org.OpenNI.StatusException;
+import org.eclipse.jetty.websocket.WebSocket.Connection;
+
+public class HandInputListener implements IInputListener {
+ private Connection connection;
+ private GestureObserver gestureRecogObserver = new GestureObserver();
+ private HandCreateObserver handCreateObserver = new HandCreateObserver();
+ private HandUpdateObserver handUpdateObserer = new HandUpdateObserver();
+ private HandTrackerController controller;
+
+ public HandInputListener(HandTrackerController controller) {
+ this.controller = controller;
+ }
+
+ public void startListening(Connection connection) {
+ this.connection = connection;
+ try {
+ controller.addGestureRecognizedEventObserver(gestureRecogObserver);
+ controller.addHandCreateEventObserver(handCreateObserver);
+ controller.addHandUpdateEventObserver(handUpdateObserer);
+ } catch (StatusException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ public void stopListening() {
+ controller.deleteGestureRecognizedEventObserver(gestureRecogObserver);
+ controller.deleteHandCreateEventObserver(handCreateObserver);
+ }
+
+ private class GestureObserver implements
+ IObserver<GestureRecognizedEventArgs> {
+
+ @Override
+ public void update(IObservable<GestureRecognizedEventArgs> observable,
+ GestureRecognizedEventArgs args) {
+ try {
+ connection.sendMessage("Gesture recognized:" + args.getGesture());
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private class HandCreateObserver implements
+ IObserver<ActiveHandEventArgs> {
+
+ @Override
+ public void update(IObservable<ActiveHandEventArgs> observable,
+ ActiveHandEventArgs args) {
+ Point3D pos = args.getPosition();
+ try {
+ pos = controller.convertRealWorldToProjective(pos);
+ String message = String.format("hand_created,%d,%d,%d",
+ (int)pos.getX(), (int)pos.getY(), (int)pos.getZ());
+ connection.sendMessage(message);
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ } catch (StatusException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private class HandUpdateObserver implements
+ IObserver<ActiveHandEventArgs> {
+
+ @Override
+ public void update(IObservable<ActiveHandEventArgs> observable,
+ ActiveHandEventArgs args) {
+
+ Point3D pos = args.getPosition();
+ try {
+ pos = controller.convertRealWorldToProjective(pos);
+ String message = String.format("hand_created,%d,%d,%d",
+ (int)pos.getX(), (int)pos.getY(), (int)pos.getZ());
+ connection.sendMessage(message);
+ } catch (IOException ioe) {
+ ioe.printStackTrace();
+ } catch (StatusException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ }
+}
View
49 server/src/edu/mit/yingyin/websocket/HandTrackerController.java
@@ -24,14 +24,24 @@
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
+import org.OpenNI.ActiveHandEventArgs;
+import org.OpenNI.GestureRecognizedEventArgs;
+import org.OpenNI.IObserver;
+import org.OpenNI.Point3D;
+import org.OpenNI.StatusException;
+
+/**
+ * A thread that controls the interaction with the <code>HandTracker</code>.
+ * @author yingyin
+ *
+ */
public class HandTrackerController extends Thread {
- private HandTracker tracker;
+ private HandTracker tracker = new HandTracker();
private boolean shouldRun = true;
private HandTrackerView view;
- public HandTrackerController (HandTracker tracker) {
- this.tracker = tracker;
+ public HandTrackerController () {
view = new HandTrackerView(tracker);
view.addKeyListener(new KeyListener() {
@Override
@@ -61,9 +71,38 @@ public void run() {
view.dispose();
}
+ public void addGestureRecognizedEventObserver(
+ IObserver<GestureRecognizedEventArgs> observer) throws StatusException {
+ tracker.addGestureRecognizedEventObserver(observer);
+ }
+
+ public void addHandCreateEventObserver(
+ IObserver<ActiveHandEventArgs> observer) throws StatusException {
+ tracker.addHandCreateEventObserver(observer);
+ }
+
+ public void addHandUpdateEventObserver(
+ IObserver<ActiveHandEventArgs> observer) throws StatusException {
+ tracker.addHandUpdateEventObserver(observer);
+ }
+
+ public void deleteGestureRecognizedEventObserver(
+ IObserver<GestureRecognizedEventArgs> observer) {
+ tracker.deleteGestureRecognizedEventObserver(observer);
+ }
+
+ public void deleteHandCreateEventObserver(
+ IObserver<ActiveHandEventArgs> observer) {
+ tracker.deleteHandCreateEventObserver(observer);
+ }
+
+ public Point3D convertRealWorldToProjective(Point3D p)
+ throws StatusException {
+ return tracker.convertRealWorldToProjective(p);
+ }
+
public static void main(String s[]) {
- HandTracker tracker = new HandTracker();
- HandTrackerController app = new HandTrackerController(tracker);
+ HandTrackerController app = new HandTrackerController();
app.run();
}
}
View
8 server/src/edu/mit/yingyin/websocket/IInputListener.java
@@ -0,0 +1,8 @@
+package edu.mit.yingyin.websocket;
+
+import org.eclipse.jetty.websocket.WebSocket.Connection;
+
+public interface IInputListener {
+ public void startListening(Connection c);
+ public void stopListening();
+}
View
126 server/src/edu/mit/yingyin/websocket/InputServer.java
@@ -1,130 +1,40 @@
package edu.mit.yingyin.websocket;
-import java.io.IOException;
+import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
-import org.OpenNI.ActiveHandEventArgs;
-import org.OpenNI.GestureRecognizedEventArgs;
-import org.OpenNI.IObservable;
-import org.OpenNI.IObserver;
-import org.OpenNI.Point3D;
-import org.OpenNI.StatusException;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.nio.SelectChannelConnector;
import org.eclipse.jetty.websocket.WebSocket;
-import org.eclipse.jetty.websocket.WebSocket.Connection;
import org.eclipse.jetty.websocket.WebSocketHandler;
/**
- * A WebSocket server that sends input events to clients.
+ * A WebSocket server that listens to input events and sends them to clients.
* @author yingyin
*
*/
public class InputServer extends Server {
+ private static Logger logger = Logger.getLogger(InputServer.class.getName());
+
private class InputWebSocket implements WebSocket {
- private InputListener inputListener;
-
@Override
public void onClose(int code, String message) {
- System.out.printf("%s#onClose %d %s\n",
- this.getClass().getSimpleName(), code, message);
+ logger.info(String.format("Connection closed with code: %d, message: %s\n",
+ code, message));
if (inputListener != null)
- inputListener.deleteObservers();
+ inputListener.stopListening();
}
/**
* A new connection is opened.
*/
@Override
public void onOpen(Connection connection) {
- System.out.printf("%s#onOpen %s\n",this.getClass().getSimpleName(),
- connection);
- inputListener = new InputListener(connection);
- }
- }
-
- private class InputListener {
- private Connection connection;
- private GestureObserver gestureRecogObserver = new GestureObserver();
- private HandCreateObserver handCreateObserver = new HandCreateObserver();
- private HandUpdateObserver handUpdateObserer = new HandUpdateObserver();
-
- public InputListener(Connection connection) {
- this.connection = connection;
- try {
- tracker.addGestureRecognizedEventObserver(gestureRecogObserver);
- tracker.addHandCreateEventObserver(handCreateObserver);
- tracker.addHandUpdateEventObserver(handUpdateObserer);
- } catch (StatusException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- public void deleteObservers() {
- tracker.deleteGestureRecognizedEventObserver(gestureRecogObserver);
- tracker.deleteHandCreateEventObserver(handCreateObserver);
- }
-
- private class GestureObserver implements
- IObserver<GestureRecognizedEventArgs> {
-
- @Override
- public void update(IObservable<GestureRecognizedEventArgs> observable,
- GestureRecognizedEventArgs args) {
- try {
- connection.sendMessage("Gesture recognized:" + args.getGesture());
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-
- private class HandCreateObserver implements
- IObserver<ActiveHandEventArgs> {
-
- @Override
- public void update(IObservable<ActiveHandEventArgs> observable,
- ActiveHandEventArgs args) {
- Point3D pos = args.getPosition();
- try {
- pos = tracker.convertRealWorldToProjective(pos);
- String message = String.format("hand_created,%d,%d,%d",
- (int)pos.getX(), (int)pos.getY(), (int)pos.getZ());
- connection.sendMessage(message);
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } catch (StatusException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
-
- private class HandUpdateObserver implements
- IObserver<ActiveHandEventArgs> {
-
- @Override
- public void update(IObservable<ActiveHandEventArgs> observable,
- ActiveHandEventArgs args) {
-
- Point3D pos = args.getPosition();
- try {
- pos = tracker.convertRealWorldToProjective(pos);
- String message = String.format("hand_created,%d,%d,%d",
- (int)pos.getX(), (int)pos.getY(), (int)pos.getZ());
- connection.sendMessage(message);
- } catch (IOException ioe) {
- ioe.printStackTrace();
- } catch (StatusException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
+ logger.info(String.format("Connection opened: %s\n", connection));
+ inputListener.startListening(connection);
}
}
@@ -134,13 +44,17 @@ public void update(IObservable<ActiveHandEventArgs> observable,
*/
private SelectChannelConnector connector;
private WebSocketHandler wsHandler;
- private HandTracker tracker;
+ private IInputListener inputListener;
- public InputServer(int port, HandTracker tracker) {
- this.tracker = tracker;
-
+ /**
+ *
+ * @param port port this sever listens to.
+ * @param tracker
+ */
+ public InputServer(int port, IInputListener inputListener) {
connector = new SelectChannelConnector();
connector.setPort(port);
+ this.inputListener = inputListener;
addConnector(connector);
wsHandler = new WebSocketHandler() {
@@ -156,9 +70,9 @@ public WebSocket doWebSocketConnect(HttpServletRequest request,
public static void main(String... args) {
try {
int port = 8081;
- HandTracker tracker = new HandTracker();
- HandTrackerController controller = new HandTrackerController(tracker);
- InputServer server = new InputServer(port, tracker);
+ HandTrackerController controller = new HandTrackerController();
+ IInputListener inputListener = new HandInputListener(controller);
+ InputServer server = new InputServer(port, inputListener);
controller.start();
server.start();
server.join();

0 comments on commit 7f7e965

Please sign in to comment.
Something went wrong with that request. Please try again.