TuioClient.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. TUIO C++ Library - 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_TUIOCLIENT_H
  18. #define INCLUDED_TUIOCLIENT_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 <cstring>
  29. #include "osc/OscReceivedElements.h"
  30. #include "osc/OscPrintReceivedElements.h"
  31. #include "ip/UdpSocket.h"
  32. #include "ip/PacketListener.h"
  33. #include "TuioListener.h"
  34. #include "TuioObject.h"
  35. #include "TuioCursor.h"
  36. namespace TUIO {
  37. /**
  38. * <p>The TuioClient class is the central TUIO protocol decoder component. It provides a simple callback infrastructure using the {@link TuioListener} interface.
  39. * In order to receive and decode TUIO messages an instance of TuioClient needs to be created. The TuioClient instance then generates TUIO events
  40. * which are broadcasted to all registered classes that implement the {@link TuioListener} interface.</p>
  41. * <p><code>
  42. * TuioClient *client = new TuioClient();<br/>
  43. * client->addTuioListener(myTuioListener);<br/>
  44. * client->connect();<br/>
  45. * </code></p>
  46. *
  47. * @author Martin Kaltenbrunner
  48. * @version 1.4
  49. */
  50. class TuioClient : public PacketListener {
  51. public:
  52. /**
  53. * This constructor creates a TuioClient that listens to the provided port
  54. *
  55. * @param port the incoming TUIO UDP port number, defaults to 3333 if no argument is provided
  56. */
  57. TuioClient(int port=3333);
  58. /**
  59. * The destructor is doing nothing in particular.
  60. */
  61. ~TuioClient();
  62. /**
  63. * The TuioClient starts listening to TUIO messages on the configured UDP port
  64. * All received TUIO messages are decoded and the resulting TUIO events are broadcasted to all registered TuioListeners
  65. *
  66. * @param lock running in the background if set to false (default)
  67. */
  68. void connect(bool lock=false);
  69. /**
  70. * The TuioClient stops listening to TUIO messages on the configured UDP port
  71. */
  72. void disconnect();
  73. /**
  74. * Returns true if this TuioClient is currently connected.
  75. * @return true if this TuioClient is currently connected
  76. */
  77. bool isConnected() { return connected; }
  78. /**
  79. * Adds the provided TuioListener to the list of registered TUIO event listeners
  80. *
  81. * @param listener the TuioListener to add
  82. */
  83. void addTuioListener(TuioListener *listener);
  84. /**
  85. * Removes the provided TuioListener from the list of registered TUIO event listeners
  86. *
  87. * @param listener the TuioListener to remove
  88. */
  89. void removeTuioListener(TuioListener *listener);
  90. /**
  91. * Removes all TuioListener from the list of registered TUIO event listeners
  92. */
  93. void removeAllTuioListeners() {
  94. listenerList.clear();
  95. }
  96. /**
  97. * Returns a List of all currently active TuioObjects
  98. *
  99. * @return a List of all currently active TuioObjects
  100. */
  101. std::list<TuioObject*> getTuioObjects();
  102. /**
  103. * Returns a List of all currently active TuioCursors
  104. *
  105. * @return a List of all currently active TuioCursors
  106. */
  107. std::list<TuioCursor*> getTuioCursors();
  108. /**
  109. * Returns the TuioObject corresponding to the provided Session ID
  110. * or NULL if the Session ID does not refer to an active TuioObject
  111. *
  112. * @return an active TuioObject corresponding to the provided Session ID or NULL
  113. */
  114. TuioObject* getTuioObject(long s_id);
  115. /**
  116. * Returns the TuioCursor corresponding to the provided Session ID
  117. * or NULL if the Session ID does not refer to an active TuioCursor
  118. *
  119. * @return an active TuioCursor corresponding to the provided Session ID or NULL
  120. */
  121. TuioCursor* getTuioCursor(long s_id);
  122. /**
  123. * Locks the TuioObject list in order to avoid updates during access
  124. */
  125. void lockObjectList();
  126. /**
  127. * Releases the lock of the TuioObject list
  128. */
  129. void unlockObjectList();
  130. /**
  131. * Locks the TuioCursor list in order to avoid updates during access
  132. */
  133. void lockCursorList();
  134. /**
  135. * Releases the lock of the TuioCursor list
  136. */
  137. void unlockCursorList();
  138. void ProcessPacket( const char *data, int size, const IpEndpointName &remoteEndpoint );
  139. UdpListeningReceiveSocket *socket;
  140. protected:
  141. void ProcessBundle( const osc::ReceivedBundle& b, const IpEndpointName& remoteEndpoint);
  142. /**
  143. * The OSC callback method where all TUIO messages are received and decoded
  144. * and where the TUIO event callbacks are dispatched
  145. *
  146. * @param message the received OSC message
  147. * @param remoteEndpoint the received OSC message origin
  148. */
  149. void ProcessMessage( const osc::ReceivedMessage& message, const IpEndpointName& remoteEndpoint);
  150. private:
  151. std::list<TuioListener*> listenerList;
  152. std::list<TuioObject*> objectList, frameObjects;
  153. std::list<long> aliveObjectList;
  154. std::list<TuioCursor*> cursorList, frameCursors;
  155. std::list<long> aliveCursorList;
  156. osc::int32 currentFrame;
  157. TuioTime currentTime;
  158. std::list<TuioCursor*> freeCursorList, freeCursorBuffer;
  159. int maxCursorID;
  160. #ifndef WIN32
  161. pthread_t thread;
  162. pthread_mutex_t objectMutex;
  163. pthread_mutex_t cursorMutex;
  164. //pthread_mutexattr_t attr_p;
  165. #else
  166. HANDLE thread;
  167. HANDLE objectMutex;
  168. HANDLE cursorMutex;
  169. #endif
  170. bool locked;
  171. bool connected;
  172. };
  173. };
  174. #endif /* INCLUDED_TUIOCLIENT_H */