java.lang.Object
net.i2p.sam.SAMStreamSession
- All Implemented Interfaces:
Closeable, AutoCloseable, SAMMessageSess
- Direct Known Subclasses:
SAMv2StreamSession, SAMv3StreamSession
SAM STREAM session — BOTH/CREATE/RECEIVE modes over an I2PSocketManager. v1 bridge; v3 extends via "__v3__" direction to share the manager.
Nested Class Summary
Modifier and TypeClassDescriptionprotected classCloses SAM session when the underlying I2P manager disconnects.classSAM STREAM session server, running in its own thread.static classReads bytes off one tunnel socket and pushes them to the SAM client.classSAMv1StreamSessionSocketReader.protected static classLets us push data through the stream without blocking, (even after exceeding the I2PSocket's buffer).private classSAM V1 stream session sender.Field Summary
Modifier and TypeFieldDescriptionprotected final booleanWhether this is our own session, as opposed to a remote peer's.protected final LogLogger for this session's messages.protected final booleanWhether this session may create outgoing connections.static final StringDefault forPROP_FORCE_FLUSH.protected final booleanShould we flush every time we get a STREAM SEND, or leave that up to the streaming lib to decide?private final HashMap<Integer, SAMStreamSession.SAMStreamSessionSocketReader> Stream id → socket reader; Integer key, auto-negative ids for inbound.private final AtomicIntegerCounter for auto-generated negative stream ids.private final intListen port.private final intListen protocol.static final StringConfig key for flushing on every STREAM SEND.protected final SAMStreamReceiverThe receiver that decodes incoming SAM messages.private final HashMap<Integer, SAMStreamSession.StreamSender> Stream id → sender for outbound data.protected final SAMStreamSession.SAMStreamSessionServerThe server this session belongs to.protected static final intRead buffer size for each socket reader.protected final I2PSocketManagerThe socket manager used for outgoing connections.Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedSAMStreamSession(InputStream destStream, String dir, Properties props, SAMStreamReceiver recv) Create a new SAM STREAM session.SAMStreamSession(String dest, String dir, Properties props, SAMStreamReceiver recv) Create a new SAM STREAM session.protectedSAMStreamSession(I2PSocketManager mgr, Properties props, SAMStreamReceiver recv, int listenport) Create a new SAM STREAM session on an existing socket manager.Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckSocketHandlerId(int id) Check whether a SAM STREAM session socket handler id is still in use.voidclose()Close a SAM STREAM session.booleancloseConnection(int id) Close a connection managed by the SAM STREAM session.booleanconnect(int id, String dest, Properties props) Connect the SAM STREAM session to the specified Destination.protected intcreateSocketHandler(I2PSocket s, int id) Create a new SAM STREAM session socket handler, detaching its thread.private intCreate a unique id, always negative.Get the SAM STREAM session Destination.intGet the port for this session's listener.intGet the I2CP protocol for this session's listener.private SAMStreamSession.StreamSendergetSender(int id) Look up stream sender by connection id.getSocketReader(int id) Get a SAM STREAM session socket handler.booleanIs the underlying streaming socket manager destroyed?lookupDest(String name) Lookup a destination through the I2CP session.newSAMStreamSessionSocketReader(I2PSocket s, int id) Create a new SAM STREAM session socket reader.protected SAMStreamSession.StreamSendernewStreamSender(I2PSocket s, int id) Creates the sender that writes this socket's outgoing data.private voidRemove and hard close all the socket handlers managed by this SAM STREAM session.protected voidremoveSocketHandler(int id) Remove and gracefully close a SAM STREAM session socket handler.booleansendBytes(int id, InputStream in, int size) Send bytes through a SAM STREAM session.booleanSending bytes is not supported in a STREAM or MASTER session; always throws.booleansendBytes(String s, byte[] b, int pr, int fp, int tp, boolean sendLeaseSet, int sendTags, int tagThreshold, int expiration) Sending bytes is not supported in a STREAM or MASTER session; always throws.(package private) booleansetReceiveLimit(int id, long limit, boolean nolimit) Set a receive limit for a stream.voidstart()Start the SAM STREAM session server, if applicable.
Field Details
_log
Logger for this session's messages.SOCKET_HANDLER_BUF_SIZE
protected static final int SOCKET_HANDLER_BUF_SIZERead buffer size for each socket reader.- See Also:
recv
The receiver that decodes incoming SAM messages.server
The server this session belongs to.socketMgr
The socket manager used for outgoing connections.handlersMap
Stream id → socket reader; Integer key, auto-negative ids for inbound.sendersMap
Stream id → sender for outbound data.lastNegativeId
Counter for auto-generated negative stream ids.canCreate
protected final boolean canCreateWhether this session may create outgoing connections.listenProtocol
private final int listenProtocolListen protocol.listenPort
private final int listenPortListen port._isOwnSession
protected final boolean _isOwnSessionWhether this is our own session, as opposed to a remote peer's.forceFlush
protected final boolean forceFlushShould we flush every time we get a STREAM SEND, or leave that up to the streaming lib to decide?PROP_FORCE_FLUSH
Config key for flushing on every STREAM SEND.- See Also:
DEFAULT_FORCE_FLUSH
Default forPROP_FORCE_FLUSH.- See Also:
Constructor Details
SAMStreamSession
public SAMStreamSession(String dest, String dir, Properties props, SAMStreamReceiver recv) throws SAMException Create a new SAM STREAM session.Caller MUST call
start().- Parameters:
dest- Base64-encoded destination and private keys, and optional offline signature section (same format as PrivateKeyFile)dir- Session direction ("RECEIVE", "CREATE" or "BOTH") or "__v3__" if extended by SAMv3StreamSessionprops- Properties to setup the I2P sessionrecv- Object that will receive incoming data- Throws:
SAMException- if dir names no known direction, ifi2cp.tcp.portis not a number, or if the I2P session cannot be created
SAMStreamSession
protected SAMStreamSession(InputStream destStream, String dir, Properties props, SAMStreamReceiver recv) throws SAMException Create a new SAM STREAM session.Caller MUST call start().
- Parameters:
destStream- Input stream containing the binary destination and private keys, and optional offline signature section (same format as PrivateKeyFile)dir- Session direction ("RECEIVE", "CREATE" or "BOTH") or "__v3__" if extended by SAMv3StreamSessionprops- Properties to setup the I2P sessionrecv- Object that will receive incoming data- Throws:
SAMException- if dir names no known direction, ifi2cp.tcp.portis not a number, or if the I2P session cannot be created
SAMStreamSession
protected SAMStreamSession(I2PSocketManager mgr, Properties props, SAMStreamReceiver recv, int listenport) Create a new SAM STREAM session on an existing socket manager. v3 only.- Parameters:
mgr- the socket manager owning the session's listening socketsprops- Properties to setup the I2P sessionrecv- Object that will receive incoming datalistenport- port for the listener, 1-65535 or PORT_ANY (0) for all
Method Details
start
public void start()Start the SAM STREAM session server, if applicable.- Specified by:
startin interfaceSAMMessageSess
getListenProtocol
public int getListenProtocol()Get the I2CP protocol for this session's listener.- Specified by:
getListenProtocolin interfaceSAMMessageSess- Returns:
- the I2CP protocol
getListenPort
public int getListenPort()Get the port for this session's listener.- Specified by:
getListenPortin interfaceSAMMessageSess- Returns:
- the listen port
getDestination
Get the SAM STREAM session Destination.- Specified by:
getDestinationin interfaceSAMMessageSess- Returns:
- The SAM STREAM session Destination.
connect
public boolean connect(int id, String dest, Properties props) throws I2PException, SAMInvalidDirectionException, IOException Connect the SAM STREAM session to the specified Destination.- Parameters:
id- Unique id for the connectiondest- Base64-encoded Destination to connect toprops- Options to be used for connection- Returns:
- true if successful
- Throws:
DataFormatException- if the destination is not validSAMInvalidDirectionException- if trying to connect through a receive-only sessionConnectException- if the destination refuses connectionsNoRouteToHostException- if the destination can't be reachedInterruptedIOException- if the connection timeoutsI2PException- if there's another I2P-related errorIOException- if the connection fails for a reason not covered by the more specific subclasses listed above
sendBytes
Send bytes through a SAM STREAM session.- Parameters:
id- stream id the bytes are sent onin- supplies the bytes to send; exactly size bytes are read from itsize- Count of bytes to send- Returns:
- True if the data was queued for sending, false otherwise
- Throws:
IOException- if the bytes cannot be read from in or written to the connection
lookupDest
Lookup a destination through the I2CP session. Blocking.- Specified by:
lookupDestin interfaceSAMMessageSess- Parameters:
name- Base64 or b32 destination name to look up- Returns:
- the Destination or null
- Throws:
I2PSessionException- if the lookup does not complete within the 10 second session timeout
close
public void close()Close a SAM STREAM session.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Specified by:
closein interfaceSAMMessageSess
isDestroyed
public boolean isDestroyed()Is the underlying streaming socket manager destroyed?- Returns:
- true if the underlying socket manager has been destroyed
closeConnection
public boolean closeConnection(int id) Close a connection managed by the SAM STREAM session.- Parameters:
id- connection id of the handler to remove- Returns:
- true on success
sendBytes
Sending bytes is not supported in a STREAM or MASTER session; always throws.- Specified by:
sendBytesin interfaceSAMMessageSess- Parameters:
s- the base32 destination or .b32.i2p hostname to send tob- the bytes to sendpr- the I2CP protocol to send underfp- the I2CP from port, 0 for the defaulttp- the I2CP to port, 0 for the default- Returns:
- True if the data was sent, false otherwise
- Throws:
I2PSessionException- always, since sending bytes is unsupported
sendBytes
public boolean sendBytes(String s, byte[] b, int pr, int fp, int tp, boolean sendLeaseSet, int sendTags, int tagThreshold, int expiration) throws I2PSessionException Sending bytes is not supported in a STREAM or MASTER session; always throws.- Specified by:
sendBytesin interfaceSAMMessageSess- Parameters:
s- the base32 destination or .b32.i2p hostname to send tob- the bytes to sendpr- the I2CP protocol to send underfp- the I2CP from port, 0 for the defaulttp- the I2CP to port, 0 for the defaultsendLeaseSet- true to include the LeaseSet in the messagesendTags- the number of tunnels to include, 0 to leave as defaulttagThreshold- the build success ratio below which no tunnel is sent, 0 for defaultexpiration- the lease lifetime in seconds from now, 0 to leave as default- Returns:
- True if the data was sent, false otherwise
- Throws:
I2PSessionException- always, since sending bytes is unsupported
createSocketHandler
Create a new SAM STREAM session socket handler, detaching its thread.- Parameters:
s- Socket to be handledid- Socket id, or 0 if it must be auto-generated- Returns:
- An id associated to the socket handler
createUniqueId
private int createUniqueId()Create a unique id, always negative.- Returns:
- the new unique handler id
getSocketReader
Get a SAM STREAM session socket handler.- Parameters:
id- the connection's handler id- Returns:
- the socket reader for that id, or null if none is registered
getSender
Look up stream sender by connection id.- Parameters:
id- the connection's handler id- Returns:
- the sender for that id, or null if none is registered
checkSocketHandlerId
protected boolean checkSocketHandlerId(int id) Check whether a SAM STREAM session socket handler id is still in use.- Parameters:
id- Handler id- Returns:
- True if in use
removeSocketHandler
protected void removeSocketHandler(int id) Remove and gracefully close a SAM STREAM session socket handler.- Parameters:
id- Handler id to be removed
removeAllSocketHandlers
private void removeAllSocketHandlers()Remove and hard close all the socket handlers managed by this SAM STREAM session.setReceiveLimit
boolean setReceiveLimit(int id, long limit, boolean nolimit) Set a receive limit for a stream. Protocol v1 does not support this.- Parameters:
id- stream id the limit would apply to; ignoredlimit- the receive limitnolimit- true for no limit- Returns:
- always false
newSAMStreamSessionSocketReader
protected SAMStreamSession.SAMStreamSessionSocketReader newSAMStreamSessionSocketReader(I2PSocket s, int id) throws IOException Create a new SAM STREAM session socket reader.- Parameters:
s- Socket to be handledid- Unique id assigned to the handler- Returns:
- the socket reader
- Throws:
IOException- if the socket reader cannot be created
newStreamSender
Creates the sender that writes this socket's outgoing data.- Parameters:
s- the I2PSocket the sender writes toid- the stream id assigned by the client- Returns:
- a sender bound to the socket and id
- Throws:
IOException- if the socket output stream cannot be obtained