root / TUIO_CPP / TUIO / TuioServer.h @ 91

View | Annotate | Download (12.6 KB)

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