com.amx.duet.tools.net.tcp
Class SocketConnection

java.lang.Object
  extended byjava.lang.Thread
      extended bycom.amx.duet.tools.net.tcp.SocketConnection
All Implemented Interfaces:
java.lang.Runnable

public class SocketConnection
extends java.lang.Thread

This class is used to manage TCP/IP socket connections on a single thread making it safe for AMX masters.

Since:
AMXTools 1.0.1
Version:
2.0.6

Field Summary
static java.lang.String className
           
static int CLOSED
           
static int CONNECTED
           
static int FAILED_CONNECT
           
static int INFINITE
           
static int INVALID_HOST
           
static int TIMEOUT
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
SocketConnection(java.net.InetAddress addr, int port, int bufferSize, SocketConnectionListener listener)
          Creates a simple socket connection along with a receive thread for reading the socket.
SocketConnection(java.net.InetAddress addr, int port, int bufferSize, SocketConnectionListener listener, int timeout)
          Creates a simple socket along with a receive thread for reading the socket.
SocketConnection(java.net.InetAddress addr, int port, int bufferSize, SocketConnectionListener listener, int timeout, boolean TcpNoDelayOn)
          Creates a simple socket along with a receive thread for reading the socket.
SocketConnection(java.lang.String host, int port, int bufferSize, SocketConnectionListener listener)
          Creates a simple socket along with a receive thread for reading the socket.
SocketConnection(java.lang.String host, int port, int bufferSize, SocketConnectionListener listener, int timeout)
          Creates a simple socket along with a receive thread for reading the socket.
SocketConnection(java.lang.String host, int port, int bufferSize, SocketConnectionListener listener, int timeout, boolean TcpNoDelayOn)
          Creates a simple socket along with a receive thread for reading the socket.
 
Method Summary
 boolean connect()
          The first time connect is called for a SocketConnection, the receive thread is started and a socket object is created connected to the address and port.
 void disconnect()
          Closes the socket.
 void dispose()
          Destroys the socket connection and terminates the receive thread.
 void finalize()
           
 void flushQueue(boolean send)
          Flushes any message awaiting send by either sending them to the SocketConnection's output stream or discarding the message based on the send argument.
 int getDebugState()
          Gets the current diagnostic state.
 void initConnection(java.lang.String addrStr, int port, int bufferSize, SocketConnectionListener listener, int timeout)
           
 void initConnection(java.lang.String addrStr, int port, int bufferSize, SocketConnectionListener listener, int timeout, boolean TcpNoDelayOn)
           
 boolean isConnected()
           
 void log(int level, java.lang.String msg)
           
 void run()
           
 boolean setAddressAndPort(java.net.InetAddress addr, int port)
          Sets the socket connections IP address and port to the specified values.
 boolean setAddressAndPort(java.lang.String host, int port)
          Sets the socket connections IP address and port to the specified values.
 void setDebug(boolean bState)
          Deprecated. Do not use this method; use setDebugState instead
 void setDebugState(int nState)
          Puts the class into a debug diagnostic state.
 void setOKtoSendTrue()
          Triggers the SocketConnection's thread that it is OK to send the next queued message.
 void setTimeout(int timeout)
          Sets the socket connection's receive timeout in milliseconds.
 boolean write(byte[] data)
          Writes data to the output stream of the Socket connection.
 boolean writeQueued(byte[] data)
          Write data to the SocketConnection's output stream if output is OK, otherwise the output is queued awaiting send until OK-to-send is triggered via setOKtoSendTrue()
 
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
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CONNECTED

public static final int CONNECTED
See Also:
Constant Field Values

TIMEOUT

public static final int TIMEOUT
See Also:
Constant Field Values

CLOSED

public static final int CLOSED
See Also:
Constant Field Values

INVALID_HOST

public static final int INVALID_HOST
See Also:
Constant Field Values

FAILED_CONNECT

public static final int FAILED_CONNECT
See Also:
Constant Field Values

INFINITE

public static final int INFINITE
See Also:
Constant Field Values

className

public static final java.lang.String className
Constructor Detail

SocketConnection

public SocketConnection(java.net.InetAddress addr,
                        int port,
                        int bufferSize,
                        SocketConnectionListener listener)
Creates a simple socket connection along with a receive thread for reading the socket. When data is received on the socket, listener will be called. The socket receive has no initial timeout value (INFINITE).

Parameters:
addr - InetAddress object indicating the host to connect to
port - integer port number to connect to
bufferSize - integer size of maximum receive buffer
listener - SocketReceiveHandler object that will be called when data is received on a sockect or when an error occurs.

SocketConnection

public SocketConnection(java.lang.String host,
                        int port,
                        int bufferSize,
                        SocketConnectionListener listener)
Creates a simple socket along with a receive thread for reading the socket. When data is received on the socket, listener will be called. The socket receive has no initial timeout value (INFINITE).

Parameters:
host - String name of host to connect to
port - port number to connect to
bufferSize - integer size of maximum receive buffer
listener - SocketReceiveHandler object that will be called when data is received on a sockect or when an error occurs.

SocketConnection

public SocketConnection(java.net.InetAddress addr,
                        int port,
                        int bufferSize,
                        SocketConnectionListener listener,
                        int timeout)
Creates a simple socket along with a receive thread for reading the socket. When data is received on the socket, listener will be called. The socket recieve has the specified timeout. If a timeout occurs, the ConnectionListener.handleSocketStatus will be called with a status of TIMEOUT.

