j_waste.network
Class Connection

java.lang.Object
  extended byjava.lang.Thread
      extended byj_waste.network.Connection
All Implemented Interfaces:
NetworkConstants, java.lang.Runnable

public class Connection
extends java.lang.Thread
implements NetworkConstants

This is the Connection class for creating and maintaing TCP connections.

Version:
1.0
Author:
Jan Lindblom (cl0wn@users.sourceforge.net), Fredrik Hultin (frehul@users.sourceforge.net)

Field Summary
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Fields inherited from interface j_waste.network.NetworkConstants
ABORT, CAP_BANDWIDTH, CAP_REMOTE_IP, CAP_SATURATION, GENERAL_TYPE_BROADCAST, GENERAL_TYPE_LOCAL, GENERAL_TYPE_ROUTED, HEADER_SIZE, MAX_BROADCAST_SIZE, MAX_ROUTED_SIZE, NO_ABORT, PREMATURE_ABORT, PRIO_CHAT, PRIO_CHAT_REPLY, PRIO_FILE_REQ_REPLY, PRIO_FILE_REQUEST, PRIO_KEYDIST, PRIO_KEYDIST_REPLY, PRIO_LCAPS, PRIO_LOCAL_SATURATE, PRIO_PING, PRIO_SEARCH, PRIO_SEARCH_REPLY, PRIO_SEARCH_USERLIST, PRIO_UPLOAD, TYPE_CHAT, TYPE_CHAT_REPLY, TYPE_FILE_REQ_REPLY, TYPE_FILE_REQUEST, TYPE_KEYDIST, TYPE_KEYDIST_REPLY, TYPE_LCAPS, TYPE_LOCAL_SATURATE, TYPE_PING, TYPE_SEARCH, TYPE_SEARCH_REPLY, TYPE_SEARCH_USERLIST, TYPE_UPLOAD
 
Constructor Summary
Connection(java.net.InetAddress hostAddr, int hostPort, SynchronizedPrioQueue pq)
          Main constructor of the Connection class.
Connection(java.net.Socket s, SynchronizedPrioQueue pq)
          Alternative constructor of the Connection class.
 
Method Summary
 void close()
          Closes this Connection gracefully.
 java.io.InputStream getInput()
          Returns the InputStream of this Connection.
 java.io.OutputStream getOutput()
          Returns the OutputStream of this Connection.
 int getPort()
          Returns the remote Port number of this Connection.
 boolean isConnected()
          Returns the connected status of this Connection.
 boolean isRunning()
          Returns the running status of this Connection.
 void run()
          Main loop of this Connection.
 void send(HeadedPacket p)
          Sends a packet on this Connection.
 void sendDecrypted(Packet p)
          Sends a packet on this Connection that is not encrypted.
 void sendDirect(Packet p)
          Sends a packet directly, this method is not thread-safe.
 void setBlowfish(Blowfish bf)
          Sets the blowfish object for this Connection.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Connection

public Connection(java.net.InetAddress hostAddr,
                  int hostPort,
                  SynchronizedPrioQueue pq)
           throws java.io.IOException,
                  java.net.ConnectException
Main constructor of the Connection class. Constructs a Connection with a Socket bound to the specified host address and port.

Parameters:
hostAddr - the InetAddress of the host.
hostPort - the Port number on the host.
pq - the PriorityQueue to use.
Throws:
java.io.IOException - upon socket failure.
java.net.ConnectException - connection failure.

Connection

public Connection(java.net.Socket s,
                  SynchronizedPrioQueue pq)
           throws java.net.ConnectException,
                  java.io.IOException
Alternative constructor of the Connection class. Constructs a Connection using the given Socket Object.

Parameters:
s - the Socket for this Connection.
pq - the PriorityQueue to use.
Throws:
java.net.ConnectException - upon connection failure.
java.io.IOException - upon socket failure.
Method Detail

setBlowfish

public void setBlowfish(Blowfish bf)
Sets the blowfish object for this Connection. The Blowfish object should be initiated during authentication and will be used for encrypting all data after the authentication completes.


getInput

public java.io.InputStream getInput()
                             throws java.io.IOException
Returns the InputStream of this Connection.

Returns:
the InputStream of this Connection.
Throws:
java.io.IOException

getOutput

public java.io.OutputStream getOutput()
                               throws java.io.IOException
Returns the OutputStream of this Connection.

Returns:
the OutputStream of this Connection.
Throws:
java.io.IOException

getPort

public int getPort()
Returns the remote Port number of this Connection.

Returns:
the remote port number of this Connection.

isConnected

public boolean isConnected()
Returns the connected status of this Connection.

Returns:
true if connected, false otherwise.

send

public void send(HeadedPacket p)
          throws java.io.IOException
Sends a packet on this Connection. This method blocks if there are too many packets in the queue.

Parameters:
p - the Packet to send.
Throws:
java.io.IOException - on failure.

sendDirect

public void sendDirect(Packet p)
                throws java.io.IOException
Sends a packet directly, this method is not thread-safe.

Parameters:
p - the Packet to send.
Throws:
java.io.IOException - on error.

sendDecrypted

public void sendDecrypted(Packet p)
                   throws java.io.IOException
Sends a packet on this Connection that is not encrypted.

Parameters:
p - the Packet to send.
Throws:
java.io.IOException - on failure.

isRunning

public boolean isRunning()
Returns the running status of this Connection.

Returns:
true if the Connection is active, false otherwise.

close

public void close()
           throws java.io.IOException
Closes this Connection gracefully.

Throws:
java.io.IOException

run

public void run()
Main loop of this Connection.

Specified by:
run in interface java.lang.Runnable