omnisockets
Class OmniSocket

java.lang.Object
  |
  +--omnisockets.OmniSocket
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Runnable

public class OmniSocket
extends java.lang.Object
implements java.lang.Runnable, java.lang.Cloneable

The OmniSocket class is a socket that runs itself in a thread, then when a message is received creates and runs an OmniMessage of the appropriate type. This relives you from having to develop program logic to receive messages, unmarshall message data and handle the message. On the send side, you simply create an Omni Message of some type, fill in its fields appropriately and call OmniSocket.send(). The message data is automatically marshalled and sent on the socket.

Since:
OmniSockets 1.0
Version:
1.0.beta.1, 10/31/2002
Author:
Richard Bockenek
See Also:
OmniMessage

Constructor Summary
OmniSocket(java.net.InetAddress inaddr, int port)
          Constructs an OmniSocket for IP addr:port.
OmniSocket(java.net.InetAddress inaddr, int port, omnisockets.OmniEventHandler eventHandler)
          Constructs an OmniSocket for IP addr:port.
OmniSocket(java.net.Socket socket)
          Constructs an OmniSocket for a vanilla socket.
OmniSocket(java.net.Socket socket, omnisockets.OmniEventHandler eventHandler)
          Constructs an OmniSocket for a vanilla socket.
OmniSocket(java.lang.String hostname, int port)
          Constructs an OmniSocket for hostname:port.
OmniSocket(java.lang.String hostname, int port, omnisockets.OmniEventHandler eventHandler)
          Constructs an OmniSocket for hostname:port.
 
Method Summary
 void close()
          Closes the socket.
 boolean equals(omnisockets.OmniSocket s)
          Tests if this sockets is equal to another by comparing their hash codes.
 omnisockets.OmniEventHandler getEventHandler()
          Returns the event handler.
 omnisockets.OmniListener getListener()
          Returns the listener.
 java.lang.String getLocalHost()
          Returns the bind address.
 int getLocalPort()
          Returns the local port.
 omnisockets.OmniMessage getMessage()
          Returns the current message.
 java.net.Socket getRawSocket()
          Returns the raw socket.
 java.net.InetAddress getRemoteHost()
          Returns the remote IP address.
 int getRemotePort()
          Returns the remote port.
 java.lang.Thread getThread()
          Returns the thread.
 int hashCode()
          Returns a distinct integer for distinct OmniSockets.
 boolean isConnected()
          Returns the connection state of the socket.
 void run()
          Runs the message dispatcher.
 void send(omnisockets.OmniMessage message)
          Sends the message on the socket.
 void setEventHandler(omnisockets.OmniEventHandler eventHandler)
          Sets the event handler.
 void setKeepAlive(boolean on)
          Enable/disable SO_KEEPALIVE.
 void setListener(omnisockets.OmniListener listener)
          Sets the listener.
 void setMessage(omnisockets.OmniMessage message)
          Sets the current message.
 void setName(java.lang.String name)
          Sets the socket name.
 void setPriority(int priority)
          Sets the socket priority.
 void setSendBufferSize(int size)
          Sets the SO_SNDBUF option to the specified value for this Socket.
 void setSoLinger(boolean on, int linger)
          Enable/disable SO_LINGER with the specified linger time in seconds.
 void setTcpNoDelay(boolean on)
          Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OmniSocket

public OmniSocket(java.lang.String hostname,
                  int port)
           throws java.net.UnknownHostException,
                  java.io.IOException
Constructs an OmniSocket for hostname:port.

Parameters:
hostname - the remote host name
port - the remote port number

OmniSocket

public OmniSocket(java.lang.String hostname,
                  int port,
                  omnisockets.OmniEventHandler eventHandler)
           throws java.net.UnknownHostException,
                  java.io.IOException
Constructs an OmniSocket for hostname:port.

Parameters:
hostname - the remote host name
port - the remote port number

OmniSocket

public OmniSocket(java.net.InetAddress inaddr,
                  int port)
           throws java.net.UnknownHostException,
                  java.io.IOException
Constructs an OmniSocket for IP addr:port.

Parameters:
port - the remote port number

OmniSocket

