Class DefaultTcpTransportMapping

All Implemented Interfaces:
ConnectionOrientedTransportMapping<TcpAddress>, TransportMapping<TcpAddress>

public class DefaultTcpTransportMapping extends TcpTransportMapping
The DefaultTcpTransportMapping implements a TCP transport mapping with the Java 1.4 new IO API. It uses a single thread for processing incoming and outgoing messages. The thread is started when the listen method is called, or when an outgoing request is sent using the sendMessage method.
Version:
2.8.5
Author:
Frank Fock
  • Field Details

    • DEFAULT_MAX_BUSY_LOOPS

      public static final int DEFAULT_MAX_BUSY_LOOPS
      The maximum number of loops trying to read data from an incoming port but no data has been received. A value of 0 or less disables the check.
      See Also:
    • sockets

      protected Map<Address,org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry> sockets
    • server

      protected WorkerTask server
    • serverThread

    • socketCleaner

      protected CommonTimer socketCleaner
    • messageLengthDecoder

      protected MessageLengthDecoder messageLengthDecoder
  • Constructor Details

    • DefaultTcpTransportMapping

      public DefaultTcpTransportMapping() throws IOException
      Creates a default TCP transport mapping with the server for incoming messages disabled.
      Throws:
      IOException - on failure of binding a local port.
    • DefaultTcpTransportMapping

      public DefaultTcpTransportMapping(TcpAddress serverAddress) throws IOException
      Creates a default TCP transport mapping that binds to the given address (interface) on the local host.
      Parameters:
      serverAddress - the TcpAddress instance that describes the server address to listen on incoming connection requests.
      Throws:
      IOException - if the given address cannot be bound.
  • Method Details

    • listen

      public void listen() throws IOException
      Listen for incoming and outgoing requests. If the serverEnabled member is false the server for incoming requests is not started. This starts the internal server thread that processes messages.
      Specified by:
      listen in interface TransportMapping<TcpAddress>
      Specified by:
      listen in class TcpTransportMapping
      Throws:
      SocketException - when the transport is already listening for incoming/outgoing messages.
      IOException - if the listen port could not be bound to the server thread.
    • setPriority

      public void setPriority(int newPriority)
      Changes the priority of the server thread for this TCP transport mapping. This method has no effect, if called before listen() has been called for this transport mapping or if SNMP4J is configured to use a non-default thread factory.
      Parameters:
      newPriority - the new priority.
      Since:
      1.2.2
      See Also:
    • getPriority

      public int getPriority()
      Returns the priority of the internal listen thread.
      Returns:
      a value between Thread.MIN_PRIORITY and Thread.MAX_PRIORITY.
      Since:
      1.2.2
    • setThreadName

      public void setThreadName(String name)
      Sets the name of the listen thread for this UDP transport mapping. This method has no effect, if called before listen() has been called for this transport mapping.
      Parameters:
      name - the new thread name.
      Since:
      1.6
    • getThreadName

      public String getThreadName()
      Returns the name of the listen thread.
      Returns:
      the thread name if in listening mode, otherwise null.
      Since:
      1.6
    • close

      public void close()
      Closes all open sockets and stops the internal server thread that processes messages and removes all queued requests and socket entries.
      Specified by:
      close in interface TransportMapping<TcpAddress>
      Specified by:
      close in class TcpTransportMapping
    • close

      public boolean close(TcpAddress remoteAddress) throws IOException
      Closes a connection to the supplied remote address, if it is open. This method is particularly useful when not using a timeout for remote connections.
      Parameters:
      remoteAddress - the address of the peer socket.
      Returns:
      true if the connection has been closed and false if there was nothing to close.
      Throws:
      IOException - if the remote address cannot be closed due to an IO exception.
      Since:
      1.7.1
    • sendMessage

      public void sendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference) throws IOException
      Sends a SNMP message to the supplied address.
      Specified by:
      sendMessage in interface TransportMapping<TcpAddress>
      Specified by:
      sendMessage in class TcpTransportMapping
      Parameters:
      address - an TcpAddress. A ClassCastException is thrown if address is not a TcpAddress instance.
      message - byte[] the message to sent.
      tmStateReference - the (optional) transport model state reference as defined by RFC 5590 section 6.1.
      Throws:
      IOException - if an IO exception occurs while trying to send the message.
    • getConnectionTimeout

      public long getConnectionTimeout()
      Gets the connection timeout. This timeout specifies the time a connection may be idle before it is closed.
      Returns:
      long the idle timeout in milliseconds.
    • setConnectionTimeout

      public void setConnectionTimeout(long connectionTimeout)
      Sets the connection timeout. This timeout specifies the time a connection may be idle before it is closed.
      Specified by:
      setConnectionTimeout in interface ConnectionOrientedTransportMapping<TcpAddress>
      Specified by:
      setConnectionTimeout in class TcpTransportMapping
      Parameters:
      connectionTimeout - the idle timeout in milliseconds. A zero or negative value will disable any timeout and connections opened by this transport mapping will stay opened until they are explicitly closed.
    • isServerEnabled

      public boolean isServerEnabled()
      Checks whether a server for incoming requests is enabled.
      Returns:
      boolean
    • getMessageLengthDecoder

      public MessageLengthDecoder getMessageLengthDecoder()
      Description copied from class: TcpTransportMapping
      Returns the MessageLengthDecoder used by this transport mapping.
      Specified by:
      getMessageLengthDecoder in interface ConnectionOrientedTransportMapping<TcpAddress>
      Specified by:
      getMessageLengthDecoder in class TcpTransportMapping
      Returns:
      a MessageLengthDecoder instance.
    • setServerEnabled

      public void setServerEnabled(boolean serverEnabled)
      Sets whether a server for incoming requests should be created when the transport is set into listen state. Setting this value has no effect until the listen() method is called (if the transport is already listening, close() has to be called before).
      Parameters:
      serverEnabled - if true if the transport will listens for incoming requests after listen() has been called.
    • setMessageLengthDecoder

      public void setMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder)
      Sets the message length decoder. Default message length decoder is the DefaultTcpTransportMapping.SnmpMesssageLengthDecoder. The message length decoder must be able to decode the total length of a message for this transport mapping protocol(s).
      Specified by:
      setMessageLengthDecoder in interface ConnectionOrientedTransportMapping<TcpAddress>
      Specified by:
      setMessageLengthDecoder in class TcpTransportMapping
      Parameters:
      messageLengthDecoder - a MessageLengthDecoder instance.
    • getMaxInboundMessageSize

      public int getMaxInboundMessageSize()
      Gets the inbound buffer size for incoming requests. When SNMP packets are received that are longer than this maximum size, the messages will be silently dropped and the connection will be closed.
      Specified by:
      getMaxInboundMessageSize in interface TransportMapping<TcpAddress>
      Overrides:
      getMaxInboundMessageSize in class AbstractTransportMapping<TcpAddress>
      Returns:
      the maximum inbound buffer size in bytes.
    • setMaxInboundMessageSize

      public void setMaxInboundMessageSize(int maxInboundMessageSize)
      Sets the maximum buffer size for incoming requests. When SNMP packets are received that are longer than this maximum size, the messages will be silently dropped and the connection will be closed.
      Parameters:
      maxInboundMessageSize - the length of the inbound buffer in bytes.
    • isListening

      public boolean isListening()
      Description copied from interface: TransportMapping
      Returns true if the transport mapping is listening for incoming messages. For connection oriented transport mappings this is a prerequisite to be able to send SNMP messages. For connectionless transport mappings it is a prerequisite to be able to receive responses.
      Returns:
      true if this transport mapping is listening for messages.
    • getMaxBusyLoops

      protected int getMaxBusyLoops()
    • setMaxBusyLoops

      protected void setMaxBusyLoops(int maxBusyLoops)
    • setSocketOptions

      protected void setSocketOptions(ServerSocket serverSocket)
      Sets optional server socket options. The default implementation does nothing.
      Parameters:
      serverSocket - the ServerSocket to apply additional non-default options.
    • getListenAddress

      public TcpAddress getListenAddress()
      Description copied from interface: TransportMapping
      Returns the address that represents the actual incoming address this transport mapping uses to listen for incoming packets.
      Specified by:
      getListenAddress in interface TransportMapping<TcpAddress>
      Overrides:
      getListenAddress in class TcpTransportMapping
      Returns:
      the address for incoming packets or null this transport mapping is not configured to listen for incoming packets.
    • addBufferToReadBuffer

      protected void addBufferToReadBuffer(org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry entry, ByteBuffer byteBuffer)
    • socketClosedRemotely

      protected void socketClosedRemotely(SelectionKey sk, SocketChannel readChannel, TcpAddress incomingAddress) throws IOException
      Throws:
      IOException
    • removeSocketEntry

      protected Object removeSocketEntry(TcpAddress incomingAddress)