Class NioServer.Event

java.lang.Object
  extended by java.util.EventObject
      extended by NioServer.Event
All Implemented Interfaces:
java.io.Serializable
Enclosing class:
NioServer

public static class NioServer.Event
extends java.util.EventObject

An event representing activity by a NioServer.

This code is released into the Public Domain. Since this is Public Domain, you don't need to worry about licensing, and you can simply copy this NioServer.java file to your own package and use it as you like. Enjoy. Please consider leaving the following statement here in this code:

This NioServer class was copied to this project from its source as found at iHarder.net.

Author:
Robert Harder, rharder@users.sourceforge.net
See Also:
NioServer, NioServer.Adapter, NioServer.Listener, Serialized Form

Field Summary
 
Fields inherited from class java.util.EventObject
source
 
Constructor Summary
NioServer.Event(NioServer src)
          Creates a Event based on the given NioServer.
 
Method Summary
 void close()
          Closes the underlying channel.
 void closeAfterWriting()
          Convenience method for indicating that you would like the connection closed after the last byte in the output buffer is written.
 java.nio.ByteBuffer getInputBuffer()
          Returns the ByteBuffer that contains the incoming data for this connection.
 java.nio.channels.SelectionKey getKey()
          Returns the SelectionKey associated with this event.
 java.net.SocketAddress getLocalSocketAddress()
          Returns the local address/port to which this connection is bound.
 NioServer getNioServer()
          Returns the source of the event, a NioServer.
 java.nio.ByteBuffer getOutputBuffer()
          Returns the ByteBuffer in which you leave data to be written to the client.
 java.net.SocketAddress getRemoteSocketAddress()
          Returns the address of the endpoint this socket is connected to, or null if it is unconnected.
 NioServer.State getState()
          Shorthand for getNioServer().getState().
 boolean isTcp()
          Convenience method for checking getKey().channel() instanceof SocketChannel.
 boolean isUdp()
          Convenience method for checking getKey().channel() instanceof DatagramChannel.
protected  void reset(java.nio.channels.SelectionKey key, java.nio.ByteBuffer inBuff, java.nio.ByteBuffer outBuff, java.net.SocketAddress remoteUdp)
          Resets an event between firings by updating the parameters that change.
 void setNotifyOnTcpWritable(boolean notify)
          Convenience method for turning on and off writable notifications.
 
Methods inherited from class java.util.EventObject
getSource, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

NioServer.Event

public NioServer.Event(NioServer src)
Creates a Event based on the given NioServer.

Parameters:
src - the source of the event
Method Detail

getNioServer

public NioServer getNioServer()
Returns the source of the event, a NioServer. Shorthand for (NioServer)getSource().

Returns:
the server

getState

public NioServer.State getState()
Shorthand for getNioServer().getState().

Returns:
the state of the server
See Also:
NioServer.State

getKey

public java.nio.channels.SelectionKey getKey()
Returns the SelectionKey associated with this event.

Returns:
the SelectionKey

reset

protected void reset(java.nio.channels.SelectionKey key,
                     java.nio.ByteBuffer inBuff,
                     java.nio.ByteBuffer outBuff,
                     java.net.SocketAddress remoteUdp)
Resets an event between firings by updating the parameters that change.

Parameters:
key - The SelectionKey for the event
remoteUdp - the remote UDP source or null for TCP

getInputBuffer

public java.nio.ByteBuffer getInputBuffer()

Returns the ByteBuffer that contains the incoming data for this connection. Read from it as much as you can. Any data that remains on or after the value of position() will be saved for the next time an event is fired. In this way, you can defer processing incomplete data until everything arrives. This applies to the NioServer.Listener.tcpDataReceived(NioServer.Event) event.

Example: You are receiving lines of text. The ByteBuffer returned here contains one and a half lines of text. When you realize this, you process the first line as you like, but you leave this outBuff's position at the beginning of the second line. In this way, The beginning of the second line will be the start of the outBuff the next time around.

Returns:
outBuff with the data

getOutputBuffer

public java.nio.ByteBuffer getOutputBuffer()

Returns the ByteBuffer in which you leave data to be written to the client. This applies to the NioServer.Listener.newConnectionReceived(NioServer.Event), NioServer.Listener.tcpDataReceived(NioServer.Event), and NioServer.Listener.tcpReadyToWrite(NioServer.Event) events.

Returns:
outBuff with the data

getLocalSocketAddress

public java.net.SocketAddress getLocalSocketAddress()

Returns the local address/port to which this connection is bound. That is, if you are listening on port 80, then this might return something like an InetSocketAddress (probably) that indicated /127.0.0.1:80.

This is essentially a convenience method for returning the same-name methods from the accKey's channel after checking the type of channel (SocketChannel or DatagramChannel).

Returns:
local address that server is bound to for this connection

getRemoteSocketAddress

public java.net.SocketAddress getRemoteSocketAddress()

Returns the address of the endpoint this socket is connected to, or null if it is unconnected.

This is essentially a convenience method for returning the same-name methods from the accKey's channel after checking the type of channel (SocketChannel or DatagramChannel).

Returns:
remote address from which connection came

isTcp

public boolean isTcp()
Convenience method for checking getKey().channel() instanceof SocketChannel.

Returns:
true if a TCP connection

isUdp

public boolean isUdp()
Convenience method for checking getKey().channel() instanceof DatagramChannel.

Returns:
true if a UDP connection

setNotifyOnTcpWritable

public void setNotifyOnTcpWritable(boolean notify)
Convenience method for turning on and off writable notifications. Equivalent to evt.getNioServer().setNotifyOnWritable(evt.getKey(), notify);.

Parameters:
notify - whether or not to provide notifications.

closeAfterWriting

public void closeAfterWriting()
Convenience method for indicating that you would like the connection closed after the last byte in the output buffer is written. Equivalent to evt.getNioServer().closeAfterWriting(evt.getKey());.


close

public void close()
           throws java.io.IOException
Closes the underlying channel. Convenience method for evt.getKey().channel().close().

Throws:
java.io.IOException