public OmniSocket(java.net.InetAddress inaddr,
                  int port,
                  omnisockets.OmniEventHandler eventHandler)
           throws java.net.UnknownHostException,
                  java.io.IOException
Constructs an OmniSocket for IP addr:port.

Parameters:
port - the remote port number

OmniSocket

public OmniSocket(java.net.Socket socket)
           throws java.net.UnknownHostException,
                  java.io.IOException
Constructs an OmniSocket for a vanilla socket.

Parameters:
socket - a connected Socket for use as a message stream

OmniSocket

public OmniSocket(java.net.Socket socket,
                  omnisockets.OmniEventHandler eventHandler)
           throws java.net.UnknownHostException,
                  java.io.IOException
Constructs an OmniSocket for a vanilla socket.

Parameters:
socket - a connected Socket for use as a message stream
Method Detail

run

public void run()
Runs the message dispatcher.

Specified by:
run in interface java.lang.Runnable

send

public void send(omnisockets.OmniMessage message)
          throws java.io.NotSerializableException,
                 java.io.IOException
Sends the message on the socket.

Parameters:
message - the OmniMessage to send
java.io.NotSerializableException
java.io.IOException

close

public void close()
           throws java.io.IOException
Closes the socket.

java.io.IOException

getRemoteHost

public java.net.InetAddress getRemoteHost()
Returns the remote IP address.


getRemotePort

public int getRemotePort()
Returns the remote port.


getLocalHost

public java.lang.String getLocalHost()
Returns the bind address.


getLocalPort

public int getLocalPort()
Returns the local port.


getListener

public omnisockets.OmniListener getListener()
Returns the listener.


setListener

public void setListener(omnisockets.OmniListener listener)
Sets the listener.


getEventHandler

public omnisockets.OmniEventHandler getEventHandler()
Returns the event handler.


setEventHandler

public void setEventHandler(omnisockets.OmniEventHandler eventHandler)
Sets the event handler.


getMessage

public omnisockets.OmniMessage getMessage()
Returns the current message.


setMessage

public void setMessage(omnisockets.OmniMessage message)
Sets the current message.


isConnected

public boolean isConnected()
Returns the connection state of the socket.

Returns:
true if the socket successfuly connected to a remote socket

hashCode

public int hashCode()
Returns a distinct integer for distinct OmniSockets.

Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(omnisockets.OmniSocket s)
Tests if this sockets is equal to another by comparing their hash codes.


setPriority

public void setPriority(int priority)
Sets the socket priority.

Parameters:
priority - a valid thread priority

setName

public void setName(java.lang.String name)
Sets the socket name.

Parameters:
name - a descriptive string

getThread

public java.lang.Thread getThread()
Returns the thread. The user may get this in order to set some thread option besides priority or name.


setTcpNoDelay

public void setTcpNoDelay(boolean on)
                   throws java.net.SocketException
Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).

Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.

setSoLinger

public void setSoLinger(boolean on,
                        int linger)
                 throws java.net.SocketException
Enable/disable SO_LINGER with the specified linger time in seconds. The maximum timeout value is platform specific.

Parameters:
on - whether or not to linger on.
linger - how long to linger for, if on is true.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
java.lang.IllegalArgumentException - if the linger value is negative.

setSendBufferSize

public void setSendBufferSize(int size)
                       throws java.net.SocketException
Sets the SO_SNDBUF option to the specified value for this Socket. The SO_SNDBUF option is used by the platform's networking code as a hint for the size to set the underlying network I/O buffers.

Parameters:
size - the size to which to set the send buffer size. This value must be greater than 0.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.
java.lang.IllegalArgumentException - if the value is 0 or is negative.

setKeepAlive

public void setKeepAlive(boolean on)
                  throws java.net.SocketException
Enable/disable SO_KEEPALIVE.

Parameters:
on - whether or not to have socket keep alive turned on.
Throws:
java.net.SocketException - if there is an error in the underlying protocol, such as a TCP error.

getRawSocket

public java.net.Socket getRawSocket()
Returns the raw socket. The user may get this in order to set a socket option that is not supported by OmniSocket directly.

Returns:
the java.net.Socket object