jarol.messages
Class TCPClientLink

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

public class TCPClientLink
extends Link

TCP client socket message link.

This message link allows simple bi-directional communication using a client TCP socket connected to a specified remote TCP server socket.

Version:
0.1
Author:
Eduardo Marques

Field Summary
 
Fields inherited from class jarol.messages.Link
DEFAULT_PORT_BUFSZ, MAX_MESSAGE_PAYLOAD
 
Constructor Summary
TCPClientLink(MessageFactory factory, java.lang.String host, int port)
          Constructor.
 
Method Summary
protected  int recv(byte[] buf, int off, int len, int timeout)
          Receive a message packet.
protected  void send(byte[] data, int off, int len)
          Send a message packet.
protected  void start()
          Start TCP client link.
protected  void stop()
          Stop TCP client 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

TCPClientLink

public TCPClientLink(MessageFactory factory,
                     java.lang.String host,
                     int port)
              throws java.net.UnknownHostException
Constructor. Note that this only initializes the TCP 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
port - port number at remote host
Throws:
java.net.UnknownHostException - as described in InetAddress.getByName()
Method Detail

start

protected void start()
              throws java.io.IOException
Start TCP client link.

Specified by:
start in class Link
Throws:
java.io.IOException - if a port-specific error occurs (eg network related)

stop

protected void stop()
             throws java.io.IOException
Stop TCP client link.

Specified by:
stop in class Link
Throws:
java.io.IOException - if a port-specific error occurs (eg network related)

recv

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

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 a message packet.

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