java.lang.Object
net.i2p.sam.SAMStreamSession
All Implemented Interfaces:
Closeable, AutoCloseable, SAMMessageSess
Direct Known Subclasses:
SAMv2StreamSession, SAMv3StreamSession

class SAMStreamSessionextends Object implements SAMMessageSess
SAM STREAM session — BOTH/CREATE/RECEIVE modes over an I2PSocketManager. v1 bridge; v3 extends via "__v3__" direction to share the manager.
  • Field Details

    • _log

      protected final Log _log
      Logger for this session's messages.
    • SOCKET_HANDLER_BUF_SIZE

      protected static final int SOCKET_HANDLER_BUF_SIZE
      Read buffer size for each socket reader.
      See Also:
    • recv

      protected final SAMStreamReceiver recv
      The receiver that decodes incoming SAM messages.
    • server

      The server this session belongs to.
    • socketMgr

      protected final I2PSocketManager socketMgr
      The socket manager used for outgoing connections.
    • handlersMap

      Stream id → socket reader; Integer key, auto-negative ids for inbound.
    • sendersMap

      private final HashMap<Integer, SAMStreamSession.StreamSender> sendersMap
      Stream id → sender for outbound data.
    • lastNegativeId

      private final AtomicInteger lastNegativeId
      Counter for auto-generated negative stream ids.
    • canCreate

      protected final boolean canCreate
      Whether this session may create outgoing connections.
    • listenProtocol

      private final int listenProtocol
      Listen protocol.
    • listenPort

      private final int listenPort
      Listen port.
    • _isOwnSession

      protected final boolean _isOwnSession
      Whether this is our own session, as opposed to a remote peer's.
    • forceFlush

      protected final boolean forceFlush
      Should we flush every time we get a STREAM SEND, or leave that up to the streaming lib to decide?
    • PROP_FORCE_FLUSH

      public static final String PROP_FORCE_FLUSH
      Config key for flushing on every STREAM SEND.
      See Also:
    • DEFAULT_FORCE_FLUSH

      public static final String DEFAULT_FORCE_FLUSH
      Default for PROP_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 SAMv3StreamSession
      props - Properties to setup the I2P session
      recv - Object that will receive incoming data
      Throws:
      SAMException - if dir names no known direction, if i2cp.tcp.port is 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 SAMv3StreamSession
      props - Properties to setup the I2P session
      recv - Object that will receive incoming data
      Throws:
      SAMException - if dir names no known direction, if i2cp.tcp.port is 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 sockets
      props - Properties to setup the I2P session
      recv - Object that will receive incoming data
      listenport - 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:
      start in interface SAMMessageSess
    • getListenProtocol

      public int getListenProtocol()
      Get the I2CP protocol for this session's listener.
      Specified by:
      getListenProtocol in interface SAMMessageSess
      Returns:
      the I2CP protocol
    • getListenPort

      public int getListenPort()
      Get the port for this session's listener.
      Specified by:
      getListenPort in interface SAMMessageSess
      Returns:
      the listen port
    • getDestination

      public Destination getDestination()
      Get the SAM STREAM session Destination.
      Specified by:
      getDestination in interface SAMMessageSess
      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 connection
      dest - Base64-encoded Destination to connect to
      props - Options to be used for connection
      Returns:
      true if successful
      Throws:
      DataFormatException - if the destination is not valid
      SAMInvalidDirectionException - if trying to connect through a receive-only session
      ConnectException - if the destination refuses connections
      NoRouteToHostException - if the destination can't be reached
      InterruptedIOException - if the connection timeouts
      I2PException - if there's another I2P-related error
      IOException - if the connection fails for a reason not covered by the more specific subclasses listed above
    • sendBytes

      public boolean sendBytes(int id, InputStream in, int size) throws IOException
      Send bytes through a SAM STREAM session.
      Parameters:
      id - stream id the bytes are sent on
      in - supplies the bytes to send; exactly size bytes are read from it
      size - 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

      public Destination lookupDest(String name) throws I2PSessionException
      Lookup a destination through the I2CP session. Blocking.
      Specified by:
      lookupDest in interface SAMMessageSess
      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:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface SAMMessageSess
    • 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

      public boolean sendBytes(String s, byte[] b, int pr, int fp, int tp) throws I2PSessionException
      Sending bytes is not supported in a STREAM or MASTER session; always throws.
      Specified by:
      sendBytes in interface SAMMessageSess
      Parameters:
      s - the base32 destination or .b32.i2p hostname to send to
      b - the bytes to send
      pr - the I2CP protocol to send under
      fp - the I2CP from port, 0 for the default
      tp - 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:
      sendBytes in interface SAMMessageSess
      Parameters:
      s - the base32 destination or .b32.i2p hostname to send to
      b - the bytes to send
      pr - the I2CP protocol to send under
      fp - the I2CP from port, 0 for the default
      tp - the I2CP to port, 0 for the default
      sendLeaseSet - true to include the LeaseSet in the message
      sendTags - the number of tunnels to include, 0 to leave as default
      tagThreshold - the build success ratio below which no tunnel is sent, 0 for default
      expiration - 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

      protected int createSocketHandler(I2PSocket s, int id)
      Create a new SAM STREAM session socket handler, detaching its thread.
      Parameters:
      s - Socket to be handled
      id - 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

      protected SAMStreamSession.SAMStreamSessionSocketReader getSocketReader(int id)
      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

      private SAMStreamSession.StreamSender getSender(int id)
      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; ignored
      limit - the receive limit
      nolimit - 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 handled
      id - Unique id assigned to the handler
      Returns:
      the socket reader
      Throws:
      IOException - if the socket reader cannot be created
    • newStreamSender

      protected SAMStreamSession.StreamSender newStreamSender(I2PSocket s, int id) throws IOException
      Creates the sender that writes this socket's outgoing data.
      Parameters:
      s - the I2PSocket the sender writes to
      id - 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