java.lang.Object
net.i2p.sam.SAMMessageSession
All Implemented Interfaces:
Closeable, AutoCloseable, SAMMessageSess
Direct Known Subclasses:
SAMDatagramSession, SAMRawSession

abstract class SAMMessageSessionextends Object implements SAMMessageSess
Base abstract class for SAM message-based sessions.
  • Field Details

    • _log

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

      private final I2PSession session
      The I2P session of this SAM session.
    • _isOwnSession

      protected final boolean _isOwnSession
      True for a session we opened, false for a remote peer's.
    • handler

      The handler processing the messages.
    • listenProtocol

      private final int listenProtocol
      The protocol listened for.
    • listenPort

      private final int listenPort
      The port listened on.
  • Constructor Details

    • SAMMessageSession

      protected SAMMessageSession(String dest, Properties props) throws I2PSessionException
      Initialize a new SAM message-based session.
      Parameters:
      dest - Base64-encoded destination and private keys, and optional offline signature section (same format as PrivateKeyFile)
      props - Properties to setup the I2P session
      Throws:
      I2PSessionException - if the session cannot be created
    • SAMMessageSession

      protected SAMMessageSession(InputStream destStream, Properties props) throws I2PSessionException
      Initialize a new SAM message-based session.
      Parameters:
      destStream - Input stream containing the binary destination and private keys, and optional offline signature section (same format as PrivateKeyFile)
      props - Properties to setup the I2P session
      Throws:
      I2PSessionException - if the session cannot be created
    • SAMMessageSession

      protected SAMMessageSession(I2PSession sess, int listenProtocol, int listenPort)
      Initialize a new SAM message-based session using an existing I2PSession.
      Parameters:
      sess - the already-established I2P session to attach to, not owned by this wrapper
      listenProtocol - the I2CP protocol to bind, or I2PSession.PROTO_ANY for all
      listenPort - the local port to bind, or I2PSession.PORT_ANY to let the router choose
  • Method Details

    • start

      public void start()
      Start the SAM message session handler.
      Specified by:
      start in interface SAMMessageSess
    • getDestination

      public Destination getDestination()
      Get the SAM message-based session Destination.
      Specified by:
      getDestination in interface SAMMessageSess
      Returns:
      The SAM message-based session Destination.
    • getListenProtocol

      public int getListenProtocol()
      Protocol family to listen for.
      Specified by:
      getListenProtocol in interface SAMMessageSess
      Returns:
      the listen protocol
    • getListenPort

      public int getListenPort()
      Port to listen on.
      Specified by:
      getListenPort in interface SAMMessageSess
      Returns:
      the listen port
    • sendBytes

      public abstract boolean sendBytes(String dest, byte[] data, int proto, int fromPort, int toPort) throws DataFormatException, I2PSessionException
      Send bytes through a SAM message-based session.
      Specified by:
      sendBytes in interface SAMMessageSess
      Parameters:
      dest - destination to send to, as base64, a .b32.i2p name or a hostname
      data - Bytes to be sent
      proto - the I2CP protocol to send under
      fromPort - the I2CP from port, 0 for the default
      toPort - the I2CP to port, 0 for the default
      Returns:
      True if the data was sent, false otherwise
      Throws:
      DataFormatException - on unknown / bad dest
      I2PSessionException - on serious error, probably session closed
    • sendBytesThroughMessageSession

      protected boolean sendBytesThroughMessageSession(String dest, byte[] data, int proto, int fromPort, int toPort) throws DataFormatException, I2PSessionException
      Actually send bytes through the SAM message-based session I2PSession (er...).
      Parameters:
      dest - destination to send to, as base64, a .b32.i2p name or a hostname
      data - Bytes to be sent
      proto - I2CP protocol
      fromPort - I2CP from port
      toPort - I2CP to port
      Returns:
      True if the data was sent, false otherwise
      Throws:
      DataFormatException - on unknown / bad dest
      I2PSessionException - on serious error, probably session closed
    • sendBytesThroughMessageSession

      protected boolean sendBytesThroughMessageSession(String dest, byte[] data, int proto, int fromPort, int toPort, boolean sendLeaseSet, int sendTags, int tagThreshold, int expiration) throws DataFormatException, I2PSessionException
      Actually send bytes through the SAM message-based session I2PSession, using per-message extended options. For efficiency, use the method without all the extra options if they are all defaults.
      Parameters:
      dest - destination to send to, as base64, a .b32.i2p name or a hostname
      data - Bytes to be sent
      proto - I2CP protocol
      fromPort - I2CP from port
      toPort - I2CP to port
      sendLeaseSet - true is the usual setting and the I2CP default
      sendTags - 0 to leave as default
      tagThreshold - 0 to leave as default
      expiration - SECONDS from now, NOT absolute time, 0 to leave as default
      Returns:
      True if the data was sent, false otherwise
      Throws:
      DataFormatException - on unknown / bad dest
      I2PSessionException - on serious error, probably session closed
    • lookupDest

      public Destination lookupDest(String name) throws I2PSessionException
      Lookup a destination through the I2CP session. Blocking.
      Specified by:
      lookupDest in interface SAMMessageSess
      Parameters:
      name - the base32 hostname, or full destination, to resolve
      Returns:
      the Destination or null
      Throws:
      I2PSessionException - if the lookup does not complete within the 10 second session timeout
    • lookupDest

      static Destination lookupDest(I2PSession session, String name) throws I2PSessionException
      Lookup a destination through the I2CP session. Blocking.
      Parameters:
      session - the I2P session whose tunnel pool performs the lookup
      name - the base32 hostname, or full destination, to resolve
      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 message-based session.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface SAMMessageSess
    • messageReceived

      protected abstract void messageReceived(byte[] msg, int proto, int fromPort, int toPort)
      Handle a new received message.
      Parameters:
      msg - the received datagram payload, passed on to the handler
      proto - I2CP protocol
      fromPort - I2CP from port
      toPort - I2CP to port
    • shutDown

      protected abstract void shutDown()
      Do whatever is needed to shutdown the SAM session.
    • getI2PSession

      protected I2PSession getI2PSession()
      Get the I2PSession object used by the SAM message-based session.
      Returns:
      The I2PSession of the SAM message-based session