- java.lang.Object
-
- org.praxislive.internal.osc.OSCClient
-
- All Implemented Interfaces:
OSCBidi,OSCChannel
public class OSCClient extends Object implements OSCBidi
This class groups together a transmitter and receiver, allowing bidirectional OSC communication from the perspective of a client. It simplifies the need to use several objects by uniting their functionality.In the following example, a client for UDP to SuperCollider server (scsynth) on the local machine is created. The client starts a synth by sending a
/s_newmessage, and stops the synth by sending a delayed a/n_setmessage. It waits for the synth to die which is recognized by an incoming/n_endmessage from scsynth after we've registered using a/notifycommand.final Object sync = new Object(); final OSCClient c; final OSCBundle bndl1, bndl2; final Integer nodeID; try { c = OSCClient.newUsing( OSCClient.UDP ); // create UDP client with any free port number c.setTarget( new InetSocketAddress( "127.0.0.1", 57110 )); // talk to scsynth on the same machine c.start(); // open channel and (in the case of TCP) connect, then start listening for replies } catch( IOException e1 ) { e1.printStackTrace(); return; } // register a listener for incoming osc messages c.addOSCListener( new OSCListener() { public void messageReceived( OSCMessage m, SocketAddress addr, long time ) { // if we get the /n_end message, wake up the main thread // ; note: we should better also check for the node ID to make sure // the message corresponds to our synth if( m.getName().equals( "/n_end" )) { synchronized( sync ) { sync.notifyAll(); } } } }); // let's see what's going out and coming in c.dumpOSC( OSCChannel.kDumpBoth, System.err ); try { // the /notify message tells scsynth to send info messages back to us c.send( new OSCMessage( "/notify", new Object[] { new Integer( 1 )})); // two bundles, one immediately (with 50ms delay), the other in 1.5 seconds bndl1 = new OSCBundle( System.currentTimeMillis() + 50 ); bndl2 = new OSCBundle( System.currentTimeMillis() + 1550 ); // this is going to be the node ID of our synth nodeID = new Integer( 1001 + i ); // this next messages creates the synth bndl1.addPacket( new OSCMessage( "/s_new", new Object[] { "default", nodeID, new Integer( 1 ), new Integer( 0 )})); // this next messages starts to releases the synth in 1.5 seconds (release time is 2 seconds) bndl2.addPacket( new OSCMessage( "/n_set", new Object[] { nodeID, "gate", new Float( -(2f + 1f) )})); // send both bundles (scsynth handles their respective timetags) c.send( bndl1 ); c.send( bndl2 ); // now wait for the signal from our osc listener (or timeout in 10 seconds) synchronized( sync ) { sync.wait( 10000 ); } catch( InterruptedException e1 ) {} // ok, unsubscribe getting info messages c.send( new OSCMessage( "/notify", new Object[] { new Integer( 0 )})); // ok, stop the client // ; this isn't really necessary as we call dispose soon c.stop(); } catch( IOException e11 ) { e11.printStackTrace(); } // dispose the client (it gets stopped if still running) c.dispose();- Since:
- NetUtil 0.30
- Version:
- 0.37, 12-May-09
- Author:
- Hanns Holger Rutz
- See Also:
OSCTransmitter,OSCReceiver,OSCServer
-
-
Field Summary
-
Fields inherited from interface org.praxislive.internal.osc.OSCChannel
DEFAULTBUFSIZE, kDumpBoth, kDumpHex, kDumpOff, kDumpText, TCP, UDP
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddOSCListener(OSCListener listener)Registers a listener that gets informed about incoming messages.voidconnect()Initializes network channel (if necessary) and establishes connection for transports requiring connectivity (e.g.voiddispose()Destroys the client and frees resources associated with it.voiddumpIncomingOSC(int mode, PrintStream stream)Changes the way incoming messages are dumped to the console.voiddumpOSC(int mode, PrintStream stream)Changes the way incoming and outgoing OSC messages are printed to the standard err console.voiddumpOutgoingOSC(int mode, PrintStream stream)Changes the way outgoing messages are dumped to the console.intgetBufferSize()Queries the buffer size used for sending and receiving OSC messages.OSCPacketCodecgetCodec()Queries the codec used in packet coding and decoding.InetSocketAddressgetLocalAddress()Queries the client side socket address.StringgetProtocol()Queries the transport protocol used by this communicator.booleanisActive()Queries whether the client was activated or not.booleanisConnected()Queries the connection state of the client.static OSCClientnewUsing(String protocol)Creates a new instance of anOSCClient, using default codec and a specific transport protocol.static OSCClientnewUsing(String protocol, int port)Creates a new instance of anOSCClient, using default codec and a specific transport protocol and port.static OSCClientnewUsing(String protocol, int port, boolean loopBack)Creates a new instance of anOSCClient, using default codec and a specific transport protocol and port.static OSCClientnewUsing(String protocol, InetSocketAddress localAddress)Creates a new instance of anOSCClient, using default codec and a specific transport protocol and local socket address.static OSCClientnewUsing(OSCPacketCodec c, String protocol)Creates a new instance of anOSCClient, using a specific codec and transport protocol.static OSCClientnewUsing(OSCPacketCodec c, String protocol, int port)Creates a new instance of anOSCClient, using a specific codec and transport protocol and port.static OSCClientnewUsing(OSCPacketCodec c, String protocol, int port, boolean loopBack)Creates a new instance of anOSCClient, using a specific codec and transport protocol and port.static OSCClientnewUsing(OSCPacketCodec c, String protocol, InetSocketAddress localAddress)Creates a new instance of anOSCClient, using a given codec, a specific transport protocol and local socket address.voidremoveOSCListener(OSCListener listener)Unregisters a listener that gets informed about incoming messagesvoidsend(OSCPacket p)Sends an OSC packet (bundle or message) to the target network address.voidsetBufferSize(int size)Adjusts the buffer size for OSC messages (both for sending and receiving).voidsetCodec(OSCPacketCodec c)Specifies which codec is used in packet coding and decoding.voidsetTarget(SocketAddress target)Specifies the client's target address, that is the address of the server to talk to.voidstart()Starts the client.voidstop()Stops the communicator.
-
-
-
Method Detail
-
newUsing
public static OSCClient newUsing(String protocol) throws IOException
Creates a new instance of anOSCClient, 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 usegetLocalAddressafterwards.- Parameters:
protocol- the protocol to use, currently eitherUDPorTCP- Returns:
- the newly created client
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used- See Also:
OSCChannel.UDP,OSCChannel.TCP,getLocalAddress()
-
newUsing
public static OSCClient newUsing(OSCPacketCodec c, String protocol) throws IOException
Creates a new instance of anOSCClient, 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 usegetLocalAddressafterwards.- Parameters:
c- the codec to useprotocol- the protocol to use, currently eitherUDPorTCP- Returns:
- the newly created client
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used- Since:
- NetUtil 0.33
- See Also:
OSCChannel.UDP,OSCChannel.TCP,getLocalAddress()
-
newUsing
public static OSCClient newUsing(String protocol, int port) throws IOException
Creates a new instance of anOSCClient, using default codec and a specific transport protocol and port. It uses the local machine's IP.Note that the
portspecifies the local socket (at which the client listens and from which it sends), it does not determine the remote sockets from which messages can be received and to which messages are sent. The target socket can be set using thesetTargetmethod!- Parameters:
protocol- the protocol to use, currently eitherUDPorTCPport- the port number for the OSC socket, or0to use an arbitrary free port- Returns:
- the newly created client
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used
-
newUsing
public static OSCClient newUsing(OSCPacketCodec c, String protocol, int port) throws IOException
Creates a new instance of anOSCClient, using a specific codec and transport protocol and port. It uses the local machine's IP.Note that the
portspecifies the local socket (at which the client listens and from which it sends), it does not determine the remote sockets from which messages can be received and to which messages are sent. The target socket can be set using thesetTargetmethod!- Parameters:
c- the codec to useprotocol- the protocol to use, currently eitherUDPorTCPport- the port number for the OSC socket, or0to use an arbitrary free port- Returns:
- the newly created client
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used- Since:
- NetUtil 0.33
-
newUsing
public static OSCClient newUsing(String protocol, int port, boolean loopBack) throws IOException
Creates a new instance of anOSCClient, using default codec and a specific transport protocol and port. It uses the local machine's IP or the "loopback" address.Note that the
portspecifies the local socket (at which the client listens and from which it sends), it does not determine the remote sockets from which messages can be received and to which messages are sent. The target socket can be set using thesetTargetmethod!- Parameters:
protocol- the protocol to use, currently eitherUDPorTCPport- the port number for the OSC socket, or0to use an arbitrary free portloopBack- iftrue, the "loopback" address ("127.0.0.1") is used which limits communication to the local machine. Iffalse, the special IP"0.0.0.0"is used which means messages from any IP as well as from the loopback are accepted- Returns:
- the newly created client
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used
-
newUsing
public static OSCClient newUsing(OSCPacketCodec c, String protocol, int port, boolean loopBack) throws IOException
Creates a new instance of anOSCClient, using a specific codec and transport protocol and port. It uses the local machine's IP or the "loopback" address.Note that the
portspecifies the local socket (at which the client listens and from which it sends), it does not determine the remote sockets from which messages can be received and to which messages are sent. The target socket can be set using thesetTargetmethod!- Parameters:
c- the codec to useprotocol- the protocol to use, currently eitherUDPorTCPport- the port number for the OSC socket, or0to use an arbitrary free portloopBack- iftrue, the "loopback" address ("127.0.0.1") is used which limits communication to the local machine. Iffalse, the special IP"0.0.0.0"is used which means messages from any IP as well as from the loopback are accepted- Returns:
- the newly created client
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used- Since:
- NetUtil 0.33
-
newUsing
public static OSCClient newUsing(String protocol, InetSocketAddress localAddress) throws IOException
Creates a new instance of anOSCClient, using default codec and a specific transport protocol and local socket address. Note thatlocalAdressspecifies the local socket (at which the receiver listens and from which the transmitter sends), it does not determine the remote sockets to which the client connects. To specify the remote socket, use thesetTargetmethod!- Parameters:
protocol- the protocol to use, currently eitherUDPorTCP- Returns:
- the newly created client
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used- Since:
- NetUtil 0.39
-
newUsing
public static OSCClient newUsing(OSCPacketCodec c, String protocol, InetSocketAddress localAddress) throws IOException
Creates a new instance of anOSCClient, using a given codec, a specific transport protocol and local socket address. Note thatlocalAdressspecifies the local socket (at which the receiver listens and from which the transmitter sends), it does not determine the remote sockets to which the client connects. To specify the remote socket, use thesetTargetmethod!- Parameters:
c- the codec to useprotocol- the protocol to use, currently eitherUDPorTCP- Returns:
- the newly created client
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used- Since:
- NetUtil 0.39
-
getProtocol
public String getProtocol()
Description copied from interface:OSCChannelQueries the transport protocol used by this communicator.- Specified by:
getProtocolin interfaceOSCChannel- Returns:
- the protocol, such as
UDPorTCP - See Also:
OSCChannel.UDP,OSCChannel.TCP
-
getLocalAddress
public InetSocketAddress getLocalAddress() throws IOException
Queries the client side socket address. This is the address from which the client sends and at which it listens for replies. You can determine the host and port from the returned address by callinggetHostName()(or for the IPgetAddress().getHostAddress()) andgetPort().Note that if the client is bound to the accept-any IP
"0.0.0.0", which happens for example when callingnewUsing( <protocol>, 0, false ), the returned IP will be the localhost's IP, so you can patch the result directly into anysetTargetcall.- Specified by:
getLocalAddressin interfaceOSCChannel- Returns:
- the address of the client's local socket.
- Throws:
IOException- if the local host could not be resolved- See Also:
InetSocketAddress.getHostName(),InetSocketAddress.getAddress(),InetSocketAddress.getPort(),getProtocol()
-
setTarget
public void setTarget(SocketAddress target)
Specifies the client's target address, that is the address of the server to talk to. You should call this method only once and you must call it before starting the client or sending messages.- Parameters:
target- the address of the server. Usually you construct an appropriateInetSocketAddress- See Also:
InetSocketAddress
-
setCodec
public void setCodec(OSCPacketCodec c)
Description copied from interface:OSCChannelSpecifies which codec is used in packet coding and decoding.- Specified by:
setCodecin interfaceOSCChannel- Parameters:
c- the codec to use
-
getCodec
public OSCPacketCodec getCodec()
Description copied from interface:OSCChannelQueries the codec used in packet coding and decoding.- Specified by:
getCodecin interfaceOSCChannel- Returns:
- the current codec of this channel
- See Also:
OSCPacketCodec.getDefaultCodec()
-
connect
public void connect() throws IOExceptionInitializes network channel (if necessary) and establishes connection for transports requiring connectivity (e.g. TCP). Do not call this method when the client is already connected. Note thatstartimplicitly callsconnectif necessary, so usually you will not need to callconnectyourself.- Throws:
IOException- if a networking error occurs. Possible reasons: - the underlying network channel had been closed by the server. - the transport is TCP and the server is not available. - the transport is TCP and the client was stopped before (unable to revive).- See Also:
isConnected(),start()
-
isConnected
public boolean isConnected()
Queries the connection state of the client.- Returns:
trueif the client is connected,falseotherwise. For transports that do not use connectivity (e.g. UDP) this returnsfalse, if the underlyingDatagramChannelhas not yet been created.- See Also:
connect()
-
send
public void send(OSCPacket p) throws IOException
Sends an OSC packet (bundle or message) to the target network address. Make sure that the client's target has been specified before by callingsetTarget()- Parameters:
p- the packet to send- Throws:
IOException- if a write error, OSC encoding error, buffer overflow error or network error occurs, for example if a TCP client was not connected before.NullPointerException- for a UDP client if the target has not been specified- See Also:
setTarget( SocketAddress )
-
addOSCListener
public void addOSCListener(OSCListener listener)
Registers a listener that gets informed about incoming messages. You can call this both when the client is active or inactive.- Parameters:
listener- the listener to register
-
removeOSCListener
public void removeOSCListener(OSCListener listener)
Unregisters a listener that gets informed about incoming messages- Parameters:
listener- the listener to remove from the list of notified objects.
-
start
public void start() throws IOExceptionStarts the client. This callsconnectif the transport requires connectivity (e.g. TCP) and the channel is not yet connected. It then tells the underlying OSC receiver to start listening.- Specified by:
startin interfaceOSCBidi- Throws:
IOException- if a networking error occurs. Possible reasons: - the underlying network channel had been closed by the server. - the transport is TCP and the server is not available. - the transport is TCP and the client was stopped before (unable to revive).
-
isActive
public boolean isActive()
Queries whether the client was activated or not. A client is activated by calling itsstart()method and deactivated by callingstop().
-
stop
public void stop() throws IOExceptionDescription copied from interface:OSCBidiStops the communicator.- Specified by:
stopin interfaceOSCBidi- Throws:
IOException- if a networking error occurs
-
setBufferSize
public void setBufferSize(int size)
Adjusts the buffer size for OSC messages (both for sending and receiving). This is the maximum size an OSC packet (bundle or message) can grow to. The initial buffer size isDEFAULTBUFSIZE. Do not call this method while the client is active!- Specified by:
setBufferSizein interfaceOSCChannel- Parameters:
size- the new size in bytes.- Throws:
IllegalStateException- if trying to change the buffer size while the client is active (listening).- See Also:
isActive(),getBufferSize()
-
getBufferSize
public int getBufferSize()
Queries the buffer size used for sending and receiving OSC messages. This is the maximum size an OSC packet (bundle or message) can grow to. The initial buffer size isDEFAULTBUFSIZE.- Specified by:
getBufferSizein interfaceOSCChannel- Returns:
- the buffer size in bytes.
- See Also:
setBufferSize( int )
-
dumpOSC
public void dumpOSC(int mode, PrintStream stream)Changes the way incoming and outgoing OSC messages are printed to the standard err console. By default messages are not printed.- Specified by:
dumpOSCin interfaceOSCChannel- Parameters:
mode- one ofkDumpOff(don't dump, default),kDumpText(dump human readable string),kDumpHex(hexdump), orkDumpBoth(both text and hex)stream- the stream to print on, ornullwhich is shorthand forSystem.err- See Also:
dumpIncomingOSC( int, PrintStream ),dumpOutgoingOSC( int, PrintStream ),OSCChannel.kDumpOff,OSCChannel.kDumpText,OSCChannel.kDumpHex,OSCChannel.kDumpBoth
-
dumpIncomingOSC
public void dumpIncomingOSC(int mode, PrintStream stream)Description copied from interface:OSCBidiChanges the way incoming messages are dumped to the console. By default incoming messages are not dumped. Incoming messages are those received by the client from the server, before they get delivered to registeredOSCListeners.- Specified by:
dumpIncomingOSCin interfaceOSCBidi- Parameters:
mode- seedumpOSC( int )for detailsstream- the stream to print on, ornullwhich is shorthand forSystem.err- See Also:
OSCChannel.dumpOSC( int, PrintStream ),OSCBidi.dumpOutgoingOSC( int, PrintStream )
-
dumpOutgoingOSC
public void dumpOutgoingOSC(int mode, PrintStream stream)Description copied from interface:OSCBidiChanges the way outgoing messages are dumped to the console. By default outgoing messages are not dumped. Outgoing messages are those send viasend.- Specified by:
dumpOutgoingOSCin interfaceOSCBidi- Parameters:
mode- seedumpOSC( int )for detailsstream- the stream to print on, ornullwhich is shorthand forSystem.err- See Also:
OSCChannel.dumpOSC( int, PrintStream ),OSCBidi.dumpIncomingOSC( int, PrintStream )
-
dispose
public void dispose()
Destroys the client and frees resources associated with it. This automatically stops the client and closes the networking channel. Do not use this client instance any more after callingdispose.- Specified by:
disposein interfaceOSCChannel
-
-