diff Source/TouchKeys/Osc.h @ 6:36fe60d0aadb

Improve OSC input support and enable in the main GUI. External OSC input isn't used yet (aside from internal OSC handling) but this allows a server to run on a specified port.
author Andrew McPherson <andrewm@eecs.qmul.ac.uk>
date Wed, 13 Nov 2013 23:40:53 +0000
parents 3580ffe87dc8
children f943785252fc
line wrap: on
line diff
--- a/Source/TouchKeys/Osc.h	Wed Nov 13 21:00:16 2013 +0000
+++ b/Source/TouchKeys/Osc.h	Wed Nov 13 23:40:53 2013 +0000
@@ -96,18 +96,42 @@
 class OscReceiver : public OscMessageSource
 {
 public:
-	OscReceiver(lo_server_thread thread, const char *prefix) {
-		oscServerThread_ = thread;
-		globalPrefix_.assign(prefix);
+	OscReceiver(const int port, const char *prefix) {
+        globalPrefix_.assign(prefix);
 		useThru_ = false;
-		lo_server_thread_add_method(thread, NULL, NULL, OscReceiver::staticHandler, (void *)this);
-	}	
+        
+        // Only start the server if the port is positive
+        if(port > 0) {
+            char portStr[16];
+            snprintf(portStr, 16, "%d", port);
+            
+            oscServerThread_ = lo_server_thread_new(portStr, staticErrorHandler);
+            if(oscServerThread_ != 0) {
+                lo_server_thread_add_method(oscServerThread_, NULL, NULL, OscReceiver::staticHandler, (void *)this);
+                lo_server_thread_start(oscServerThread_);
+            }
+        }
+        else
+            oscServerThread_ = 0;
+	}
 	
 	void setThruAddress(lo_address thruAddr, const char *prefix) {
 		thruAddress_ = thruAddr;
 		thruPrefix_.assign(prefix);
 		useThru_ = true;
 	}
+    
+    // Check whether the server is operating
+    bool running() { return (oscServerThread_ != 0); }
+    
+    // Get or set the current port. Setting the port requires restarting the server.
+    // setPort() returns true on success; false if an error occurred (which will leave the server not running).
+    const int port() {
+        if(oscServerThread_ == 0)
+            return 0;
+        return lo_server_get_port(oscServerThread_);
+    }
+    bool setPort(const int port);
 	
 	// staticHandler() is called by liblo with new OSC messages.  Its only function is to pass control
 	// to the object-specific handler method, which has access to all internal variables.
@@ -115,11 +139,19 @@
 	int handler(const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *data);
 	static int staticHandler(const char *path, const char *types, lo_arg **argv, int argc, lo_message msg, void *userData) {
 		return ((OscReceiver *)userData)->handler(path, types, argv, argc, msg, userData);
-	}	
+	}
+    
+    // staticErrorHandler() is called by liblo when an error occurs. For now, ignore errors.
+    
+    static void staticErrorHandler(int num, const char *msg, const char *path) {}
 	
 	~OscReceiver() {
-		lo_server_thread_del_method(oscServerThread_, NULL, NULL);
-	}	
+        if(oscServerThread_ != 0) {
+            lo_server_thread_del_method(oscServerThread_, NULL, NULL);
+            lo_server_thread_stop(oscServerThread_);
+            lo_server_thread_free(oscServerThread_);
+        }
+	}
 	
 private:
 	lo_server_thread oscServerThread_;		// Thread that handles received OSC messages