|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objecteneter.messaging.messagingsystems.udpmessagingsystem.UdpMessagingSystemFactory
public class UdpMessagingSystemFactory
Messaging system delivering messages via UDP.
It creates the communication channels using UDP for sending and receiving messages.
The channel id must be a valid UDP URI address. E.g.: udp://127.0.0.1:6080/.
The messaging via UDP supports unicast, multicast and broadcast communication.
The unicast communication is the routing of messages from one sender to one receiver.
(E.g. a client-service communication where a client sends messages to one service and the service
can send response messages to one client.)
The multicast communication is the routing of messages from one sender to multiple receivers
(the receivers which joined the specific multicast group and listen to the specific port).
The broadcast communication is the routing of messages from one sender to all receivers within the sub-net which listen
to the specific port.
UDP unicast communication.
Unicast input channel:
// 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 Summary | |
---|---|
UdpMessagingSystemFactory()
Constructs the UDP messaging factory. |
|
UdpMessagingSystemFactory(IProtocolFormatter protocolFromatter)
Constructs the UDP messaging factory. |
Method Summary | |
---|---|
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. |
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 |
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. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public UdpMessagingSystemFactory()
public UdpMessagingSystemFactory(IProtocolFormatter protocolFromatter)
protocolFromatter
- formatter used for low-level messaging between output and input channelsMethod Detail |
---|
public 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 setInputChannelThreading(IThreadDispatcherProvider inputChannelThreading)
inputChannelThreading
- threading model
public 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()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |