net.clackrouter.application
Class TCPSocket

java.lang.Object
  extended by net.clackrouter.component.base.ClackComponent
      extended by net.clackrouter.application.TCPSocket
All Implemented Interfaces:
Alarm

public class TCPSocket
extends ClackComponent

Implementation of a TCP Socket for use by Clack applications.

Note: public methods exist both from application side interaction and for use of the TCPSocket ClackComponent within a ClackGraph.


Nested Class Summary
 
Nested classes/interfaces inherited from class net.clackrouter.component.base.ClackComponent
ClackComponent.Waiter
 
Field Summary
static int NUM_PORTS
           
static int PORT_NET_IN
          Port to receive incoming packets from the TCP/IP stack components
static int PORT_NET_OUT
          Port to send outgoing packets to the TCP/IP stack components
 
Fields inherited from class net.clackrouter.component.base.ClackComponent
componentCell, m_has_error, m_log, m_name, m_num_ports, m_packetcount_in, m_packetcount_out, m_ports, mListeners, mRouter, pendingError, SIGNAL_ERROR_LEN_MSEC, UNIQUE_COUNT, view
 
Constructor Summary
TCPSocket(TCB t, Router router, java.lang.String name)
           
 
Method Summary
 TCPSocket accept()
          Returns a client socket once an incoming client connection has been received.
 void bind()
          Default bind behavior selects a random port in the range between 1024 and 2024 and binds to the first listed interface on the router (often eth0)
 void bind(java.net.InetAddress local_address, int local_port)
          Binds the socket to a specified address and port.
 void close()
          Closes this end of the socket connection, meaning no more data can be sent.
 void connect(java.net.InetAddress remote_address, int remote_port)
          Create a client connection to a remote host.
 ClackApplication getCurrentApplication()
          the application using this TCPSocket
 TCB getTCB()
          The transmission control block (TCB) associated with this socket
 void listen()
          Puts the socket into the LISTENING state, ready to receive incoming connections as a server.
 void poll()
          Callback to be called once per router-processing loop.
 java.nio.ByteBuffer recv(int max_size, long msec_timeout)
          Read in data from this socket.
 int send(java.nio.ByteBuffer buf_out)
          Sends a buffer of data
 void setCurrentApplication(ClackApplication app)
          Set the Clack application that is the parent of this application (used internally)
protected  void setupPorts(int numports)
          Allocates an array of ClackPort objects to be used by this component
 
Methods inherited from class net.clackrouter.component.base.ClackComponent
acceptPacket, createCopy, createInputPullPort, createInputPushPort, createOutputPullPort, createOutputPushPort, error, fireListeners, getColor, getComponentCell, getHierarchicalView, getLog, getName, getNumPorts, getPacketCountIn, getPacketCountOut, getPendingError, getPort, getPropertiesView, getRouter, getSerializableProperties, getTime, getTypeName, getUniqueCount, getView, handlePullRequest, hasError, initializeProperties, isHierarchical, isModifying, log, notifyAlarm, registerListener, sendOutPort, setAlarm, setComponentCell, setName, setPendingError, setView, showErrorDialog, signalError, try_repaint, unregisterListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PORT_NET_OUT

public static final int PORT_NET_OUT
Port to send outgoing packets to the TCP/IP stack components

See Also:
Constant Field Values

PORT_NET_IN

public static final int PORT_NET_IN
Port to receive incoming packets from the TCP/IP stack components

See Also:
Constant Field Values

NUM_PORTS

public static final int NUM_PORTS
See Also:
Constant Field Values
Constructor Detail

TCPSocket

public TCPSocket(TCB t,
                 Router router,
                 java.lang.String name)
Method Detail

setupPorts

protected void setupPorts(int numports)
Description copied from class: ClackComponent
Allocates an array of ClackPort objects to be used by this component

Overrides:
setupPorts in class ClackComponent
Parameters:
numports - the number of ports to create

poll

public void poll()
Callback to be called once per router-processing loop.

Checks if the application has closed this socket, and if all data has been sent notifies the socket to send a FIN

Overrides:
poll in class ClackComponent

bind

public void bind()
          throws java.lang.Exception
Default bind behavior selects a random port in the range between 1024 and 2024 and binds to the first listed interface on the router (often eth0)

Throws:
java.lang.Exception

bind

public void bind(java.net.InetAddress local_address,
                 int local_port)
          throws java.lang.Exception
Binds the socket to a specified address and port.

At this time, no restrictions are placed on what addresses or ports may be bound to.

Parameters:
local_address - address to bind as source
local_port - port to bind as source
Throws:
java.lang.Exception

listen

public void listen()
            throws java.lang.Exception
Puts the socket into the LISTENING state, ready to receive incoming connections as a server.

Throws:
java.lang.Exception

connect

public void connect(java.net.InetAddress remote_address,
                    int remote_port)
             throws java.lang.Exception
Create a client connection to a remote host. Automatically binds the socket if it is not already bound.

Parameters:
remote_address - remote destination address
remote_port - remote destination port
Throws:
java.lang.Exception

recv

public java.nio.ByteBuffer recv(int max_size,
                                long msec_timeout)
Read in data from this socket.

Parameters:
max_size - largest amount of data to be read
msec_timeout - millisecs to wait before returning an empty buffer
Returns:
buffer of data, empty buffer if no data was available, or null if the connection has been closed remotely

accept

public TCPSocket accept()
Returns a client socket once an incoming client connection has been received.

This call loops until a client connection is recieved, which it returns. This is effectively a blocking socket.

Returns:
client socket created

send

public int send(java.nio.ByteBuffer buf_out)
         throws java.lang.Exception
Sends a buffer of data

Throws:
java.lang.Exception

close

public void close()
Closes this end of the socket connection, meaning no more data can be sent.


setCurrentApplication

public void setCurrentApplication(ClackApplication app)
Set the Clack application that is the parent of this application (used internally)


getCurrentApplication

public ClackApplication getCurrentApplication()
the application using this TCPSocket


getTCB

public TCB getTCB()
The transmission control block (TCB) associated with this socket