public class NettyTcpModbusServer extends Object
This server listens for Modbus requests, decodes them into
ModbusMessage instances, and then passes those to the handler
configured via setMessageHandler(BiConsumer). The handler must
provide a response ModbusMessage, which this server will then encode
and send back to the connected client.
| Modifier and Type | Field | Description |
|---|---|---|
static String |
DEFAULT_BIND_ADDRESS |
The default
bindAddress property value. |
static long |
DEFAULT_PENDING_MESSAGE_TTL |
The
pendingMessageTtl property default value. |
| Constructor | Description |
|---|---|
NettyTcpModbusServer(int port) |
Constructor.
|
NettyTcpModbusServer(int port,
ConcurrentMap<Integer,TcpModbusMessage> pendingMessages,
IntSupplier transactionIdSupplier) |
Constructor.
|
NettyTcpModbusServer(String bindAddress,
int port) |
Constructor.
|
NettyTcpModbusServer(String bindAddress,
int port,
ConcurrentMap<Integer,TcpModbusMessage> pendingMessages,
IntSupplier transactionIdSupplier) |
Constructor.
|
| Modifier and Type | Method | Description |
|---|---|---|
String |
getBindAddress() |
Get the address the server will listen on.
|
BiFunction<InetSocketAddress,Boolean,Boolean> |
getClientConnectionListener() |
Get an optional listener for client connection events.
|
BiConsumer<net.solarnetwork.io.modbus.ModbusMessage,Consumer<net.solarnetwork.io.modbus.ModbusMessage>> |
getMessageHandler() |
Get the message handler.
|
long |
getPendingMessageTtl() |
Get the pending Modbus message time-to-live expiration time.
|
int |
getPort() |
Get the port the server is listening to.
|
boolean |
isWireLogging() |
Get the "wire logging" setting.
|
void |
setClientConnectionListener(BiFunction<InetSocketAddress,Boolean,Boolean> clientConnectionListener) |
Set an optional listener for client connection events.
|
void |
setMessageHandler(BiConsumer<net.solarnetwork.io.modbus.ModbusMessage,Consumer<net.solarnetwork.io.modbus.ModbusMessage>> messageHandler) |
Set the message handler.
|
void |
setPendingMessageTtl(long pendingMessageTtl) |
Set the pending Modbus message time-to-live expiration time.
|
void |
setWireLogging(boolean wireLogging) |
Set the "wire logging" setting.
|
void |
start() |
Start the server.
|
void |
stop() |
Stop the server.
|
public static final long DEFAULT_PENDING_MESSAGE_TTL
pendingMessageTtl property default value.public static final String DEFAULT_BIND_ADDRESS
bindAddress property value.public NettyTcpModbusServer(int port)
Defaults to DEFAULT_BIND_ADDRESS.
port - the port to listen onIllegalArgumentException - if any argument is nullpublic NettyTcpModbusServer(String bindAddress, int port)
bindAddress - the address to listen onport - the port to listen onIllegalArgumentException - if any argument is nullpublic NettyTcpModbusServer(int port,
ConcurrentMap<Integer,TcpModbusMessage> pendingMessages,
IntSupplier transactionIdSupplier)
Defaults to DEFAULT_BIND_ADDRESS.
port - the port to listen onpendingMessages - a map to use for saving request messages, using transaction IDs
for keystransactionIdSupplier - the transaction ID supplierIllegalArgumentException - if any argument is nullpublic NettyTcpModbusServer(String bindAddress, int port, ConcurrentMap<Integer,TcpModbusMessage> pendingMessages, IntSupplier transactionIdSupplier)
bindAddress - the address to listen onport - the port to listen onpendingMessages - a map to use for saving request messages, using transaction IDs
for keystransactionIdSupplier - the transaction ID supplierIllegalArgumentException - if any argument is nullpublic void start()
throws IOException
Upon return the server will be bound and ready to accept connections on the configured port.
IOExceptionpublic void stop()
public String getBindAddress()
DEFAULT_BIND_ADDRESSpublic int getPort()
public BiConsumer<net.solarnetwork.io.modbus.ModbusMessage,Consumer<net.solarnetwork.io.modbus.ModbusMessage>> getMessageHandler()
public void setMessageHandler(BiConsumer<net.solarnetwork.io.modbus.ModbusMessage,Consumer<net.solarnetwork.io.modbus.ModbusMessage>> messageHandler)
This handler will be passed an inbound message along with another
Consumer for the reply message.
messageHandler - the handler to setpublic BiFunction<InetSocketAddress,Boolean,Boolean> getClientConnectionListener()
nullsetClientConnectionListener(BiFunction)public void setClientConnectionListener(BiFunction<InetSocketAddress,Boolean,Boolean> clientConnectionListener)
The client remote address is passed to the consumer as the first
argument. When a client connects, true will be passed as the
second argument; when a client disconnects, false will be passed.
The return argument is inspected only after a connection event. If
false is returned, the client connection will be closed. This
provides a way to deny a client connection.
clientConnectionListener - the client connection listener, or nullpublic boolean isWireLogging()
public void setWireLogging(boolean wireLogging)
wireLogging - true to enable wire-level logging of all messagespublic long getPendingMessageTtl()
DEFAULT_PENDING_MESSAGE_TTLpublic void setPendingMessageTtl(long pendingMessageTtl)
This timeout represents the minimum amount of time the client will wait for a Modbus message response, before it qualifies for removal from the pending message queue.
pendingMessageTtl - the pending Modbus message time-to-live, in milliseconds