com.mindbright.nio
Class Switchboard

java.lang.Object
  extended by com.mindbright.nio.Switchboard
All Implemented Interfaces:
java.lang.Runnable

public class Switchboard
extends java.lang.Object
implements java.lang.Runnable

A central switchboard class which handles all the waiting on different events. This subclass handles the java.nio case.


Method Summary
 void checkForDeadlock()
          Check if the current thread is a thread which is allowed to wait.
 NetworkConnection connect(java.net.InetAddress addr, int port, boolean block)
          Establish a new network connection.
 void debug2(java.lang.String cls, java.lang.String meth, java.lang.String msg)
           
static Switchboard getSwitchboard()
          Get the global switchboard instance.
 void notifyWhenConnected(NetworkConnection conn, long timeout, NIOCallback callback)
          Wait for a network connection to become connected.
protected  void read(java.nio.channels.spi.AbstractSelectableChannel channel, java.nio.ByteBuffer buf, NIOCallback callback, NonBlockingInput lock, boolean shortDataOk)
           
 java.lang.Object registerTimer(long interval, TimerCallback callback)
          Register a callback which is called regularly
 void run()
          This is the main loop which never exits.
 void setLog(Log log)
          Set the log to use
 void unregisterTimer(java.lang.Object key)
          Unregister a previously registered timer callback
protected  void write(java.nio.channels.spi.AbstractSelectableChannel channel, java.nio.ByteBuffer buf, NonBlockingOutput lock)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getSwitchboard

public static Switchboard getSwitchboard()
Get the global switchboard instance. This will create a new instance if none exists.


setLog

public void setLog(Log log)
Set the log to use

Parameters:
log - log to use

registerTimer

public java.lang.Object registerTimer(long interval,
                                      TimerCallback callback)
Register a callback which is called regularly

Parameters:
interval - minimum number of milliseconds between calls
callback - class to call back
Returns:
a key which should be used to cancel this callback once it is not longer needed.

unregisterTimer

public void unregisterTimer(java.lang.Object key)
Unregister a previously registered timer callback

Parameters:
key - the handler returned from registerTimer

connect

public NetworkConnection connect(java.net.InetAddress addr,
                                 int port,
                                 boolean block)
                          throws java.io.IOException
Establish a new network connection.

Parameters:
addr - address of server to connect to
port - port to connect to
block - true if the call should block until the connection is established
Returns:
the new network connection object. Note that this object may not be actually connected yet.
Throws:
java.io.IOException

notifyWhenConnected

public void notifyWhenConnected(NetworkConnection conn,
                                long timeout,
                                NIOCallback callback)
Wait for a network connection to become connected.

Parameters:
conn - the network connection to wait on
timeout - max time, in milliseconds, the connect may take
callback - function to call back once connection is done or has timed out.

debug2

public final void debug2(java.lang.String cls,
                         java.lang.String meth,
                         java.lang.String msg)

read

protected void read(java.nio.channels.spi.AbstractSelectableChannel channel,
                    java.nio.ByteBuffer buf,
                    NIOCallback callback,
                    NonBlockingInput lock,
                    boolean shortDataOk)
             throws java.io.IOException
Throws:
java.io.IOException

checkForDeadlock

public void checkForDeadlock()
                      throws java.lang.Exception
Check if the current thread is a thread which is allowed to wait. This is uspposed to be called before waiting on an event and should help guard against deadlocks. The function will throw an exception if the thread may not wait.

Throws:
java.lang.Exception - if the current thread is the event handling thread

write

protected void write(java.nio.channels.spi.AbstractSelectableChannel channel,
                     java.nio.ByteBuffer buf,
                     NonBlockingOutput lock)
              throws java.io.IOException
Throws:
java.io.IOException

run

public void run()
This is the main loop which never exits. It waits for stuff to happen and calls the relevant callbacks.

Specified by:
run in interface java.lang.Runnable