Rice Pastry API

org.mpisws.p2p.transport.wire
Class SocketManager

java.lang.Object
  extended by rice.selector.SelectionKeyHandler
      extended by org.mpisws.p2p.transport.wire.SocketManager
All Implemented Interfaces:
P2PSocket<java.net.InetSocketAddress>, SocketRequestHandle<java.net.InetSocketAddress>, Cancellable

public class SocketManager
extends SelectionKeyHandler
implements P2PSocket<java.net.InetSocketAddress>, SocketRequestHandle<java.net.InetSocketAddress>


Field Summary
protected  java.nio.channels.SocketChannel channel
           
protected  java.nio.channels.SelectionKey key
           
protected  P2PSocketReceiver<java.net.InetSocketAddress> reader
           
protected  TCPLayer tcp
           
protected  TimerTask timer
           
protected  P2PSocketReceiver<java.net.InetSocketAddress> writer
           
 
Constructor Summary
SocketManager(TCPLayer tcp, java.net.InetSocketAddress addr, SocketCallback<java.net.InetSocketAddress> c, java.util.Map<java.lang.String,java.lang.Object> options)
          Constructor which creates an outgoing connection to the given node handle using the provided address as a source route intermediate node.
SocketManager(TCPLayer tcp, java.nio.channels.SelectionKey serverKey)
          Constructor which accepts an incoming connection, represented by the selection key.
 
Method Summary
 boolean cancel()
           
 void close()
          Method which closes down this socket manager, by closing the socket, cancelling the key and setting the key to be interested in nothing
 java.net.InetSocketAddress getIdentifier()
          The identification of the node at the other end of the socket.
 java.util.Map<java.lang.String,java.lang.Object> getOptions()
          Details on the connectivity of the socket (encrypted, source-routed etc)
 java.nio.channels.SocketChannel getSocketChannel()
           
 void modifyKey(java.nio.channels.SelectionKey key)
          Method which should change the interestOps of the handler's key.
 long read(java.nio.ByteBuffer dst)
          Reads a sequence of bytes from this channel into a subsequence of the given buffer.
 void read(java.nio.channels.SelectionKey key)
          Reads from the socket attached to this connector.
 void register(boolean wantToRead, boolean wantToWrite, P2PSocketReceiver<java.net.InetSocketAddress> receiver)
          Must be called every time a Read/Write occurs to continue operation.
 void shutdownOutput()
          Method which initiates a shutdown of this socket by calling shutdownOutput().
 java.lang.String toString()
           
 long write(java.nio.ByteBuffer src)
          Writes a sequence of bytes to this channel from a subsequence of the given buffers.
 void write(java.nio.channels.SelectionKey key)
          Writes to the socket attached to this socket manager.
 
Methods inherited from class rice.selector.SelectionKeyHandler
accept, connect
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

key

protected java.nio.channels.SelectionKey key

channel

protected java.nio.channels.SocketChannel channel

timer

protected TimerTask timer

tcp

protected TCPLayer tcp

reader

protected P2PSocketReceiver<java.net.InetSocketAddress> reader

writer

protected P2PSocketReceiver<java.net.InetSocketAddress> writer
Constructor Detail

SocketManager

public SocketManager(TCPLayer tcp,
                     java.nio.channels.SelectionKey serverKey)
              throws java.io.IOException
Constructor which accepts an incoming connection, represented by the selection key. This constructor builds a new SocketManager, and waits until the greeting message is read from the other end. Once the greeting is received, the manager makes sure that a socket for this handle is not already open, and then proceeds as normal.

Parameters:
key - The server accepting key for the channel
manager - TODO
Throws:
java.io.IOException - DESCRIBE THE EXCEPTION

SocketManager

public SocketManager(TCPLayer tcp,
                     java.net.InetSocketAddress addr,
                     SocketCallback<java.net.InetSocketAddress> c,
                     java.util.Map<java.lang.String,java.lang.Object> options)
              throws java.io.IOException
Constructor which creates an outgoing connection to the given node handle using the provided address as a source route intermediate node. This creates the connection by building the socket and sending across the greeting message. Once the response greeting message is received, everything proceeds as normal.

Parameters:
manager - TODO
address - The ultimate destination of this socket
proxy - The intermediate destination of this socket (if a source route)
Throws:
java.io.IOException - An error
Method Detail

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

close

public void close()
Method which closes down this socket manager, by closing the socket, cancelling the key and setting the key to be interested in nothing

Specified by:
close in interface P2PSocket<java.net.InetSocketAddress>

modifyKey

public void modifyKey(java.nio.channels.SelectionKey key)
Method which should change the interestOps of the handler's key. This method should *ONLY* be called by the selection thread in the context of a select().

Overrides:
modifyKey in class SelectionKeyHandler
Parameters:
key - The key in question

read

public void read(java.nio.channels.SelectionKey key)
Reads from the socket attached to this connector.

Overrides:
read in class SelectionKeyHandler
Parameters:
key - The selection key for this manager

write

public void write(java.nio.channels.SelectionKey key)
Writes to the socket attached to this socket manager.

Overrides:
write in class SelectionKeyHandler
Parameters:
key - The selection key for this manager

register

public void register(boolean wantToRead,
                     boolean wantToWrite,
                     P2PSocketReceiver<java.net.InetSocketAddress> receiver)
Description copied from interface: P2PSocket
Must be called every time a Read/Write occurs to continue operation. Can cancel this task by calling with null.

Specified by:
register in interface P2PSocket<java.net.InetSocketAddress>
Parameters:
wantToRead - if you want to read from this socket
wantToWrite - if you want to write to this socket
receiver - will have receiveSelectResult() called on it note that you must call select() each time receiveSelectResult() is called. This is so your application can properly handle flow control

shutdownOutput

public void shutdownOutput()
Method which initiates a shutdown of this socket by calling shutdownOutput(). This has the effect of removing the manager from the open list.

Specified by:
shutdownOutput in interface P2PSocket<java.net.InetSocketAddress>

read

public long read(java.nio.ByteBuffer dst)
          throws java.io.IOException
Description copied from interface: P2PSocket
Reads a sequence of bytes from this channel into a subsequence of the given buffer.

Specified by:
read in interface P2PSocket<java.net.InetSocketAddress>
Returns:
Throws:
java.io.IOException

write

public long write(java.nio.ByteBuffer src)
           throws java.io.IOException
Description copied from interface: P2PSocket
Writes a sequence of bytes to this channel from a subsequence of the given buffers.

Specified by:
write in interface P2PSocket<java.net.InetSocketAddress>
Throws:
java.io.IOException

cancel

public boolean cancel()
Specified by:
cancel in interface Cancellable
Returns:
true if it was cancelled, false if it was already complete, or cancelled.

getIdentifier

public java.net.InetSocketAddress getIdentifier()
Description copied from interface: P2PSocket
The identification of the node at the other end of the socket.

Specified by:
getIdentifier in interface P2PSocket<java.net.InetSocketAddress>
Specified by:
getIdentifier in interface SocketRequestHandle<java.net.InetSocketAddress>
Returns:
The identification of the node at the other end of the socket.

getOptions

public java.util.Map<java.lang.String,java.lang.Object> getOptions()
Description copied from interface: P2PSocket
Details on the connectivity of the socket (encrypted, source-routed etc)

Specified by:
getOptions in interface P2PSocket<java.net.InetSocketAddress>
Specified by:
getOptions in interface SocketRequestHandle<java.net.InetSocketAddress>
Returns:
a read-only list of options on this socket

getSocketChannel

public java.nio.channels.SocketChannel getSocketChannel()

Rice Pastry API

Copyright © 2001-2005 - Rice Pastry.


Imprint-Dataprotection