Parameters:
addr - host String name of host to connect to
port - port number to connect to
bufferSize - integer size of maximum receive buffer
listener - SocketReceiveHandler object that will be called when data is received on a sockect or when an error occurs.
timeout - number of milliseconds to wait for received data

SocketConnection

public SocketConnection(java.lang.String host,
                        int port,
                        int bufferSize,
                        SocketConnectionListener listener,
                        int timeout)
Creates a simple socket along with a receive thread for reading the socket. When data is received on the socket, listener will be called. The socket recieve has the specified timeout. If a timeout occurs, the ConnectionListener.handleSocketStatus will be called with a status of TIMEOUT.

Parameters:
host - String name of host to connect to
port - port number to connect to
bufferSize - integer size of maximum receive buffer
listener - SocketReceiveHandler object that will be called when data is received on a sockect or when an error occurs.
timeout - number of milliseconds to wait for received data

SocketConnection

public SocketConnection(java.net.InetAddress addr,
                        int port,
                        int bufferSize,
                        SocketConnectionListener listener,
                        int timeout,
                        boolean TcpNoDelayOn)
Creates a simple socket along with a receive thread for reading the socket. When data is received on the socket, listener will be called. The socket recieve has the specified timeout. If a timeout occurs, the ConnectionListener.handleSocketStatus will be called with a status of TIMEOUT.

Parameters:
addr - host String name of host to connect to
port - port number to connect to
bufferSize - integer size of maximum receive buffer
listener - SocketReceiveHandler object that will be called when data is received on a sockect or when an error occurs.
timeout - number of milliseconds to wait for received data
TcpNoDelayOn - Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm)

SocketConnection

public SocketConnection(java.lang.String host,
                        int port,
                        int bufferSize,
                        SocketConnectionListener listener,
                        int timeout,
                        boolean TcpNoDelayOn)
Creates a simple socket along with a receive thread for reading the socket. When data is received on the socket, listener will be called. The socket recieve has the specified timeout. If a timeout occurs, the ConnectionListener.handleSocketStatus will be called with a status of TIMEOUT.

Parameters:
host - String name of host to connect to
port - port number to connect to
bufferSize - integer size of maximum receive buffer
listener - SocketReceiveHandler object that will be called when data is received on a sockect or when an error occurs.
timeout - number of milliseconds to wait for received data
TcpNoDelayOn - Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm)
Method Detail

initConnection

public void initConnection(java.lang.String addrStr,
                           int port,
                           int bufferSize,
                           SocketConnectionListener listener,
                           int timeout,
                           boolean TcpNoDelayOn)

initConnection

public void initConnection(java.lang.String addrStr,
                           int port,
                           int bufferSize,
                           SocketConnectionListener listener,
                           int timeout)

setAddressAndPort

public boolean setAddressAndPort(java.lang.String host,
                                 int port)
Sets the socket connections IP address and port to the specified values. The new values will not take affect until a connection is created. If a connection already exists, it must be disconnected and re-connected with the new values.

Parameters:
host - String containing the new host name to connect to.
port - integer value containing the new port number to connect to.
Returns:
true if successful set, false if set failed due to invalid host.

setAddressAndPort

public boolean setAddressAndPort(java.net.InetAddress addr,
                                 int port)
Sets the socket connections IP address and port to the specified values. The new values will not take affect until a connection is created. If a connection already exists, it must be disconnected and re-connected with the new values.

Parameters:
addr - InetAddress object containing the new IP address
port - integer value containing the new port.
Returns:
,i>true if set was successful, else false

setTimeout

public void setTimeout(int timeout)
Sets the socket connection's receive timeout in milliseconds. The new timeout will take affect upon the next receive iteration. Use 0 for NO_TIMEOUT. If a negative timeout is specified, infinite is assumed.

Parameters:
timeout - [0..n]

connect

public boolean connect()
The first time connect is called for a SocketConnection, the receive thread is started and a socket object is created connected to the address and port. The connection is made asynchronously so the connect call will return immediately. A call into the listeners handleSocketStatus routine with a status of CONNECTED will occur when the socket has been created.

Returns:
true if the connection was initiated, false if the address is invalid

disconnect

public void disconnect()
Closes the socket. The receive thread remains running in an idle state waiting for connect to be called.


isConnected

public boolean isConnected()

dispose

public void dispose()
Destroys the socket connection and terminates the receive thread.


finalize

public void finalize()

write

public boolean write(byte[] data)
Writes data to the output stream of the Socket connection.

Parameters:
data - byte array containing data to write
Returns:
true if write was successful, else false

writeQueued

public boolean writeQueued(byte[] data)
Write data to the SocketConnection's output stream if output is OK, otherwise the output is queued awaiting send until OK-to-send is triggered via setOKtoSendTrue()

Parameters:
data - byte array containing data to send.
Returns:
true if write or queuing was successful, else false

flushQueue

public void flushQueue(boolean send)
Flushes any message awaiting send by either sending them to the SocketConnection's output stream or discarding the message based on the send argument.

Parameters:
send - boolean flag indicating if the queued messages should be send (true) or discarded (false)

setOKtoSendTrue

public void setOKtoSendTrue()
Triggers the SocketConnection's thread that it is OK to send the next queued message.


run

public void run()

setDebug

public void setDebug(boolean bState)
Deprecated. Do not use this method; use setDebugState instead

Parameters:
bState -

setDebugState

public void setDebugState(int nState)
Puts the class into a debug diagnostic state.

Parameters:
nState -

getDebugState

public int getDebugState()
Gets the current diagnostic state.

Returns:

log

public void log(int level,
                java.lang.String msg)
Parameters:
level -
msg -


Copyright © 2008 AMX LLC. All Rights Reserved.