public class UdpMessagingSystemFactory extends java.lang.Object implements IMessagingSystemFactory
// Create UDP input channel.
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory();
IDuplexInputChannel anInputChannel = aMessaging.createDuplexInputChannel("udp://127.0.0.1:8043/");
// Subscribe for receiving messages.
anInputChannel.messageReceived().subscribe(myOnMessageReceived);
// Start listening.
anInputChannel.startListening();
...
// Stop listening.
anInputChannel.stopListening();
// Handling of messages.
private void onMessageReceived(object sender, DuplexChannelMessageEventArgs e)
{
// Handle incoming message.
....
// Send response message.
IDuplexInputChannel anInputChannel = (IDuplexInputChannel)sender;
anInputChannel.sendResponseMessage(e.ResponseReceiverId, "Hi");
}
private EventHandler<DuplexChannelMessageEventArgs> myOnMessageReceived = new EventHandler<DuplexChannelMessageEventArgs>()
{
@Override
public void onEvent(Object sender, DuplexChannelMessageEventArgs e)
{
onMessageReceived(sender, e);
}
};
Unicast output channel:
// Create UDP output channel.
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory();
IDuplexOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("udp://127.0.0.1:8043/");
// Subscribe to receive messages.
anOutputChannel.responseMessageReceived().subscribe(myOnResponseMessageReceived);
// Open the connection.
anOutputChannel.openConnection();
...
// Send a message.
anOutputChannel.sendMessage("Hello");
...
// Close connection.
anOutputChannel.closeConnection();
// Handling of received message.
private void onResponseMessageReceived(object sender, DuplexChannelMessageEventArgs e)
{
string aMessage = (string)e.Message;
....
}
private EventHandler<DuplexChannelMessageEventArgs> myOnResponseMessageReceived = new EventHandler<DuplexChannelMessageEventArgs>()
{
@Override
public void onEvent(Object sender, DuplexChannelMessageEventArgs e)
{
onResponseMessageReceived(sender, e);
}
};
// Create UDP input channel.
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory()
// The communication will be multicast or broadcast.
.setUnicastCommunication(false)
// The multicast group which shall be joined.
.setMulticastGroupToReceive("234.5.6.7");
// This input channel will be able to receive messages sent to udp://127.0.0.1:8043/ or
// to the multicast group udp://234.5.6.7:8043/.
IDuplexInputChannel anInputChannel = aMessaging.createDuplexInputChannel("udp://127.0.0.1:8043/")
// Subscribe for receiving messages.
anInputChannel.messageReceived().subscribe(myOnMessageReceived);
// Start listening.
anInputChannel.startListening();
...
// Stop listening.
anInputChannel.stopListening();
// Handling of messages.
private void onMessageReceived(object sender, DuplexChannelMessageEventArgs e)
{
// Handle incoming message.
....
// Send response message.
IDuplexInputChannel anInputChannel = (IDuplexInputChannel)sender;
anInputChannel.sendResponseMessage(e.ResponseReceiverId, "Hi");
}
private EventHandler<DuplexChannelMessageEventArgs> myOnMessageReceived = new EventHandler<DuplexChannelMessageEventArgs>()
{
@Override
public void onEvent(Object sender, DuplexChannelMessageEventArgs e)
{
onMessageReceived(sender, e);
}
};
Output channel sending messages to the multicast group.
// Create UDP output channel which will send messages to the multicast group udp://234.5.6.7:8043/.
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory()
// The communication will be multicast or broadcast.
.setUnicastCommunication(false);
IDuplexOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("udp://234.5.6.7:8043/");
// Subscribe to receive messages.
anOutputChannel.responseMessageReceived(myOnResponseMessageReceived);
// Open the connection.
anOutputChannel.openConnection();
...
// Send a message to all receivers which have joined
// the multicast group udp://234.5.6.7:8043/.
anOutputChannel.sendMessage("Hello");
...
// Close connection.
anOutputChannel.closeConnection();
// Handling of received message.
private void onResponseMessageReceived(object sender, DuplexChannelMessageEventArgs e)
{
string aMessage = (string)e.Message;
....
}
private EventHandler<DuplexChannelMessageEventArgs> myOnResponseMessageReceived = new EventHandler<DuplexChannelMessageEventArgs>()
{
@Override
public void onEvent(Object sender, DuplexChannelMessageEventArgs e)
{
onResponseMessageReceived(sender, e);
}
};
Constructor and Description |
---|
UdpMessagingSystemFactory()
Constructs the UDP messaging factory.
|
UdpMessagingSystemFactory(IProtocolFormatter protocolFromatter)
Constructs the UDP messaging factory.
|
Modifier and Type | Method and Description |
---|---|
IDuplexInputChannel |
createDuplexInputChannel(java.lang.String channelId)
Creates the duplex input channel which can receive and send messages to the duplex output channel using UDP.
|
IDuplexOutputChannel |
createDuplexOutputChannel(java.lang.String channelId)
Creates duplex output channel which can send and receive messages from the duplex input channel using UDP.
|
IDuplexOutputChannel |
createDuplexOutputChannel(java.lang.String channelId,
java.lang.String responseReceiverId)
Creates duplex output channel which can send and receive messages from the duplex input channel using UDP.
|
boolean |
getAllowSendingBroadcasts()
Gets whether sending of broadcasts is allowed.
|
IThreadDispatcherProvider |
getInputChannelThreading()
Gets threading mode used for input channels.
|
int |
getMaxAmountOfConnections()
Returns the maximum amount of connections the input channel can accept.
|
java.lang.String |
getMulticastGroupToReceive()
Gets the multicast group to receive messages from.
|
boolean |
getMulticastLoopback()
Returns whether the sender can receive the multicast message which sent in itself.
|
IThreadDispatcherProvider |
getOutputChannelThreading()
Gets threading mode used for output channels.
|
int |
getResponseReceiverPort()
Returns port number which shall be used for receiving response messages in unicast communication.
|
boolean |
getReuseAddress()
Gets the flag indicating whether the socket can be bound to the address which is already used.
|
int |
getTtl()
Gets time to live value for UDP datagrams.
|
boolean |
getUnicastCommunication()
Gets whether the communication is unicast.
|
UdpMessagingSystemFactory |
setAllowSendingBroadcasts(boolean allowBroadcasts)
Enables / disables sending broadcast messages.
|
UdpMessagingSystemFactory |
setInputChannelThreading(IThreadDispatcherProvider inputChannelThreading)
Sets threading mode for input channels.
|
UdpMessagingSystemFactory |
setMaxAmountOfConnections(int maxAmountOfConnections)
Sets the maximum amount of connections the inout channel can accept.
|
UdpMessagingSystemFactory |
setMulticastGroupToReceive(java.lang.String multicastGroup)
Sets the multicast group to receive messages from.
|
UdpMessagingSystemFactory |
setMulticastLoopback(boolean allowMulticastLoopback)
Enables /disables receiving multicast messages from the same IP address from which they were sent.
|
UdpMessagingSystemFactory |
setOutputChannelThreading(IThreadDispatcherProvider outputChannelThreading)
Sets threading mode for output channels.
|
UdpMessagingSystemFactory |
setResponseReceiverPort(int port)
Sets or gets the port which shall be used for receiving response messages by output channel in case of unicast communication.
|
UdpMessagingSystemFactory |
setReuseAddress(boolean allowReuseAddressFlag)
Sets the flag indicating whether the socket can be bound to the address which is already used.
|
UdpMessagingSystemFactory |
setTtl(int ttl)
Sets time to live value for UDP datagrams.
|
UdpMessagingSystemFactory |
setUnicastCommunication(boolean isUnicast)
Sets whether the communication is unicast.
|
public UdpMessagingSystemFactory()
public UdpMessagingSystemFactory(IProtocolFormatter protocolFromatter)
protocolFromatter
- formatter used for low-level messaging between output and input channelspublic IDuplexOutputChannel createDuplexOutputChannel(java.lang.String channelId) throws java.lang.Exception
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory();
IDuplexOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("udp://127.0.0.1:8765/");
Creating the duplex output channel for sending mulitcast messages (e.g. for streaming video to multiple receivers).
IProtocolFormatter aProtocolFormatter = new EasyProtocolFormatter();
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(aProtocolFormatter)
// Setup the factory to create channels for mulitcast or broadcast communication.
.setUnicastCommunication(false);
// Create output channel which will send messages to the mulitcast group 234.4.5.6 on the port 8765.
IDuplexOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("udp://234.4.5.6:8765/");
Creating the duplex output channel for sending broadcast messages.
IProtocolFormatter aProtocolFormatter = new EasyProtocolFormatter();
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(aProtocolFormatter)
// Setup the factory to create channels for mulitcast or broadcast communication.
.setUnicastCommunication(false)
// Setup the factory to create chennels which are allowed to send broadcast messages.
.setAllowSendingBroadcasts(true);
// Create output channel which will send broadcast messages to the port 8765.
IDuplexOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("udp://255.255.255.255:8765/");
createDuplexOutputChannel
in interface IMessagingSystemFactory
channelId
- UDP address in a valid URI format e.g. udp://127.0.0.1:8090/java.lang.Exception
public IDuplexOutputChannel createDuplexOutputChannel(java.lang.String channelId, java.lang.String responseReceiverId) throws java.lang.Exception
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory();
String aSessionId = UUID.randomUUID().toString();
IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("udp://127.0.0.1:8765/", aSessionId);
Creating the duplex output channel which can send messages to a particular UDP address and
which can recieve messages on a specific UDP address and which can receive mulitcast messages.
IProtocolFormatter aProtocolFormatter = new EasyProtocolFormatter();
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(aProtocolFormatter)
// Setup the factory to create channels for mulitcast or broadcast communication.
.setUnicastCommunication(false)
// Specify the mulitcast group to receive messages from.
.setMulticastGroupToReceive("234.4.5.6");
// Create output channel which can send messages to the input channel listening to udp://127.0.0.1:8095/
// and which is listening to udp://127.0.0.1:8099/ and which can also receive messages sent for the mulitcast
// group 234.4.5.6.
IDuplexOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("udp://127.0.0.1:8095/", "udp://127.0.0.1:8099/");
createDuplexOutputChannel
in interface IMessagingSystemFactory
channelId
- Identifies the receiving duplex input channel. The channel id must be a valid URI address e.g. udp://127.0.0.1:8090/responseReceiverId
- Unique identifier of the output channel.java.lang.Exception
public IDuplexInputChannel createDuplexInputChannel(java.lang.String channelId) throws java.lang.Exception
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory();
IDuplexInputChannel anInputChannel = aMessaging.createDuplexInputChannel("udp://127.0.0.1:8765/");
Creating the duplex input channel for multicast communication.
IProtocolFormatter aProtocolFormatter = new EasyProtocolFormatter();
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(aProtocolFormatter)
// Setup the factory to create channels for mulitcast or broadcast communication.
.setUnicastCommunication(false)
// Specify the mulitcast group to receive messages from.
.setMulticastGroupToReceive("234.4.5.6");
// Create duplex input channel which is listening to udp://127.0.0.1:8095/ and can also receive multicast messages
// sent to udp://234.4.5.6:8095/.
IDuplexInputChannel anInputChannel = aMessaging.createDuplexInputChannel("udp://127.0.0.1:8095/");
Sending mulitcast and broadcast messages from the duplex input channel.
IProtocolFormatter aProtocolFormatter = new EasyProtocolFormatter();
IMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(aProtocolFormatter)
// Setup the factory to create channels for mulitcast or broadcast communication.
.setUnicastCommunication(false)
// Setup the factory to create chennels which are allowed to send broadcast messages.
.setAllowSendingBroadcasts(true);
// Create duplex input channel which is listening to udp://127.0.0.1:8095/.
IDuplexInputChannel anInputChannel = aMessaging.createDuplexInputChannel("udp://127.0.0.1:8095/");
// Subscribe to handle messages.
anIputChannel.messageReceived().subscribe(myOnMessageReceived);
// Start listening.
anIputChannel.startListening();
...
// Send a multicast message.
// Note: it will be received by all output and input channels which have joined the multicast group 234.4.5.6
// and are listening to the port 8095.
anInputChannel.sendResponseMessage("udp://234.4.5.6:8095/", "Hello");
...
// Send a broadcast message.
// Note: it will be received by all output and input channels within the sub-network which are listening to the port 8095.
anInputChannel.sendResponseMessage("udp://255.255.255.255:8095/", "Hello");
...
// Stop listening.
anInputChannel.stopListening();
createDuplexInputChannel
in interface IMessagingSystemFactory
channelId
- Identifies this duplex input channel. The channel id must be a valid URI address (e.g. udp://127.0.0.1:8090/) the input channel will listen to.java.lang.Exception
public UdpMessagingSystemFactory setUnicastCommunication(boolean isUnicast)
isUnicast
- if true the communication is unicast. If false the communication is multicast or broadcast.public boolean getUnicastCommunication()
public UdpMessagingSystemFactory setTtl(int ttl)
ttl
- number of routers the datagram can travel.public int getTtl()
public UdpMessagingSystemFactory setMulticastGroupToReceive(java.lang.String multicastGroup)
// Create UDP messaging factory using simple protocol formatter.
IProtocolFormatter aProtocolFormatter = new EasyProtocolFormatter();
UdpMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(aProtocolFormatter)
// Setup messaging factory to create channels for mulitcast or broadcast communication.
.setUnicastCommunication(false)
// Set the multicast group which shall be joined for receiving messages.
.setMulticastGroupToReceive("234.5.6.7");
// Create input channel which will listen to udp://192.168.30.1:8043/ and which will also
// receive messages from the multicast group udp://234.5.6.7:8043/.
IInputChannel anInputChannel = aMessaging.createDuplexInputChannel("udp://192.168.30.1:8043/");
Creating output channel which can send multicast messages.
// Create UDP messaging factory using simple protocol formatter.
IProtocolFormatter aProtocolFormatter = new EasyProtocolFormatter();
UdpMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(aProtocolFormatter)
// Setup messaging factory to create channels for mulitcast or broadcast communication.
.setUnicastCommunication(false);
// Create output channel which can send messages to the multicast address udp://234.5.6.7:8043/.
IOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("udp://234.5.6.7:8043/");
multicastGroup
- multicast group which shall be joined e.g. "234.5.6.7"public java.lang.String getMulticastGroupToReceive()
public UdpMessagingSystemFactory setAllowSendingBroadcasts(boolean allowBroadcasts)
// Create UDP messaging factory using simple protocol formatter.
IProtocolFormatter aProtocolFormatter = new EasyProtocolFormatter();
UdpMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(aProtocolFormatter);
// Setup messaging factory to create channels for mulitcast or broadcast communication.
aMessaging.setUnicastCommunication(false);
// Enable sending broadcasts.
aMessaging.setAllowSendingBroadcasts(true);
// Create output channel which will send broadcast messages to all devices within the sub-network
// which listen to the port 8055.
IOutputChannel anOutputChannel = aMessaging.createDuplexOutputChannel("udp://255.255.255.255:8055/");
// Initialize output channel for sending broadcast messages and receiving responses.
anOutputChannel.openConnection();
// Send UDP broadcast.
anOutputChannel.sendMessage("Hello");
...
// Close channel - it will release listening thread.
anOutputChannel.closeConnection();
Input channel which can receive broadcast messages.
// Create UDP messaging factory using simple protocol formatter.
IProtocolFormatter aProtocolFormatter = new EasyProtocolFormatter();
UdpMessagingSystemFactory aMessaging = new UdpMessagingSystemFactory(aProtocolFormatter);
// Setup messaging factory to create channels for mulitcast or broadcast communication.
aMessaging.setUnicastCommunication(false);
// Create input channel which can receive broadcast messages to the port 8055.
IInputChannel anInputChannel = aMessaging.createDuplexInputChannel("udp://0.0.0.0:8055/");
// Subscribe to receive messages.
anInputChannel.messageReceived().subscribe(myOnMessageReceived);
// Start listening for messages.
anInputChannel.startListening();
...
// Stop listening.
anInputChannel.stopListening();
allowBroadcasts
- tru if sending of broadcasts is allowed.public boolean getAllowSendingBroadcasts()
public UdpMessagingSystemFactory setMulticastLoopback(boolean allowMulticastLoopback)
allowMulticastLoopback
- true if the message shall be delivered to sender too.public boolean getMulticastLoopback()
public UdpMessagingSystemFactory setResponseReceiverPort(int port)
port
- port number which shall be used for receiving response messages.public int getResponseReceiverPort()
public UdpMessagingSystemFactory setMaxAmountOfConnections(int maxAmountOfConnections)
maxAmountOfConnections
- public int getMaxAmountOfConnections()
public UdpMessagingSystemFactory setInputChannelThreading(IThreadDispatcherProvider inputChannelThreading)
inputChannelThreading
- threading modelpublic IThreadDispatcherProvider getInputChannelThreading()
public UdpMessagingSystemFactory setOutputChannelThreading(IThreadDispatcherProvider outputChannelThreading)
outputChannelThreading
- public IThreadDispatcherProvider getOutputChannelThreading()
public UdpMessagingSystemFactory setReuseAddress(boolean allowReuseAddressFlag)
allowReuseAddressFlag
- true if the socket can bound address and port which is already in use.public boolean getReuseAddress()