jarol.messages
Class TCPServerLink

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

public class TCPServerLink
extends Link

TCP server socket message link.

This message link allows simple bi-directional communication using a server TCP socket.

Note that the TCP server link may have multiple peers. Messages from all peers will be placed on the link's receive port and any messages in the link's send port will be sent to all connected peers at any time.

Version:
0.1
Author:
Eduardo Marques

Field Summary
 
Fields inherited from class jarol.messages.Link
DEFAULT_PORT_BUFSZ, MAX_MESSAGE_PAYLOAD
 
Constructor Summary
TCPServerLink(MessageFactory factory, int port)
          Constructor.
 
Method Summary
protected  int recv(byte[] buf, int off, int len, int timeout)
          Receive data over the TCP server link.
protected  void send(byte[] data, int off, int len)
          Send data over the TCP server link.
protected  void start()
          Start the TCP server link.
protected  void stop()
          Stop the TCP server 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

TCPServerLink

public TCPServerLink(MessageFactory factory,
                     int port)
Constructor. Note that this only initializes the TCP link, but does not connect it. This is done through the start() method in the parent MessageLink class.

Parameters:
factory - the message factory to use
port - server port to use
Method Detail

start

protected void start()
              throws java.io.IOException
Start the TCP server link.
This will open and setup the link's TCP server socket.

Specified by:
start in class Link
Throws:
java.io.IOException - if a network error occurs

stop

protected void stop()
             throws java.io.IOException
Stop the TCP server link.
This will disconnect the link's TCP server socket.

Specified by:
stop in class Link
Throws:
java.io.IOException - if a network error occurs

recv

protected int recv(byte[] buf,
                   int off,
                   int len,
                   int timeout)
            throws java.io.IOException
Receive data over the TCP server link.
Note that multiple messages (from potential multiple clients) will be placed onto the output buffer.

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 read data 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 over the TCP server link.
Note that all active client peers get the data sent.

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