net.jackofalltrades.irc
Class BaseConnection

java.lang.Object
  extended bynet.jackofalltrades.irc.BaseConnection
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
BaseDccConnection, IrcConnection

public abstract class BaseConnection
extends java.lang.Object
implements java.lang.Runnable

Provides functionality for all connection types (ie. IRC, binary DCC, and ascii DCC).

Since:
0.5-alpha
Version:
$Revision: 1.7 $ $Date: 2003/08/03 21:01:37 $
Author:
Bradley M. Handy

Field Summary
static int DEFAULT_BUFFER_SIZE
          default size of the send and receive buffers (1Kb).
static int DEFAULT_CONNECTION_TIMEOUT
          default amount of time for a connection attempt to fail (30 seconds).
static int DEFAULT_READ_WRITE_TIMEOUT
          default amount of time for an I/O operation to fail (30 seconds).
static int MAX_PORT
          maximum allowable port number.
static int MIN_PORT
          minimum allowable port number on the remote host.
 
Constructor Summary
BaseConnection()
          Creates a new BaseConnection.
BaseConnection(java.net.InetAddress remoteHost, int port)
          Creates a new BaseConnection to connect to the specified remote host.
BaseConnection(java.net.Socket socket)
          Creates a new BaseConnection connected on the specified connected java.net.Socket.
BaseConnection(java.lang.String remoteHost, int port)
          Creates a new BaseConnection to connect to the specified remote host.
 
Method Summary
 void addConnectionListener(ConnectionListener l)
          Registers a ConnectionListener instance to listen for connected and disconnected events.
protected  void addEventListener(java.lang.Class clazz, java.util.EventListener l)
          Adds a java.util.EventListener to the registered list of listeners.
 boolean connect()
          Connects to the configured remote host on the remote port, and starts the reader thread.
 void disconnect()
          Disconnects from the remote host, and as a result the reader thread will stop.
 int getBufferSize()
          Returns the send/receive buffer size.
 int getConnectionTimeout()
          Returns the time limit for a connection attempt.
protected  java.util.EventListener[] getEventListeners(java.lang.Class clazz)
          Returns an array of java.util.EventListener objects.
 java.lang.Exception getLastException()
          Returns the last exception thrown.
 int getPortNumber()
          Returns the port number.
 int getReadWriteTimeout()
          Return the timeout period for an I/O operation.
 java.net.InetAddress getRemoteHost()
          Returns the java.net.InetAddress representing the IP address of the IRC server.
 boolean isConnected()
          Returns the connected status of the underlying java.net.Socket.
protected abstract  void processInput()
          Reads from the java.io.InputStream, or java.io.Reader provided by the subclass.
 void removeConnectionListener(ConnectionListener l)
          Removes a ConnectionListener instance from the collection of registered listeners.
protected  void removeEventListener(java.lang.Class clazz, java.util.EventListener l)
          Remove a java.util.EventListener from the registered list of listeners.
 void run()
          Continuously reads from the underlying java.net.Socket until disconnection.
 void setBufferSize(int bufferSize)
          Sets the send/receive buffer size.
 void setConnectionTimeout(int timeout)
          Sets the time limit for a connection attempt.
protected  void setLastException(java.lang.Exception e)
          Sets the last exception thrown.
 void setPortNumber(int portNumber)
          Sets the port number.
 void setReadWriteTimeout(int timeout)
          Sets the new I/O timeout period.
 void setRemoteHost(java.net.InetAddress remoteHost)
          Sets the java.net.InetAddress of the remote host, to which, a connection will be established.
protected abstract  void wrapInput(java.io.InputStream input)
          Provides a hook for subclasses to implement either binary or ascii/unicode connections.
protected abstract  void wrapOutput(java.io.OutputStream output)
          Provides a hook for subclasses to implement either binary or ascii/unicode connections.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MIN_PORT

public static final int MIN_PORT
minimum allowable port number on the remote host.

See Also:
Constant Field Values

MAX_PORT

public static final int MAX_PORT
maximum allowable port number.

See Also:
Constant Field Values

DEFAULT_CONNECTION_TIMEOUT

public static final int DEFAULT_CONNECTION_TIMEOUT
default amount of time for a connection attempt to fail (30 seconds).

See Also:
Constant Field Values

DEFAULT_READ_WRITE_TIMEOUT

public static final int DEFAULT_READ_WRITE_TIMEOUT
default amount of time for an I/O operation to fail (30 seconds).

See Also:
Constant Field Values

DEFAULT_BUFFER_SIZE

public static final int DEFAULT_BUFFER_SIZE
default size of the send and receive buffers (1Kb).

See Also:
Constant Field Values
Constructor Detail

BaseConnection

public BaseConnection()
Creates a new BaseConnection.


BaseConnection

public BaseConnection(java.net.InetAddress remoteHost,
                      int port)
Creates a new BaseConnection to connect to the specified remote host.

Parameters:
remoteHost - the java.net.InetAddress of the remote host.
port - the port number on the remote host.
Throws:
java.lang.IllegalArgumentException - if remoteHost is null or if port is not in the range 1 - 65535.

BaseConnection

public BaseConnection(java.lang.String remoteHost,
                      int port)
               throws java.net.UnknownHostException
Creates a new BaseConnection to connect to the specified remote host.

Parameters:
remoteHost - the name of the remote host.
port - the port number on the server.
Throws:
java.net.UnknownHostException - if the remoteHost cannot be resolved.
java.lang.IllegalArgumentException - if the remoteHost null or blank.

