2
0

TuioServer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /*
  2. TUIO Server Component - part of the reacTIVision project
  3. http://reactivision.sourceforge.net/
  4. Copyright (c) 2005-2009 Martin Kaltenbrunner <[email protected]>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. */
  17. #ifndef INCLUDED_TuioServer_H
  18. #define INCLUDED_TuioServer_H
  19. #ifndef WIN32
  20. #include <pthread.h>
  21. #include <sys/time.h>
  22. #else
  23. #include <windows.h>
  24. #endif
  25. #include <iostream>
  26. #include <list>
  27. #include <algorithm>
  28. #include "osc/OscOutboundPacketStream.h"
  29. #include "ip/NetworkingUtils.h"
  30. #include "ip/UdpSocket.h"
  31. #include "TuioObject.h"
  32. #include "TuioCursor.h"
  33. #define IP_MTU_SIZE 1500
  34. #define MAX_UDP_SIZE 65536
  35. #define MIN_UDP_SIZE 576
  36. #define OBJ_MESSAGE_SIZE 108 // setMessage + seqMessage size
  37. #define CUR_MESSAGE_SIZE 88
  38. namespace TUIO {
  39. /**
  40. * <p>The TuioServer class is the central TUIO protocol encoder component.
  41. * In order to encode and send TUIO messages an instance of TuioServer needs to be created. The TuioServer instance then generates TUIO messaged
  42. * which are sent via OSC over UDP to the configured IP address and port.</p>
  43. * <p>During runtime the each frame is marked with the initFrame and commitFrame methods,
  44. * while the currently present TuioObjects are managed by the server with ADD, UPDATE and REMOVE methods in analogy to the TuioClient's TuioListener interface.</p>
  45. * <p><code>
  46. * TuioClient *server = new TuioServer();<br/>
  47. * ...<br/>
  48. * server->initFrame(TuioTime::getSessionTime());<br/>
  49. * TuioObject *tobj = server->addTuioObject(xpos,ypos, angle);<br/>
  50. * TuioCursor *tcur = server->addTuioObject(xpos,ypos);<br/>
  51. * server->commitFrame();<br/>
  52. * ...<br/>
  53. * server->initFrame(TuioTime::getSessionTime());<br/>
  54. * server->updateTuioObject(tobj, xpos,ypos, angle);<br/>
  55. * server->updateTuioCursor(tcur, xpos,ypos);<br/>
  56. * server->commitFrame();<br/>
  57. * ...<br/>
  58. * server->initFrame(TuioTime::getSessionTime());<br/>
  59. * server->removeTuioObject(tobj);<br/>
  60. * server->removeTuioCursor(tcur);<br/>
  61. * server->commitFrame();<br/>
  62. * </code></p>
  63. *
  64. * @author Martin Kaltenbrunner
  65. * @version 1.4
  66. */
  67. class TuioServer {
  68. public:
  69. /**
  70. * The default constructor creates a TuioServer that sends to the default TUIO port 3333 on localhost
  71. * using the maximum packet size of 65536 bytes to use single packets on the loopback device
  72. */
  73. TuioServer();
  74. /**
  75. * This constructor creates a TuioServer that sends to the provided port on the the given host
  76. * using a default packet size of 1492 bytes to deliver unfragmented UDP packets on a LAN
  77. *
  78. * @param host the receiving host name
  79. * @param port the outgoing TUIO UDP port number
  80. */
  81. TuioServer(const char *host, int port);
  82. /**
  83. * This constructor creates a TuioServer that sends to the provided port on the the given host
  84. * the packet UDP size can be set to a value between 576 and 65536 bytes
  85. *
  86. * @param host the receiving host name
  87. * @param port the outgoing TUIO UDP port number
  88. * @param size the maximum UDP packet size
  89. */
  90. TuioServer(const char *host, int port, int size);
  91. /**
  92. * The destructor is doing nothing in particular.
  93. */
  94. ~TuioServer();
  95. /**
  96. * Creates a new TuioObject based on the given arguments.
  97. * The new TuioObject is added to the TuioServer's internal list of active TuioObjects
  98. * and a reference is returned to the caller.
  99. *
  100. * @param sym the Symbol ID to assign
  101. * @param xp the X coordinate to assign
  102. * @param yp the Y coordinate to assign
  103. * @param a the angle to assign
  104. * @return reference to the created TuioObject
  105. */
  106. TuioObject* addTuioObject(int sym, float xp, float yp, float a);
  107. /**
  108. * Updates the referenced TuioObject based on the given arguments.
  109. *
  110. * @param tobj the TuioObject to update
  111. * @param xp the X coordinate to assign
  112. * @param yp the Y coordinate to assign
  113. * @param a the angle to assign
  114. */
  115. void updateTuioObject(TuioObject *tobj, float xp, float yp, float a);
  116. /**
  117. * Removes the referenced TuioObject from the TuioServer's internal list of TuioObjects
  118. * and deletes the referenced TuioObject afterwards
  119. *
  120. * @param tobj the TuioObject to remove
  121. */
  122. void removeTuioObject(TuioObject *tobj);
  123. /**
  124. * Adds an externally managed TuioObject to the TuioServer's internal list of active TuioObjects
  125. *
  126. * @param tobj the TuioObject to add
  127. */
  128. void addExternalTuioObject(TuioObject *tobj);
  129. /**
  130. * Updates an externally managed TuioObject
  131. *
  132. * @param tobj the TuioObject to update
  133. */
  134. void updateExternalTuioObject(TuioObject *tobj);
  135. /**
  136. * Removes an externally managed TuioObject from the TuioServer's internal list of TuioObjects
  137. * The referenced TuioObject is not deleted
  138. *
  139. * @param tobj the TuioObject to remove
  140. */
  141. void removeExternalTuioObject(TuioObject *tobj);
  142. /**
  143. * Creates a new TuioCursor based on the given arguments.
  144. * The new TuioCursor is added to the TuioServer's internal list of active TuioCursors
  145. * and a reference is returned to the caller.
  146. *
  147. * @param xp the X coordinate to assign
  148. * @param yp the Y coordinate to assign
  149. * @return reference to the created TuioCursor
  150. */
  151. TuioCursor* addTuioCursor(float xp, float yp);
  152. /**
  153. * Updates the referenced TuioCursor based on the given arguments.
  154. *
  155. * @param tcur the TuioObject to update
  156. * @param xp the X coordinate to assign
  157. * @param yp the Y coordinate to assign
  158. */
  159. void updateTuioCursor(TuioCursor *tcur, float xp, float yp);
  160. /**
  161. * Removes the referenced TuioCursor from the TuioServer's internal list of TuioCursors
  162. * and deletes the referenced TuioCursor afterwards
  163. *
  164. * @param tcur the TuioCursor to remove
  165. */
  166. void removeTuioCursor(TuioCursor *tcur);
  167. /**
  168. * Updates an externally managed TuioCursor
  169. *
  170. * @param tcur the TuioCursor to update
  171. */
  172. void addExternalTuioCursor(TuioCursor *tcur);
  173. /**
  174. * Updates an externally managed TuioCursor
  175. *
  176. * @param tcur the TuioCursor to update
  177. */
  178. void updateExternalTuioCursor(TuioCursor *tcur);
  179. /**
  180. * Removes an externally managed TuioCursor from the TuioServer's internal list of TuioCursor
  181. * The referenced TuioCursor is not deleted
  182. *
  183. * @param tcur the TuioCursor to remove
  184. */
  185. void removeExternalTuioCursor(TuioCursor *tcur);
  186. /**
  187. * Initializes a new frame with the given TuioTime
  188. *
  189. * @param ttime the frame time
  190. */
  191. void initFrame(TuioTime ttime);
  192. /**
  193. * Commits the current frame.
  194. * Generates and sends TUIO messages of all currently active and updated TuioObjects and TuioCursors.
  195. */
  196. void commitFrame();
  197. /**
  198. * Returns the next available Session ID for external use.
  199. * @return the next available Session ID for external use
  200. */
  201. long getSessionID();
  202. /**
  203. * Returns the current frame ID for external use.
  204. * @return the current frame ID for external use
  205. */
  206. long getFrameID();
  207. /**
  208. * Returns the current frame ID for external use.
  209. * @return the current frame ID for external use
  210. */
  211. TuioTime getFrameTime();
  212. /**
  213. * Generates and sends TUIO messages of all currently active TuioObjects and TuioCursors.
  214. */
  215. void sendFullMessages();
  216. /**
  217. * Disables the periodic full update of all currently active TuioObjects and TuioCursors
  218. *
  219. * @param interval update interval in seconds, defaults to one second
  220. */
  221. void enablePeriodicMessages(int interval=1);
  222. /**
  223. * Disables the periodic full update of all currently active and inactive TuioObjects and TuioCursors
  224. */
  225. void disablePeriodicMessages();
  226. /**
  227. * Enables the full update of all currently active and inactive TuioObjects and TuioCursors
  228. *
  229. */
  230. void enableFullUpdate() {
  231. full_update = true;
  232. }
  233. /**
  234. * Disables the full update of all currently active and inactive TuioObjects and TuioCursors
  235. */
  236. void disableFullUpdate() {
  237. full_update = false;
  238. }
  239. /**
  240. * Returns true if the periodic full update of all currently active TuioObjects and TuioCursors is enabled.
  241. * @return true if the periodic full update of all currently active TuioObjects and TuioCursors is enabled
  242. */
  243. bool periodicMessagesEnabled() {
  244. return periodic_update;
  245. }
  246. /**
  247. * Returns the periodic update interval in seconds.
  248. * @return the periodic update interval in seconds
  249. */
  250. int getUpdateInterval() {
  251. return update_interval;
  252. }
  253. /**
  254. * Returns a List of all currently inactive TuioObjects
  255. *
  256. * @return a List of all currently inactive TuioObjects
  257. */
  258. std::list<TuioObject*> getUntouchedObjects();
  259. /**
  260. * Returns a List of all currently inactive TuioCursors
  261. *
  262. * @return a List of all currently inactive TuioCursors
  263. */
  264. std::list<TuioCursor*> getUntouchedCursors();
  265. /**
  266. * Calculates speed and acceleration values for all currently inactive TuioObjects
  267. */
  268. void stopUntouchedMovingObjects();
  269. /**
  270. * Calculates speed and acceleration values for all currently inactive TuioCursors
  271. */
  272. void stopUntouchedMovingCursors();
  273. /**
  274. * Removes all currently inactive TuioObjects from the TuioServer's internal list of TuioObjects
  275. */
  276. void removeUntouchedStoppedObjects();
  277. /**
  278. * Removes all currently inactive TuioCursors from the TuioServer's internal list of TuioCursors
  279. */
  280. void removeUntouchedStoppedCursors();
  281. /**
  282. * Returns a List of all currently active TuioObjects
  283. *
  284. * @return a List of all currently active TuioObjects
  285. */
  286. std::list<TuioObject*> getTuioObjects();
  287. /**
  288. * Returns a List of all currently active TuioCursors
  289. *
  290. * @return a List of all currently active TuioCursors
  291. */
  292. std::list<TuioCursor*> getTuioCursors();
  293. /**
  294. * Returns the TuioObject corresponding to the provided Session ID
  295. * or NULL if the Session ID does not refer to an active TuioObject
  296. *
  297. * @return an active TuioObject corresponding to the provided Session ID or NULL
  298. */
  299. TuioObject* getTuioObject(long s_id);
  300. /**
  301. * Returns the TuioCursor corresponding to the provided Session ID
  302. * or NULL if the Session ID does not refer to an active TuioCursor
  303. *
  304. * @return an active TuioCursor corresponding to the provided Session ID or NULL
  305. */
  306. TuioCursor* getTuioCursor(long s_id);
  307. /**
  308. * Returns the TuioObject closest to the provided coordinates
  309. * or NULL if there isn't any active TuioObject
  310. *
  311. * @return the closest TuioObject to the provided coordinates or NULL
  312. */
  313. TuioObject* getClosestTuioObject(float xp, float yp);
  314. /**
  315. * Returns the TuioCursor closest to the provided coordinates
  316. * or NULL if there isn't any active TuioCursor
  317. *
  318. * @return the closest TuioCursor corresponding to the provided coordinates or NULL
  319. */
  320. TuioCursor* getClosestTuioCursor(float xp, float yp);
  321. /**
  322. * Returns true if this TuioServer is currently connected.
  323. * @return true if this TuioServer is currently connected
  324. */
  325. bool isConnected() { return connected; }
  326. /**
  327. * The TuioServer prints verbose TUIO event messages to the console if set to true.
  328. * @param verbose verbose message output if set to true
  329. */
  330. void setVerbose(bool verbose) { this->verbose=verbose; }
  331. private:
  332. std::list<TuioObject*> objectList;
  333. std::list<TuioCursor*> cursorList;
  334. int maxCursorID;
  335. std::list<TuioCursor*> freeCursorList;
  336. std::list<TuioCursor*> freeCursorBuffer;
  337. UdpTransmitSocket *socket;
  338. osc::OutboundPacketStream *oscPacket;
  339. char *oscBuffer;
  340. osc::OutboundPacketStream *fullPacket;
  341. char *fullBuffer;
  342. void initialize(const char *host, int port, int size);
  343. void sendEmptyCursorBundle();
  344. void startCursorBundle();
  345. void addCursorMessage(TuioCursor *tcur);
  346. void sendCursorBundle(long fseq);
  347. void sendEmptyObjectBundle();
  348. void startObjectBundle();
  349. void addObjectMessage(TuioObject *tobj);
  350. void sendObjectBundle(long fseq);
  351. bool full_update;
  352. int update_interval;
  353. bool periodic_update;
  354. long currentFrame;
  355. TuioTime currentFrameTime;
  356. bool updateObject, updateCursor;
  357. long lastCursorUpdate, lastObjectUpdate;
  358. long sessionID;
  359. bool verbose;
  360. #ifndef WIN32
  361. pthread_t thread;
  362. #else
  363. HANDLE thread;
  364. #endif
  365. bool connected;
  366. };
  367. };
  368. #endif /* INCLUDED_TuioServer_H */