net.i2p.sam
Class SAMHandler

java.lang.Object
  extended by net.i2p.sam.SAMHandler
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
SAMv1Handler

public abstract class SAMHandler
extends java.lang.Object
implements java.lang.Runnable

Base class for SAM protocol handlers. It implements common methods, but is not able to actually parse the protocol itself: this task is delegated to subclasses.

Author:
human

Field Summary
protected  SAMBridge bridge
           
protected  java.util.Properties i2cpProps
          I2CP options configuring the I2CP connection (port, host, numHops, etc)
protected  java.nio.channels.SocketChannel socket
           
protected  I2PAppThread thread
           
protected  int verMajor
           
protected  int verMinor
           
 
Constructor Summary
protected SAMHandler(java.nio.channels.SocketChannel s, int verMajor, int verMinor, java.util.Properties i2cpProps)
          SAMHandler constructor (to be called by subclasses)
 
Method Summary
protected  void closeClientSocket()
          Close the socket connected to the SAM client.
protected  java.nio.channels.SocketChannel getClientSocket()
          Get the input stream of the socket connected to the SAM client
protected  java.lang.Object getWriteLock()
          If you're crazy enough to write to the raw socket, grab the write lock with getWriteLock(), synchronize against it, and write to the getOut()
protected abstract  void handle()
          Actually handle the SAM protocol.
 void run()
           
 void setBridge(SAMBridge bridge)
           
protected  boolean shouldStop()
          Should the handler be stopped?
 void startHandling()
          Start handling the SAM connection, detaching an handling thread.
 void stopHandling()
          Stop the SAM handler
 java.lang.String toString()
          Get a string describing the handler.
protected  void writeBytes(java.nio.ByteBuffer data)
          Write a byte array on the handler's socket.
static void writeBytes(java.nio.ByteBuffer data, java.nio.channels.SocketChannel out)
           
protected  boolean writeString(java.lang.String str)
          Write a string to the handler's socket.
static boolean writeString(java.lang.String str, java.nio.channels.SocketChannel out)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

thread

protected I2PAppThread thread

bridge

protected SAMBridge bridge

socket

protected java.nio.channels.SocketChannel socket

verMajor

protected int verMajor

verMinor

protected int verMinor

i2cpProps

protected java.util.Properties i2cpProps
I2CP options configuring the I2CP connection (port, host, numHops, etc)

Constructor Detail

SAMHandler

protected SAMHandler(java.nio.channels.SocketChannel s,
                     int verMajor,
                     int verMinor,
                     java.util.Properties i2cpProps)
              throws java.io.IOException
SAMHandler constructor (to be called by subclasses)

Parameters:
s - Socket attached to a SAM client
verMajor - SAM major version to manage
verMinor - SAM minor version to manage
i2cpProps - properties to configure the I2CP connection (host, port, etc)
Throws:
java.io.IOException
Method Detail

startHandling

public final void startHandling()
Start handling the SAM connection, detaching an handling thread.


setBridge

public void setBridge(SAMBridge bridge)

handle

protected abstract void handle()
Actually handle the SAM protocol.


getClientSocket

protected final java.nio.channels.SocketChannel getClientSocket()
Get the input stream of the socket connected to the SAM client

Returns:
input stream
Throws:
java.io.IOException

writeBytes

protected final void writeBytes(java.nio.ByteBuffer data)
                         throws java.io.IOException
Write a byte array on the handler's socket. This method must always be used when writing data, unless you really know what you're doing.

Parameters:
data - A byte array to be written
Throws:
java.io.IOException

writeBytes

public static void writeBytes(java.nio.ByteBuffer data,
                              java.nio.channels.SocketChannel out)
                       throws java.io.IOException
Throws:
java.io.IOException

getWriteLock

protected java.lang.Object getWriteLock()
If you're crazy enough to write to the raw socket, grab the write lock with getWriteLock(), synchronize against it, and write to the getOut()

Returns:
socket Write lock object

writeString

protected final boolean writeString(java.lang.String str)
Write a string to the handler's socket. This method must always be used when writing strings, unless you really know what you're doing.

Parameters:
str - A byte array to be written
Returns:
True if the string was successfully written, false otherwise

writeString

public static boolean writeString(java.lang.String str,
                                  java.nio.channels.SocketChannel out)

closeClientSocket

protected final void closeClientSocket()
                                throws java.io.IOException
Close the socket connected to the SAM client.

Throws:
java.io.IOException

stopHandling

public final void stopHandling()
Stop the SAM handler


shouldStop

protected final boolean shouldStop()
Should the handler be stopped?

Returns:
True if the handler should be stopped, false otherwise

toString

public final java.lang.String toString()
Get a string describing the handler.

Overrides:
toString in class java.lang.Object
Returns:
A String describing the handler;

run

public final void run()
Specified by:
run in interface java.lang.Runnable