CTL.Comm
Class TCPCommunicator

java.lang.Object
  extended by CTL.Comm.Communicator
      extended by CTL.Comm.TCPCommunicator

public class TCPCommunicator
extends Communicator

TCP/IP communication handling


Field Summary
private  java.net.Socket dbgsock
          Socket connection to the remote debugger
private  IPaddr host
          Local IP
private  int port
          Local port
private  java.nio.channels.Selector sel
          Selector for this communicator
private  java.net.Socket sock
          Socket connection to the communication partner
private  java.net.ServerSocket ssock
           
 
Constructor Summary
TCPCommunicator()
          Default constructor
TCPCommunicator(int port)
          Constructor for known remote port
TCPCommunicator(java.lang.String host, int port, int lport)
          Constructor for known remote host and port
 
Method Summary
 void accept()
          Accept connections
private  void attachDebugger(PeerID dbgHost)
          Initialize connection to debugger
 void close()
          Close the connection
 void connect(PeerID pid)
          Connect to peer
static IPaddr determineIP(java.lang.String host, int port)
          Open a temporary TCP connection to determine the outgoing IP for communication with the target IP
 void listen()
          Listen for new connections
 PeerID pid()
          Retrieve the PeerID
 byte[] recv()
          Receive a CTL packet (length is determined from header)
 byte[] recv(long len)
          Receive a specific amount of data
 void send(byte[] data, long len, boolean debug)
          Send a specific amount of data
 void sendDebug(byte[] data)
          Send data to the debugger
 void setHost(IPaddr host)
          Change the contained IP address
 
Methods inherited from class CTL.Comm.Communicator
notifyErr, send, send, send
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

host

private IPaddr host
Local IP


port

private int port
Local port


sel

private java.nio.channels.Selector sel
Selector for this communicator


sock

private java.net.Socket sock
Socket connection to the communication partner


dbgsock

private java.net.Socket dbgsock
Socket connection to the remote debugger


ssock

private java.net.ServerSocket ssock
Constructor Detail

TCPCommunicator

public TCPCommunicator()
Default constructor


TCPCommunicator

public TCPCommunicator(java.lang.String host,
                       int port,
                       int lport)
Constructor for known remote host and port


TCPCommunicator

public TCPCommunicator(int port)
Constructor for known remote port

Method Detail

setHost

public void setHost(IPaddr host)
Change the contained IP address

Parameters:
host - Host

accept

public void accept()
            throws java.io.IOException
Accept connections

Specified by:
accept in class Communicator
Throws:
java.io.IOException

listen

public void listen()
            throws java.io.IOException
Listen for new connections

Specified by:
listen in class Communicator
Throws:
java.io.IOException

pid

public PeerID pid()
Retrieve the PeerID

Specified by:
pid in class Communicator
Returns:
PeerID

connect

public void connect(PeerID pid)
             throws java.net.UnknownHostException,
                    java.io.IOException
Connect to peer

Specified by:
connect in class Communicator
Parameters:
pid - PeerID of remote host
Throws:
java.net.UnknownHostException
java.io.IOException

attachDebugger

private void attachDebugger(PeerID dbgHost)
                     throws java.net.UnknownHostException,
                            java.io.IOException
Initialize connection to debugger

Parameters:
dbgHost - Location of the debugger
Throws:
java.net.UnknownHostException
java.io.IOException

close

public void close()
           throws java.io.IOException
Close the connection

Specified by:
close in class Communicator
Throws:
java.io.IOException

send

public void send(byte[] data,
                 long len,
                 boolean debug)
          throws java.io.IOException
Send a specific amount of data

Specified by:
send in class Communicator
Parameters:
data - Bytes to send
len - Number of bytes to send
debug - Whether or not to send the data to the debugger
Throws:
java.io.IOException

sendDebug

public void sendDebug(byte[] data)
               throws java.io.IOException
Send data to the debugger

Parameters:
data - Bytes to send
Throws:
java.io.IOException

recv

public byte[] recv(long len)
            throws java.io.IOException,
                   CTLException
Receive a specific amount of data

Specified by:
recv in class Communicator
Parameters:
len - Number of bytes to receive
Returns:
Data
Throws:
java.io.IOException
CTLException

recv

public byte[] recv()
            throws java.io.IOException,
                   CTLException
Receive a CTL packet (length is determined from header)

Specified by:
recv in class Communicator
Returns:
Data
Throws:
java.io.IOException
CTLException

determineIP

public static IPaddr determineIP(java.lang.String host,
                                 int port)
Open a temporary TCP connection to determine the outgoing IP for communication with the target IP

Returns:
Determined IP address