diff -r 000000000000 -r 6fefd4afe506 front_processing/extern/TUIO_JAVA/src/TUIO/TuioClient.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/front_processing/extern/TUIO_JAVA/src/TUIO/TuioClient.java Fri Mar 09 14:52:11 2012 +0100 @@ -0,0 +1,463 @@ +/* + TUIO Java backend - part of the reacTIVision project + http://reactivision.sourceforge.net/ + + Copyright (c) 2005-2009 Martin Kaltenbrunner + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +*/ +package TUIO; + +import com.illposed.osc.*; +import java.util.*; + +/** + * The TuioClient class is the central TUIO protocol decoder component. It provides a simple callback infrastructure using the {@link TuioListener} interface. + * In order to receive and decode TUIO messages an instance of TuioClient needs to be created. The TuioClient instance then generates TUIO events + * which are broadcasted to all registered classes that implement the {@link TuioListener} interface.

+ * + * TuioClient client = new TuioClient();
+ * client.addTuioListener(myTuioListener);
+ * client.connect();
+ *
+ * + * @author Martin Kaltenbrunner + * @version 1.4 + */ +public class TuioClient implements OSCListener { + + private int port = 3333; + private OSCPortIn oscPort; + private boolean connected = false; + private Hashtable objectList = new Hashtable(); + private Vector aliveObjectList = new Vector(); + private Vector newObjectList = new Vector(); + private Hashtable cursorList = new Hashtable(); + private Vector aliveCursorList = new Vector(); + private Vector newCursorList = new Vector(); + + private Vector frameObjects = new Vector(); + private Vector frameCursors = new Vector(); + + private Vector freeCursorList = new Vector(); + private int maxCursorID = -1; + + private long currentFrame = 0; + private TuioTime currentTime; + + private Vector listenerList = new Vector(); + + /** + * The default constructor creates a client that listens to the default TUIO port 3333 + */ + public TuioClient() {} + + /** + * This constructor creates a client that listens to the provided port + * + * @param port the listening port number + */ + public TuioClient(int port) { + this.port = port; + } + + /** + * The TuioClient starts listening to TUIO messages on the configured UDP port + * All reveived TUIO messages are decoded and the resulting TUIO events are broadcasted to all registered TuioListeners + */ + public void connect() { + + TuioTime.initSession(); + currentTime = new TuioTime(); + currentTime.reset(); + + try { + oscPort = new OSCPortIn(port); + oscPort.addListener("/tuio/2Dobj",this); + oscPort.addListener("/tuio/2Dcur",this); + oscPort.startListening(); + connected = true; + } catch (Exception e) { + System.out.println("TuioClient: failed to connect to port "+port); + connected = false; + } + } + + /** + * The TuioClient stops listening to TUIO messages on the configured UDP port + */ + public void disconnect() { + oscPort.stopListening(); + try { Thread.sleep(100); } + catch (Exception e) {}; + oscPort.close(); + connected = false; + } + + /** + * Returns true if this TuioClient is currently connected. + * @return true if this TuioClient is currently connected + */ + public boolean isConnected() { return connected; } + + /** + * Adds the provided TuioListener to the list of registered TUIO event listeners + * + * @param listener the TuioListener to add + */ + public void addTuioListener(TuioListener listener) { + listenerList.addElement(listener); + } + + /** + * Removes the provided TuioListener from the list of registered TUIO event listeners + * + * @param listener the TuioListener to remove + */ + public void removeTuioListener(TuioListener listener) { + listenerList.removeElement(listener); + } + + /** + * Removes all TuioListener from the list of registered TUIO event listeners + */ + public void removeAllTuioListeners() { + listenerList.clear(); + } + + /** + * Returns a Vector of all currently active TuioObjects + * + * @return a Vector of all currently active TuioObjects + */ + public Vector getTuioObjects() { + return new Vector(objectList.values()); + } + + /** + * Returns a Vector of all currently active TuioCursors + * + * @return a Vector of all currently active TuioCursors + */ + public Vector getTuioCursors() { + return new Vector(cursorList.values()); + } + + /** + * Returns the TuioObject corresponding to the provided Session ID + * or NULL if the Session ID does not refer to an active TuioObject + * + * @return an active TuioObject corresponding to the provided Session ID or NULL + */ + public TuioObject getTuioObject(long s_id) { + return objectList.get(s_id); + } + + /** + * Returns the TuioCursor corresponding to the provided Session ID + * or NULL if the Session ID does not refer to an active TuioCursor + * + * @return an active TuioCursor corresponding to the provided Session ID or NULL + */ + public TuioCursor getTuioCursor(long s_id) { + return cursorList.get(s_id); + } + + /** + * The OSC callback method where all TUIO messages are received and decoded + * and where the TUIO event callbacks are dispatched + * + * @param date the time stamp of the OSC bundle + * @param message the received OSC message + */ + public void acceptMessage(Date date, OSCMessage message) { + + Object[] args = message.getArguments(); + String command = (String)args[0]; + String address = message.getAddress(); + + if (address.equals("/tuio/2Dobj")) { + + if (command.equals("set")) { + + long s_id = ((Integer)args[1]).longValue(); + int c_id = ((Integer)args[2]).intValue(); + float xpos = ((Float)args[3]).floatValue(); + float ypos = ((Float)args[4]).floatValue(); + float angle = ((Float)args[5]).floatValue(); + float xspeed = ((Float)args[6]).floatValue(); + float yspeed = ((Float)args[7]).floatValue(); + float rspeed = ((Float)args[8]).floatValue(); + float maccel = ((Float)args[9]).floatValue(); + float raccel = ((Float)args[10]).floatValue(); + + if (objectList.get(s_id) == null) { + + TuioObject addObject = new TuioObject(s_id,c_id,xpos,ypos,angle); + frameObjects.addElement(addObject); + + } else { + + TuioObject tobj = objectList.get(s_id); + if (tobj==null) return; + if ((tobj.xpos!=xpos) || (tobj.ypos!=ypos) || (tobj.angle!=angle) || (tobj.x_speed!=xspeed) || (tobj.y_speed!=yspeed) || (tobj.rotation_speed!=rspeed) || (tobj.motion_accel!=maccel) || (tobj.rotation_accel!=raccel)) { + + TuioObject updateObject = new TuioObject(s_id,c_id,xpos,ypos,angle); + updateObject.update(xpos,ypos,angle,xspeed,yspeed,rspeed,maccel,raccel); + frameObjects.addElement(updateObject); + } + + } + + } else if (command.equals("alive")) { + + newObjectList.clear(); + for (int i=1;i0) { + if (fseq>currentFrame) currentTime = TuioTime.getSessionTime(); + if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame=fseq; + else lateFrame = true; + } else if (TuioTime.getSessionTime().subtract(currentTime).getTotalMilliseconds()>100) { + currentTime = TuioTime.getSessionTime(); + } + + if (!lateFrame) { + Enumeration frameEnum = frameObjects.elements(); + while(frameEnum.hasMoreElements()) { + TuioObject tobj = frameEnum.nextElement(); + + switch (tobj.getTuioState()) { + case TuioObject.TUIO_REMOVED: + TuioObject removeObject = tobj; + removeObject.remove(currentTime); + for (int i=0;i buffer = aliveObjectList; + aliveObjectList = newObjectList; + // recycling the vector + newObjectList = buffer; + } + frameObjects.clear(); + } + } else if (address.equals("/tuio/2Dcur")) { + + if (command.equals("set")) { + + long s_id = ((Integer)args[1]).longValue(); + float xpos = ((Float)args[2]).floatValue(); + float ypos = ((Float)args[3]).floatValue(); + float xspeed = ((Float)args[4]).floatValue(); + float yspeed = ((Float)args[5]).floatValue(); + float maccel = ((Float)args[6]).floatValue(); + + if (cursorList.get(s_id) == null) { + + TuioCursor addCursor = new TuioCursor(s_id, -1 ,xpos,ypos); + frameCursors.addElement(addCursor); + + } else { + + TuioCursor tcur = cursorList.get(s_id); + if (tcur==null) return; + if ((tcur.xpos!=xpos) || (tcur.ypos!=ypos) || (tcur.x_speed!=xspeed) || (tcur.y_speed!=yspeed) || (tcur.motion_accel!=maccel)) { + + TuioCursor updateCursor = new TuioCursor(s_id,tcur.getCursorID(),xpos,ypos); + updateCursor.update(xpos,ypos,xspeed,yspeed,maccel); + frameCursors.addElement(updateCursor); + } + } + + //System.out.println("set cur " + s_id+" "+xpos+" "+ypos+" "+xspeed+" "+yspeed+" "+maccel); + + } else if (command.equals("alive")) { + + newCursorList.clear(); + for (int i=1;i0) { + if (fseq>currentFrame) currentTime = TuioTime.getSessionTime(); + if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame = fseq; + else lateFrame = true; + } else if (TuioTime.getSessionTime().subtract(currentTime).getTotalMilliseconds()>100) { + currentTime = TuioTime.getSessionTime(); + } + if (!lateFrame) { + + Enumeration frameEnum = frameCursors.elements(); + while(frameEnum.hasMoreElements()) { + TuioCursor tcur = frameEnum.nextElement(); + + switch (tcur.getTuioState()) { + case TuioCursor.TUIO_REMOVED: + + TuioCursor removeCursor = tcur; + removeCursor.remove(currentTime); + + for (int i=0;i0) { + Enumeration clist = cursorList.elements(); + while (clist.hasMoreElements()) { + int c_id = clist.nextElement().getCursorID(); + if (c_id>maxCursorID) maxCursorID=c_id; + } + + Enumeration flist = freeCursorList.elements(); + while (flist.hasMoreElements()) { + int c_id = flist.nextElement().getCursorID(); + if (c_id>=maxCursorID) freeCursorList.removeElement(c_id); + } + } else freeCursorList.clear(); + } else if (removeCursor.getCursorID()0)) { + TuioCursor closestCursor = freeCursorList.firstElement(); + Enumeration testList = freeCursorList.elements(); + while (testList.hasMoreElements()) { + TuioCursor testCursor = testList.nextElement(); + if (testCursor.getDistance(tcur) buffer = aliveCursorList; + aliveCursorList = newCursorList; + // recycling the vector + newCursorList = buffer; + } + + frameCursors.clear(); + } + + } + } +}