Debian Codegraph @

src/core/ulib_opsc.cpp

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: ulib_opsc.cpp
28 // desc: class library for open sound control
29 //
30 // author: Philip L. Davidson ([email protected])
31 // Ge Wang ([email protected])
32 // Perry R. Cook ([email protected])
33 // date: Spring 2005
34 //-----------------------------------------------------------------------------
35
36 // lo.h ----> lo_osc_types.h needs to import <cstdint> before running itself,
37 // but ONLY when compiling this file, and NOT when compiling liblo .c files
38 // so, set a flag | REFACTOR-2017
39 #include "chuck_def.h" // to pick up platform macros
40 #ifdef __PLATFORM_WINDOWS__
41 #define ULIB_OPSC_CPP
42 #endif
43
44 #ifdef __UNITY_FLAT__
45 #include "lo.h"
46 #else
47 #include "lo/lo.h"
48 #endif
49
50 // on windows, the above must remain above these includes, else winsock errors
51 #include "ulib_opsc.h"
52 #include "chuck_compile.h"
53 #include "chuck_instr.h"
54 #include "chuck_type.h"
55 #include "chuck_vm.h"
56 #include "util_buffers.h"
57 #include "util_opsc.h"
58 #include "util_string.h"
59 #include "util_thread.h"
60
61 #include <algorithm> // 1.5.1.3 | for find
62
63 #if _MSC_VER
64 #define snprintf _snprintf
65 #endif
66
67
68 // Osc DL implementation
69 CK_DLL_CTOR( osc_send_ctor );
70 CK_DLL_DTOR( osc_send_dtor );
71 CK_DLL_MFUN( osc_send_setHost );
72 CK_DLL_MFUN( osc_send_startMesg );
73 CK_DLL_MFUN( osc_send_startMesg_spec );
74 CK_DLL_MFUN( osc_send_addInt );
75 CK_DLL_MFUN( osc_send_addFloat );
76 CK_DLL_MFUN( osc_send_addString );
77 CK_DLL_MFUN( osc_send_openBundle );
78 CK_DLL_MFUN( osc_send_closeBundle );
79 CK_DLL_MFUN( osc_send_holdMesg );
80 CK_DLL_MFUN( osc_send_kickMesg );
81
82 CK_DLL_CTOR( osc_address_ctor );
83 CK_DLL_DTOR( osc_address_dtor );
84 CK_DLL_MFUN( osc_address_set );
85 CK_DLL_MFUN( osc_address_can_wait );
86 CK_DLL_MFUN( osc_address_has_mesg );
87 CK_DLL_MFUN( osc_address_next_mesg );
88 CK_DLL_MFUN( osc_address_next_int );
89 CK_DLL_MFUN( osc_address_next_float );
90 CK_DLL_MFUN( osc_address_next_string );
91
92 CK_DLL_CTOR( osc_recv_ctor );
93 CK_DLL_DTOR( osc_recv_dtor );
94 CK_DLL_MFUN( osc_recv_port );
95 CK_DLL_MFUN( osc_recv_new_address );
96 CK_DLL_MFUN( osc_recv_new_address_type );
97 CK_DLL_MFUN( osc_recv_add_address );
98 CK_DLL_MFUN( osc_recv_remove_address );
99 CK_DLL_MFUN( osc_recv_listen );
100 CK_DLL_MFUN( osc_recv_listen_port );
101 CK_DLL_MFUN( osc_recv_listen_stop );
102
103
104 // typetag characters
105 const std::string CK_OSC_TYPETAG_CHARS = "ifs";
106
107
108
109
110 //-----------------------------------------------------------------------------
111 // name: struct OSC message
112 // desc: internal OSC message struct
113 //-----------------------------------------------------------------------------
114 struct OscMsg
115 {
116 std::string path;
117 std::string type;
118
119 // an argument, of type int, float, or string
120 struct OscArg
121 {
122 OscArg() : i( 0 ), f( 0 ), s() { }
123 OscArg( int _i ) : i( _i ), f( 0 ), s() { }
124 OscArg( float _f ) : i( 0 ), f( _f ), s() { }
125 OscArg( const std::string & _s ) : i( 0 ), f( 0 ), s( _s ) { }
126
127 int i;
128 float f;
129 std::string s;
130 };
131
132 // vector of args
133 std::vector<OscArg> args;
134 };
135
136
137
138
139 //-----------------------------------------------------------------------------
140 // TODO: use this to cache ck objects to not leak memory so rampantly
141 //-----------------------------------------------------------------------------
142 struct CkOscMsg
143 {
144 OscMsg msg;
145
146 Chuck_String * path;
147 Chuck_String * type;
148 std::vector<Chuck_Object> args;
149 };
150
151
152
153
154 // forward reference
155 class OscInServer;
156
157
158
159
160
161 //-----------------------------------------------------------------------------
162 // name: class OscIn
163 // desc: data structure for OscIn implementation
164 //-----------------------------------------------------------------------------
165 class OscIn : public Chuck_VM_Object
166 {
167 public:
168 // constructor
169 OscIn( Chuck_Event * event, Chuck_VM * vm ) :
170 m_event( event ),
171 m_vm( vm ),
172 m_port( -1 ),
173 m_oscMsgBuffer( CircularBuffer<OscMsg>( 1024 ) )
174 {
175 // create event buffer from VM
176 m_eventBuffer = m_vm->create_event_buffer();
177 // add references | 1.5.1.3
178 CK_SAFE_ADD_REF( m_vm );
179 CK_SAFE_ADD_REF( m_event );
180 }
181
182 // destructor | 1.5.1.3 (ge) reworked in tracking down this years-old issue
183 //-------------------------------------------------------------------------
184 // https://github.com/ccrma/chuck/issues/93 (filed 2017; resolved 2013)
185 // FYI issue was caused by two related problems, especially when multiple
186 // OscIn and shreds are listening to the same address:
187 // 1) audio thread deletes OscIn; server thread may still call it afterwards
188 // 2) previously ADD_METHOD, REMOVE_METHOD, REMOVEALL_METHODS do not
189 // correctly handle multiple client OscIn listening for same OSC method
190 // ---
191 // CLEANUP address 1) race condition between audio thread & server thread
192 // -> (chuck) OscIn object deletion
193 // -> (c++; VM thread) trigger this shutdown_begin()
194 // - which sends a message to unregister all methods from server
195 // - OscIn, an internal data structure no visibile in from chuck
196 // now inherits Chuck_VM_Object to use its reference counting
197 // - also sends a message to release ref count on this client
198 // - OscIn release ref count on this OscIn (from oscin_dtor())
199 // -> (c++; server thread) eventually processes the unregistering of methods
200 // - race condition: this thread may be within OscIn's handler with stale data
201 // (previously caused a crash before we moved to reference-counting OscIn)
202 // - now the there is a mechanism to release ref count on this OscIn
203 // (this OscIn should be deleted only when both threads are done with it)
204 // ---
205 // (SERVER DISPATCH REWORK addresses part 2), throughout OscInServer above)
206 //-------------------------------------------------------------------------
207 ~OscIn()
208 {
209 // delete event buffer (see shutdown_begin() for more explanation)
210 m_vm->destroy_event_buffer( m_eventBuffer );
211 // zero the pointer
212 m_eventBuffer = NULL;
213
214 // release VM and even treference
215 CK_SAFE_RELEASE( m_vm );
216 CK_SAFE_RELEASE( m_event );
217 }
218
219 void shutdown_begin()
220 {
221 // detach event buffer from VM without deleting yet
222 // NOTE event buffer may still be in play on server thread and used in OscIn::handler()
223 m_vm->detach_event_buffer_without_delete( m_eventBuffer );
224 // unregister OSC methods
225 removeAllMethods();
226 // queue up message to release this client
227 releaseThisClient();
228 }
229
230 public:
231 // create server message: add OSC method associated with this OscIn client
232 void addMethod( const std::string & method );
233 // create server message: remove OSC method associated with this OscIn client
234 void removeMethod( const std::string & method );
235 // create server message: remove all OSC method associated with this OscIn client
236 void removeAllMethods();
237 // create server message: release ref count this OscIn client
238 void releaseThisClient();
239
240 // get next message from buffer; return false if currently empty
241 t_CKBOOL get( OscMsg & msg )
242 {
243 return m_oscMsgBuffer.get( msg );
244 }
245
246 // set port; get port; also primes the port's server
247 int port( int port );
248
249 // get port (FYI implemented below since it needs to access OscInServer)
250 int port();
251
252 // handle get port notification from server (for auto-assigned ports)
253 void portNotification( int port );
254
255 public:
256 // called by OscInServer to process a message
257 // handles an incoming OSC message; prep data and queue up event broadcasting
258 int handler( const char * path, const char * types, lo_arg ** argv, int argc, lo_message _msg )
259 {
260 OscMsg msg;
261 msg.path = path;
262 msg.type = types;
263
264 for( int i = 0; i < argc; i++ )
265 {
266 OscMsg::OscArg arg;
267
268 switch( types[i] )
269 {
270 case 'i': arg.i = argv[i]->i; break;
271 case 'f': arg.f = argv[i]->f; break;
272 case 's': arg.s = &(argv[i]->s); break;
273 default:
274 EM_error3( "OscIn: error: unhandled OSC type '%c'", types[i] );
275 }
276
277 msg.args.push_back( arg );
278 }
279
280 // put into msg buffer
281 m_oscMsgBuffer.put( msg );
282 // queue event broadcast; FYI this handler will most likely be called from server thread
283 // instead of audio thread, so queue_event() instead of directly manipulating event
284 m_vm->queue_event( m_event, 1, m_eventBuffer );
285
286 // why -1? something liblo expects?
287 return -1;
288 }
289
290 private:
291 // VM reference
292 Chuck_VM * m_vm;
293 // event reference
294 Chuck_Event * m_event;
295 // port number
296 int m_port;
297 // event buffer
298 CBufferSimple * m_eventBuffer;
299 // OSC message buffer (stores incoming messages, until chuck calls recv to drain buffer)
300 CircularBuffer<OscMsg> m_oscMsgBuffer;
301 };
302
303
304
305
306 //-----------------------------------------------------------------------------
307 // name: struct OscInServer
308 // desc: OSC incoming server
309 //-----------------------------------------------------------------------------
310 class OscInServer
311 {
312 public:
313 // get server for a particular port; OscInServer constructor is designated
314 // private, so this is the intended and only way to get OscInServer instance
315 static OscInServer * forPort( int port )
316 {
317 // lock
318 s_portMapMutex.acquire();
319 // if no server on the port
320 if( s_oscIn.count( port ) == 0 )
321 {
322 // make a new server and map to port
323 s_oscIn[port] = new OscInServer( port );
324 }
325 // the thing to return
326 OscInServer * server = s_oscIn[port];
327 // unlock
328 s_portMapMutex.release();
329
330 // return the server
331 return server;
332 }
333
334 // alias a server pointer into a particular port; useful only for auto-assigned ports
335 static t_CKBOOL forPortCreateAlias( int port, OscInServer * server )
336 {
337 // lock
338 s_portMapMutex.acquire();
339 // if no server on the port
340 if( s_oscIn.count( port ) != 0 )
341 {
342 // print warning
343 EM_error3( "(OscIn) warning: cannot create server alias on port %i...", port );
344 // done
345 return FALSE;
346 }
347 // make a new server and map to port
348 s_oscIn[port] = server;
349 // unlock
350 s_portMapMutex.release();
351
352 // return success
353 return TRUE;
354 }
355
356 // shutdown
357 static void shutdownAll()
358 {
359 // TODO
360 }
361
362 // add OSC method, e.g., "/foo/frequency"
363 void addMethod( const std::string & method, OscIn * obj )
364 {
365 // message to server
366 OscInMsg msg;
367 // message type
368 msg.msg_type = OscInMsg::ADD_METHOD;
369 // parse from method to path and type
370 methodToPathAndType( method, msg.path, msg.nopath, msg.type, msg.notype );
371 // copy oscin obj ref
372 msg.obj = obj;
373 // copy msg into message buffer
374 m_inMsgBuffer.put( msg );
375 }
376
377 void removeMethod( const std::string & method, OscIn * obj )
378 {
379 // message to server
380 OscInMsg msg;
381 // message type
382 msg.msg_type = OscInMsg::REMOVE_METHOD;
383 // parse from method to path and type
384 methodToPathAndType( method, msg.path, msg.nopath, msg.type, msg.notype );
385 // copy oscin obj ref
386 msg.obj = obj;
387 // copy msg into message buffer
388 m_inMsgBuffer.put( msg );
389 }
390
391 void removeAllMethods( OscIn * obj )
392 {
393 // message to server
394 OscInMsg msg;
395 // message type
396 msg.msg_type = OscInMsg::REMOVEALL_METHODS;
397 // copy oscin obj ref
398 msg.obj = obj;
399 // copy msg into message buffer
400 m_inMsgBuffer.put( msg );
401 }
402
403 // release ref for the specified client (OscIn)
404 void releaseClient( OscIn * obj )
405 {
406 // message to server
407 OscInMsg msg;
408 // message type
409 msg.msg_type = OscInMsg::RELEASE_CLIENT;
410 // copy oscin obj ref
411 msg.obj = obj;
412 // copy msg into message buffer
413 m_inMsgBuffer.put( msg );
414 }
415
416 // return port number (might need to wait slightly for server to spin up)
417 int port()
418 {
419 // return assigned port
420 return m_assignedPort;
421 }
422
423 private:
424 // static map of port # to OscInServer
425 static std::map<int, OscInServer *> s_oscIn;
426 // static mutex to protext s_oscIn, since both audio thread calls forPort()
427 // and server thread could call forPortCreateAlias() | added 1.5.1.3
428 static XMutex s_portMapMutex;
429
430
431 //-------------------------------------------------------------------------
432 // server thread entry point
433 //-------------------------------------------------------------------------
434 #ifdef __PLATFORM_WINDOWS__
435 static unsigned int __stdcall s_server_cb( void * data )
436 #else
437 static void * s_server_cb( void * data )
438 #endif // __PLATFORM_WINDOWS__
439 {
440 OscInServer * _this = (OscInServer *)data;
441
442 #ifdef __PLATFORM_WINDOWS__
443 return (t_CKINT)_this->server_cb();
444 #else
445 return _this->server_cb();
446 #endif // __PLATFORM_WINDOWS__
447 }
448 //-------------------------------------------------------------------------
449
450 // static error handler
451 static void s_err_handler( int num, const char * msg, const char * where )
452 {
453 if( msg )
454 EM_error3( "OscIn: error: %s", msg );
455 else
456 EM_error3( "OscIn: unknown error" );
457 }
458
459 // parse method into path and type
460 static void methodToPathAndType( const std::string & method,
461 std::string & path, t_CKBOOL & nopath,
462 std::string & type, t_CKBOOL & notype )
463 {
464 // look for comma
465 t_CKINT comma_pos = method.find( ',' );
466
467 // yes comma
468 if( comma_pos != method.npos )
469 {
470 path = ltrim( rtrim( method.substr( 0, comma_pos ) ) );
471 nopath = FALSE;
472
473 std::string dirty_type = method.substr( comma_pos+1 );
474 type.clear();
475 for( int i = 0; i < dirty_type.size(); i++ )
476 if( CK_OSC_TYPETAG_CHARS.find( dirty_type[i] ) != std::string::npos )
477 type.push_back( dirty_type[i] );
478 notype = FALSE;
479 }
480 else // no comma
481 {
482 path = ltrim( rtrim( method ) );
483 nopath = (path.size() == 0);
484 notype = TRUE;
485 }
486 }
487
488 // inner struct def for an OSC server request message
489 struct OscInMsg
490 {
491 enum Type
492 {
493 NO_METHOD,
494 ADD_METHOD,
495 REMOVE_METHOD,
496 REMOVEALL_METHODS,
497 RELEASE_CLIENT, // 1.5.1.3 (ge) added
498 };
499
500 Type msg_type;
501 std::string path;
502 t_CKBOOL nopath;
503 std::string type;
504 t_CKBOOL notype;
505 OscIn * obj;
506
507 // 1.4.1.0 (ge) added
508 OscInMsg()
509 : msg_type( NO_METHOD ), nopath( FALSE ), notype( FALSE ), obj( NULL ) { }
510 };
511
512 // constructor
513 OscInServer( int port ) :
514 m_port( port ),
515 m_inMsgBuffer( CircularBuffer<OscInMsg>( 1024 ) ),
516 m_thread( XThread() ),
517 m_quit( false ),
518 m_assignedPort( -1 ),
519 m_portNotifyTargets( CircularBuffer<OscIn *>(1024) )
520 {
521 // start server on thread
522 m_thread.start( s_server_cb, this );
523 }
524
525 // server callback for a particular server
526 void * server_cb();
527
528 // data
529 XThread m_thread;
530 bool m_quit;
531 const int m_port;
532 int m_assignedPort;
533 CircularBuffer<OscInMsg> m_inMsgBuffer;
534
535 // inner struct for OSC method
536 struct Method
537 {
538 std::string path;
539 t_CKBOOL nopath;
540 std::string type;
541 t_CKBOOL notype;
542
543 // default constructor
544 Method() : nopath( FALSE ), notype( FALSE ) { }
545
546 // copy constructor
547 Method( const Method & m )
548 {
549 path = m.path;
550 nopath = m.nopath;
551 type = m.type;
552 notype = m.notype;
553 }
554
555 // == operator
556 bool operator==( const Method & m ) const
557 {
558 return path == m.path && nopath == m.nopath && type == m.type && notype == m.notype;
559 }
560
561 // < operator need in order to use as key in map
562 bool operator<( const Method & rhs ) const
563 {
564 // comparing nopath and notype works because t_CKBOOL are really ints (but we will cast anyway)
565 return (path < rhs.path || type < rhs.type ||
566 (t_CKUINT)nopath < (t_CKUINT)rhs.nopath || (t_CKUINT)notype < (t_CKUINT)rhs.notype);
567 }
568 };
569
570 // data structure to hold user_data for liblo callback on incoming message
571 struct HandlerData
572 {
573 OscInServer * server;
574 Method method;
575
576 HandlerData( OscInServer * s, const Method & m )
577 : server( s ), method( m )
578 {
579 // std::cerr << "HANDLER DATA CREATE: " << (t_CKUINT)this << std::endl;
580 }
581
582 ~HandlerData()
583 {
584 // std::cerr << "HANDLER DATA DESTROY: " << (t_CKUINT)this << std::endl;
585 }
586
587 };
588
589 // map from OscIn to its associated list of added OSC methods
590 std::map<OscIn *, std::list<Method> > m_methods;
591 // handler data map to track the handler data structre for each unique OSC method | 1.5.1.3
592 std::map<Method, HandlerData *> m_handlerDataMap;
593 // OscIns to notification for auto-assigned ports | 1.5.1.3
594 CircularBuffer<OscIn *> m_portNotifyTargets;
595
596 // assess how many TOTAL OscIn clients have added a particular OSC method | 1.5.1.3
597 t_CKUINT total_instances_in_use( const Method & method )
598 {
599 // the count
600 t_CKUINT count = 0;
601 // get iterator to methods map
602 std::map<OscIn *, std::list<Method> >::iterator m;
603 // iterate over all clients
604 for( m = m_methods.begin(); m != m_methods.end(); m++ )
605 {
606 // each entry is <OscIn *, list<Method> >
607 // if see this entry contains the method in question
608 if( std::find( m->second.begin(), m->second.end(), method ) != m->second.end() )
609 count++;
610 }
611 // done
612 return count;
613 }
614
615 // called by liblo to handle a message | 1.5.1.3
616 static int s_handler( const char * path, const char * types,
617 lo_arg ** argv, int argc,
618 lo_message msg, void * user_data )
619 {
620 // our user data
621 HandlerData * data = (HandlerData *)user_data;
622 // call to dispatch message
623 data->server->dispatch_to_client( path, types, argv, argc, msg, data->method );
624
625 // not sure what to return, OscIn::handler() returned -1 so...
626 return -1;
627 }
628
629 public:
630 // is this server tracking a particular client (OscIn) | 1.5.1.3
631 t_CKBOOL is_tracking( OscIn * obj )
632 {
633 return m_methods.count( obj ) > 0;
634 }
635
636 // attach port auto-assign notify target
637 void attachPortNotification( OscIn * client )
638 {
639 // check for NULL
640 if( !client ) return;
641 // put on thread-safe buffer
642 m_portNotifyTargets.put( client );
643 // add ref
644 CK_SAFE_ADD_REF( client );
645 }
646
647 // dispatch an incoming OSC message on this port to all clients listening | 1.5.1.3
648 void dispatch_to_client( const char * path, const char * types,
649 lo_arg ** argv, int argc,
650 lo_message msg, const Method & method )
651 {
652 // get iterator to methods map
653 std::map<OscIn *, std::list<Method> >::iterator m;
654 // iterate over all clients
655 for( m = m_methods.begin(); m != m_methods.end(); m++ )
656 {
657 // each entry is <OscIn *, list<Method> >
658 // FILTER if see this entry contains the method in question
659 if( std::find( m->second.begin(), m->second.end(), method ) != m->second.end() )
660 {
661 // call this entry/client's handler
662 m->first->handler( path, types, argv, argc, msg );
663 }
664 }
665 }
666 };
667
668 // static instantiation: map of port to OscInServer
669 std::map<int, OscInServer *> OscInServer::s_oscIn;
670 // static instantiation: mutex to protect map
671 XMutex OscInServer::s_portMapMutex;
672
673
674
675
676 //-----------------------------------------------------------------------------
677 // create server message: add OSC method associated with this OscIn client
678 //-----------------------------------------------------------------------------
679 void OscIn::addMethod( const std::string & method )
680 {
681 OscInServer::forPort( m_port )->addMethod( method, this );
682 }
683
684 //-----------------------------------------------------------------------------
685 // create server message: remove OSC method associated with this OscIn client
686 //-----------------------------------------------------------------------------
687 void OscIn::removeMethod( const std::string & method )
688 {
689 OscInServer::forPort( m_port )->removeMethod( method, this );
690 }
691
692 //-----------------------------------------------------------------------------
693 // create server message: remove all OSC method associated with this OscIn client
694 //-----------------------------------------------------------------------------
695 void OscIn::removeAllMethods()
696 {
697 OscInServer::forPort( m_port )->removeAllMethods( this );
698 }
699
700 //-----------------------------------------------------------------------------
701 // create server message: release ref count this OscIn client
702 //-----------------------------------------------------------------------------
703 void OscIn::releaseThisClient()
704 {
705 OscInServer::forPort( m_port )->releaseClient( this );
706 }
707
708 //-----------------------------------------------------------------------------
709 // set port; get port (this also begins priming the port)
710 //-----------------------------------------------------------------------------
711 int OscIn::port( int port )
712 {
713 // if same, then do nothing
714 if( m_port == port ) return port;
715
716 // if we have an existing port, including 0
717 if( m_port >= 0 )
718 {
719 // request to unsubscribe from current server
720 this->removeAllMethods();
721 }
722
723 // set port
724 m_port = port;
725
726 // if port > 0
727 if( port > 0 )
728 {
729 // call forPort() to get that port's server going
730 OscInServer::forPort( port );
731 }
732 else if( port == 0 ) // auto-assign
733 {
734 // get server on port 0 (the auto-assign slot)
735 OscInServer * server = OscInServer::forPort( 0 );
736 // if already been assigned, and we are done
737 if( server->port() > 0 ) m_port = server->port();
738 // else need to be notified
739 else server->attachPortNotification( this );
740 }
741 else // port < 0
742 {
743 // set back to -1
744 m_port = -1;
745 }
746
747 return m_port;
748 }
749
750 //-----------------------------------------------------------------------------
751 // get port (if 0 => port, then return assigned port
752 //-----------------------------------------------------------------------------
753 int OscIn::port()
754 {
755 // 1.5.0.0 (ge) simply return the current m_port
756 // 1.5.1.3 (ge) if port was set to 0 for auto-assigned port, this variable
757 // will be notified when the port # is obtained in the server
758 // (which is asynchronous relative to the audio thread)
759 return m_port;
760 }
761
762 //-----------------------------------------------------------------------------
763 // handle get port notification from server (for auto-assigned ports)
764 //-----------------------------------------------------------------------------
765 void OscIn::portNotification( int port )
766 {
767 // already have port number, ignore notification (was updated already or set to another port #
768 if( m_port > 0 ) return;
769 // copy
770 m_port = port;
771 }
772
773
774
775
776 //-----------------------------------------------------------------------------
777 // name: server_cb()
778 // desc: instanced (one per port) server processing loop
779 //-----------------------------------------------------------------------------
780 void * OscInServer::server_cb()
781 {
782 // liblo server handle
783 lo_server m_server = NULL;
784
785 // check the port request
786 if( m_port > 0 )
787 {
788 char portStr[32];
789 snprintf( portStr, 32, "%d", m_port );
790 // create server instance
791 m_server = lo_server_new( portStr, s_err_handler );
792 }
793 else
794 {
795 // create server instance with NULL port; system will choose unused port
796 // (internally, liblo tries up to ~16 random in-range numbers; there is
797 // a small chance this will fail)
798 m_server = lo_server_new( NULL, s_err_handler );
799 }
800
801 // check
802 if( m_server == NULL )
803 {
804 EM_error3( "OscIn: error: unable to create server instance" );
805 return NULL;
806 }
807
808 // get the actual port in use
809 m_assignedPort = lo_server_get_port( m_server );
810 // if port was 0, then need to also put this server into the auto-assigned port slot
811 if( m_port == 0 && m_assignedPort > 0 )
812 {
813 // create alias
814 OscInServer::forPortCreateAlias( m_assignedPort, this );
815 }
816
817 // log
818 EM_log( CK_LOG_INFO, "OscIn: starting OSC server at port '%d'", m_assignedPort );
819
820 // loop
821 while( !m_quit )
822 {
823 OscIn * client = NULL;
824 // drain thread-safe buffer of clients to notify
825 while( m_portNotifyTargets.get( client ) )
826 {
827 // notify
828 client->portNotification( m_assignedPort );
829 // release
830 CK_SAFE_RELEASE( client );
831 }
832
833 OscInMsg msg;
834 // get the next message
835 while( m_inMsgBuffer.get( msg ) )
836 {
837 switch( msg.msg_type )
838 {
839 case OscInMsg::NO_METHOD: // 1.4.1.0 (ge) added
840 {
841 // just skip
842 continue;
843 }
844 break;
845
846 case OscInMsg::ADD_METHOD:
847 {
848 Method m;
849 m.path = msg.path;
850 m.nopath = msg.nopath;
851 m.type = msg.type;
852 m.notype = msg.notype;
853
854 // check how many different OscIn care about this OSC method on this server | 1.5.1.3
855 if( total_instances_in_use( m ) == 0 )
856 {
857 // instantiate handler data
858 HandlerData * data = new HandlerData( this, m );
859 // add to map so we can delete later
860 m_handlerDataMap[m] = data;
861 // add method
862 if( !lo_server_add_method( m_server,
863 msg.nopath ? NULL : msg.path.c_str(),
864 msg.notype ? NULL : msg.type.c_str(),
865 OscInServer::s_handler, (void *)data ) )
866 {
867 EM_error3( "OscIn: error: add_method failed for %s, %s", msg.path.c_str(), msg.type.c_str() );
868 }
869 }
870
871 // if no entry for this client OscIn, add one
872 if( !m_methods.count( msg.obj ) )
873 {
874 // add ref for the first
875 CK_SAFE_ADD_REF( msg.obj );
876 // create entry with empty list
877 m_methods[msg.obj] = std::list<Method>();
878 }
879
880 // check whether already added for this client
881 if( std::find( m_methods[msg.obj].begin(), m_methods[msg.obj].end(), m ) == m_methods[msg.obj].end() )
882 {
883 // if not already added, add OSC method to list
884 m_methods[msg.obj].push_back( m );
885 }
886 }
887 break;
888
889 case OscInMsg::REMOVE_METHOD:
890 {
891 if( m_methods.count( msg.obj ) )
892 {
893 Method m;
894 m.path = msg.path;
895 m.nopath = msg.nopath;
896 m.type = msg.type;
897 m.notype = msg.notype;
898
899 // check this client cares
900 if( std::find( m_methods[msg.obj].begin(), m_methods[msg.obj].end(), m ) != m_methods[msg.obj].end() )
901 {
902 // remove from ourselves
903 m_methods[msg.obj].remove( m );
904 }
905
906 // check how many different OscIn still care about this OSC method on this server
907 t_CKUINT count = total_instances_in_use( m );
908 // no one cares; no one is thinking about you (on the positive side, as one grows older, this realization can be quite liberating)
909 if( count == 0 )
910 {
911 // remove from liblo
912 lo_server_del_method( m_server,
913 msg.nopath ? NULL : msg.path.c_str(),
914 msg.notype ? NULL : msg.type.c_str() );
915 // free memory
916 CK_SAFE_DELETE( m_handlerDataMap[m] );
917 // erase the entry
918 m_handlerDataMap.erase( m );
919 }
920 }
921 }
922 break;
923
924 case OscInMsg::REMOVEALL_METHODS:
925 {
926 // if there is an entry for the client OscIn
927 if( m_methods.count( msg.obj ) )
928 {
929 // iterate
930 std::list<Method>::iterator i;
931 for( i = m_methods[msg.obj].begin(); i != m_methods[msg.obj].end(); i++ )
932 {
933 // check how many different OscIn care about this OSC method on this server
934 t_CKUINT count = total_instances_in_use( *i );
935 // count == 1 means we are the only one
936 // count > 1 means others care as well so we should leave it be)
937 if( count == 1 )
938 {
939 // delete OSC method from liblo
940 lo_server_del_method( m_server,
941 i->nopath ? NULL : i->path.c_str(),
942 i->notype ? NULL : i->type.c_str() );
943 // free memory for handler data
944 CK_SAFE_DELETE( m_handlerDataMap[*i] );
945 // erase the entry from the handler data map
946 m_handlerDataMap.erase( *i );
947 }
948 }
949
950 // clear the contents of the list
951 m_methods[msg.obj].clear();
952 // erase the entry in the methods map
953 m_methods.erase( msg.obj );
954 }
955 }
956 break;
957
958 // release ref count for client | 1.5.1.3
959 case OscInMsg::RELEASE_CLIENT:
960 {
961 // release ref | 1.5.1.3
962 CK_SAFE_RELEASE( msg.obj );
963 }
964 break;
965 }
966 }
967
968 // FYI this should be the server thread (not audio thread)
969 // check for new message with a 2ms timeout; there are are incoming message,
970 // a mechanism within this call will call us back on s_handler()
971 lo_server_recv_noblock( m_server, 2 );
972 }
973
974 // done with this OscInServer
975 lo_server_free( m_server );
976
977 // local
978 EM_log( CK_LOG_INFO, "OscIn: shutting down OSC server on port %i", m_assignedPort );
979
980 return NULL;
981 }
982
983
984
985
986 //-----------------------------------------------------------------------------
987 // name: class OscOut
988 // desc: OSC sender, internal data structure
989 //-----------------------------------------------------------------------------
990 class OscOut
991 {
992 public:
993 // constructor
994 OscOut()
995 {
996 m_address = NULL;
997 m_message = NULL;
998 // 1.4.1.1 (ge) added
999 m_port = 0;
1000 }
1001
1002 // destructor
1003 ~OscOut()
1004 {
1005 // if we have one
1006 if( m_address != NULL )
1007 {
1008 // free address
1009 lo_address_free( m_address );
1010 // set to NULL
1011 m_address = NULL;
1012 }
1013
1014 // if we have unsent message
1015 if( m_message != NULL )
1016 {
1017 // free message
1018 lo_message_free( m_message );
1019 // set to NULL
1020 m_message = NULL;
1021 }
1022 }
1023
1024 // where to aim the transmission
1025 t_CKBOOL setDestination( const std::string & host, int port )
1026 {
1027 if( m_address != NULL )
1028 {
1029 lo_address_free( m_address );
1030 m_address = NULL;
1031 }
1032
1033 char portStr[32];
1034 snprintf( portStr, 32, "%d", port );
1035
1036 // 1.4.1.1 (ge) remember current hostname and port
1037 m_hostname = host;
1038 m_port = port;
1039
1040 // allocate address
1041 m_address = lo_address_new( host.c_str(), portStr );
1042
1043 // check it
1044 if( m_address == NULL )
1045 {
1046 const char * msg = ""; // lo_address_errstr(NULL); // 1.4.1.0 commented out
1047 EM_error3( "OscOut: error: failed to set destination address '%s:%d'%s%s",
1048 host.c_str(), port, msg ? ": " : "", msg ? msg : "" );
1049 return FALSE;
1050 }
1051
1052 return TRUE;
1053 }
1054
1055 // start, with OSC method
1056 t_CKBOOL start( const std::string & path, const std::string & arg )
1057 {
1058 return start( path + "," + arg );
1059 }
1060
1061 // start, with OSC method
1062 t_CKBOOL start( const std::string & method )
1063 {
1064 std::string path;
1065 t_CKINT comma_pos = method.find( ',' );
1066 if( comma_pos != method.npos ) m_path = method.substr( 0, comma_pos );
1067 else m_path = method;
1068
1069 // clean up if there is an unsent message | 1.5.1.3
1070 if( m_message != NULL ) lo_message_free( m_message );
1071
1072 // allocate new messaage
1073 m_message = lo_message_new();
1074
1075 // return whether msg is NULL | 1.5.1.3
1076 return m_message != NULL;
1077 }
1078
1079 // add an integer argument
1080 t_CKBOOL add( t_CKINT i )
1081 {
1082 if( m_message == NULL )
1083 {
1084 EM_error3( "OscOut: error: attempt to add argument to message with no OSC address" );
1085 return FALSE;
1086 }
1087
1088 lo_message_add_int32( m_message, (int32_t)i );
1089
1090 return TRUE;
1091 }
1092
1093 // add a floating point argument
1094 t_CKBOOL add( t_CKFLOAT f )
1095 {
1096 if( m_message == NULL )
1097 {
1098 EM_error3( "OscOut: error: attempt to add argument to message with no OSC address" );
1099 return FALSE;
1100 }
1101
1102 lo_message_add_float( m_message, (float)f );
1103
1104 return TRUE;
1105 }
1106
1107 // add a string argument
1108 t_CKBOOL add( const std::string & s )
1109 {
1110 if( m_message == NULL )
1111 {
1112 EM_error3( "OscOut: error: attempt to add argument to message with no OSC address" );
1113 return FALSE;
1114 }
1115
1116 lo_message_add_string( m_message, s.c_str() );
1117
1118 return TRUE;
1119 }
1120
1121 // send the message
1122 t_CKBOOL send()
1123 {
1124 // check if we have necessary info
1125 if( m_message == NULL || m_address == NULL || m_path.size() == 0 )
1126 {
1127 EM_error3( "OscOut: error: attempt to send message with no destination or OSC address" );
1128 return FALSE;
1129 }
1130
1131 // send message
1132 int result = lo_send_message( m_address, m_path.c_str(), m_message );
1133
1134 // free message
1135 lo_message_free( m_message );
1136 // set to NULL
1137 m_message = NULL;
1138
1139 // check result
1140 if( result == -1 )
1141 {
1142 const char * msg = lo_address_errstr( m_address );
1143 // 1.4.1.1 (ge) updated to also print out hostname:port
1144 EM_error3( "OscOut: error: sending OSC message%s%s (%s:%d)",
1145 msg ? ": " : "", msg ? msg : "", m_hostname.c_str(), m_port );
1146 return FALSE;
1147 }
1148
1149 return TRUE;
1150 }
1151
1152 private:
1153 lo_address m_address;
1154 std::string m_path;
1155 lo_message m_message;
1156 // 1.4.1.1 (ge) added to print hostname:port in error reporting
1157 std::string m_hostname;
1158 int m_port;
1159 };
1160
1161 // data offset for OscIn
1162 static t_CKUINT oscin_offset_data = 0;
1163 // data offset for OscOut
1164 static t_CKUINT oscout_offset_data = 0;
1165 // data offsets for OscMsg
1166 static t_CKUINT oscmsg_offset_address = 0;
1167 static t_CKUINT oscmsg_offset_typetag = 0;
1168 static t_CKUINT oscmsg_offset_args = 0;
1169 // data offsets for OscArg
1170 static t_CKUINT oscarg_offset_type = 0;
1171 static t_CKUINT oscarg_offset_i = 0;
1172 static t_CKUINT oscarg_offset_f = 0;
1173 static t_CKUINT oscarg_offset_s = 0;
1174
1175
1176
1177
1178 #pragma mark - OscOut
1179 //-----------------------------------------------------------------------------
1180 // name: OscOut
1181 //-----------------------------------------------------------------------------
1182 CK_DLL_CTOR( oscout_ctor )
1183 {
1184 OBJ_MEMBER_INT( SELF, oscout_offset_data ) = (t_CKINT)new OscOut;
1185 }
1186
1187 CK_DLL_DTOR( oscout_dtor )
1188 {
1189 OscOut * out = (OscOut *)OBJ_MEMBER_INT( SELF, oscout_offset_data );
1190 CK_SAFE_DELETE( out );
1191 OBJ_MEMBER_INT( SELF, oscout_offset_data ) = 0;
1192 }
1193
1194 CK_DLL_MFUN( oscout_dest )
1195 {
1196 OscOut * out = (OscOut *)OBJ_MEMBER_INT( SELF, oscout_offset_data );
1197
1198 Chuck_String * host = GET_NEXT_STRING( ARGS );
1199 t_CKINT port = GET_NEXT_INT( ARGS );
1200
1201 if( host == NULL )
1202 {
1203 ck_throw_exception( SHRED, "NullPointer", "OscOut.start: argument 'host' is null" );
1204 goto error;
1205 }
1206
1207 if( !out->setDestination( host->str(), port ) )
1208 goto error;
1209
1210 RETURN->v_object = SELF;
1211
1212 return;
1213
1214 error:
1215 RETURN->v_object = NULL;
1216 }
1217
1218 CK_DLL_MFUN( oscout_start )
1219 {
1220 OscOut * out = (OscOut *)OBJ_MEMBER_INT( SELF, oscout_offset_data );
1221
1222 Chuck_String * method = GET_NEXT_STRING( ARGS );
1223
1224 if( method == NULL )
1225 {
1226 ck_throw_exception( SHRED, "NullPointer", "OscOut.start: argument 'method' is null" );
1227 goto error;
1228 }
1229
1230 if( !out->start( method->str() ) )
1231 goto error;
1232
1233 RETURN->v_object = SELF;
1234
1235 return;
1236
1237 error:
1238 RETURN->v_object = NULL;
1239 }
1240
1241 CK_DLL_MFUN( oscout_startDest )
1242 {
1243 OscOut * out = (OscOut *)OBJ_MEMBER_INT( SELF, oscout_offset_data );
1244
1245 Chuck_String * method = GET_NEXT_STRING( ARGS );
1246 Chuck_String * host = GET_NEXT_STRING( ARGS );
1247 t_CKINT port = GET_NEXT_INT( ARGS );
1248
1249 if( method == NULL )
1250 {
1251 ck_throw_exception( SHRED, "NullPointer", "OscOut.start: argument 'method' is null" );
1252 goto error;
1253 }
1254
1255 if( host == NULL )
1256 {
1257 ck_throw_exception( SHRED, "NullPointer", "OscOut.start: argument 'host' is null" );
1258 goto error;
1259 }
1260
1261 if( !out->setDestination( host->str(), port ) )
1262 goto error;
1263
1264 if( !out->start( method->str() ) )
1265 goto error;
1266
1267 RETURN->v_object = SELF;
1268
1269 return;
1270
1271 error:
1272 RETURN->v_object = NULL;
1273 }
1274
1275 CK_DLL_MFUN( oscout_addInt )
1276 {
1277 OscOut * out = (OscOut *)OBJ_MEMBER_INT( SELF, oscout_offset_data );
1278
1279 t_CKINT i = GET_NEXT_INT( ARGS );
1280
1281 if( !out->add( i ) )
1282 goto error;
1283
1284 RETURN->v_object = SELF;
1285
1286 return;
1287
1288 error:
1289 RETURN->v_object = NULL;
1290 }
1291
1292 CK_DLL_MFUN( oscout_addFloat )
1293 {
1294 OscOut * out = (OscOut *)OBJ_MEMBER_INT( SELF, oscout_offset_data );
1295
1296 t_CKFLOAT f = GET_NEXT_FLOAT( ARGS );
1297
1298 if( !out->add( f ) )
1299 goto error;
1300
1301 RETURN->v_object = SELF;
1302
1303 return;
1304
1305 error:
1306 RETURN->v_object = NULL;
1307 }
1308
1309 CK_DLL_MFUN( oscout_addString )
1310 {
1311 OscOut * out = (OscOut *)OBJ_MEMBER_INT( SELF, oscout_offset_data );
1312
1313 Chuck_String * s = GET_NEXT_STRING( ARGS );
1314
1315 if( !out->add( s->str() ) )
1316 goto error;
1317
1318 RETURN->v_object = SELF;
1319
1320 return;
1321
1322 error:
1323 RETURN->v_object = NULL;
1324 }
1325
1326 CK_DLL_MFUN( oscout_send )
1327 {
1328 OscOut * out = (OscOut *)OBJ_MEMBER_INT( SELF, oscout_offset_data );
1329
1330 if( !out->send() )
1331 goto error;
1332
1333 RETURN->v_object = SELF;
1334
1335 return;
1336
1337 error:
1338 RETURN->v_object = NULL;
1339 }
1340
1341
1342
1343
1344 #pragma mark - OscArg
1345 //-----------------------------------------------------------------------------
1346 // name: OscArg
1347 //-----------------------------------------------------------------------------
1348 CK_DLL_CTOR( oscarg_ctor )
1349 {
1350 Chuck_String * type = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
1351 CK_SAFE_ADD_REF( type );
1352 OBJ_MEMBER_STRING( SELF, oscarg_offset_type ) = type;
1353
1354 OBJ_MEMBER_INT( SELF, oscarg_offset_i ) = 0;
1355
1356 OBJ_MEMBER_FLOAT( SELF, oscarg_offset_f ) = 0.0;
1357
1358 Chuck_String * s = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
1359 CK_SAFE_ADD_REF( s );
1360 OBJ_MEMBER_STRING( SELF, oscarg_offset_s ) = s;
1361 }
1362
1363 CK_DLL_DTOR( oscarg_dtor )
1364 {
1365 CK_SAFE_RELEASE( OBJ_MEMBER_STRING( SELF, oscarg_offset_type ) );
1366 OBJ_MEMBER_STRING( SELF, oscarg_offset_type ) = NULL;
1367
1368 CK_SAFE_RELEASE( OBJ_MEMBER_STRING( SELF, oscarg_offset_s ) );
1369 OBJ_MEMBER_STRING( SELF, oscarg_offset_s ) = NULL;
1370 }
1371
1372
1373
1374
1375 #pragma mark - OscIn
1376 //-----------------------------------------------------------------------------
1377 // name: OscIn
1378 //-----------------------------------------------------------------------------
1379 CK_DLL_CTOR( oscin_ctor )
1380 {
1381 // create internal OscIn data structure; pass in SELF (OscIn is defined as a subclass of Event)
1382 OscIn * oscin = new OscIn( (Chuck_Event *)SELF, SHRED->vm_ref );
1383 // add reference for cross-thread memory managment
1384 CK_SAFE_ADD_REF( oscin );
1385 // set into chuck object's data field
1386 OBJ_MEMBER_INT( SELF, oscin_offset_data ) = (t_CKINT)oscin;
1387 }
1388
1389 CK_DLL_DTOR( oscin_dtor )
1390 {
1391 // get internal data structure
1392 OscIn * in = (OscIn *)OBJ_MEMBER_INT( SELF, oscin_offset_data );
1393 // begin shutdown between audio thread (likely what called this) and server thread
1394 in->shutdown_begin();
1395 // release on this side
1396 CK_SAFE_RELEASE( in );
1397 // zero out
1398 OBJ_MEMBER_INT( SELF, oscin_offset_data ) = 0;
1399 }
1400
1401 CK_DLL_MFUN( oscin_setport )
1402 {
1403 // get internal data structure
1404 OscIn * in = (OscIn *)OBJ_MEMBER_INT( SELF, oscin_offset_data );
1405 // get the argument
1406 t_CKINT port = GET_NEXT_INT( ARGS );
1407 // set the port
1408 RETURN->v_int = in->port( port );
1409 }
1410
1411 CK_DLL_MFUN( oscin_getport )
1412 {
1413 // get internal data structure
1414 OscIn * in = (OscIn *)OBJ_MEMBER_INT( SELF, oscin_offset_data );
1415 // get the port
1416 RETURN->v_int = in->port();
1417 }
1418
1419 CK_DLL_MFUN( oscin_addAddress )
1420 {
1421 // get internal data structure
1422 OscIn * in = (OscIn *)OBJ_MEMBER_INT( SELF, oscin_offset_data );
1423 // get the arg
1424 Chuck_String * address = GET_NEXT_STRING( ARGS );
1425 // check the arg
1426 if( address == NULL )
1427 {
1428 ck_throw_exception( SHRED, "NullPointer", "OscIn.address: argument 'address' is null" );
1429 goto error;
1430 }
1431 // add OSC method associated with this OscIn
1432 in->addMethod( address->str() );
1433
1434 error:
1435 // nothing to do
1436 return;
1437 }
1438
1439 CK_DLL_MFUN( oscin_removeAddress )
1440 {
1441 // get internal data structure
1442 OscIn * in = (OscIn *)OBJ_MEMBER_INT( SELF, oscin_offset_data );
1443 // get the arg
1444 Chuck_String * address = GET_NEXT_STRING( ARGS );
1445 // check the arg
1446 if( address == NULL )
1447 {
1448 ck_throw_exception( SHRED, "NullPointer", "OscIn.address: argument 'address' is null" );
1449 goto error;
1450 }
1451 // remove OSC method associated with this OscIn
1452 in->removeMethod( address->str() );
1453
1454 error:
1455 // nothing to do
1456 return;
1457 }
1458
1459 CK_DLL_MFUN( oscin_removeAllAddresses )
1460 {
1461 // get internal data structure
1462 OscIn * in = (OscIn *)OBJ_MEMBER_INT( SELF, oscin_offset_data );
1463 // remove all OSC methods associated with this OscIn
1464 in->removeAllMethods();
1465 }
1466
1467 CK_DLL_MFUN( oscin_listenAll )
1468 {
1469 // get internal data structure
1470 OscIn * in = (OscIn *)OBJ_MEMBER_INT( SELF, oscin_offset_data );
1471 // add "ALL" OSC method string
1472 in->addMethod( "" );
1473 }
1474
1475 CK_DLL_MFUN( oscin_recv )
1476 {
1477 // get internal data structure
1478 OscIn * in = (OscIn *)OBJ_MEMBER_INT( SELF, oscin_offset_data );
1479 // get the OscMsg arg
1480 Chuck_Object * msg_obj = GET_NEXT_OBJECT( ARGS );
1481 // internal representation
1482 OscMsg msg;
1483
1484 // some local for later
1485 Chuck_ArrayInt * args_obj = NULL;
1486 Chuck_Type * oscarg_type = NULL;
1487 int i;
1488
1489 // check for NULL
1490 if( msg_obj == NULL )
1491 {
1492 ck_throw_exception( SHRED, "NullPointer", "OscIn.recv: argument 'msg' is null" );
1493 goto error;
1494 }
1495
1496 // get the message
1497 RETURN->v_int = in->get( msg );
1498
1499 // set it in the chuck-side OscMsg
1500 OBJ_MEMBER_STRING( msg_obj, oscmsg_offset_address )->set( msg.path );
1501 OBJ_MEMBER_STRING( msg_obj, oscmsg_offset_typetag )->set( msg.type );
1502 // get the array at OscMsg.args
1503 args_obj = (Chuck_ArrayInt *)OBJ_MEMBER_OBJECT( msg_obj, oscmsg_offset_args );
1504 // clear it (which will also memory-manage existing contents)
1505 args_obj->clear();
1506 // find the type corresponding to chuck-side OscArg
1507 oscarg_type = type_engine_find_type( SHRED->vm_ref->env(), str2list( "OscArg" ) );
1508
1509 // go through args
1510 for( i = 0; i < msg.args.size(); i++ )
1511 {
1512 // instantiate a OscArg
1513 Chuck_Object * arg_obj = instantiate_and_initialize_object( oscarg_type, SHRED );
1514 // HACK: manually call osc_arg ctor
1515 oscarg_ctor( arg_obj, NULL, VM, SHRED, API );
1516 // check message type tag and set
1517 switch( msg.type[i] )
1518 {
1519 case 'i':
1520 OBJ_MEMBER_STRING( arg_obj, oscarg_offset_type )->set( "i" );
1521 OBJ_MEMBER_INT( arg_obj, oscarg_offset_i ) = msg.args[i].i;
1522 break;
1523 case 'f':
1524 OBJ_MEMBER_STRING( arg_obj, oscarg_offset_type )->set( "f" );
1525 OBJ_MEMBER_FLOAT( arg_obj, oscarg_offset_f ) = msg.args[i].f;
1526 break;
1527 case 's':
1528 OBJ_MEMBER_STRING( arg_obj, oscarg_offset_type )->set( "s" );
1529 OBJ_MEMBER_STRING( arg_obj, oscarg_offset_s )->set( msg.args[i].s );
1530 break;
1531 }
1532 // push back into chuck-side array
1533 args_obj->push_back( (t_CKINT)arg_obj );
1534 }
1535
1536 // done
1537 return;
1538
1539 error:
1540 // return failure code
1541 RETURN->v_int = 0;
1542 }
1543
1544
1545
1546
1547 #pragma mark - OscMsg
1548 //-----------------------------------------------------------------------------
1549 // name: OscMsg
1550 //-----------------------------------------------------------------------------
1551 CK_DLL_CTOR( oscmsg_ctor )
1552 {
1553 Chuck_String * address = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
1554 CK_SAFE_ADD_REF( address );
1555 OBJ_MEMBER_STRING( SELF, oscmsg_offset_address ) = address;
1556
1557 Chuck_String * typetag = (Chuck_String *)instantiate_and_initialize_object( SHRED->vm_ref->env()->ckt_string, SHRED );
1558 CK_SAFE_ADD_REF( typetag );
1559 OBJ_MEMBER_STRING( SELF, oscmsg_offset_typetag ) = typetag;
1560
1561 Chuck_ArrayInt * args = new Chuck_ArrayInt( TRUE );
1562 initialize_object( args, SHRED->vm_ref->env()->ckt_array, SHRED, VM );
1563 args->clear();
1564 CK_SAFE_ADD_REF( args );
1565 OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args ) = args;
1566 }
1567
1568 CK_DLL_DTOR( oscmsg_dtor )
1569 {
1570 CK_SAFE_RELEASE( OBJ_MEMBER_STRING( SELF, oscmsg_offset_address ) );
1571 OBJ_MEMBER_STRING( SELF, oscmsg_offset_address ) = NULL;
1572
1573 CK_SAFE_RELEASE( OBJ_MEMBER_STRING( SELF, oscmsg_offset_typetag ) );
1574 OBJ_MEMBER_STRING( SELF, oscmsg_offset_typetag ) = NULL;
1575
1576 CK_SAFE_RELEASE( OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args ) );
1577 OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args ) = NULL;
1578 }
1579
1580 CK_DLL_MFUN( oscmsg_numArgs )
1581 {
1582 Chuck_ArrayInt * args_obj = (Chuck_ArrayInt *)OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args );
1583 RETURN->v_int = args_obj->size();
1584 }
1585
1586 CK_DLL_MFUN( oscmsg_getInt )
1587 {
1588 t_CKINT i = GET_NEXT_INT( ARGS );
1589 Chuck_ArrayInt * args_obj = (Chuck_ArrayInt *)OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args );
1590 Chuck_Object * arg_obj;
1591
1592 // check bounds
1593 if( i >= 0 && i < args_obj->size() )
1594 {
1595 args_obj->get( i, (t_CKUINT *)&arg_obj );
1596 RETURN->v_int = OBJ_MEMBER_INT( arg_obj, oscarg_offset_i );
1597 }
1598 else
1599 {
1600 RETURN->v_int = 0;
1601 }
1602 }
1603
1604 CK_DLL_MFUN( oscmsg_getFloat )
1605 {
1606 t_CKINT i = GET_NEXT_INT( ARGS );
1607 Chuck_ArrayInt * args_obj = (Chuck_ArrayInt *)OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args );
1608 Chuck_Object * arg_obj;
1609 if( i >= 0 && i < args_obj->size() )
1610 {
1611 args_obj->get( i, (t_CKUINT *)&arg_obj );
1612 RETURN->v_float = OBJ_MEMBER_FLOAT( arg_obj, oscarg_offset_f );
1613 }
1614 else
1615 RETURN->v_float = 0;
1616 }
1617
1618 CK_DLL_MFUN( oscmsg_getString )
1619 {
1620 t_CKINT i = GET_NEXT_INT( ARGS );
1621 Chuck_ArrayInt * args_obj = (Chuck_ArrayInt *)OBJ_MEMBER_OBJECT( SELF, oscmsg_offset_args );
1622 Chuck_Object * arg_obj;
1623
1624 if( i >= 0 && i < args_obj->size() )
1625 {
1626 args_obj->get( i, (t_CKUINT *)&arg_obj );
1627 RETURN->v_string = OBJ_MEMBER_STRING( arg_obj, oscarg_offset_s );
1628 }
1629 else
1630 RETURN->v_string = NULL;
1631 }
1632
1633 static t_CKUINT osc_send_offset_data = 0;
1634 static t_CKUINT osc_recv_offset_data = 0;
1635 static t_CKUINT osc_address_offset_data = 0;
1636 static Chuck_Type * osc_addr_type_ptr = 0;
1637
1638
1639
1640
1641 //-----------------------------------------------------------------------------
1642 // name: opensoundcontrol_query()
1643 // desc: OSC import into chuck type-system
1644 //-----------------------------------------------------------------------------
1645 DLL_QUERY opensoundcontrol_query( Chuck_DL_Query * query )
1646 {
1647 /*** OscOut ***/
1648 query->begin_class( query, "OscOut", "Object" );
1649 query->doc_class( query, "Class for sending Open Sound Control (OSC) messages. See examples for usage." );
1650 oscout_offset_data = query->add_mvar( query, "int", "@OscOut_data", FALSE );
1651
1652 query->add_ctor( query, oscout_ctor );
1653 query->add_dtor( query, oscout_dtor );
1654
1655 query->add_mfun( query, oscout_dest, "OscOut", "dest" );
1656 query->add_arg( query, "string", "hostname" );
1657 query->add_arg( query, "int", "port" );
1658 query->doc_func( query, "Set the destination hostname and port for sending OSC message." );
1659
1660 query->add_mfun( query, oscout_start, "OscOut", "start" );
1661 query->add_arg( query, "string", "address" );
1662 query->doc_func( query, "Start an OSC message with a particular address." );
1663
1664 query->add_mfun( query, oscout_startDest, "OscOut", "start" );
1665 query->add_arg( query, "string", "address" );
1666 query->add_arg( query, "string", "host" );
1667 query->add_arg( query, "int", "port" );
1668 query->doc_func( query, "Start an OSC message with a particular address, aimed at a destination host and port." );
1669
1670 query->add_mfun( query, oscout_addInt, "OscOut", "add" );
1671 query->add_arg( query, "int", "i" );
1672 query->doc_func( query, "Add an integer value to an OSC message." );
1673
1674 query->add_mfun( query, oscout_addFloat, "OscOut", "add" );
1675 query->add_arg( query, "float", "f" );
1676 query->doc_func( query, "Add a floating-point value to an OSC message." );
1677
1678 query->add_mfun( query, oscout_addString, "OscOut", "add" );
1679 query->add_arg( query, "string", "s" );
1680 query->doc_func( query, "Add an string value to an OSC message." );
1681
1682 query->add_mfun( query, oscout_send, "OscOut", "send" );
1683 query->doc_func( query, "Send the current OSC message." );
1684
1685 // add examples | 1.5.0.0 (ge) added
1686 query->add_ex( query, "osc/s.ck" );
1687 query->add_ex( query, "osc/r.ck" );
1688 query->add_ex( query, "osc/r2.ck" );
1689 query->add_ex( query, "osc/osc-dump.ck" );
1690
1691 query->end_class( query );
1692
1693
1694 /*** OscArg ***/
1695
1696 query->begin_class( query, "OscArg", "Object" );
1697
1698 oscarg_offset_type = query->add_mvar( query, "string", "type", FALSE );
1699 oscarg_offset_i = query->add_mvar( query, "int", "i", FALSE );
1700 oscarg_offset_f = query->add_mvar( query, "float", "f", FALSE );
1701 oscarg_offset_s = query->add_mvar( query, "string", "s", FALSE );
1702
1703 query->add_ctor( query, oscarg_ctor );
1704 query->add_dtor( query, oscarg_dtor );
1705
1706 query->end_class( query );
1707
1708
1709 /*** OscMsg ***/
1710 query->begin_class( query, "OscMsg", "Object" );
1711 query->doc_class( query, "Helper class for receiving the contents of an OSC message." );
1712
1713 oscmsg_offset_address = query->add_mvar( query, "string", "address", FALSE );
1714 query->doc_var( query, "The OSC address string (e.g., \"/foo/param\")." );
1715 oscmsg_offset_typetag = query->add_mvar( query, "string", "typetag", FALSE );
1716 query->doc_var( query, "The OSC type tag string (e.g., \"iif\" for int, int, float)." );
1717 oscmsg_offset_args = query->add_mvar( query, "OscArg[]", "args", FALSE );
1718 query->doc_var( query, "Array of OscArgs contained in this message." );
1719
1720 query->add_mfun( query, oscmsg_numArgs, "int", "numArgs" );
1721 query->doc_func( query, "Get the number of arguments contained in this OscMsg." );
1722
1723 query->add_mfun( query, oscmsg_getInt, "int", "getInt" );
1724 query->add_arg( query, "int", "i" );
1725 query->doc_func( query, "Get argument (at index 'i') as an integer." );
1726
1727 query->add_mfun( query, oscmsg_getFloat, "float", "getFloat" );
1728 query->add_arg( query, "int", "i" );
1729 query->doc_func( query, "Get argument (at index 'i') as a float." );
1730
1731 query->add_mfun( query, oscmsg_getString, "string", "getString" );
1732 query->add_arg( query, "int", "i" );
1733 query->doc_func( query, "Get argument (at index 'i') as a string." );
1734
1735 query->add_ctor( query, oscmsg_ctor );
1736 query->add_dtor( query, oscmsg_dtor );
1737
1738 // add examples | 1.5.0.0 (ge) added
1739 query->add_ex( query, "osc/r.ck" );
1740 query->add_ex( query, "osc/r2.ck" );
1741 query->add_ex( query, "osc/s.ck" );
1742 query->add_ex( query, "osc/osc-dump.ck" );
1743 query->add_ex( query, "osc/multi-msg/r-multi-msg.ck" );
1744
1745 query->end_class( query );
1746
1747
1748 /*** OscIn ***/
1749 query->begin_class( query, "OscIn", "Event" );
1750 query->doc_class( query, "Class for receiving Open Sound Control (OSC) messages. See examples for usage." );
1751
1752 // add member reference
1753 oscin_offset_data = query->add_mvar( query, "int", "@OscIn_data", FALSE );
1754
1755 query->add_ctor( query, oscin_ctor );
1756 query->add_dtor( query, oscin_dtor );
1757
1758 // get port
1759 query->add_mfun( query, oscin_getport, "int", "port" );
1760 query->doc_func( query, "Get which port to listen on." );
1761 // set port
1762 query->add_mfun( query, oscin_setport, "int", "port" );
1763 query->add_arg( query, "int", "p" );
1764 query->doc_func( query, "Set which port to listen on; this will begin priming the background OSC listener "
1765 "on the named port. If port is set to 0, a usable port would be automatically assigned; the auto-assigned "
1766 "port number can be retrieved by calling .port() but may initially take some time to acquire (e.g., hundreds of milliseconds); "
1767 "if there is more than one OscIn client on port 0, they all will eventually share the same auto-assigned port." );
1768
1769 // add address
1770 query->add_mfun( query, oscin_addAddress, "void", "addAddress" );
1771 query->add_arg( query, "string", "address" );
1772 query->doc_func( query, "Add an OSC address to receive messages from." );
1773 // remove address
1774 query->add_mfun( query, oscin_removeAddress, "void", "removeAddress" );
1775 query->add_arg( query, "string", "address" );
1776 query->doc_func( query, "Stop listening on a particular OSC address." );
1777 // remove all addresses
1778 query->add_mfun( query, oscin_removeAllAddresses, "void", "removeAllAddresses" );
1779 query->doc_func( query, "Stop listening on all OSC addresses." );
1780
1781 // listenAll()
1782 query->add_mfun( query, oscin_listenAll, "void", "listenAll" );
1783 query->doc_func( query, "Set OscIn to receive messages of any OSC address." );
1784 // recv
1785 query->add_mfun( query, oscin_recv, "int", "recv" );
1786 query->add_arg( query, "OscMsg", "msg" );
1787 query->doc_func( query, "Receive the next queued incoming OSC message, returning its contents in `msg`." );
1788
1789 // add examples | 1.5.0.0 (ge) added
1790 query->add_ex( query, "osc/r.ck" );
1791 query->add_ex( query, "osc/r2.ck" );
1792 query->add_ex( query, "osc/s.ck" );
1793 query->add_ex( query, "osc/osc-dump.ck" );
1794 query->add_ex( query, "osc/multi-msg/r-multi-msg.ck" );
1795
1796 // end class definition
1797 query->end_class( query );
1798
1799
1800 // get the env
1801 Chuck_Env * env = query->env();
1802 Chuck_DL_Func * func = NULL;
1803
1804 // init base class
1805 if( !type_engine_import_class_begin( env, "OscSend", "Object",
1806 env->global(), osc_send_ctor,
1807 osc_send_dtor ) )
1808 return FALSE;
1809
1810 // add member variable - OSCTransmitter object
1811 osc_send_offset_data = type_engine_import_mvar( env, "int", "@OscSend_data", FALSE );
1812 if( osc_send_offset_data == CK_INVALID_OFFSET ) goto error;
1813
1814 func = make_new_mfun( "int", "setHost", osc_send_setHost );
1815 func->add_arg( "string", "address" );
1816 func->add_arg( "int", "port" );
1817 if( !type_engine_import_mfun( env, func ) ) goto error;
1818
1819 func = make_new_mfun( "int", "startMsg", osc_send_startMesg );
1820 func->add_arg( "string", "address" );
1821 func->add_arg( "string", "args" );
1822 if( !type_engine_import_mfun( env, func ) ) goto error;
1823
1824 func = make_new_mfun( "int", "startMsg", osc_send_startMesg_spec );
1825 func->add_arg( "string", "spec" );
1826 if( !type_engine_import_mfun( env, func ) ) goto error;
1827
1828 func = make_new_mfun( "int", "addInt", osc_send_addInt );
1829 func->add_arg( "int", "value" );
1830 if( !type_engine_import_mfun( env, func ) ) goto error;
1831
1832 func = make_new_mfun( "float", "addFloat", osc_send_addFloat );
1833 func->add_arg( "float", "value" );
1834 if( !type_engine_import_mfun( env, func ) ) goto error;
1835
1836 func = make_new_mfun( "string", "addString", osc_send_addString );
1837 func->add_arg( "string", "value" );
1838 if( !type_engine_import_mfun( env, func ) ) goto error;
1839
1840 func = make_new_mfun( "int", "openBundle", osc_send_openBundle );
1841 func->add_arg( "time", "value" );
1842 if( !type_engine_import_mfun( env, func ) ) goto error;
1843
1844 func = make_new_mfun( "int", "closeBundle", osc_send_closeBundle );
1845 if( !type_engine_import_mfun( env, func ) ) goto error;
1846
1847 func = make_new_mfun( "int", "hold", osc_send_holdMesg );
1848 func->add_arg( "int", "on" );
1849 if( !type_engine_import_mfun( env, func ) ) goto error;
1850
1851 func = make_new_mfun( "int", "kick", osc_send_kickMesg );
1852 if( !type_engine_import_mfun( env, func ) ) goto error;
1853
1854 type_engine_import_class_end( env );
1855
1856 // init base class
1857 if( !type_engine_import_class_begin( env, "OscEvent", "Event",
1858 env->global(), osc_address_ctor,
1859 osc_address_dtor ) )
1860 return FALSE;
1861
1862 // add member variable - OSCAddress object
1863
1864 osc_address_offset_data = type_engine_import_mvar( env, "int", "@OscEvent_Data", FALSE );
1865 if( osc_recv_offset_data == CK_INVALID_OFFSET ) goto error;
1866
1867 // keep type around for initialization ( so other classes can return it )
1868
1869 osc_addr_type_ptr = env->class_def;
1870
1871 func = make_new_mfun( "int", "set", osc_address_set );
1872 func->add_arg( "string", "addr" );
1873 if( !type_engine_import_mfun( env, func ) ) goto error;
1874
1875 func = make_new_mfun( "int", "hasMsg", osc_address_has_mesg );
1876 if( !type_engine_import_mfun( env, func ) ) goto error;
1877
1878 func = make_new_mfun( "int", "nextMsg", osc_address_next_mesg );
1879 if( !type_engine_import_mfun( env, func ) ) goto error;
1880
1881 func = make_new_mfun( "int", "getInt", osc_address_next_int );
1882 if( !type_engine_import_mfun( env, func ) ) goto error;
1883
1884 func = make_new_mfun( "float", "getFloat", osc_address_next_float );
1885 if( !type_engine_import_mfun( env, func ) ) goto error;
1886
1887 func = make_new_mfun( "string", "getString", osc_address_next_string );
1888 if( !type_engine_import_mfun( env, func ) ) goto error;
1889
1890 func = make_new_mfun( "int", "can_wait", osc_address_can_wait );
1891 func->doc = "(internal) used by virtual machine for synthronization.";
1892 if( !type_engine_import_mfun( env, func ) ) goto error;
1893
1894 type_engine_import_class_end( env );
1895
1896 // init base class
1897 if( !type_engine_import_class_begin( env, "OscRecv", "Object",
1898 env->global(), osc_recv_ctor,
1899 osc_recv_dtor ) )
1900 return FALSE;
1901
1902 // add member variable - OSCReceiver object
1903 osc_recv_offset_data = type_engine_import_mvar( env, "int", "@OscRecv_data", FALSE );
1904 if( osc_recv_offset_data == CK_INVALID_OFFSET ) goto error;
1905
1906 func = make_new_mfun( "int", "port", osc_recv_port );
1907 func->add_arg( "int", "port" );
1908 if( !type_engine_import_mfun( env, func ) ) goto error;
1909
1910 func = make_new_mfun( "int", "listen", osc_recv_listen_port );
1911 func->add_arg( "int", "port" );
1912 if( !type_engine_import_mfun( env, func ) ) goto error;
1913
1914 func = make_new_mfun( "int", "listen", osc_recv_listen );
1915 if( !type_engine_import_mfun( env, func ) ) goto error;
1916
1917 func = make_new_mfun( "int", "stop", osc_recv_listen_stop );
1918 if( !type_engine_import_mfun( env, func ) ) goto error;
1919
1920 func = make_new_mfun( "int", "add_address", osc_recv_add_address );
1921 func->add_arg( "OscEvent", "addr" );
1922 if( !type_engine_import_mfun( env, func ) ) goto error;
1923
1924 func = make_new_mfun( "int", "remove_address", osc_recv_remove_address );
1925 func->add_arg( "OscEvent", "addr" );
1926 if( !type_engine_import_mfun( env, func ) ) goto error;
1927
1928 func = make_new_mfun( "OscEvent", "event", osc_recv_new_address );
1929 func->add_arg( "string", "spec" );
1930 if( !type_engine_import_mfun( env, func ) ) goto error;
1931
1932 func = make_new_mfun( "OscEvent", "event", osc_recv_new_address_type );
1933 func->add_arg( "string", "address" );
1934 func->add_arg( "string", "type" );
1935 if( !type_engine_import_mfun( env, func ) ) goto error;
1936
1937 func = make_new_mfun( "OscEvent", "address", osc_recv_new_address );
1938 func->add_arg( "string", "spec" );
1939 if( !type_engine_import_mfun( env, func ) ) goto error;
1940
1941 func = make_new_mfun( "OscEvent", "address", osc_recv_new_address_type );
1942 func->add_arg( "string", "address" );
1943 func->add_arg( "string", "type" );
1944 if( !type_engine_import_mfun( env, func ) ) goto error;
1945
1946 type_engine_import_class_end( env );
1947 return TRUE;
1948
1949 error:
1950 CK_FPRINTF_STDERR( "class import error!\n" );
1951 // end the class import
1952 type_engine_import_class_end( env );
1953 return FALSE;
1954 }
1955
1956 //----------------------------------------------
1957 // name : osc_send_ctor
1958 // desc : CTOR function
1959 //-----------------------------------------------
1960 CK_DLL_CTOR( osc_send_ctor ) {
1961 OSC_Transmitter * xmit = new OSC_Transmitter();
1962 OBJ_MEMBER_INT( SELF, osc_send_offset_data ) = (t_CKINT)xmit;
1963 }
1964
1965 //----------------------------------------------
1966 // name : osc_send_dtor
1967 // desc : DTOR function
1968 //-----------------------------------------------
1969 CK_DLL_DTOR( osc_send_dtor ) {
1970 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
1971 CK_SAFE_DELETE( xmit );
1972 OBJ_MEMBER_UINT( SELF, osc_send_offset_data ) = 0;
1973 }
1974
1975 //----------------------------------------------
1976 // name : osc_send_setHost
1977 // desc : MFUN function
1978 //-----------------------------------------------
1979 CK_DLL_MFUN( osc_send_setHost ) {
1980 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
1981 Chuck_String * host = GET_NEXT_STRING( ARGS );
1982 t_CKINT port = GET_NEXT_INT( ARGS );
1983 xmit->setHost( (char *)host->str().c_str(), port );
1984 }
1985
1986 //----------------------------------------------
1987 // name : osc_send_startMesg
1988 // desc : MFUN function
1989 //-----------------------------------------------
1990 CK_DLL_MFUN( osc_send_startMesg ) {
1991 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
1992 Chuck_String * address = GET_NEXT_STRING( ARGS );
1993 Chuck_String * args = GET_NEXT_STRING( ARGS );
1994 xmit->startMessage( (char *)address->str().c_str(), (char *)args->str().c_str() );
1995 }
1996
1997 //----------------------------------------------
1998 // name : osc_send_startMesg
1999 // desc : MFUN function
2000 //-----------------------------------------------
2001 CK_DLL_MFUN( osc_send_startMesg_spec ) {
2002 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
2003 Chuck_String * spec = GET_NEXT_STRING( ARGS );
2004 xmit->startMessage( (char *)spec->str().c_str() );
2005 }
2006
2007 //----------------------------------------------
2008 // name : osc_send_addInt
2009 // desc : MFUN function
2010 //-----------------------------------------------
2011 CK_DLL_MFUN( osc_send_addInt ) {
2012 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
2013 xmit->addInt( GET_NEXT_INT( ARGS ) );
2014 }
2015
2016 //----------------------------------------------
2017 // name : osc_send_addFloat
2018 // desc : MFUN function
2019 //-----------------------------------------------
2020 CK_DLL_MFUN( osc_send_addFloat ) {
2021 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
2022 xmit->addFloat( (float)(GET_NEXT_FLOAT( ARGS )) );
2023 }
2024
2025 //----------------------------------------------
2026 // name : osc_send_addString
2027 // desc : MFUN function
2028 //-----------------------------------------------
2029 CK_DLL_MFUN( osc_send_addString ) {
2030 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
2031 xmit->addString( (char *)(GET_NEXT_STRING( ARGS ))->str().c_str() );
2032 }
2033
2034 //----------------------------------------------
2035 // name : osc_send_openBundle
2036 // desc : MFUN function
2037 //-----------------------------------------------
2038 CK_DLL_MFUN( osc_send_openBundle ) {
2039 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
2040 // we should add in an option to translate from chuck-time to current time to timetag.
2041 // but for now let's just spec. immediately.
2042 xmit->openBundle( OSCTT_Immediately() );
2043 }
2044
2045 //----------------------------------------------
2046 // name : osc_send_closeBundle
2047 // desc : MFUN function
2048 //-----------------------------------------------
2049 CK_DLL_MFUN( osc_send_closeBundle ) {
2050 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
2051 xmit->closeBundle();
2052 }
2053
2054 //----------------------------------------------
2055 // name : osc_send_holdMesg
2056 // desc : MFUN function
2057 //-----------------------------------------------
2058 CK_DLL_MFUN( osc_send_holdMesg ) {
2059 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
2060 xmit->holdMessage( GET_NEXT_INT( ARGS ) != 0 );
2061 }
2062
2063 //----------------------------------------------
2064 // name : osc_send_kickMesg
2065 // desc : MFUN function
2066 //-----------------------------------------------
2067 CK_DLL_MFUN( osc_send_kickMesg ) {
2068 OSC_Transmitter * xmit = (OSC_Transmitter *)OBJ_MEMBER_INT( SELF, osc_send_offset_data );
2069 xmit->kickMessage();
2070 }
2071
2072 //----------------------------------------------
2073 // name : osc_address_ctor
2074 // desc : CTOR function
2075 //-----------------------------------------------
2076 CK_DLL_CTOR( osc_address_ctor ) {
2077 OSC_Address_Space * addr = new OSC_Address_Space();
2078 addr->SELF = SELF;
2079 // CK_FPRINTF_STDERR("address:ptr %x\n", (uint)addr );
2080 // CK_FPRINTF_STDERR("self:ptr %x\n", (uint)SELF );
2081 OBJ_MEMBER_INT( SELF, osc_address_offset_data ) = (t_CKINT)addr;
2082 }
2083
2084 CK_DLL_DTOR( osc_address_dtor ) {
2085 delete (OSC_Address_Space *)OBJ_MEMBER_UINT( SELF, osc_address_offset_data );
2086 OBJ_MEMBER_UINT( SELF, osc_address_offset_data ) = 0;
2087 }
2088
2089 CK_DLL_MFUN( osc_address_set ) {
2090 OSC_Address_Space * addr = (OSC_Address_Space *)OBJ_MEMBER_INT( SELF, osc_address_offset_data );
2091 addr->setSpec( (char *)(GET_NEXT_STRING( ARGS ))->str().c_str() );
2092 RETURN->v_int = 0;
2093 }
2094
2095 //----------------------------------------------
2096 // name : osc_address_can_wait
2097 // desc : MFUN function
2098 //-----------------------------------------------
2099 CK_DLL_MFUN( osc_address_can_wait ) {
2100 OSC_Address_Space * addr = (OSC_Address_Space *)OBJ_MEMBER_INT( SELF, osc_address_offset_data );
2101 RETURN->v_int = (addr->has_mesg()) ? 0 : 1;
2102 }
2103
2104 //----------------------------------------------
2105 // name : osc_address_has_mesg
2106 // desc : MFUN function
2107 //-----------------------------------------------
2108 CK_DLL_MFUN( osc_address_has_mesg ) {
2109 OSC_Address_Space * addr = (OSC_Address_Space *)OBJ_MEMBER_INT( SELF, osc_address_offset_data );
2110 RETURN->v_int = (addr->has_mesg()) ? 1 : 0;
2111 }
2112
2113 //----------------------------------------------
2114 // name : osc_address_next_mesg
2115 // desc : MFUN function
2116 //-----------------------------------------------
2117 CK_DLL_MFUN( osc_address_next_mesg ) {
2118 OSC_Address_Space * addr = (OSC_Address_Space *)OBJ_MEMBER_INT( SELF, osc_address_offset_data );
2119 RETURN->v_int = (addr->next_mesg()) ? 1 : 0;
2120 }
2121
2122 //----------------------------------------------
2123 // name : osc_address_next_int
2124 // desc : MFUN function
2125 //-----------------------------------------------
2126 CK_DLL_MFUN( osc_address_next_int ) {
2127 OSC_Address_Space * addr = (OSC_Address_Space *)OBJ_MEMBER_INT( SELF, osc_address_offset_data );
2128 RETURN->v_int = addr->next_int();
2129 }
2130
2131 //----------------------------------------------
2132 // name : osc_address_next_float
2133 // desc : MFUN function
2134 //-----------------------------------------------
2135 CK_DLL_MFUN( osc_address_next_float ) {
2136 OSC_Address_Space * addr = (OSC_Address_Space *)OBJ_MEMBER_INT( SELF, osc_address_offset_data );
2137 RETURN->v_float = addr->next_float();
2138 }
2139
2140 //----------------------------------------------
2141 // name : osc_address_next_string
2142 // desc : MFUN function
2143 //-----------------------------------------------
2144 CK_DLL_MFUN( osc_address_next_string ) {
2145 OSC_Address_Space * addr = (OSC_Address_Space *)OBJ_MEMBER_INT( SELF, osc_address_offset_data );
2146 char * cs = addr->next_string();
2147 Chuck_String * ckstr = (cs) ? new Chuck_String( cs ) : new Chuck_String( "" );
2148 initialize_object( ckstr, SHRED->vm_ref->env()->ckt_string, SHRED, VM );
2149 RETURN->v_string = ckstr;
2150 }
2151
2152
2153
2154
2155 // OscRecv functions
2156 //----------------------------------------------
2157 // name : osc_recv_ctor
2158 // desc : CTOR function
2159 //-----------------------------------------------
2160 CK_DLL_CTOR( osc_recv_ctor )
2161 {
2162 OSC_Receiver * recv = new OSC_Receiver( SHRED->vm_ref );
2163 OBJ_MEMBER_INT( SELF, osc_send_offset_data ) = (t_CKINT)recv;
2164 }
2165
2166 //----------------------------------------------
2167 // name : osc_recv_dtor
2168 // desc : DTOR function
2169 //-----------------------------------------------
2170 CK_DLL_DTOR( osc_recv_dtor )
2171 {
2172 OSC_Receiver * recv = (OSC_Receiver *)OBJ_MEMBER_INT( SELF, osc_recv_offset_data );
2173 CK_SAFE_DELETE( recv );
2174 OBJ_MEMBER_INT( SELF, osc_recv_offset_data ) = 0;
2175 }
2176
2177 //----------------------------------------------
2178 // name : osc_recv_port
2179 // desc : specify port to listen on
2180 //-----------------------------------------------
2181 CK_DLL_MFUN( osc_recv_port )
2182 {
2183 OSC_Receiver * recv = (OSC_Receiver *)OBJ_MEMBER_INT( SELF, osc_recv_offset_data );
2184 recv->bind_to_port( (int)GET_NEXT_INT( ARGS ) );
2185 }
2186
2187 // need to add a listen function in Receiver which opens up a recv loop on another thread.
2188 // address then subscribe to a receiver to take in events.
2189
2190 //----------------------------------------------
2191 // name : osc_recv_listen
2192 // desc : start listening
2193 //-----------------------------------------------
2194 CK_DLL_MFUN( osc_recv_listen )
2195 {
2196 OSC_Receiver * recv = (OSC_Receiver *)OBJ_MEMBER_INT( SELF, osc_recv_offset_data );
2197 recv->listen();
2198 }
2199
2200 //----------------------------------------------
2201 // name : osc_recv_listen_port
2202 // desc : listen to a given port ( disconnects from current )
2203 //-----------------------------------------------
2204 CK_DLL_MFUN( osc_recv_listen_port )
2205 {
2206 OSC_Receiver * recv = (OSC_Receiver *)OBJ_MEMBER_INT( SELF, osc_recv_offset_data );
2207 recv->listen( (int)GET_NEXT_INT( ARGS ) );
2208 }
2209
2210 //----------------------------------------------
2211 // name : osc_recv_listen_port
2212 // desc : listen to a given port ( disconnects from current )
2213 //-----------------------------------------------
2214 CK_DLL_MFUN( osc_recv_listen_stop )
2215 {
2216 OSC_Receiver * recv = (OSC_Receiver *)OBJ_MEMBER_INT( SELF, osc_recv_offset_data );
2217 recv->stopListening();
2218 }
2219
2220 //----------------------------------------------
2221 // name : osc_recv_add_listener
2222 // desc : MFUN function
2223 //-----------------------------------------------
2224 CK_DLL_MFUN( osc_recv_add_address )
2225 {
2226 OSC_Receiver * recv = (OSC_Receiver *)OBJ_MEMBER_INT( SELF, osc_recv_offset_data );
2227 Chuck_Object * addr_obj = GET_NEXT_OBJECT( ARGS ); //address object class...
2228 OSC_Address_Space * addr = (OSC_Address_Space *)OBJ_MEMBER_INT( addr_obj, osc_address_offset_data );
2229 recv->add_address( addr );
2230 }
2231
2232 //----------------------------------------------
2233 // name : osc_recv_remove_listener
2234 // desc : MFUN function
2235 //-----------------------------------------------
2236 CK_DLL_MFUN( osc_recv_remove_address )
2237 {
2238 OSC_Receiver * recv = (OSC_Receiver *)OBJ_MEMBER_INT( SELF, osc_recv_offset_data );
2239 Chuck_Object * addr_obj = GET_NEXT_OBJECT( ARGS ); //listener object class...
2240 OSC_Address_Space * addr = (OSC_Address_Space *)OBJ_MEMBER_INT( addr_obj, osc_address_offset_data );
2241 recv->remove_address( addr );
2242 }
2243
2244 //----------------------------------------------
2245 // name : osc_recv_new_address
2246 // desc : MFUN function
2247 //-----------------------------------------------
2248 CK_DLL_MFUN( osc_recv_new_address )
2249 {
2250 OSC_Receiver * recv = (OSC_Receiver *)OBJ_MEMBER_INT( SELF, osc_recv_offset_data );
2251 Chuck_String * spec_obj = (Chuck_String *)GET_NEXT_STRING( ARGS ); // listener object class...
2252
2253 // added 1.3.1.1: fix potential race condition
2254 //OSC_Address_Space * new_addr_obj = recv->new_event( (char*)spec_obj->str.c_str() );
2255 OSC_Address_Space * new_addr_obj = new OSC_Address_Space( (char *)spec_obj->str().c_str() );
2256
2257 /* wolf in sheep's clothing
2258 initialize_object( new_addr_obj , osc_addr_type_ptr ); //initialize in vm
2259 new_addr_obj->SELF = new_addr_obj;
2260 OBJ_MEMBER_INT( new_addr_obj, osc_address_offset_data ) = (t_CKINT)new_addr_obj;
2261 */
2262
2263 // more correct...?
2264 Chuck_Event * new_event_obj = new Chuck_Event();
2265 initialize_object( new_event_obj, osc_addr_type_ptr, SHRED, VM );
2266 new_addr_obj->SELF = new_event_obj;
2267 OBJ_MEMBER_INT( new_event_obj, osc_address_offset_data ) = (t_CKINT)new_addr_obj;
2268
2269 recv->add_address( new_addr_obj );
2270
2271 RETURN->v_object = new_event_obj;
2272 }
2273
2274
2275
2276 //----------------------------------------------
2277 // name : osc_recv_new_address
2278 // desc : MFUN function
2279 //-----------------------------------------------
2280 CK_DLL_MFUN( osc_recv_new_address_type )
2281 {
2282 OSC_Receiver * recv = (OSC_Receiver *)OBJ_MEMBER_INT( SELF, osc_recv_offset_data );
2283 Chuck_String * addr_obj = (Chuck_String *)GET_NEXT_STRING( ARGS ); //listener object class...
2284 Chuck_String * type_obj = (Chuck_String *)GET_NEXT_STRING( ARGS ); //listener object class...
2285 OSC_Address_Space * new_addr_obj = recv->new_event( (char *)addr_obj->str().c_str(), (char *)type_obj->str().c_str() );
2286
2287 /* wolf in sheep's clothing
2288 initialize_object( new_addr_obj , osc_addr_type_ptr ); //initialize in vm
2289 new_addr_obj->SELF = new_addr_obj;
2290 OBJ_MEMBER_INT( new_addr_obj, osc_address_offset_data ) = (t_CKINT)new_addr_obj;
2291 */
2292
2293 // more correct...?
2294 Chuck_Event * new_event_obj = new Chuck_Event();
2295 initialize_object( new_event_obj, osc_addr_type_ptr, SHRED, VM );
2296 new_addr_obj->SELF = new_event_obj;
2297 OBJ_MEMBER_INT( new_event_obj, osc_address_offset_data ) = (t_CKINT)new_addr_obj;
2298
2299 RETURN->v_object = new_event_obj;
2300 }
2301
2302 // No longer compiling ulib_opsc.cpp | REFACTOR-2017
2303 #ifdef __PLATFORM_WINDOWS__
2304 #undef ULIB_OPSC_CPP
2305 #endif