Package net.i2p.sam
Class SAMHandler
java.lang.Object
net.i2p.sam.SAMHandler
- Direct Known Subclasses:
SAMv1Handler
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
FieldsModifier and TypeFieldDescriptionprotected final Logprotected final SAMBridgeprotected final PropertiesI2CP options configuring the I2CP connection (port, host, numHops, etc)protected final SocketChannelprotected booleanprotected final Objectprotected I2PAppThreadfinal intfinal int -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSAMHandler(SocketChannel s, int verMajor, int verMinor, Properties i2cpProps, SAMBridge parent) SAMHandler constructor (to be called by subclasses) -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidClose the socket connected to the SAM client.protected final SocketChannelGet the channel of the socket connected to the SAM clientprotected ObjectIf 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 voidhandle()Actually handle the SAM protocol.final voidrun()Register with the bridge, call handle(), unregister with the bridge.protected final booleanShould the handler be stopped?final voidStart handling the SAM connection, detaching an handling thread.voidStop the SAM handler, close the client socket, unregister with the bridge.final StringtoString()Get a string describing the handler.protected final voidwriteBytes(ByteBuffer data) Write a byte array on the handler's socket.protected final booleanwriteString(String str) Write a string to the handler's socket.static booleanwriteString(String str, SocketChannel out) Unsynchronized, use with caution
-
Field Details
-
_log
-
thread
-
bridge
-
socket
-
verMajor
public final int verMajor -
verMinor
public final int verMinor -
i2cpProps
I2CP options configuring the I2CP connection (port, host, numHops, etc) -
stopLock
-
stopHandler
protected boolean stopHandler
-
-
Constructor Details
-
SAMHandler
protected SAMHandler(SocketChannel s, int verMajor, int verMinor, Properties i2cpProps, SAMBridge parent) throws IOException SAMHandler constructor (to be called by subclasses)- Parameters:
s- Socket attached to a SAM clientverMajor- SAM major version to manageverMinor- SAM minor version to managei2cpProps- properties to configure the I2CP connection (host, port, etc)- Throws:
IOException
-
-
Method Details
-
startHandling
public final void startHandling()Start handling the SAM connection, detaching an handling thread. -
handle
protected abstract void handle()Actually handle the SAM protocol. -
getClientSocket
Get the channel of the socket connected to the SAM client- Returns:
- channel
-
writeBytes
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:
IOException
-
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
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
Unsynchronized, use with caution- Returns:
- success
-
closeClientSocket
Close the socket connected to the SAM client.- Throws:
IOException
-
stopHandling
public void stopHandling()Stop the SAM handler, close the client socket, unregister with the bridge.- Specified by:
stopHandlingin interfaceHandler
-
shouldStop
protected final boolean shouldStop()Should the handler be stopped?- Returns:
- True if the handler should be stopped, false otherwise
-
toString
Get a string describing the handler. -
run
public final void run()Register with the bridge, call handle(), unregister with the bridge.
-