|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectde.sciss.net.OSCReceiver
public abstract class OSCReceiver
An OSCReceiver
manages reception
of incoming OSC messages. A receiver can be either revivable or
non-revivable.
DatagramChannel
(for UDP) or SocketChannel
(for TCP)).
When the channel is closed, the receiver cannot be restarted. The network channel
is closed for example when a TCP server shuts down, but also when trying to connect
to a TCP server that is not yet reachable.newUsing
constructors that takes a
protocol and address argument. A revivable receiver can be restarted because it
recreates the network channel if necessary.
The receiver launches a listening Thread
when
startListening
is called.
The OSCReceiver
has methods for registering and unregistering listeners
that get informed about incoming messages. Filtering out specific messages must be
done by the listeners.
The listening thread is stopped using stopListening
method.
Note that as of v0.3, you will most likely want to use preferably one of
OSCClient
or OSCServer
over OSCReceiver
.
Also note that as of v0.3, OSCReceiver
is abstract, which renders
direct instantiation impossible. To update old code, occurrences of
new OSCReceiver()
must be replaced by one of the
OSCReceiver.newUsing
methods! The "filter" functionality of
NetUtil 0.2 is now implied by calling setTarget( SocketAddress )
.
Here is an example that also demonstrates message sending using an instance of
OSCTransmitter
:
OSCReceiver rcv = null; OSCTransmitter trns; DatagramChannel dch = null; try { final SocketAddress addr = new InetSocketAddress( InetAddress.getLocalHost(), 57110 ); final Object notify = new Object(); // note: using constructors with SelectableChannel implies the receivers and // transmitters cannot be revived. to create revivable channels on the same socket, // you must use one of the newUsing methods that take an IP address and/or port // number. dch = DatagramChannel.open(); dch.socket().bind( null ); // assigns an automatic local socket address rcv = OSCReceiver.newUsing( dch ); trns = OSCTransmitter.newUsing( dch ); rcv.addOSCListener( new OSCListener() { public void messageReceived( OSCMessage msg, SocketAddress sender, long time ) { if( msg.getName().equals( "status.reply" )) { System.out.println( "scsynth is running. contains " + msg.getArg( 1 ) + " unit generators, " + msg.getArg( 2 ) + " synths, " + msg.getArg( 3 ) + " groups, " + msg.getArg( 4 ) + " synth defs.\n" + "CPU load is " + msg.getArg( 5 ) + "% (average) / " + msg.getArg( 6 ) + "% (peak)" ); synchronized( notify ) { notify.notifyAll(); } } } }); rcv.startListening(); trns.send( new OSCMessage( "/status", OSCMessage.NO_ARGS ), addr ); synchronized( notify ) { notify.wait( 5000 ); } } catch( InterruptedException e1 ) {} catch( IOException e2 ) { System.err.println( e2.getLocalizedMessage() ); } finally { if( rcv != null ) { rcv.dispose(); } else if( dch != null ) { try { dch.close(); } catch( IOException e4 ) {}; } }Note that the datagram channel needs to be bound to a valid reachable address, because
stopListening
will be sending a terminating message to
this channel. You can bind the channel using dch.socket().bind()
,
as shown in the example above.
Note that someone has reported trouble with the InetAddress.getLocalHost()
method
on a machine that has no proper IP configuration or DNS problems. In such a case when
you need to communicate only on this machine and not a network, use the loopback
address "127.0.0.1" as the filtering address or bind the socket to the loop
address manually before calling new OSCReceiver()
.
OSCClient
,
OSCServer
,
OSCTransmitter
Field Summary | |
---|---|
protected boolean |
allocBuf
|
protected ByteBuffer |
byteBuf
|
protected Object |
generalSync
|
protected boolean |
isListening
|
protected InetSocketAddress |
localAddress
|
protected boolean |
revivable
|
protected SocketAddress |
target
|
protected Thread |
thread
|
protected Object |
threadSync
|
Fields inherited from interface de.sciss.net.OSCChannel |
---|
DEFAULTBUFSIZE, kDumpBoth, kDumpHex, kDumpOff, kDumpText, TCP, UDP |
Constructor Summary | |
---|---|
protected |
OSCReceiver(OSCPacketCodec c,
String protocol,
InetSocketAddress localAddress,
boolean revivable)
|
Method Summary | |
---|---|
void |
addOSCListener(OSCListener listener)
Registers a listener that gets informed about incoming messages. |
protected void |
checkBuffer()
|
protected abstract void |
closeChannel()
|
abstract void |
connect()
Establishes connection for transports requiring connectivity (e.g. |
protected static String |
debugTimeString()
|
void |
dispose()
Disposes the resources associated with the OSC communicator. |
void |
dumpOSC(int mode,
PrintStream stream)
Changes the way processed OSC messages are printed to the standard err console. |
protected void |
flipDecodeDispatch(SocketAddress sender)
|
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 InetSocketAddress |
getLocalAddress()
Queries the receiver's local socket address. |
protected InetSocketAddress |
getLocalAddress(InetAddress addr,
int port)
|
String |
getProtocol()
Queries the transport protocol used by this communicator. |
abstract boolean |
isConnected()
Queries the connection state of the receiver. |
boolean |
isListening()
Queries whether the OSCReceiver is
listening or not. |
static OSCReceiver |
newUsing(DatagramChannel dch)
Creates a new instance of a non-revivable OSCReceiver , using
default codec and UDP transport on a given channel. |
static OSCReceiver |
newUsing(OSCPacketCodec c,
DatagramChannel dch)
Creates a new instance of a non-revivable OSCReceiver , using
a specific codec and UDP transport on a given channel. |
static OSCReceiver |
newUsing(OSCPacketCodec c,
SocketChannel sch)
Creates a new instance of a non-revivable OSCReceiver , using
a specific codec and TCP transport on a given channel. |
static OSCReceiver |
newUsing(OSCPacketCodec c,
String protocol)
Creates a new instance of a revivable OSCReceiver , using
a specific codec and transport protocol. |
static OSCReceiver |
newUsing(OSCPacketCodec c,
String protocol,
InetSocketAddress localAddress)
Creates a new instance of a revivable OSCReceiver , using
a specific codec and transport protocol and local socket address. |
static OSCReceiver |
newUsing(OSCPacketCodec c,
String protocol,
int port)
Creates a new instance of a revivable OSCReceiver , using
a specific codec and transport protocol and port. |
static OSCReceiver |
newUsing(OSCPacketCodec c,
String protocol,
int port,
boolean loopBack)
Creates a new instance of a revivable OSCReceiver , using
a specific codec and transport protocol and port. |
static OSCReceiver |
newUsing(SocketChannel sch)
Creates a new instance of a non-revivable OSCReceiver , using
default codec and TCP transport on a given channel. |
static OSCReceiver |
newUsing(String protocol)
Creates a new instance of a revivable OSCReceiver , using
default codec and a specific transport protocol. |
static OSCReceiver |
newUsing(String protocol,
InetSocketAddress localAddress)
Creates a new instance of a revivable OSCReceiver , using
default codec and a specific transport protocol and local socket address. |
static OSCReceiver |
newUsing(String protocol,
int port)
Creates a new instance of a revivable OSCReceiver , using
default codec and a specific transport protocol and port. |
static OSCReceiver |
newUsing(String protocol,
int port,
boolean loopBack)
Creates a new instance of a revivable OSCReceiver , using
default codec and a specific transport protocol and port. |
void |
removeOSCListener(OSCListener listener)
Unregisters a listener that gets informed about incoming messages |
protected abstract void |
sendGuardSignal()
|
void |
setBufferSize(int size)
Adjusts the buffer size for OSC messages. |
protected abstract void |
setChannel(SelectableChannel ch)
|
void |
setCodec(OSCPacketCodec c)
Specifies which codec is used in packet coding and decoding. |
abstract void |
setTarget(SocketAddress target)
|
void |
startListening()
Starts to wait for incoming messages. |
void |
stopListening()
Stops waiting for incoming messages. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface java.lang.Runnable |
---|
run |
Field Detail |
---|
protected Thread thread
protected final Object generalSync
protected final Object threadSync
protected boolean isListening
protected ByteBuffer byteBuf
protected boolean allocBuf
protected final InetSocketAddress localAddress
protected final boolean revivable
protected SocketAddress target
Constructor Detail |
---|
protected OSCReceiver(OSCPacketCodec c, String protocol, InetSocketAddress localAddress, boolean revivable)
Method Detail |
---|
public static OSCReceiver newUsing(String protocol) throws IOException
OSCReceiver
, 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.
TCP receivers are required
to be connected to one particular target, so setTarget
is
must be called prior to connect
or startListening
!
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 OSCReceiver newUsing(OSCPacketCodec c, String protocol) throws IOException
OSCReceiver
, 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.
TCP receivers are required
to be connected to one particular target, so setTarget
is
must be called prior to connect
or startListening
!
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 OSCReceiver newUsing(String protocol, int port) throws IOException
OSCReceiver
, 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 receiver listens), it does not determine the
remote sockets from which messages can be received. If you want to filter
out a particular remote (or target) socket, this can be done
using the setTarget
method!
TCP receivers are required
to be connected to one particular target, so setTarget
is
must be called prior to connect
or startListening
!
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 OSCReceiver newUsing(OSCPacketCodec c, String protocol, int port) throws IOException
OSCReceiver
, 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 receiver listens), it does not determine the
remote sockets from which messages can be received. If you want to filter
out a particular remote (or target) socket, this can be done
using the setTarget
method!
TCP receivers are required
to be connected to one particular target, so setTarget
is
must be called prior to connect
or startListening
!
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 OSCReceiver newUsing(String protocol, int port, boolean loopBack) throws IOException
OSCReceiver
, 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 receiver listens), it does not determine the
remote sockets from which messages can be received. If you want to filter
out a particular remote (or target) socket, this can be done
using the setTarget
method!
TCP receivers are required
to be connected to one particular target, so setTarget
is
must be called prior to connect
or startListening
!
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 OSCReceiver newUsing(OSCPacketCodec c, String protocol, int port, boolean loopBack) throws IOException
OSCReceiver
, 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 receiver listens), it does not determine the
remote sockets from which messages can be received. If you want to filter
out a particular remote (or target) socket, this can be done
using the setTarget
method!
TCP receivers are required
to be connected to one particular target, so setTarget
is
must be called prior to connect
or startListening
!
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 OSCReceiver newUsing(String protocol, InetSocketAddress localAddress) throws IOException
OSCReceiver
, using
default codec and a specific transport protocol and local socket address.
Note that localAdress
specifies the
local socket (at which the receiver listens), it does not determine the
remote sockets from which messages can be received. If you want to filter
out a particular remote (or target) socket, this can be done
using the setTarget
method!
TCP receivers are required
to be connected to one particular target, so setTarget
is
must be called prior to connect
or startListening
!
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
receiver was started).
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedpublic static OSCReceiver newUsing(OSCPacketCodec c, String protocol, InetSocketAddress localAddress) throws IOException
OSCReceiver
, using
a specific codec and transport protocol and local socket address.
Note that the port
specifies the
local socket (at which the receiver listens), it does not determine the
remote sockets from which messages can be received. If you want to filter
out a particular remote (or target) socket, this can be done
using the setTarget
method!
TCP receivers are required
to be connected to one particular target, so setTarget
is
must be called prior to connect
or startListening
!
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
receiver was started).
IOException
- if a networking error occurs while creating the socket
IllegalArgumentException
- if an illegal protocol is usedpublic static OSCReceiver newUsing(DatagramChannel dch) throws IOException
OSCReceiver
, using
default codec and UDP transport on a given channel. The caller should ensure that
the provided channel's socket was bound to a valid address
(using dch.socket().bind( SocketAddress )
).
Note that dch
specifies the
local socket (at which the receiver listens), it does not determine the
remote sockets from which messages can be received. If you want to filter
out a particular remote (or target) socket, this can be done
using the setTarget
method!
dch
- the DatagramChannel
to use as UDP socket.
IOException
- if a networking error occurs while configuring the socketpublic static OSCReceiver newUsing(OSCPacketCodec c, DatagramChannel dch) throws IOException
OSCReceiver
, using
a specific codec and UDP transport on a given channel. The caller should ensure that
the provided channel's socket was bound to a valid address
(using dch.socket().bind( SocketAddress )
).
Note that dch
specifies the
local socket (at which the receiver listens), it does not determine the
remote sockets from which messages can be received. If you want to filter
out a particular remote (or target) socket, this can be done
using the setTarget
method!
c
- the codec to usedch
- the DatagramChannel
to use as UDP socket.
IOException
- if a networking error occurs while configuring the socketpublic static OSCReceiver newUsing(SocketChannel sch) throws IOException
OSCReceiver
, using
default codec and TCP transport on a given channel. The caller should ensure that
the provided channel's socket was bound to a valid address
(using sch.socket().bind( SocketAddress )
). Furthermore,
the channel must be connected (using connect()
) before
being able to receive messages. Note that sch
specifies the
local socket (at which the receiver listens), it does not determine the
remote sockets from which messages can be received. The remote (or target)
socket must be explicitly specified using setTarget
before
trying to connect!
sch
- the SocketChannel
to use as TCP socket.
IOException
- if a networking error occurs while configuring the socketpublic static OSCReceiver newUsing(OSCPacketCodec c, SocketChannel sch) throws IOException
OSCReceiver
, using
a specific codec and TCP transport on a given channel. The caller should ensure that
the provided channel's socket was bound to a valid address
(using sch.socket().bind( SocketAddress )
). Furthermore,
the channel must be connected (using connect()
) before
being able to receive messages. Note that sch
specifies the
local socket (at which the receiver listens), it does not determine the
remote sockets from which messages can be received. The remote (or target)
socket must be explicitly specified using setTarget
before
trying to connect!
c
- the codec to usesch
- the SocketChannel
to use as TCP socket.
IOException
- if a networking error occurs while configuring the socketpublic 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()
. This port number may be 0
if the receiver was called with an unspecified port and has not yet been
started. In this case, to determine the port actually used, call this
method after the receiver has been started.
Note that if the receiver 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()
public abstract void setTarget(SocketAddress target)
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 void addOSCListener(OSCListener listener)
listener
- the listener to registerpublic void removeOSCListener(OSCListener listener)
listener
- the listener to remove from
the list of notified objects.public void startListening() throws IOException
startListening
. This method will check
the connection status of the channel, using isConnected
and establish the connection if necessary. Therefore,
calling connect
prior to startListening
is not necessary.
To find out at which port we are listening, call
getLocalAddress().getPort()
.
If the OSCReceiver
is already listening,
this method does nothing.
IOException
- when an error occurs
while establishing the channel connection.
In that case, no thread has been started
and hence stopListening() needn't be called
IllegalStateException
- when trying to call this method from within the OSC receiver thread
(which would obviously cause a loop)public boolean isListening()
OSCReceiver
is
listening or not.
public void stopListening() throws IOException
close()
on the datagram
channel, which causes the listening thread to die because
of a channel-closing exception.
IOException
- if an error occurs while shutting down
IllegalStateException
- when trying to call this method from within the OSC receiver thread
(which would obviously cause a loop)public void setBufferSize(int size)
OSCChannel
setBufferSize
in interface OSCChannel
size
- the new size in bytes.OSCChannel.getBufferSize()
public int getBufferSize()
OSCChannel
getBufferSize
in interface OSCChannel
OSCChannel.setBufferSize( int )
public 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 void dispose()
OSCChannel
dispose
in interface OSCChannel
protected abstract void sendGuardSignal() throws IOException
IOException
protected abstract void setChannel(SelectableChannel ch) throws IOException
IOException
protected abstract void closeChannel() throws IOException
IOException
protected static String debugTimeString()
protected void flipDecodeDispatch(SocketAddress sender) throws IOException
IOException
protected void checkBuffer()
protected InetSocketAddress getLocalAddress(InetAddress addr, int port) throws UnknownHostException
UnknownHostException
public abstract void connect() throws IOException
Having a connected channel without actually listening to incoming messages
is usually not making sense. You can call startListening
without
explicit prior call to connect
, because startListening
will establish the connection if necessary.
When a UDP transmitter
is created without an explicit DatagramChannel
– say by
calling OSCReceiver.newUsing( "udp" )
, calling
connect()
will actually create and bind a DatagramChannel
.
For a UDP receiver which was created with an explicit
DatagramChannel
. However, for TCP receivers,
this may throw an IOException
if the receiver
was already connected, therefore be sure to check isConnected()
before.
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.
IOException
isConnected()
,
startListening()
public abstract boolean isConnected()
true
if the receiver is connected, false
otherwise. For transports that do not use
connectivity (e.g. UDP) this returns false
, if the
underlying DatagramChannel
has not yet been created.connect()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |