java.io.Closeable
, java.lang.AutoCloseable
, ConnectionOrientedTransportMapping<TcpAddress>
, TransportMapping<TcpAddress>
public class DefaultTcpTransportMapping extends TcpTransportMapping<org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry>
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.
Modifier and Type | Class | Description |
---|---|---|
protected class |
DefaultTcpTransportMapping.ServerThread |
|
static class |
DefaultTcpTransportMapping.SnmpMesssageLengthDecoder |
Modifier and Type | Field | Description |
---|---|---|
static int |
DEFAULT_MAX_BUSY_LOOPS |
The maximum number of loops trying to read data from an incoming port but no data has been received.
|
protected MessageLengthDecoder |
messageLengthDecoder |
|
protected DefaultTcpTransportMapping.ServerThread |
serverThread |
asyncMsgProcessingSupported, maxInboundMessageSize, transportListener
connectionTimeout, server, socketCleaner, sockets, tcpAddress
Constructor | Description |
---|---|
DefaultTcpTransportMapping() |
Creates a default TCP transport mapping with the server for incoming
messages disabled.
|
DefaultTcpTransportMapping(TcpAddress serverAddress) |
Creates a default TCP transport mapping that binds to the given address
(interface) on the local host.
|
Modifier and Type | Method | Description |
---|---|---|
protected void |
addBufferToReadBuffer(org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry entry,
java.nio.ByteBuffer byteBuffer) |
|
void |
close() |
Closes all open sockets and stops the internal server thread that
processes messages.
|
TcpAddress |
getListenAddress() |
Returns the address that represents the actual incoming address this transport
mapping uses to listen for incoming packets.
|
protected int |
getMaxBusyLoops() |
|
int |
getMaxInboundMessageSize() |
Gets the inbound buffer size for incoming requests.
|
MessageLengthDecoder |
getMessageLengthDecoder() |
Returns the
MessageLengthDecoder used by this transport
mapping. |
int |
getPriority() |
Returns the priority of the internal listen thread.
|
CommonTimer |
getSocketCleaner() |
Gets the
CommonTimer that controls socket cleanup operations. |
java.lang.String |
getThreadName() |
Returns the name of the listen thread.
|
boolean |
isListening() |
Returns
true if the transport mapping is listening for
incoming messages. |
boolean |
isServerEnabled() |
Checks whether a server for incoming requests is enabled.
|
void |
listen() |
Listen for incoming and outgoing requests.
|
void |
sendMessage(TcpAddress address,
byte[] message,
TransportStateReference tmStateReference,
long timeoutMillis,
int maxRetries) |
Sends a SNMP message to the supplied address.
|
protected void |
setMaxBusyLoops(int maxBusyLoops) |
|
void |
setMaxInboundMessageSize(int maxInboundMessageSize) |
Sets the maximum buffer size for incoming requests.
|
void |
setMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder) |
Sets the message length decoder.
|
void |
setPriority(int newPriority) |
Changes the priority of the server thread for this TCP transport mapping.
|
void |
setServerEnabled(boolean serverEnabled) |
Sets whether a server for incoming requests should be created when
the transport is set into listen state.
|
void |
setThreadName(java.lang.String name) |
Sets the name of the listen thread for this UDP transport mapping.
|
protected void |
socketClosedRemotely(java.nio.channels.SelectionKey sk,
java.nio.channels.SocketChannel readChannel,
TcpAddress incomingAddress) |
addTransportListener, fireProcessMessage, isAsyncMsgProcessingSupported, removeTransportListener, setAsyncMsgProcessingSupported
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addTransportStateListener, close, closeSockets, fireConnectionStateChanged, getAddress, getConnectionTimeout, getServer, getSupportedAddressClass, removeTransportStateListener, setConnectionTimeout, setSocketOptions, timeoutSocket
addTransportListener, removeTransportListener
public static final int DEFAULT_MAX_BUSY_LOOPS
protected DefaultTcpTransportMapping.ServerThread serverThread
protected MessageLengthDecoder messageLengthDecoder
public DefaultTcpTransportMapping() throws java.io.IOException
java.io.IOException
- on failure of binding a local port.public DefaultTcpTransportMapping(TcpAddress serverAddress) throws java.io.IOException
serverAddress
- the TcpAddress instance that describes the server address to listen
on incoming connection requests.java.io.IOException
- if the given address cannot be bound.public void listen() throws java.io.IOException
serverEnabled
member is false
the server for incoming requests is not
started. This starts the internal server thread that processes messages.listen
in interface TransportMapping<TcpAddress>
listen
in class TcpTransportMapping<org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry>
java.net.SocketException
- when the transport is already listening for incoming/outgoing messages.java.io.IOException
- if the listen port could not be bound to the server thread.public void setPriority(int newPriority)
listen()
has been
called for this transport mapping or if SNMP4J is configured to use
a non-default thread factory.newPriority
- the new priority.Thread.setPriority(int)
public int getPriority()
Thread.MIN_PRIORITY
and
Thread.MAX_PRIORITY
.public void setThreadName(java.lang.String name)
listen()
has been
called for this transport mapping.name
- the new thread name.public java.lang.String getThreadName()
null
.public void close()
close
in interface java.lang.AutoCloseable
close
in interface java.io.Closeable
close
in interface TransportMapping<TcpAddress>
close
in class TcpTransportMapping<org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry>
public void sendMessage(TcpAddress address, byte[] message, TransportStateReference tmStateReference, long timeoutMillis, int maxRetries) throws java.io.IOException
sendMessage
in interface TransportMapping<TcpAddress>
sendMessage
in class TcpTransportMapping<org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry>
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.timeoutMillis
- maximum number of milli seconds the connection creation might take (if connection based).maxRetries
- maximum retries during connection creation.java.io.IOException
- if an IO exception occurs while trying to send the message.public boolean isServerEnabled()
public MessageLengthDecoder getMessageLengthDecoder()
TcpTransportMapping
MessageLengthDecoder
used by this transport
mapping.getMessageLengthDecoder
in interface ConnectionOrientedTransportMapping<TcpAddress>
getMessageLengthDecoder
in class TcpTransportMapping<org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry>
public void setServerEnabled(boolean serverEnabled)
listen()
method is called (if the transport is already
listening, close()
has to be called before).serverEnabled
- if true
if the transport will listens for incoming
requests after listen()
has been called.public void setMessageLengthDecoder(MessageLengthDecoder messageLengthDecoder)
DefaultTcpTransportMapping.SnmpMesssageLengthDecoder
. The message length decoder must be
able to decode the total length of a message for this transport mapping
protocol(s).setMessageLengthDecoder
in interface ConnectionOrientedTransportMapping<TcpAddress>
setMessageLengthDecoder
in class TcpTransportMapping<org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry>
messageLengthDecoder
- a MessageLengthDecoder
instance.public CommonTimer getSocketCleaner()
CommonTimer
that controls socket cleanup operations.public int getMaxInboundMessageSize()
getMaxInboundMessageSize
in interface TransportMapping<TcpAddress>
getMaxInboundMessageSize
in class AbstractTransportMapping<TcpAddress>
public void setMaxInboundMessageSize(int maxInboundMessageSize)
maxInboundMessageSize
- the length of the inbound buffer in bytes.public boolean isListening()
TransportMapping
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.true
if this transport mapping is listening for messages.protected int getMaxBusyLoops()
protected void setMaxBusyLoops(int maxBusyLoops)
public TcpAddress getListenAddress()
TransportMapping
getListenAddress
in interface TransportMapping<TcpAddress>
getListenAddress
in class TcpTransportMapping<org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry>
null
this transport
mapping is not configured to listen for incoming packets.protected void addBufferToReadBuffer(org.snmp4j.transport.DefaultTcpTransportMapping.SocketEntry entry, java.nio.ByteBuffer byteBuffer)
protected void socketClosedRemotely(java.nio.channels.SelectionKey sk, java.nio.channels.SocketChannel readChannel, TcpAddress incomingAddress) throws java.io.IOException
java.io.IOException
Copyright © 2018 SNMP4J.org. All rights reserved.