- java.lang.Object
-
- org.praxislive.internal.osc.OSCTransmitter
-
- All Implemented Interfaces:
OSCChannel
public abstract class OSCTransmitter extends Object implements OSCChannel
A class that sends OSC packets (messages or bundles) to a target OSC server. Each instance takes a network channel, either explictly specified by aDatagramChannel(for UDP) orSocketChannel(for TCP), or internally opened when a protocol type is specified.Messages are send by invoking one of the
sendmethods. For an example, please refer to theOSCReceiverdocument.Note that as of v0.3, you will most likely want to use preferably one of
OSCClientorOSCServeroverOSCTransmitter. Also note that as of v0.3,OSCTransmitteris abstract, which renders direct instantiation impossible. To update old code, occurences ofnew OSCTransmitter()must be replaced by one of theOSCTransmitter.newUsingmethods!- Version:
- 0.33, 05-Mar-09
- Author:
- Hanns Holger Rutz
- See Also:
OSCClient,OSCServer,OSCReceiver
-
-
Field Summary
Fields Modifier and Type Field Description protected booleanallocBufprotected ByteBufferbyteBufprotected intdumpModeprotected InetSocketAddresslocalAddressprotected PrintStreamprintStreamprotected booleanrevivableprotected Objectsyncprotected SocketAddresstarget-
Fields inherited from interface org.praxislive.internal.osc.OSCChannel
DEFAULTBUFSIZE, kDumpBoth, kDumpHex, kDumpOff, kDumpText, TCP, UDP
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedOSCTransmitter(OSCPacketCodec c, String protocol, InetSocketAddress localAddress, boolean revivable)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected voidcheckBuffer()abstract voidconnect()Establishes connection for transports requiring connectivity (e.g.voiddispose()Disposes the resources associated with the OSC communicator.voiddumpOSC(int mode, PrintStream stream)Changes the way processed OSC messages are printed to the standard err console.intgetBufferSize()Queries the buffer size used for coding or decoding OSC messages.protected abstract SelectableChannelgetChannel()OSCPacketCodecgetCodec()Queries the codec used in packet coding and decoding.abstract InetSocketAddressgetLocalAddress()Queries the transmitter's local socket address.protected InetSocketAddressgetLocalAddress(InetAddress addr, int port)StringgetProtocol()Queries the transport protocol used by this communicator.abstract booleanisConnected()Queries the connection state of the transmitter.static OSCTransmitternewUsing(String protocol)Creates a new instance of anOSCTransmitter, using default codec and a specific transport protocol.static OSCTransmitternewUsing(String protocol, int port)Creates a new instance of anOSCTransmitter, using default codec and a specific transport protocol and port.static OSCTransmitternewUsing(String protocol, int port, boolean loopBack)Creates a new instance of anOSCTransmitter, using default codec and a specific transport protocol and port.static OSCTransmitternewUsing(String protocol, InetSocketAddress localAddress)Creates a new instance of anOSCTransmitter, using default codec and a specific transport protocol and local socket address.static OSCTransmitternewUsing(DatagramChannel dch)Creates a new instance of anOSCTransmitter, using default codec and UDP transport on a given channel.static OSCTransmitternewUsing(SocketChannel sch)Creates a new instance of anOSCTransmitter, using default codec and TCP transport on a given channel.static OSCTransmitternewUsing(OSCPacketCodec c, String protocol)Creates a new instance of anOSCTransmitter, using a specific codec and transport protocol.static OSCTransmitternewUsing(OSCPacketCodec c, String protocol, int port)Creates a new instance of anOSCTransmitter, using a specific codec and transport protocol and port.static OSCTransmitternewUsing(OSCPacketCodec c, String protocol, int port, boolean loopBack)Creates a new instance of anOSCTransmitter, using a specific codec and transport protocol and port.static OSCTransmitternewUsing(OSCPacketCodec c, String protocol, InetSocketAddress localAddress)Creates a new instance of anOSCTransmitter, using a specific codec and transport protocol and local socket address.static OSCTransmitternewUsing(OSCPacketCodec c, DatagramChannel dch)Creates a new instance of anOSCTransmitter, using a specific codec and UDP transport on a given channel.static OSCTransmitternewUsing(OSCPacketCodec c, SocketChannel sch)Creates a new instance of anOSCTransmitter, using a specific codec and TCP transport on a given channel.voidsend(OSCPacket p)Sends an OSC packet (bundle or message) to the default network address, using the current codec.abstract voidsend(OSCPacketCodec c, OSCPacket p)Sends an OSC packet (bundle or message) to the default network address, using a particular codec.abstract voidsend(OSCPacketCodec c, OSCPacket p, SocketAddress target)Sends an OSC packet (bundle or message) to the given network address, using a particular codec.voidsend(OSCPacket p, SocketAddress target)Sends an OSC packet (bundle or message) to the given network address, using the current codec.voidsetBufferSize(int size)Adjusts the buffer size for OSC messages.voidsetCodec(OSCPacketCodec c)Specifies which codec is used in packet coding and decoding.voidsetTarget(SocketAddress target)Specifies the transmitter's target address, that is the address of the remote side to talk to.
-
-
-
Field Detail
-
sync
protected final Object sync
-
allocBuf
protected boolean allocBuf
-
byteBuf
protected ByteBuffer byteBuf
-
dumpMode
protected int dumpMode
-
printStream
protected PrintStream printStream
-
target
protected SocketAddress target
-
localAddress
protected final InetSocketAddress localAddress
-
revivable
protected final boolean revivable
-
-
Constructor Detail
-
OSCTransmitter
protected OSCTransmitter(OSCPacketCodec c, String protocol, InetSocketAddress localAddress, boolean revivable)
-
-
Method Detail
-
newUsing
public static OSCTransmitter newUsing(String protocol) throws IOException
Creates a new instance of anOSCTransmitter, 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 transmitter
- 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 OSCTransmitter newUsing(OSCPacketCodec c, String protocol) throws IOException
Creates a new instance of anOSCTransmitter, 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 transmitter
- 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 OSCTransmitter newUsing(String protocol, int port) throws IOException
Creates a new instance of anOSCTransmitter, using default codec and a specific transport protocol and port. It uses the local machine's IP. Note that theportspecifies the local socket, not the remote (or target) port. This 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 transmitter
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used
-
newUsing
public static OSCTransmitter newUsing(OSCPacketCodec c, String protocol, int port) throws IOException
Creates a new instance of anOSCTransmitter, using a specific codec and transport protocol and port. It uses the local machine's IP. Note that theportspecifies the local socket, not the remote (or target) port. This 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 transmitter
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used- Since:
- NetUtil 0.33
-
newUsing
public static OSCTransmitter newUsing(String protocol, int port, boolean loopBack) throws IOException
Creates a new instance of anOSCTransmitter, using default codec and a specific transport protocol and port. It uses the local machine's IP or the "loopback" address. Note that theportspecifies the local socket, not the remote (or target) port. This 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 any local host is picked- Returns:
- the newly created transmitter
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used
-
newUsing
public static OSCTransmitter newUsing(OSCPacketCodec c, String protocol, int port, boolean loopBack) throws IOException
Creates a new instance of anOSCTransmitter, using a specific codec and transport protocol and port. It uses the local machine's IP or the "loopback" address. Note that theportspecifies the local socket, not the remote (or target) port. This 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 any local host is picked- Returns:
- the newly created transmitter
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used- Since:
- NetUtil 0.33
-
newUsing
public static OSCTransmitter newUsing(String protocol, InetSocketAddress localAddress) throws IOException
Creates a new instance of anOSCTransmitter, using default codec and a specific transport protocol and local socket address. Note thatlocalAddressspecifies the local socket, not the remote (or target) socket. This can be set using thesetTargetmethod!- Parameters:
protocol- the protocol to use, currently eitherUDPorTCPlocalAddress- a valid address to use for the OSC socket. If the port is0, an arbitrary free port is picked when the transmitter is connected. (you can find out the actual port in this case by callinggetLocalAddress()after the transmitter was connected).- Returns:
- the newly created transmitter
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used
-
newUsing
public static OSCTransmitter newUsing(OSCPacketCodec c, String protocol, InetSocketAddress localAddress) throws IOException
Creates a new instance of anOSCTransmitter, using a specific codec and transport protocol and local socket address. Note thatlocalAddressspecifies the local socket, not the remote (or target) socket. This can be set using thesetTargetmethod!- Parameters:
c- the codec to useprotocol- the protocol to use, currently eitherUDPorTCPlocalAddress- a valid address to use for the OSC socket. If the port is0, an arbitrary free port is picked when the transmitter is connected. (you can find out the actual port in this case by callinggetLocalAddress()after the transmitter was connected).- Returns:
- the newly created transmitter
- Throws:
IOException- if a networking error occurs while creating the socketIllegalArgumentException- if an illegal protocol is used- Since:
- NetUtil 0.33
-
newUsing
public static OSCTransmitter newUsing(DatagramChannel dch) throws IOException
Creates a new instance of anOSCTransmitter, 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 (usingdch.socket().bind( SocketAddress )). Note thatdchspecifies the local socket, not the remote (or target) socket. This can be set using thesetTargetmethod!- Parameters:
dch- theDatagramChannelto use as UDP socket.- Returns:
- the newly created transmitter
- Throws:
IOException- if a networking error occurs while configuring the socket
-
newUsing
public static OSCTransmitter newUsing(OSCPacketCodec c, DatagramChannel dch) throws IOException
Creates a new instance of anOSCTransmitter, 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 (usingdch.socket().bind( SocketAddress )). Note thatdchspecifies the local socket, not the remote (or target) socket. This can be set using thesetTargetmethod!- Parameters:
c- the codec to usedch- theDatagramChannelto use as UDP socket.- Returns:
- the newly created transmitter
- Throws:
IOException- if a networking error occurs while configuring the socket- Since:
- NetUtil 0.33
-
newUsing
public static OSCTransmitter newUsing(SocketChannel sch) throws IOException
Creates a new instance of anOSCTransmitter, 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 (usingsch.socket().bind( SocketAddress )). Furthermore, the channel must be connected (usingconnect()) before being able to transmit messages. Note thatschspecifies the local socket, not the remote (or target) socket. This can be set using thesetTargetmethod!- Parameters:
sch- theSocketChannelto use as TCP socket.- Returns:
- the newly created transmitter
- Throws:
IOException- if a networking error occurs while configuring the socket
-
newUsing
public static OSCTransmitter newUsing(OSCPacketCodec c, SocketChannel sch) throws IOException
Creates a new instance of anOSCTransmitter, 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 (usingsch.socket().bind( SocketAddress )). Furthermore, the channel must be connected (usingconnect()) before being able to transmit messages. Note thatschspecifies the local socket, not the remote (or target) socket. This can be set using thesetTargetmethod!- Parameters:
c- the codec to usesch- theSocketChannelto use as TCP socket.- Returns:
- the newly created transmitter
- Throws:
IOException- if a networking error occurs while configuring the socket- Since:
- NetUtil 0.33
-
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 abstract InetSocketAddress getLocalAddress() throws IOException
Queries the transmitter's local socket address. You can determine the host and port from the returned address by callinggetHostName()(or for the IPgetAddress().getHostAddress()) andgetPort(). This port number may be0if the transmitter was called with an unspecified port and has not yet been connected. In this case, to determine the port actually used, call this method after the transmitter has been connected.- Specified by:
getLocalAddressin interfaceOSCChannel- Returns:
- the address of the transmitter's local socket.
- Throws:
IOException- if the local host could not be resolved- See Also:
InetSocketAddress.getHostName(),InetSocketAddress.getAddress(),InetSocketAddress.getPort()
-
setTarget
public void setTarget(SocketAddress target)
Specifies the transmitter's target address, that is the address of the remote side to talk to. You should call this method only once and you must call it before starting to send messages using the shortcut callsend( OSCPacket ).- Parameters:
target- the address of the remote target socket. 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 abstract void connect() throws IOExceptionEstablishes connection for transports requiring connectivity (e.g. TCP). For transports that do not require connectivity (e.g. UDP), this ensures the communication channel is created and bound.When a UDP transmitter is created without an explicit
DatagramChannel– say by callingOSCTransmitter.newUsing( "udp" ), you are required to callconnect()so that an actualDatagramChannelis created and bound. For a UDP transmitter which was created with an explicitDatagramChannel, this method does noting, so it is always safe to callconnect(). However, for TCP transmitters, this may throw anIOExceptionif the transmitter was already connected, therefore be sure to checkisConnected()before.- 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 anOSCReceiversharing the same socket was stopped before (unable to revive).- See Also:
isConnected()
-
getLocalAddress
protected InetSocketAddress getLocalAddress(InetAddress addr, int port) throws UnknownHostException
- Throws:
UnknownHostException
-
isConnected
public abstract boolean isConnected()
Queries the connection state of the transmitter.- Returns:
trueif the transmitter 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 final void send(OSCPacket p, SocketAddress target) throws IOException
Sends an OSC packet (bundle or message) to the given network address, using the current codec.- Parameters:
p- the packet to sendtarget- the target address to send the packet to- Throws:
IOException- if a write error, OSC encoding error, buffer overflow error or network error occurs
-
send
public abstract void send(OSCPacketCodec c, OSCPacket p, SocketAddress target) throws IOException
Sends an OSC packet (bundle or message) to the given network address, using a particular codec.- Parameters:
c- the codec to usep- the packet to sendtarget- the target address to send the packet to- Throws:
IOException- if a write error, OSC encoding error, buffer overflow error or network error occurs- Since:
- NetUtil 0.33
-
send
public final void send(OSCPacket p) throws IOException
Sends an OSC packet (bundle or message) to the default network address, using the current codec. The default address is the one specified using thesetTargetmethod. Therefore this will throw aNullPointerExceptionif no default address was specified.- Parameters:
p- the packet to send- Throws:
IOException- if a write error, OSC encoding error, buffer overflow error or network error occursNullPointerException- if no default address was specified- See Also:
setTarget( SocketAddress )
-
send
public abstract void send(OSCPacketCodec c, OSCPacket p) throws IOException
Sends an OSC packet (bundle or message) to the default network address, using a particular codec. The default address is the one specified using thesetTargetmethod. Therefore this will throw aNullPointerExceptionif no default address was specified.- Parameters:
c- the codec to usep- the packet to send- Throws:
IOException- if a write error, OSC encoding error, buffer overflow error or network error occursNullPointerException- if no default address was specified- Since:
- NetUtil 0.33
- See Also:
setTarget( SocketAddress )
-
setBufferSize
public void setBufferSize(int size)
Description copied from interface:OSCChannelAdjusts the buffer size for OSC messages. This is the maximum size an OSC packet (bundle or message) can grow to.- Specified by:
setBufferSizein interfaceOSCChannel- Parameters:
size- the new size in bytes.- See Also:
OSCChannel.getBufferSize()
-
getBufferSize
public int getBufferSize()
Description copied from interface:OSCChannelQueries the buffer size used for coding or decoding OSC messages. This is the maximum size an OSC packet (bundle or message) can grow to.- Specified by:
getBufferSizein interfaceOSCChannel- Returns:
- the buffer size in bytes.
- See Also:
OSCChannel.setBufferSize( int )
-
dumpOSC
public void dumpOSC(int mode, PrintStream stream)Description copied from interface:OSCChannelChanges the way processed 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:
OSCChannel.kDumpOff,OSCChannel.kDumpText,OSCChannel.kDumpHex,OSCChannel.kDumpBoth
-
dispose
public void dispose()
Description copied from interface:OSCChannelDisposes the resources associated with the OSC communicator. The object should not be used any more after calling this method.- Specified by:
disposein interfaceOSCChannel
-
checkBuffer
protected void checkBuffer()
-
getChannel
protected abstract SelectableChannel getChannel()
-
-