front_processing/extern/TUIO_JAVA/src/TUIO/TuioClient.java
changeset 3 92f19af39024
parent 0 6fefd4afe506
child 9 0f44b7360c8d
equal deleted inserted replaced
2:11234537653b 3:92f19af39024
    36  * @author Martin Kaltenbrunner
    36  * @author Martin Kaltenbrunner
    37  * @version 1.4
    37  * @version 1.4
    38  */ 
    38  */ 
    39 public class TuioClient implements OSCListener {
    39 public class TuioClient implements OSCListener {
    40 	
    40 	
       
    41 	public String comm;
       
    42 	
    41 	private int port = 3333;
    43 	private int port = 3333;
    42 	private OSCPortIn oscPort;
    44 	private OSCPortIn oscPort;
    43 	private boolean connected = false;
    45 	private boolean connected = false;
    44 	private Hashtable<Long,TuioObject> objectList = new Hashtable<Long,TuioObject>();
    46 	private Hashtable<Long,TuioObject> objectList = new Hashtable<Long,TuioObject>();
    45 	private Vector<Long> aliveObjectList = new Vector<Long>();
    47 	private Vector<Long> aliveObjectList = new Vector<Long>();
    46 	private Vector<Long> newObjectList = new Vector<Long>();
    48 	private Vector<Long> newObjectList = new Vector<Long>();
    47 	private Hashtable<Long,TuioCursor> cursorList = new Hashtable<Long,TuioCursor>();
    49 	private Hashtable<Long,TuioCursor> cursorList = new Hashtable<Long,TuioCursor>();
    48 	private Vector<Long> aliveCursorList = new Vector<Long>();
    50 	private Vector<Long> aliveCursorList = new Vector<Long>();
    49 	private Vector<Long> newCursorList = new Vector<Long>();
    51 	private Vector<Long> newCursorList = new Vector<Long>();
       
    52 	private Hashtable<Long,TuioString> stringList = new Hashtable<Long,TuioString>();
       
    53 	private Vector<Long> aliveStringList = new Vector<Long>();
       
    54 	private Vector<Long> newStringList = new Vector<Long>();
    50 
    55 
    51 	private Vector<TuioObject> frameObjects = new Vector<TuioObject>();
    56 	private Vector<TuioObject> frameObjects = new Vector<TuioObject>();
    52 	private Vector<TuioCursor> frameCursors = new Vector<TuioCursor>();
    57 	private Vector<TuioCursor> frameCursors = new Vector<TuioCursor>();
       
    58 	private Vector<TuioString> frameStrings = new Vector<TuioString>();
    53 
    59 
    54 	private Vector<TuioCursor> freeCursorList = new Vector<TuioCursor>();
    60 	private Vector<TuioCursor> freeCursorList = new Vector<TuioCursor>();
    55 	private int maxCursorID = -1;
    61 	private int maxCursorID = -1;
       
    62 	
       
    63 	private Vector<TuioString> freeStringList = new Vector<TuioString>();
       
    64 	private int maxStringID = -1;
    56 	
    65 	
    57 	private long currentFrame = 0;
    66 	private long currentFrame = 0;
    58 	private TuioTime currentTime;
    67 	private TuioTime currentTime;
    59 
    68 
    60 	private Vector<TuioListener> listenerList = new Vector<TuioListener>();
    69 	private Vector<TuioListener> listenerList = new Vector<TuioListener>();
    84 		currentTime.reset();
    93 		currentTime.reset();
    85 		
    94 		
    86 		try {
    95 		try {
    87 			oscPort = new OSCPortIn(port);
    96 			oscPort = new OSCPortIn(port);
    88 			oscPort.addListener("/tuio/2Dobj",this);
    97 			oscPort.addListener("/tuio/2Dobj",this);
    89 			oscPort.addListener("/tuio/2Dcur",this);
    98 			oscPort.addListener("/tuio/3Dcur",this);
       
    99 			oscPort.addListener("/tuio/_siP",this);
    90 			oscPort.startListening();
   100 			oscPort.startListening();
    91 			connected = true;
   101 			connected = true;
    92 		} catch (Exception e) {
   102 		} catch (Exception e) {
    93 			System.out.println("TuioClient: failed to connect to port "+port);
   103 			System.out.println("TuioClient: failed to connect to port "+port);
    94 			connected = false;
   104 			connected = false;
   151 	 *
   161 	 *
   152 	 * @return  a Vector of all currently active TuioCursors
   162 	 * @return  a Vector of all currently active TuioCursors
   153 	 */
   163 	 */
   154 	public Vector<TuioCursor> getTuioCursors() {
   164 	public Vector<TuioCursor> getTuioCursors() {
   155 		return new Vector<TuioCursor>(cursorList.values());
   165 		return new Vector<TuioCursor>(cursorList.values());
   156 	}	
   166 	}
       
   167 	
       
   168 	/**
       
   169 	 * Returns a Vector of all currently active TuioStrings
       
   170 	 *
       
   171 	 * @return  a Vector of all currently active TuioStrings
       
   172 	 */
       
   173 	public Vector<TuioString> getTuioStrings() {
       
   174 		return new Vector<TuioString>(stringList.values());
       
   175 	}
   157 
   176 
   158 	/**
   177 	/**
   159 	 * Returns the TuioObject corresponding to the provided Session ID
   178 	 * Returns the TuioObject corresponding to the provided Session ID
   160 	 * or NULL if the Session ID does not refer to an active TuioObject
   179 	 * or NULL if the Session ID does not refer to an active TuioObject
   161 	 *
   180 	 *
   171 	 *
   190 	 *
   172 	 * @return  an active TuioCursor corresponding to the provided Session ID or NULL
   191 	 * @return  an active TuioCursor corresponding to the provided Session ID or NULL
   173 	 */
   192 	 */
   174 	public TuioCursor getTuioCursor(long s_id) {
   193 	public TuioCursor getTuioCursor(long s_id) {
   175 		return cursorList.get(s_id);
   194 		return cursorList.get(s_id);
       
   195 	}
       
   196 
       
   197 	/**
       
   198 	 * Returns the TuioString corresponding to the provided Session ID
       
   199 	 * or NULL if the Session ID does not refer to an active TuioString
       
   200 	 *
       
   201 	 * @return  an active TuioString corresponding to the provided Session ID or NULL
       
   202 	 */
       
   203 	public TuioString getTuioString(long s_id) {
       
   204 		return stringList.get(s_id);
   176 	}	
   205 	}	
   177 
   206 
   178 	/**
   207 	/**
   179 	 * The OSC callback method where all TUIO messages are received and decoded
   208 	 * The OSC callback method where all TUIO messages are received and decoded
   180 	 * and where the TUIO event callbacks are dispatched
   209 	 * and where the TUIO event callbacks are dispatched
   303 					// recycling the vector
   332 					// recycling the vector
   304 					newObjectList = buffer;					
   333 					newObjectList = buffer;					
   305 				}
   334 				}
   306 				frameObjects.clear();
   335 				frameObjects.clear();
   307 			}
   336 			}
   308 		} else if (address.equals("/tuio/2Dcur")) {
   337 		} else if (address.equals("/tuio/3Dcur")) {
   309 
   338 
   310 			if (command.equals("set")) {
   339 			if (command.equals("set")) {
   311 
   340 
   312 				long s_id  = ((Integer)args[1]).longValue();
   341 				long s_id  = ((Integer)args[1]).longValue();
   313 				float xpos = ((Float)args[2]).floatValue();
   342 				float xpos = ((Float)args[2]).floatValue();
   314 				float ypos = ((Float)args[3]).floatValue();
   343 				float ypos = ((Float)args[3]).floatValue();
   315 				float xspeed = ((Float)args[4]).floatValue();
   344 				float zpos = ((Float)args[4]).floatValue();
   316 				float yspeed = ((Float)args[5]).floatValue();
   345 				float xspeed = ((Float)args[5]).floatValue();
   317 				float maccel = ((Float)args[6]).floatValue();
   346 				float yspeed = ((Float)args[6]).floatValue();
       
   347 				float maccel = ((Float)args[7]).floatValue();
   318 				
   348 				
   319 				if (cursorList.get(s_id) == null) {
   349 				if (cursorList.get(s_id) == null) {
   320 									
   350 									
   321 					TuioCursor addCursor = new TuioCursor(s_id, -1 ,xpos,ypos);
   351 					TuioCursor addCursor = new TuioCursor(s_id, -1 ,xpos,ypos,zpos);
   322 					frameCursors.addElement(addCursor);
   352 					frameCursors.addElement(addCursor);
   323 					
   353 					
   324 				} else {
   354 				} else {
   325 				
   355 				
   326 					TuioCursor tcur = cursorList.get(s_id);
   356 					TuioCursor tcur = cursorList.get(s_id);
   327 					if (tcur==null) return;
   357 					if (tcur==null) return;
   328 					if ((tcur.xpos!=xpos) || (tcur.ypos!=ypos) || (tcur.x_speed!=xspeed) || (tcur.y_speed!=yspeed) || (tcur.motion_accel!=maccel)) {
   358 					if ((tcur.xpos!=xpos) || (tcur.ypos!=ypos) || (tcur.zpos!=zpos) || (tcur.x_speed!=xspeed) || (tcur.y_speed!=yspeed) || (tcur.motion_accel!=maccel)) {
   329 
   359 
   330 						TuioCursor updateCursor = new TuioCursor(s_id,tcur.getCursorID(),xpos,ypos);
   360 						TuioCursor updateCursor = new TuioCursor(s_id,tcur.getCursorID(),xpos,ypos,zpos);
   331 						updateCursor.update(xpos,ypos,xspeed,yspeed,maccel);
   361 						updateCursor.update(xpos,ypos,zpos,xspeed,yspeed,maccel);
   332 						frameCursors.addElement(updateCursor);
   362 						frameCursors.addElement(updateCursor);
   333 					}
   363 					}
   334 				}
   364 				}
   335 				
   365 				
   336 				//System.out.println("set cur " + s_id+" "+xpos+" "+ypos+" "+xspeed+" "+yspeed+" "+maccel);
   366 				//System.out.println("set cur " + s_id+" "+xpos+" "+ypos+" "+xspeed+" "+yspeed+" "+maccel);
   418 									}
   448 									}
   419 									c_id = closestCursor.getCursorID();
   449 									c_id = closestCursor.getCursorID();
   420 									freeCursorList.removeElement(closestCursor);
   450 									freeCursorList.removeElement(closestCursor);
   421 								} else maxCursorID = c_id;		
   451 								} else maxCursorID = c_id;		
   422 								
   452 								
   423 								TuioCursor addCursor = new TuioCursor(currentTime,tcur.getSessionID(),c_id,tcur.getX(),tcur.getY());
   453 								TuioCursor addCursor = new TuioCursor(currentTime,tcur.getSessionID(),c_id,tcur.getX(),tcur.getY(),tcur.getZ());
   424 								cursorList.put(addCursor.getSessionID(),addCursor);
   454 								cursorList.put(addCursor.getSessionID(),addCursor);
   425 								
   455 								
   426 								for (int i=0;i<listenerList.size();i++) {
   456 								for (int i=0;i<listenerList.size();i++) {
   427 									TuioListener listener = (TuioListener)listenerList.elementAt(i);
   457 									TuioListener listener = (TuioListener)listenerList.elementAt(i);
   428 									if (listener!=null) listener.addTuioCursor(addCursor);
   458 									if (listener!=null) listener.addTuioCursor(addCursor);
   430 								break;
   460 								break;
   431 								
   461 								
   432 							default:
   462 							default:
   433 								
   463 								
   434 								TuioCursor updateCursor = cursorList.get(tcur.getSessionID());
   464 								TuioCursor updateCursor = cursorList.get(tcur.getSessionID());
   435 								if ( (tcur.getX()!=updateCursor.getX() && tcur.getXSpeed()==0) || (tcur.getY()!=updateCursor.getY() && tcur.getYSpeed()==0) )
   465 								if ( (tcur.getX()!=updateCursor.getX() && tcur.getXSpeed()==0) || (tcur.getY()!=updateCursor.getY() && tcur.getYSpeed()==0) || (tcur.getZ()!=updateCursor.getZ()) )
   436 									updateCursor.update(currentTime,tcur.getX(),tcur.getY());
   466 									updateCursor.update(currentTime,tcur.getX(),tcur.getY(),tcur.getZ());
   437 								else 
   467 								else 
   438 									updateCursor.update(currentTime,tcur.getX(),tcur.getY(),tcur.getXSpeed(),tcur.getYSpeed(),tcur.getMotionAccel());
   468 									updateCursor.update(currentTime,tcur.getX(),tcur.getY(),tcur.getZ(),tcur.getXSpeed(),tcur.getYSpeed(),tcur.getMotionAccel());
   439 									
   469 									
   440 								for (int i=0;i<listenerList.size();i++) {
   470 								for (int i=0;i<listenerList.size();i++) {
   441 									TuioListener listener = (TuioListener)listenerList.elementAt(i);
   471 									TuioListener listener = (TuioListener)listenerList.elementAt(i);
   442 									if (listener!=null) listener.updateTuioCursor(updateCursor);
   472 									if (listener!=null) listener.updateTuioCursor(updateCursor);
   443 								}
   473 								}
   455 					newCursorList = buffer;				
   485 					newCursorList = buffer;				
   456 				}
   486 				}
   457 				
   487 				
   458 				frameCursors.clear();
   488 				frameCursors.clear();
   459 			} 
   489 			} 
   460 
   490 		} else if (address.equals("/tuio/_siP")) {
       
   491 
       
   492 			if (command.equals("set")) {
       
   493 
       
   494 				long s_id  = ((Integer)args[1]).longValue();
       
   495 				String msg = args[2].toString();
       
   496 				
       
   497 				if (stringList.get(s_id) == null) {
       
   498 									
       
   499 					TuioString addString = new TuioString(s_id, -1, msg);
       
   500 					frameStrings.addElement(addString);
       
   501 					
       
   502 				} else {
       
   503 				
       
   504 					TuioString tstr = stringList.get(s_id);
       
   505 					if (tstr==null) return;
       
   506 					if (!tstr.getMessage().equals(msg)) {
       
   507 
       
   508 						TuioString updateString = new TuioString(s_id,tstr.getStringID(),msg);
       
   509 						updateString.update(msg);
       
   510 						frameStrings.addElement(updateString);
       
   511 					}
       
   512 				}
       
   513 				
       
   514 				//System.out.println("set cur " + s_id+" "+xpos+" "+ypos+" "+xspeed+" "+yspeed+" "+maccel);
       
   515 				
       
   516 			} else if (command.equals("alive")) {
       
   517 	
       
   518 				newStringList.clear();
       
   519 				for (int i=1;i<args.length;i++) {
       
   520 					// get the message content
       
   521 					long s_id = ((Integer)args[i]).longValue();
       
   522 					newStringList.addElement(s_id);
       
   523 					// reduce the cursor list to the lost cursors
       
   524 					if (aliveStringList.contains(s_id)) 
       
   525 						aliveStringList.removeElement(s_id);
       
   526 				}
       
   527 				
       
   528 				// remove the remaining cursors
       
   529 				for (int i=0;i<aliveStringList.size();i++) {
       
   530 					TuioString removeString = stringList.get(aliveStringList.elementAt(i));
       
   531 					if (removeString==null) continue;
       
   532 					removeString.remove(currentTime);
       
   533 					frameStrings.addElement(removeString);
       
   534 				}
       
   535 								
       
   536 			} else if (command.equals("fseq")) {
       
   537 				long fseq = ((Integer)args[1]).longValue();
       
   538 				boolean lateFrame = false;
       
   539 				
       
   540 				if (fseq>0) {
       
   541 					if (fseq>currentFrame) currentTime = TuioTime.getSessionTime();
       
   542 					if ((fseq>=currentFrame) || ((currentFrame-fseq)>100)) currentFrame = fseq;
       
   543 					else lateFrame = true;
       
   544 				} else if (TuioTime.getSessionTime().subtract(currentTime).getTotalMilliseconds()>100) {
       
   545 					currentTime = TuioTime.getSessionTime();
       
   546 				}
       
   547 				if (!lateFrame) {
       
   548 
       
   549 					Enumeration<TuioString> frameEnum = frameStrings.elements();
       
   550 					while(frameEnum.hasMoreElements()) {
       
   551 						TuioString tstr = frameEnum.nextElement();
       
   552 						
       
   553 						switch (tstr.getTuioState()) {
       
   554 							case TuioString.TUIO_REMOVED:
       
   555 							
       
   556 								TuioString removeString = tstr;
       
   557 								removeString.remove(currentTime);
       
   558 								
       
   559 								for (int i=0;i<listenerList.size();i++) {
       
   560 									TuioListener listener = (TuioListener)listenerList.elementAt(i);
       
   561 									if (listener!=null) listener.removeTuioString(removeString);
       
   562 								}
       
   563 
       
   564 								stringList.remove(removeString.getSessionID());
       
   565 
       
   566 								if (removeString.getStringID()==maxStringID) {
       
   567 									maxStringID = -1;
       
   568 									if (stringList.size()>0) {
       
   569 										Enumeration<TuioString> slist = stringList.elements();
       
   570 										while (slist.hasMoreElements()) {
       
   571 											int sl_id = slist.nextElement().getStringID();
       
   572 											if (sl_id>maxStringID) maxStringID=sl_id;
       
   573 										}
       
   574 										
       
   575 										Enumeration<TuioString> flist = freeStringList.elements();
       
   576 										while (flist.hasMoreElements()) {
       
   577 											int sl_id = flist.nextElement().getStringID();
       
   578 											if (sl_id>=maxStringID) freeStringList.removeElement(sl_id);
       
   579 										}
       
   580 									} else freeStringList.clear();
       
   581 								} else if (removeString.getStringID()<maxStringID) {
       
   582 									freeStringList.addElement(removeString);
       
   583 								}
       
   584 								
       
   585 								break;
       
   586 
       
   587 							case TuioString.TUIO_ADDED:
       
   588 
       
   589 								int sl_id = stringList.size();
       
   590 								if ((stringList.size()<=maxStringID) && (freeStringList.size()>0)) {
       
   591 									TuioString closestString = freeStringList.firstElement();
       
   592 									Enumeration<TuioString> testList = freeStringList.elements();
       
   593 									while (testList.hasMoreElements()) {
       
   594 										TuioString testString = testList.nextElement();
       
   595 										//if (testString.getDistance(tstr)<closestString.getDistance(tstr))
       
   596 										closestString = testString;
       
   597 									}
       
   598 									sl_id = closestString.getStringID();
       
   599 									freeStringList.removeElement(closestString);
       
   600 								} else maxStringID = sl_id;		
       
   601 								
       
   602 								TuioString addString = new TuioString(currentTime,tstr.getSessionID(),sl_id,tstr.getMessage());
       
   603 								stringList.put(addString.getSessionID(),addString);
       
   604 								
       
   605 								for (int i=0;i<listenerList.size();i++) {
       
   606 									TuioListener listener = (TuioListener)listenerList.elementAt(i);
       
   607 									if (listener!=null) listener.addTuioString(addString);
       
   608 								}
       
   609 								break;
       
   610 								
       
   611 							default:
       
   612 								
       
   613 								TuioString updateString = stringList.get(tstr.getSessionID());
       
   614 								if ( tstr.getMessage()!=updateString.getMessage() )
       
   615 									updateString.update(currentTime,tstr.getMessage());
       
   616 								else 
       
   617 									updateString.update(currentTime,tstr.getMessage());
       
   618 									
       
   619 								for (int i=0;i<listenerList.size();i++) {
       
   620 									TuioListener listener = (TuioListener)listenerList.elementAt(i);
       
   621 									if (listener!=null) listener.updateTuioString(updateString);
       
   622 								}
       
   623 						}
       
   624 					}
       
   625 					
       
   626 					for (int i=0;i<listenerList.size();i++) {
       
   627 						TuioListener listener = (TuioListener)listenerList.elementAt(i);
       
   628 						if (listener!=null) listener.refresh(new TuioTime(currentTime));
       
   629 					}
       
   630 					
       
   631 					Vector<Long> buffer = aliveStringList;
       
   632 					aliveStringList = newStringList;
       
   633 					// recycling the vector
       
   634 					newStringList = buffer;				
       
   635 				}
       
   636 				
       
   637 				frameStrings.clear();
       
   638 			} 
   461 		}
   639 		}
       
   640 		
       
   641 		
   462 	}
   642 	}
   463 }
   643 }