public class DefaultTcpTransportMapping extends TcpTransportMapping
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 and Description |
|---|---|
static class |
DefaultTcpTransportMapping.SnmpMesssageLengthDecoder |
tcpAddressasyncMsgProcessingSupported, maxInboundMessageSize, transportListener| Constructor and 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 and Description |
|---|---|
void |
close()
Closes all open sockets and stops the internal server thread that
processes messages.
|
boolean |
close(Address remoteAddress)
Closes a connection to the supplied remote address, if it is open.
|
long |
getConnectionTimeout()
Gets the connection timeout.
|
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.
|
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(Address address,
byte[] message)
Sends a SNMP message to the supplied address.
|
void |
setConnectionTimeout(long connectionTimeout)
Sets the connection timeout.
|
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.
|
protected void |
setSocketOptions(ServerSocket serverSocket)
Sets optional server socket options.
|
void |
setThreadName(String name)
Sets the name of the listen thread for this UDP transport mapping.
|
addTransportStateListener, fireConnectionStateChanged, getAddress, getListenAddress, getSupportedAddressClass, removeTransportStateListeneraddMessageDispatcher, addTransportListener, fireProcessMessage, isAsyncMsgProcessingSupported, removeMessageDispatcher, removeTransportListener, setAsyncMsgProcessingSupportedclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitaddMessageDispatcher, addTransportListener, removeMessageDispatcher, removeTransportListenerpublic DefaultTcpTransportMapping()
throws UnknownHostException,
IOException
UnknownHostExceptionIOException - on failure of binding a local port.public DefaultTcpTransportMapping(TcpAddress serverAddress) throws UnknownHostException, IOException
serverAddress - the TcpAddress instance that describes the server address to listen
on incoming connection requests.UnknownHostException - if the specified interface does not exist.IOException - if the given address cannot be bound.public void listen()
throws 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 TransportMappinglisten in class TcpTransportMappingSocketException - when the transport is already listening for incoming/outgoing messages.IOExceptionpublic 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(String name)
listen() has been
called for this transport mapping.name - the new thread name.public String getThreadName()
null.public void close()
close in interface TransportMappingclose in class TcpTransportMappingpublic boolean close(Address remoteAddress) throws IOException
remoteAddress - the address of the peer socket.true if the connection has been closed and
false if there was nothing to close.IOException - if the remote address cannot be closed due to an IO exception.public void sendMessage(Address address, byte[] message) throws IOException
sendMessage in interface TransportMappingsendMessage in class TcpTransportMappingaddress - an TcpAddress. A ClassCastException is thrown
if address is not a TcpAddress instance.message - byte[]
the message to sent.IOExceptionpublic long getConnectionTimeout()
public void setConnectionTimeout(long connectionTimeout)
setConnectionTimeout in interface ConnectionOrientedTransportMappingsetConnectionTimeout in class TcpTransportMappingconnectionTimeout - 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.public boolean isServerEnabled()
public MessageLengthDecoder getMessageLengthDecoder()
TcpTransportMappingMessageLengthDecoder used by this transport
mapping.getMessageLengthDecoder in interface ConnectionOrientedTransportMappinggetMessageLengthDecoder in class TcpTransportMappingpublic 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 ConnectionOrientedTransportMappingsetMessageLengthDecoder in class TcpTransportMappingmessageLengthDecoder - a MessageLengthDecoder instance.public int getMaxInboundMessageSize()
getMaxInboundMessageSize in interface TransportMappinggetMaxInboundMessageSize in class AbstractTransportMappingpublic void setMaxInboundMessageSize(int maxInboundMessageSize)
maxInboundMessageSize - the length of the inbound buffer in bytes.public boolean isListening()
TransportMappingtrue 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 void setSocketOptions(ServerSocket serverSocket)
serverSocket - the ServerSocket to apply additional non-default options.Copyright © 2014 SNMP4J.org. All rights reserved.