|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.sciss.net.OSCServer
public abstract class OSCServer
This class dynamically groups together a transmitters and receivers, allowing bidirectional OSC communication from the perspective of a server. It simplifies the need to use several objects by uniting their functionality, and by automatically establishing child connections.
In UDP
mode, simply one receiver and transmitter are handling all the
communication. In TCP
mode, a ServerSocketChannel
is set up to
wait for incoming connection requests. Requests are satisfied by opening a new receiver
and transmitter for each connection.
In the following example, a simple TCP server is created that accepts connections at
port 0x5454. The connections understand the OSC commands /pause
(disconnect the server for a few seconds), /quit
(quit the server),
and /dumpOSC
(turn on/off printing of message traffic). Each incoming
message is replied with a /done
message.
private boolean pause = false; // (must be an instance or static field to be useable // from an anonymous inner class) final Object sync = new Object(); final OSCServer c; try { // create TCP server on loopback port 0x5454 c = OSCServer.newUsing( OSCServer.TCP, 0x5454, true ); } catch( IOException e1 ) { e1.printStackTrace(); return; } // now add a listener for incoming messages from // any of the active connections c.addOSCListener( new OSCListener() { public void messageReceived( OSCMessage m, SocketAddress addr, long time ) { // first of all, send a reply message (just a demo) try { c.send( new OSCMessage( "/done", new Object[] { m.getName() }), addr ); } catch( IOException e1 ) { e1.printStackTrace(); } if( m.getName().equals( "/pause" )) { // tell the main thread to pause the server, // wake up the main thread pause = true; synchronized( sync ) { sync.notifyAll(); } } else if( m.getName().equals( "/quit" )) { // wake up the main thread synchronized( sync ) { sync.notifyAll(); } } else if( m.getName().equals( "/dumpOSC" )) { // change dumping behaviour c.dumpOSC( ((Number) m.getArg( 0 )).intValue(), System.err ); } } }); try { do { if( pause ) { System.out.println( " waiting four seconds..." ); try { Thread.sleep( 4000 ); } catch( InterruptedException e1 ) {} pause = false; } System.out.println( " start()" ); // start the server (make it attentive for incoming connection requests) c.start(); try { synchronized( sync ) { sync.wait(); } } catch( InterruptedException e1 ) {} System.out.println( " stop()" ); c.stop(); } while( pause ); } catch( IOException e1 ) { e1.printStackTrace(); } // kill the server, free its resources c.dispose();Here is an example of sending commands to this server from SuperCollider:
n = NetAddr( "127.0.0.1", 0x5454 ); r = OSCresponderNode( n, '/done', { arg time, resp, msg; ("reply : " ++ msg.asString).postln; }).add; n.connect; n.sendMsg( '/dumpOSC', 3 ); n.sendMsg( '/pause' ); n.isConnected; // --> false n.connect; n.sendMsg( '/quit' ); r.remove;
OSCClient
Field Summary | |
---|---|
protected OSCPacketCodec |
defaultCodec
|
Fields inherited from interface de.sciss.net.OSCChannel |
---|
DEFAULTBUFSIZE, kDumpBoth, kDumpHex, kDumpOff, kDumpText, TCP, UDP |
Constructor Summary | |
---|---|
protected |
OSCServer(OSCPacketCodec c,
String protocol)
|
Method Summary | |
---|---|
abstract void |
addOSCListener(OSCListener listener)
Registers a listener that gets informed about incoming messages (from any of the connected clients). |
abstract void |
dispose()
Destroys the server and frees resources associated with it. |
abstract void |
dumpIncomingOSC(int mode,
PrintStream stream)
Changes the way incoming messages are dumped to the console. |
void |
dumpOSC(int mode,
PrintStream stream)
Changes the way processed OSC messages are printed to the standard err console. |
abstract void |
dumpOutgoingOSC(int mode,
PrintStream stream)
Changes the way outgoing messages are dumped to the console. |
abstract int |
getBufferSize()
Queries the buffer size used for coding or decoding OSC messages. |
OSCPacketCodec |
getCodec()
Queries the codec used in packet coding and decoding. |
abstract OSCPacketCodec |
getCodec(SocketAddress target)
Queries the codec used in packet coding and decoding for a given client socket. |
abstract InetSocketAddress |
getLocalAddress()
Queries the server socket's address. |
protected InetSocketAddress |
getLocalAddress(InetAddress addr,
int port)
|
String |
getProtocol()
Queries the transport protocol used by this communicator. |
abstract boolean |
isActive()
Checks whether the server is active (was started) or not (is stopped). |
static OSCServer |
newUsing(OSCPacketCodec c,
String protocol)
Creates a new instance of an OSCServer , using
a specific codec and transport protocol. |
static OSCServer |
newUsing(OSCPacketCodec c,
String protocol,
InetSocketAddress localAddress)
Creates a new instance of an OSCServer , using
a given codec, a specific transport protocol and local socket address. |
static OSCServer |
newUsing(OSCPacketCodec c,
String protocol,
int port)
Creates a new instance of an OSCServer , using
a specific codec and transport protocol and port. |
static OSCServer |
newUsing(OSCPacketCodec c,
String protocol,
int port,
boolean loopBack)
Creates a new instance of an OSCServer , using
a specific codec and transport protocol and port. |
static OSCServer |
newUsing(String protocol)
Creates a new instance of an OSCServer , using
default codec and a specific transport protocol. |
static OSCServer |
newUsing(String protocol,
InetSocketAddress localAddress)
Creates a new instance of an OSCServer , using
default codec and a specific transport protocol and local socket address. |
static OSCServer |
newUsing(String protocol,
int port)
Creates a new instance of an OSCServer , using
default codec and a specific transport protocol and port. |
static OSCServer |
newUsing(String protocol,
int port,
boolean loopBack)
Creates a new instance of an OSCServer , using
default codec and a specific transport protocol and port. |
abstract void |
removeOSCListener(OSCListener listener)
Unregisters a listener that gets informed about incoming messages |
abstract void |
send(OSCPacket p,
SocketAddress target)
Sends an OSC packet (bundle or message) to the given network address. |
abstract void |
setBufferSize(int size)
Adjusts the buffer size for OSC messages. |
void |
setCodec(OSCPacketCodec c)
Specifies which codec is used in packet coding and decoding. |
abstract void |
setCodec(OSCPacketCodec c,
SocketAddress target)
Specifies which codec is used in packet coding and decoding for a given client socket. |
abstract void |
start()
Starts the server. |
abstract void |
stop()
Stops the server. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected OSCPacketCodec defaultCodec
Constructor Detail |
---|
protected OSCServer(OSCPacketCodec c, String protocol)
Method Detail |
---|
public static OSCServer newUsing(String protocol) throws IOException
OSCServer
, using
default codec and a specific transport protocol. It picks an arbitrary free port
and uses the local machine's IP. To determine the resulting
port, you can use getLocalAddress
afterwards.
protocol
- the protocol to use, currently either UDP
or TCP
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedOSCChannel.UDP
,
OSCChannel.TCP
,
getLocalAddress()
public static OSCServer newUsing(OSCPacketCodec c, String protocol) throws IOException
OSCServer
, using
a specific codec and transport protocol. It picks an arbitrary free port
and uses the local machine's IP. To determine the resulting
port, you can use getLocalAddress
afterwards.
c
- the codec to useprotocol
- the protocol to use, currently either UDP
or TCP
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedOSCChannel.UDP
,
OSCChannel.TCP
,
getLocalAddress()
public static OSCServer newUsing(String protocol, int port) throws IOException
OSCServer
, using
default codec and a specific transport protocol and port. It
uses the local machine's IP.
Note that the port
specifies the
local socket (at which the server listens and from which it sends, or
in the case of TCP transport, from which it establishes client connections),
it does not determine the remote sockets. The address of a remote client
communicating to this server is passed in the messageReceived
method of any registered OSCListener
, and must be picked
up and handed in to the send
method to reply back to the client!
protocol
- the protocol to use, currently either UDP
or TCP
port
- the port number for the OSC socket, or 0
to use an arbitrary free port
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedpublic static OSCServer newUsing(OSCPacketCodec c, String protocol, int port) throws IOException
OSCServer
, using
a specific codec and transport protocol and port. It
uses the local machine's IP.
Note that the port
specifies the
local socket (at which the server listens and from which it sends, or
in the case of TCP transport, from which it establishes client connections),
it does not determine the remote sockets. The address of a remote client
communicating to this server is passed in the messageReceived
method of any registered OSCListener
, and must be picked
up and handed in to the send
method to reply back to the client!
c
- the codec to useprotocol
- the protocol to use, currently either UDP
or TCP
port
- the port number for the OSC socket, or 0
to use an arbitrary free port
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedpublic static OSCServer newUsing(String protocol, int port, boolean loopBack) throws IOException
OSCServer
, using
default codec and a specific transport protocol and port. It
uses the local machine's IP or the "loopback" address.
Note that the port
specifies the
local socket (at which the server listens and from which it sends, or
in the case of TCP transport, from which it establishes client connections),
it does not determine the remote sockets. The address of a remote client
communicating to this server is passed in the messageReceived
method of any registered OSCListener
, and must be picked
up and handed in to the send
method to reply back to the client!
protocol
- the protocol to use, currently either UDP
or TCP
port
- the port number for the OSC socket, or 0
to use an arbitrary free portloopBack
- if true
, the "loopback" address ("127.0.0.1"
)
is used which limits communication to the local machine. If false
, the
special IP "0.0.0.0"
is used which means messages from any IP as well as from
the loopback are accepted
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedpublic static OSCServer newUsing(OSCPacketCodec c, String protocol, int port, boolean loopBack) throws IOException
OSCServer
, using
a specific codec and transport protocol and port. It
uses the local machine's IP or the "loopback" address.
Note that the port
specifies the
local socket (at which the server listens and from which it sends, or
in the case of TCP transport, from which it establishes client connections),
it does not determine the remote sockets. The address of a remote client
communicating to this server is passed in the messageReceived
method of any registered OSCListener
, and must be picked
up and handed in to the send
method to reply back to the client!
c
- the codec to useprotocol
- the protocol to use, currently either UDP
or TCP
port
- the port number for the OSC socket, or 0
to use an arbitrary free portloopBack
- if true
, the "loopback" address ("127.0.0.1"
)
is used which limits communication to the local machine. If false
, the
special IP "0.0.0.0"
is used which means messages from any IP as well as from
the loopback are accepted
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedpublic static OSCServer newUsing(String protocol, InetSocketAddress localAddress) throws IOException
OSCServer
, using
default codec and a specific transport protocol and local socket address.
protocol
- the protocol to use, currently either UDP
or TCP
localAddress
- a valid address to use for the OSC socket. If the port is 0
,
an arbitrary free port is picked when the receiver is started. (you can find out
the actual port in this case by calling getLocalAddress()
after the
server was started).
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedOSCChannel.UDP
,
OSCChannel.TCP
,
getLocalAddress()
public static OSCServer newUsing(OSCPacketCodec c, String protocol, InetSocketAddress localAddress) throws IOException
OSCServer
, using
a given codec, a specific transport protocol and local socket address.
c
- the codec to useprotocol
- the protocol to use, currently either UDP
or TCP
localAddress
- a valid address to use for the OSC socket. If the port is 0
,
an arbitrary free port is picked when the receiver is started. (you can find out
the actual port in this case by calling getLocalAddress()
after the
server was started).
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedOSCChannel.UDP
,
OSCChannel.TCP
,
getLocalAddress()
public String getProtocol()
OSCChannel
getProtocol
in interface OSCChannel
UDP
or TCP
OSCChannel.UDP
,
OSCChannel.TCP
public abstract InetSocketAddress getLocalAddress() throws IOException
getHostName()
(or for the IP getAddress().getHostAddress()
)
and getPort()
.
Note that if the server is bound to the accept-any IP "0.0.0.0"
,
which happens for example when calling newUsing( <protocol>, 0, false )
,
the returned IP will be the localhost's IP, so you can
patch the result directly into any setTarget
call.
getLocalAddress
in interface OSCChannel
IOException
- if the local host could not be resolvedInetSocketAddress.getHostName()
,
InetSocketAddress.getAddress()
,
InetSocketAddress.getPort()
,
getProtocol()
public abstract void send(OSCPacket p, SocketAddress target) throws IOException
TCP
mode, trying to send
to a client which is not connected will throw an exception.
In a future version of NetUtil, there will be an interface
to detect clients connecting and disconnecting. For now,
clients can be implicitly detected by a registered OSCListener
.
p
- the packet to sendtarget
- the target address to send the packet to
IOException
- if a write error, OSC encoding error,
buffer overflow error or network error occurs,
if no client connection for the given address existspublic abstract void addOSCListener(OSCListener listener)
listener
- the listener to registerpublic abstract void removeOSCListener(OSCListener listener)
listener
- the listener to remove from
the list of notified objects.public abstract void start() throws IOException
start
in interface OSCBidi
IOException
- if a networking error occurspublic abstract boolean isActive()
isActive
in interface OSCBidi
true
if the server is active, false
otherwisepublic abstract void stop() throws IOException
TCP
mode, this implies
that all client connections are closed. Stops listening for
incoming OSC traffic.
stop
in interface OSCBidi
IOException
- if a networking error occurspublic abstract void setBufferSize(int size)
OSCChannel
setBufferSize
in interface OSCChannel
size
- the new size in bytes.OSCChannel.getBufferSize()
public abstract int getBufferSize()
OSCChannel
getBufferSize
in interface OSCChannel
OSCChannel.setBufferSize( int )
public void setCodec(OSCPacketCodec c)
OSCChannel
setCodec
in interface OSCChannel
c
- the codec to usepublic OSCPacketCodec getCodec()
OSCChannel
getCodec
in interface OSCChannel
OSCPacketCodec.getDefaultCodec()
public abstract void setCodec(OSCPacketCodec c, SocketAddress target) throws IOException
c
- the codec to usetarget
- the client's address for whom the codec is changed
IOException
- if a networking error occurs or the client does not existpublic abstract OSCPacketCodec getCodec(SocketAddress target) throws IOException
target
- the client's address for whom the codec is queried
IOException
- if a networking error occurs or the client does not existOSCPacketCodec.getDefaultCodec()
public final void dumpOSC(int mode, PrintStream stream)
OSCChannel
dumpOSC
in interface OSCChannel
mode
- one of kDumpOff
(don't dump, default),
kDumpText
(dump human readable string),
kDumpHex
(hexdump), or
kDumpBoth
(both text and hex)stream
- the stream to print on, or null
which
is shorthand for System.err
OSCChannel.kDumpOff
,
OSCChannel.kDumpText
,
OSCChannel.kDumpHex
,
OSCChannel.kDumpBoth
public abstract void dumpIncomingOSC(int mode, PrintStream stream)
OSCBidi
OSCListener
s.
dumpIncomingOSC
in interface OSCBidi
mode
- see dumpOSC( int )
for detailsstream
- the stream to print on, or null
which
is shorthand for System.err
OSCChannel.dumpOSC( int, PrintStream )
,
OSCBidi.dumpOutgoingOSC( int, PrintStream )
public abstract void dumpOutgoingOSC(int mode, PrintStream stream)
OSCBidi
send
.
dumpOutgoingOSC
in interface OSCBidi
mode
- see dumpOSC( int )
for detailsstream
- the stream to print on, or null
which
is shorthand for System.err
OSCChannel.dumpOSC( int, PrintStream )
,
OSCBidi.dumpIncomingOSC( int, PrintStream )
public abstract void dispose()
dispose.
dispose
in interface OSCChannel
protected InetSocketAddress getLocalAddress(InetAddress addr, int port) throws UnknownHostException
UnknownHostException
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |