edu.nps.moves.disutil
Class NioServer.Event

java.lang.Object
  extended by java.util.EventObject
      extended by edu.nps.moves.disutil.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.

Version:
0.1
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
 java.nio.ByteBuffer getBuffer()
          Returns the ByteBuffer that contains the 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.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 buffer, java.net.SocketAddress remoteUdp)
          Resets an event between firings by updating the parameters that change.
 
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 buffer,
                     java.net.SocketAddress remoteUdp)
Resets an event between firings by updating the parameters that change.

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

getBuffer

public java.nio.ByteBuffer getBuffer()

Returns the ByteBuffer that contains the 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.

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 buffer's position at the beginning of the second line. In this way, The beginning of the second line will be the start of the buffer the next time around.

Returns:
buffer 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 key'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 key'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