Created
June 14, 2016 01:44
-
-
Save lisongx/45ee050028ceedd47c122bb6f66305c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @author Tadashi Usami | |
*/ | |
//package oschub; | |
import java.io.*; | |
import java.net.*; | |
import java.util.*; | |
import de.sciss.net.*; | |
class OSCHubServer extends Thread{ | |
private final Object sync = new Object(); | |
OSCServer sv; | |
int portNumber; | |
boolean pause = false; | |
static List<SocketAddress> contacts = new ArrayList<>(); | |
final String[] scCommands = { | |
"/quit", "/notify", "/cmd", "/dumpOSC", "/sync", "/clearSched", "/error", | |
"/d_recv", "/d_load", "/d_loadDir", "/d_free", | |
"/n_free", "/n_run", "/n_set", "/n_setn", "/n_fill", "/n_map", "/n_mapn", "/n_mapa", "/n_mapan", "/n_before", "/n_after", "/n_query", "/n_trace", "/n_order", | |
"/s_new", "/s_get", "/s_getn", "/s_noid", | |
"/g_new", "/p_new", "/g_head", "/g_tail", "/g_freeAll", "/g_deepFree", "/g_dumpTree", "/g_queryTree", | |
"/u_cmd", | |
"/b_alloc", "/b_allocRead", "/b_allocReadChannel", "/b_read", "/b_readChannel", "/b_write", "/b_free", "/b_zero", "/b_set", "/b_setn", "/b_fill", "/b_gen", "/b_close", "/b_query", "/b_get", "/b_getn", | |
"/c_set", "/c_setn", "/c_fill", "/c_get", "/c_getn", | |
"/nrt_end", | |
"/late", | |
"/n_go", "/n_end", "/n_off", "/n_on", "/n_move", "/n_info", | |
"/tr", | |
"/chat", | |
"/done", | |
"/keepAlive","/h_comm", | |
"/pauseHUB", "/quitHUB", "/dumpHUB" | |
}; // "/fail" and "/status" are omitted | |
OSCHubServer(int portNumber) throws Exception { | |
// create UDP server on port 57110, 57120; "true" for loopback | |
this.portNumber = portNumber; | |
sv = OSCServer.newUsing( OSCServer.UDP, portNumber);//, true ); | |
} | |
@Override | |
public void run() { | |
// now add a listener for incoming messages from | |
// any of the active connections | |
sv.addOSCListener( new OSCListener() { | |
@Override | |
public void messageReceived( OSCMessage m, SocketAddress addr, long time ) { | |
if (Arrays.asList(scCommands).contains(m.getName())) { | |
if (!contacts.contains(addr)) { | |
contacts.add(addr); | |
System.out.println( "Address list: "+contacts ); | |
} | |
Date date = new Date(); | |
System.out.println( | |
"received "+m.getName() | |
+" from "+addr | |
+", "+date | |
); | |
// send the message that the hub (server) just have received | |
try { | |
if (!("/pauseHUB".equals(m.getName())||"/quitHUB".equals(m.getName()) | |
||"/dumpHUB".equals(m.getName())||"/keepAlive".equals(m.getName()))){ | |
for (SocketAddress sa : contacts) { | |
sv.send(m, sa); | |
System.out.println( m.getName()+" sent to "+sa ); | |
} | |
} | |
} | |
catch( IOException e1 ) {} | |
switch (m.getName()) { | |
case "/pauseHUB": | |
// tell the main thread to pause the server, | |
// wake up the main thread | |
pause = true; // proceed and turn around do-while loop | |
synchronized( sync ) { | |
sync.notifyAll(); // resume after wait() | |
} | |
break; | |
case "/quitHUB": // "/quit" | |
// wake up the main thread | |
synchronized( sync ) { | |
sync.notifyAll(); // resume after wait() -> | |
} | |
break; | |
case "/dumpHUB": // "/dumpOSC" | |
// change dumping behavior | |
sv.dumpOSC( ((Number) m.getArg( 0 )).intValue(), System.err ); | |
break; | |
} | |
} | |
} | |
}); | |
try { | |
do { | |
if( pause ) { | |
System.out.println( " waiting three seconds..." ); | |
try { | |
Thread.sleep( 3000 ); | |
} | |
catch( InterruptedException e2 ) {} | |
pause = false; | |
} | |
System.out.println( " start() at "+portNumber ); | |
// start the server (make it attentive for incoming connection requests) | |
sv.start(); | |
try { | |
synchronized( sync ) { | |
sync.wait(); | |
} | |
} | |
catch( InterruptedException e3 ) {} | |
System.out.println( " stop() at "+portNumber ); | |
sv.stop(); | |
} while( pause ); | |
} | |
catch( IOException e4 ) {} | |
// kill the server, free its resources | |
sv.dispose(); | |
} | |
} | |
public class OSCHub { | |
public static void main(String[] args) { | |
try { | |
//new Thread(new OSCHubServer(57110)).start(); | |
new Thread(new OSCHubServer(57120)).start(); | |
} | |
catch(Exception e) { | |
System.out.println(e.getMessage() + "caught Exception: "); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment