jarol.messages
Class UDPLink

java.lang.Object
  extended by jarol.messages.Link
      extended by jarol.messages.UDPLink

public class UDPLink
extends Link

UDP socket message link.

This message link allows simple bi-directional communication between UDP ports. The local host will listen on a specified local port for messages and send messages to a specified UDP port on a remote host.

Version:
0.1
Author:
Eduardo Marques

Field Summary
 
Fields inherited from class jarol.messages.Link
DEFAULT_PORT_BUFSZ, MAX_MESSAGE_PAYLOAD
 
Constructor Summary
UDPLink(MessageFactory factory, java.lang.String host, int localPort, int remotePort)
          Constructor.
UDPLink(MessageFactory factory, java.lang.String host, int localPort, int remotePort, int recvBufSz, int sendBufSz)
          Constructor with specified receive / send buffer size.
 
Method Summary
protected  int recv(byte[] buf, int off, int len, int timeout)
          Receive data.
protected  void send(byte[] data, int off, int len)
          Send data to the remote UDP socket.
protected  void start()
          Start the UDP link.
protected  void stop()
          Stop the UDP link.
 
Methods inherited from class jarol.messages.Link
connect, connected, disconnect, getRecvPort, getSendPort, getSendSignal
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UDPLink

public UDPLink(MessageFactory factory,
               java.lang.String host,
               int localPort,
               int remotePort)
        throws java.net.UnknownHostException
Constructor. Note that this only initializes the UDP link, but does not start it. This is done through the connect() method in the parent MessageLink class.

Parameters:
factory - the message factory to use
host - host name or IP address
localPort - local UDP port
remotePort - remote UDP port
Throws:
java.net.UnknownHostException - as described in InetAddress.getByName()

UDPLink

public UDPLink(MessageFactory factory,
               java.lang.String host,
               int localPort,
               int remotePort,
               int recvBufSz,
               int sendBufSz)
        throws java.net.UnknownHostException
Constructor with specified receive / send buffer size. Note that this only initializes the UDP link, but does not start it. This is done through the connect() method in the parent MessageLink class.

Parameters:
factory - the message factory to use
host - host name or IP address
localPort - local UDP port
remotePort - remote UDP port
recvBufSz - receive buffer size
sendBufSz - send buffer size
Throws:
java.net.UnknownHostException - as described in InetAddress.getByName()
Method Detail

start

protected void start()
              throws java.io.IOException
Start the UDP link.

This will set up the UDP datagram socket listening onto the local UDP port.

Specified by:
start in class Link
Throws:
java.io.IOException - if an error occurs creating the socket.

stop

protected void stop()
             throws java.io.IOException
Stop the UDP link.

This will close the UDP datagram socket.

Specified by:
stop in class Link
Throws:
java.io.IOException - if an error occurs closing the socket.

recv

protected int recv(byte[] buf,
                   int off,
                   int len,
                   int timeout)
            throws java.io.IOException
Receive data.

Specified by:
recv in class Link
Parameters:
buf - read buffer
off - read buffer offset
len - read buffer usable length
timeout - timeout for receive
Returns:
the next datagram packet's payload length or 0 if timeout has been reached
Throws:
java.io.IOException - if a network error occurs

send

protected void send(byte[] data,
                    int off,
                    int len)
             throws java.io.IOException
Send data to the remote UDP socket.

Specified by:
send in class Link
Parameters:
data - data buffer
off - data buffer offset
len - data buffer usable length
Throws:
java.io.IOException - if a network error occurs