BaseConnection

public BaseConnection(java.net.Socket socket)
Creates a new BaseConnection connected on the specified connected java.net.Socket.

Retrieves the java.net.InetAddress and port number of the remote host, Wraps the input and output, and starts the reader thread. No buffer sizes are set and neither are the timeouts. These can be set using the helper methods.

Parameters:
socket - the Socket connected to the remote host.
Throws:
java.lang.IllegalArgumentException - if socket is null.
Method Detail

getRemoteHost

public java.net.InetAddress getRemoteHost()
Returns the java.net.InetAddress representing the IP address of the IRC server.


setRemoteHost

public void setRemoteHost(java.net.InetAddress remoteHost)
Sets the java.net.InetAddress of the remote host, to which, a connection will be established.

Parameters:
remoteHost - the new remote host address.
Throws:
java.lang.IllegalArgumentException - if remoteHost is null.
java.lang.IllegalStateException - if this connection is still connected.

getPortNumber

public int getPortNumber()
Returns the port number.


setPortNumber

public void setPortNumber(int portNumber)
Sets the port number.

Parameters:
portNumber - the new port number.
Throws:
java.lang.IllegalArgumentException - if portNumber is less than 1 or greater than 65535.
java.lang.IllegalStateException - if this connection is still connected.

getConnectionTimeout

public int getConnectionTimeout()
Returns the time limit for a connection attempt.


setConnectionTimeout

public void setConnectionTimeout(int timeout)
Sets the time limit for a connection attempt.

Parameters:
timeout - the maximum time to wait for an accepted connection in milliseconds.
Throws:
java.lang.IllegalArgumentException - if timeout is less than zero.

getReadWriteTimeout

public int getReadWriteTimeout()
Return the timeout period for an I/O operation.


setReadWriteTimeout

public void setReadWriteTimeout(int timeout)
Sets the new I/O timeout period.

Parameters:
timeout - the new timeout period.
Throws:
java.lang.IllegalArgumentException - if timeout is less than zero.

getBufferSize

public int getBufferSize()
Returns the send/receive buffer size.


setBufferSize

public void setBufferSize(int bufferSize)
Sets the send/receive buffer size. This value is only a hint to the underlying java.net.Socket connection. NOTE: According to the javadoc for java.net.Socket buffer sizes of 64Kb and higher MUST be set BEFORE connecting.

Parameters:
bufferSize - the new buffer size hint.
Throws:
java.net.IllegalStateException - if the connection is connected and a buffer size of 64Kb, or higher, is set.

getLastException

public java.lang.Exception getLastException()
Returns the last exception thrown.


setLastException

protected void setLastException(java.lang.Exception e)
Sets the last exception thrown.

Parameters:
e - the last exception thrown.

isConnected

public boolean isConnected()
Returns the connected status of the underlying java.net.Socket.


addEventListener

protected void addEventListener(java.lang.Class clazz,
                                java.util.EventListener l)
Adds a java.util.EventListener to the registered list of listeners.

Parameters:
clazz - the java.lang.Class of the of listener interface type NOT the actual class type of the listener.
l - the listener object to add.

removeEventListener

protected void removeEventListener(java.lang.Class clazz,
                                   java.util.EventListener l)
Remove a java.util.EventListener from the registered list of listeners.

Parameters:
clazz - the java.lang.Class of the of listener interface type NOT the actual class type of the listener.
l - the listener object to add.

getEventListeners

protected java.util.EventListener[] getEventListeners(java.lang.Class clazz)
Returns an array of java.util.EventListener objects.

Parameters:
clazz - the java.lang.Class of the interface type implemented by the objects being returned.

connect

public final boolean connect()
Connects to the configured remote host on the remote port, and starts the reader thread. If an error occurs, use getLastException() to get the cause of the connection failure.

Returns:
true if the connection is successful, false otherwise.

disconnect

public void disconnect()
Disconnects from the remote host, and as a result the reader thread will stop.


run

public void run()
Continuously reads from the underlying java.net.Socket until disconnection.

Specified by:
run in interface java.lang.Runnable

wrapInput

protected abstract void wrapInput(java.io.InputStream input)
                           throws java.io.IOException
Provides a hook for subclasses to implement either binary or ascii/unicode connections.

Parameters:
input - the java.io.InputStream from the underlying java.net.Socket.
Throws:
java.io.IOException - if an I/O error occurs.

wrapOutput

protected abstract void wrapOutput(java.io.OutputStream output)
                            throws java.io.IOException
Provides a hook for subclasses to implement either binary or ascii/unicode connections.

Parameters:
output - the java.io.OutputStream from the underlying java.net.Socket.
Throws:
java.io.IOException - if an I/O error occurs.

processInput

protected abstract void processInput()
                              throws java.io.IOException
Reads from the java.io.InputStream, or java.io.Reader provided by the subclass.

Throws:
java.io.IOException - if an I/O error occurs, or if the underlying java.net.Socket times out.

addConnectionListener

public void addConnectionListener(ConnectionListener l)
Registers a ConnectionListener instance to listen for connected and disconnected events.

Parameters:
l - the listener to register.

removeConnectionListener

public void removeConnectionListener(ConnectionListener l)
Removes a ConnectionListener instance from the collection of registered listeners.

Parameters:
l - the listener to remove.


Copyright © 2002-2003 Jack-of-all-trades Programming Services. All Rights Reserved.