omnisockets
Class OmniEventHandler

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

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

The OmniEventHandler enables the OmniSockets framework to asynchronously notify the application that a socket event has occurred. The supported events are: a connection is made (EV_CONNECT), a connection is dropped (EV_DISCONNECT), a message is received (EV_MESSAGE), and an unrecognized message is received (EV_BAD_MESSAGE).

You must subclass OmniEventHandler and either override the run() method to perform whatever actions are desirable on any of the events. Or you can retain the run() method and override one or more of the separate event methods as appropriate for your protocol.

How to use it depends on whether your application is a client or a server. First create an event handler of your subtype. On the client side, then create an OmniSocket and pass the event handler to its constructor. On the server, you would instead create an OmniListener and pass the event handler to its constructor.

Note that you can choose not to use an event handler on the OmniListener if you are willing to accept connections yourself. Simply create the OmniListener without passing an event handler. Subsequently, you have to call OmniListener.accept() to accept new connnections. In this case, the calling thread will block until a connection is made.

Since:
OmniSockets 1.0
Version:
1.0.beta.1, 1/9/2003
Author:
Richard Bockenek
See Also:
OmniListener

Field Summary
static int EV_BAD_MESSAGE
          An unrecognized message type has been received
static int EV_CONNECT
          A connection has been established
static int EV_DISCONNECT
          A connection has been dropped
static int EV_MESSAGE
          A message has been received
static int EV_NONE
          Initial event code
 
Constructor Summary
OmniEventHandler()
          Constructs an emply event handler
OmniEventHandler(java.lang.Object cookie)
          Constructs an event handler with a cookie.
 
Method Summary
 void badMessage()
          The default badMessage event handler, may be overridden in subclass.
 void connected()
          The default connected event handler, may be overridden in subclass.
 void disconnected()
          The default disconnected event handler, may be overridden in subclass.
 java.lang.Object getCookie()
          Returns the cookie.
 int getEvent()
          Returns the event code.
 omnisockets.OmniListener getListener()
          Returns the listener
 omnisockets.OmniMessage getMessage()
          Returns the message.
 omnisockets.OmniSocket getSocket()
          Returns the socket.
 void message()
          The default message event handler, may be overridden in subclass.
 void run()
          Runs the event handler, may be overridden in subclass
 void setCookie(java.lang.Object cookie)
          Sets the cookie.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EV_NONE

public static final int EV_NONE
Initial event code

See Also:
Constant Field Values

EV_CONNECT

public static final int EV_CONNECT
A connection has been established

See Also:
Constant Field Values

EV_DISCONNECT

public static final int EV_DISCONNECT
A connection has been dropped

See Also:
Constant Field Values

EV_MESSAGE

public static final int EV_MESSAGE
A message has been received

See Also:
Constant Field Values

EV_BAD_MESSAGE

public static final int EV_BAD_MESSAGE
An unrecognized message type has been received

See Also:
Constant Field Values
Constructor Detail

OmniEventHandler

public OmniEventHandler()
Constructs an emply event handler


OmniEventHandler

public OmniEventHandler(java.lang.Object cookie)
Constructs an event handler with a cookie.

Parameters:
cookie - an object for use as a correlator
Method Detail

getListener

public omnisockets.OmniListener getListener()
Returns the listener


getSocket

public omnisockets.OmniSocket getSocket()
Returns the socket.


getMessage

public omnisockets.OmniMessage getMessage()
Returns the message.


getEvent

public int getEvent()
Returns the event code.


setCookie

public void setCookie(java.lang.Object cookie)
Sets the cookie.

Parameters:
cookie - any object used for correlation

getCookie

public java.lang.Object getCookie()
Returns the cookie.


run

public void run()
Runs the event handler, may be overridden in subclass

Specified by:
run in interface java.lang.Runnable

connected

public void connected()
The default connected event handler, may be overridden in subclass.


disconnected

public void disconnected()
The default disconnected event handler, may be overridden in subclass.


message

public void message()
The default message event handler, may be overridden in subclass.


badMessage

public void badMessage()
The default badMessage event handler, may be overridden in subclass.