NodeIDType
- MessageType
- This class exists primarily as a wrapper around NIOTransport to
support messages. NIOTransport is for general-purpose NIO byte
stream communication between numbered nodes as specified by the
NodeConfig interface and a data processing worker as specified by
the DataProcessingWorker interface that handles a byte stream.
This class provides the abstraction of messages and a
corresponding PacketDemultiplexer that handles messages instead of
a continuous byte stream.
MessageType can be any type whose toString() method results in a
meaningful serialization of a MessageType instance, i.e., the
packet demultiplexer on the other end must be able to reconstruct
the MessageType object from the String. The one exception is
byte[] that is supported here. However, MessageType can not extend
Object[] or be any primitive array other than byte[]. If byte[] is
MessageType, the corresponding
AbstractPacketDemultiplexer.getMessage(byte[]) method should
return a String decoded from the byte[] assuming ISO-8859-1
encoding.
This class short-circuits local sends by directly sending it to
the packet demultiplexer.public class MessageNIOTransport<NodeIDType,MessageType> extends NIOTransport<NodeIDType> implements InterfaceNIOTransport<NodeIDType,MessageType>
NIOTransport.AlternatingByteBuffer
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
NIO_CHARSET_ENCODING
We send even byte arrays encoded as strings because it is easier to do
demultiplexing (in JSONMesageExtractor) working with strings than byte
arrays.
|
static java.lang.String |
RCVR_ADDRESS_FIELD
JSON key corresponding to receiver socket address.
|
static java.lang.String |
RCVR_IP_FIELD
Deprecated.
|
static java.lang.String |
RCVR_PORT_FIELD
Deprecated.
|
static java.lang.String |
SNDR_ADDRESS_FIELD
JSON key corresponding to sender socket address.
|
static java.lang.String |
SNDR_IP_FIELD
Deprecated.
|
static java.lang.String |
SNDR_PORT_FIELD
Deprecated.
|
DEFAULT_SSL_MODE, MAX_PAYLOAD_SIZE, MAX_QUEUED_SENDS, MIN_INTER_CONNECT_TIME, myID, nodeConfig, worker, WRITE_BUFFER_SIZE
Constructor and Description |
---|
MessageNIOTransport(java.net.InetAddress address,
int port,
AbstractPacketDemultiplexer<?> pd,
SSLDataProcessingWorker.SSL_MODES sslMode) |
MessageNIOTransport(java.net.InetSocketAddress isa,
NodeConfig<NodeIDType> nodeConfig,
SSLDataProcessingWorker.SSL_MODES sslMode) |
MessageNIOTransport(NodeIDType id,
NodeConfig<NodeIDType> nodeConfig)
Initiates transporter with id and nodeConfig.
|
MessageNIOTransport(NodeIDType id,
NodeConfig<NodeIDType> nodeConfig,
AbstractPacketDemultiplexer<?> pd,
boolean start) |
MessageNIOTransport(NodeIDType id,
NodeConfig<NodeIDType> nodeConfig,
AbstractPacketDemultiplexer<?> pd,
boolean start,
SSLDataProcessingWorker.SSL_MODES sslMode) |
MessageNIOTransport(NodeIDType id,
NodeConfig<NodeIDType> nodeConfig,
SSLDataProcessingWorker.SSL_MODES sslMode) |
Modifier and Type | Method and Description |
---|---|
void |
addPacketDemultiplexer(AbstractPacketDemultiplexer<?> pd) |
NodeIDType |
getMyID() |
NodeConfig<NodeIDType> |
getNodeConfig() |
static java.net.InetSocketAddress |
getReceiverAddress(byte[] bytes) |
static java.net.InetSocketAddress |
getReceiverAddress(org.json.JSONObject json) |
static java.net.InetSocketAddress |
getReceiverAddressJSONSmart(net.minidev.json.JSONObject json) |
static java.net.InetSocketAddress |
getSenderAddress(byte[] bytes) |
static java.net.InetSocketAddress |
getSenderAddress(org.json.JSONObject json) |
static java.lang.String |
getSenderAddressAsString(org.json.JSONObject json) |
static java.net.InetSocketAddress |
getSenderAddressJSONSmart(net.minidev.json.JSONObject json) |
static java.net.InetAddress |
getSenderInetAddress(org.json.JSONObject json) |
static void |
main(java.lang.String[] args) |
void |
precedePacketDemultiplexer(AbstractPacketDemultiplexer<?> pd) |
int |
sendToAddress(java.net.InetSocketAddress isa,
byte[] msg) |
int |
sendToAddress(java.net.InetSocketAddress isa,
MessageType msg)
Send a JSON packet to an inet socket address (ip and port).
|
int |
sendToID(NodeIDType id,
byte[] msg) |
int |
sendToID(NodeIDType id,
MessageType msg)
Send a JSON packet to a node id.
|
protected int |
sendToIDInternal(NodeIDType destID,
MessageType msg) |
MessageNIOTransport<NodeIDType,MessageType> |
setName(java.lang.String name) |
void |
stop()
To close NIOTransport instances gracefully.
|
cleanup, getCompression, getCompressionThreshold, getListeningAddress, getListeningPort, getListeningSocketAddress, getLogger, getNodeAddress, getNodePort, getPayloadLength, getPendingSize, getSSLMode, handshakeComplete, isDisconnected, isStarted, isStopped, outOfRange, run, send, send, send, setCompression, setCompressionThreshold, setMaxQueuedSends, setMinInterConnectTime, setUseSenderTask, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getListeningSocketAddress, getSSLMode, isDisconnected, isStopped
@Deprecated public static final java.lang.String SNDR_IP_FIELD
MessageType
is JSONObject. These can be optimized to have just a
single short key, e.g., "SA" for sender socket address, but it doesn't
really matter because all four fields below are stamped only at the
receiver, so they don't induce more network traffic or (de-)serialization
overhead.@Deprecated public static final java.lang.String SNDR_PORT_FIELD
MessageType
is JSONObject.public static final java.lang.String SNDR_ADDRESS_FIELD
MessageType
is JSONObject.@Deprecated public static final java.lang.String RCVR_IP_FIELD
MessageType
is JSONObject.@Deprecated public static final java.lang.String RCVR_PORT_FIELD
MessageType
is JSONObject.public static final java.lang.String RCVR_ADDRESS_FIELD
MessageType
is JSONObject.public static final java.lang.String NIO_CHARSET_ENCODING
public MessageNIOTransport(NodeIDType id, NodeConfig<NodeIDType> nodeConfig) throws java.io.IOException
id
- My node ID.nodeConfig
- A map from all nodes' IDs to their respective socket
addresses.java.io.IOException
public MessageNIOTransport(NodeIDType id, NodeConfig<NodeIDType> nodeConfig, SSLDataProcessingWorker.SSL_MODES sslMode) throws java.io.IOException
id
- nodeConfig
- sslMode
- java.io.IOException
public MessageNIOTransport(java.net.InetSocketAddress isa, NodeConfig<NodeIDType> nodeConfig, SSLDataProcessingWorker.SSL_MODES sslMode) throws java.io.IOException
isa
- nodeConfig
- sslMode
- java.io.IOException
public MessageNIOTransport(NodeIDType id, NodeConfig<NodeIDType> nodeConfig, AbstractPacketDemultiplexer<?> pd, boolean start) throws java.io.IOException
id
- My node ID.nodeConfig
- A map from all nodes' IDs to their respective socket
addresses.pd
- The packet demultiplexer to handle received messages.start
- If a server thread must be automatically started upon
construction. If false, the caller must explicitly invoke (new
Thread(JSONNIOTransport)).start() to start the server.java.io.IOException
public MessageNIOTransport(NodeIDType id, NodeConfig<NodeIDType> nodeConfig, AbstractPacketDemultiplexer<?> pd, boolean start, SSLDataProcessingWorker.SSL_MODES sslMode) throws java.io.IOException
id
- nodeConfig
- pd
- start
- sslMode
- To enable SSL.java.io.IOException
public MessageNIOTransport(java.net.InetAddress address, int port, AbstractPacketDemultiplexer<?> pd, SSLDataProcessingWorker.SSL_MODES sslMode) throws java.io.IOException
address
- port
- pd
- sslMode
- java.io.IOException
public void addPacketDemultiplexer(AbstractPacketDemultiplexer<?> pd)
addPacketDemultiplexer
in interface InterfaceNIOTransport<NodeIDType,MessageType>
pd
- The demultiplxer to add at the end of the current chain.public void precedePacketDemultiplexer(AbstractPacketDemultiplexer<?> pd)
precedePacketDemultiplexer
in interface InterfaceNIOTransport<NodeIDType,MessageType>
pd
- The demultiplxer to add before the current chain.public NodeIDType getMyID()
getMyID
in interface InterfaceNIOTransport<NodeIDType,MessageType>
public void stop()
NIOTransport
stop
in interface InterfaceNIOTransport<NodeIDType,MessageType>
stop
in class NIOTransport<NodeIDType>
public int sendToID(NodeIDType id, MessageType msg) throws java.io.IOException
sendToID
in interface InterfaceNIOTransport<NodeIDType,MessageType>
id
- msg
- JSONMessenger.sendToID(Object, JSONObject)
.java.io.IOException
public int sendToAddress(java.net.InetSocketAddress isa, MessageType msg) throws java.io.IOException
sendToAddress
in interface AddressMessenger<MessageType>
sendToAddress
in interface InterfaceNIOTransport<NodeIDType,MessageType>
isa
- msg
- JSONMessenger.sendToID(Object, JSONObject)
.java.io.IOException
public static final java.net.InetSocketAddress getSenderAddress(org.json.JSONObject json)
json
- public static final java.lang.String getSenderAddressAsString(org.json.JSONObject json)
json
- public static final java.net.InetSocketAddress getSenderAddress(byte[] bytes) throws java.net.UnknownHostException
bytes
- java.net.UnknownHostException
public static final java.net.InetSocketAddress getReceiverAddress(byte[] bytes) throws java.net.UnknownHostException
bytes
- java.net.UnknownHostException
public static final java.net.InetSocketAddress getSenderAddressJSONSmart(net.minidev.json.JSONObject json)
json
- public static final java.net.InetSocketAddress getReceiverAddress(org.json.JSONObject json)
json
- public static final java.net.InetSocketAddress getReceiverAddressJSONSmart(net.minidev.json.JSONObject json)
json
- getReceiverAddress(JSONObject)
public static final java.net.InetAddress getSenderInetAddress(org.json.JSONObject json) throws org.json.JSONException
json
- org.json.JSONException
protected int sendToIDInternal(NodeIDType destID, MessageType msg) throws java.io.IOException
java.io.IOException
public static void main(java.lang.String[] args)
public NodeConfig<NodeIDType> getNodeConfig()
getNodeConfig
in interface InterfaceNIOTransport<NodeIDType,MessageType>
public int sendToID(NodeIDType id, byte[] msg) throws java.io.IOException
sendToID
in interface InterfaceNIOTransport<NodeIDType,MessageType>
java.io.IOException
public int sendToAddress(java.net.InetSocketAddress isa, byte[] msg) throws java.io.IOException
sendToAddress
in interface AddressMessenger<MessageType>
sendToAddress
in interface InterfaceNIOTransport<NodeIDType,MessageType>
java.io.IOException
public MessageNIOTransport<NodeIDType,MessageType> setName(java.lang.String name)
setName
in class NIOTransport<NodeIDType>
this