ctf.network.tcp
Class Connection

java.lang.Object
  extended by java.lang.Thread
      extended by ctf.network.tcp.Connection
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
TestConnection

public class Connection
extends java.lang.Thread

A line-oriented TCP connection. This is a TCP connection capable of sending and receiving '\n'-terminated lines. Transmission is simple (@see send() ). Reception can be done either through polling (@see getLine() ) or a dedicated Rx thread (@see start() ).


Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
 
Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Connection()
          Empty constructor
Connection(java.net.Socket socket)
          Construct a new (already-opened) connection
Connection(java.net.Socket socket, int timeout)
          Construct a new (already-opened) connection
Connection(java.lang.String host, int port)
          Construct a new connection to a server
Connection(java.lang.String host, int port, int timeout, int retries)
          Construct a new connection to a server
 
Method Summary
 void close()
          Close the connection
 java.lang.String connectedTo()
           
 void finalize()
           
 java.lang.String getLine()
          Receive a line (string terminated by newline) from the socket with the newline character removed.
 void run()
          Continuously poll for '\n'-terminated lines and forward them to the connection's owner.
 void send(java.lang.String string)
          Send a string of characters across the socket
 void sendLine(java.lang.String s)
          Send a newline-terminated line across the socket
 void setOwner(ConnectionOwner owner)
          Who "owns" this connection (receives events)?
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, yield
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Connection

public Connection()
Empty constructor


Connection

public Connection(java.lang.String host,
                  int port)
           throws java.io.IOException,
                  UninitializedSocketException,
                  java.net.UnknownHostException
Construct a new connection to a server

Parameters:
host - The IP host to connect to
port - The TCP port to connect to
Throws:
java.io.IOException - for general connection problems
UninitializedSocketException - if socket doesn't initialize
java.net.UnknownHostException - if the host is unknown

Connection

public Connection(java.lang.String host,
                  int port,
                  int timeout,
                  int retries)
           throws java.io.IOException,
                  UninitializedSocketException,
                  java.net.UnknownHostException
Construct a new connection to a server

Parameters:
host - The IP host to connect to
port - The TCP port to connect to
timeout - Milliseconds to wait for timeout (0 means 'infinite')
retries - How many times to retry for a connection
Throws:
java.io.IOException - for general connection problems
UninitializedSocketException - if socket doesn't initialize
java.net.UnknownHostException - if the host is unknown

Connection

public Connection(java.net.Socket socket)
           throws java.io.IOException,
                  UninitializedSocketException
Construct a new (already-opened) connection

Parameters:
socket - The socket to connect with
Throws:
java.io.IOException - on error getting an I/O stream from Socket
UninitializedSocketException - if connection not established

Connection

public Connection(java.net.Socket socket,
                  int timeout)
           throws java.io.IOException,
                  UninitializedSocketException
Construct a new (already-opened) connection

Parameters:
socket - The socket to connect with
timeout - Milliseconds to wait for timeout (0 means "infinite")
Throws:
java.io.IOException - on error getting an I/O stream from Socket
UninitializedSocketException - if connection not established
Method Detail

finalize

public void finalize()
Overrides:
finalize in class java.lang.Object

close

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

Throws:
java.io.IOException

sendLine

public void sendLine(java.lang.String s)
              throws java.io.IOException,
                     SocketClosedException,
                     UninitializedSocketException
Send a newline-terminated line across the socket

Throws:
java.io.IOException
SocketClosedException
UninitializedSocketException

send

public void send(java.lang.String string)
          throws java.io.IOException,
                 SocketClosedException,
                 UninitializedSocketException
Send a string of characters across the socket

Parameters:
string - The string to send
Throws:
java.io.IOException
SocketClosedException
UninitializedSocketException

getLine

public java.lang.String getLine()
                         throws java.io.IOException,
                                NetworkException,
                                SocketClosedException,
                                UninitializedSocketException
Receive a line (string terminated by newline) from the socket with the newline character removed.

Throws:
java.io.IOException
NetworkException
SocketClosedException
UninitializedSocketException

setOwner

public void setOwner(ConnectionOwner owner)
Who "owns" this connection (receives events)?


run

public void run()
Continuously poll for '\n'-terminated lines and forward them to the connection's owner.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Thread

connectedTo

public java.lang.String connectedTo()