org.softamis.net.exchange.udp
Class UDPCommunicationHelper<K extends java.io.Serializable,V extends java.io.Serializable>

java.lang.Object
  extended by org.softamis.net.exchange.support.AbstractCommunicationHelper<byte[],K,V>
      extended by org.softamis.net.exchange.udp.UDPCommunicationHelper<K,V>
Type Parameters:
K - type of keys used to identify registry items
V - type of values stored in the registry
All Implemented Interfaces:
CommunicationHelper<K,V>, MulticastListener

public class UDPCommunicationHelper<K extends java.io.Serializable,V extends java.io.Serializable>
extends AbstractCommunicationHelper<byte[],K,V>
implements MulticastListener

Implementation of CommunicationHelper that uses UDP multicast as underlying trasport mechanizm of network communications.

Author:
Andrew Sazonov
See Also:
RegistryEventProcessor

Field Summary
static java.lang.String DEFAULT_GROUP_NAME
          Default multicast group.
static int DEFAULT_PORT
          Default port used for multicasting
protected  int fDefaultPort
          Default port for datagram packets.
protected  int fDefaultTimeToLive
          Default time to live for datagram packets.
protected static org.apache.commons.logging.Log fLog
           
protected  Multicaster fMulticaster
          Multicaster that is used to perfom UDP packets sending and receiving
protected  boolean fOwnMulticaster
          Internal flag that indicates whether multicaster was not assigned outside.
 
Fields inherited from class org.softamis.net.exchange.support.AbstractCommunicationHelper
fCommunicationProtocol, fDefaultGroupName, fDefaultMessageSignature, fOwnCommunicationProtocol, fRegistryEventsMulticaster
 
Fields inherited from interface org.softamis.net.exchange.CommunicationHelper
COMMAND_ITEM_INVALID, COMMAND_ITEM_REGISTERED, COMMAND_ITEM_REQUEST, COMMAND_ITEM_UNREGISTERED
 
Constructor Summary
UDPCommunicationHelper()
           
 
Method Summary
 void close()
          Closing helper and multicaster (if one was not set from outside)
 void dataReceived(MulticastDataEvent aEvent)
          Listens for incoming multicaster' events and calls AbstractCommunicationHelper.receiveData(D) for futher processing
 int getDefaultPort()
          Returns default port for datagram packets.
 int getDefaultTimeToLive()
          Returns default time to live for datagram packets.
 Multicaster getMulticaster()
          Returns multicaster that is used to perfom UDP packets sending and receiving
 void init()
          Method used for initialization of communication helper.
protected  void initMulticaster()
           
protected  void insureCommunicationProtocolAndMulticasterSet()
           
protected  void processUnexpectedMessage(byte aMessageType, ItemEntry<K,V> aEntry)
          Method that is used to handle messages with unexpected type.
protected  void sendData(byte[] aDataToSend)
          Sends newtorking notification with provided data by delegating data provided to multicaster.
 void setDefaultMessageSignature(java.lang.String aDefaultMessageSignature)
          Sets signature that will be set to CommunicationProtocol if one is created (and not specified explicitely)
 void setDefaultPort(int aDefaultPort)
          Sets default port for datagram packets.
 void setDefaultTimeToLive(int aDefaultTimeToLive)
          Sets default time to live for datagram packets.
 void setMulticaster(Multicaster aMulticaster)
          Sets multicaster that is used to perfom UDP packets sending and receiving.
 
Methods inherited from class org.softamis.net.exchange.support.AbstractCommunicationHelper
addRegistryEventProcessor, createEntryToReceiveData, dispatchCommunicationMessage, doSendItemInvalidNotification, doSendItemRegisteredNotification, doSendItemsRequestNotification, doSendItemUnRegisteredNotification, getCommunicationProtocol, getDefaultGroupName, getDefaultMessageSignature, processReceivedData, processServiceInvalid, processServiceRegistered, processServiceRequest, processServiceUnRegistered, receiveData, removeRegistryEventProcessor, sendItemInvalidNotification, sendItemRegisteredNotification, sendItemsRequestNotification, sendItemUnRegisteredNotification, setCommunicationProtocol, setDefaultGroupName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

fLog

protected static final org.apache.commons.logging.Log fLog

fMulticaster

protected Multicaster fMulticaster
Multicaster that is used to perfom UDP packets sending and receiving


fDefaultTimeToLive

protected int fDefaultTimeToLive
Default time to live for datagram packets. Used for multicaster creation if one is not specified.


fDefaultPort

protected int fDefaultPort
Default port for datagram packets. Used for multicaster creation if one is not specified.


fOwnMulticaster

protected boolean fOwnMulticaster
Internal flag that indicates whether multicaster was not assigned outside.


DEFAULT_GROUP_NAME

public static final java.lang.String DEFAULT_GROUP_NAME
Default multicast group.

See Also:
Constant Field Values

DEFAULT_PORT

public static final int DEFAULT_PORT
Default port used for multicasting

See Also:
Constant Field Values
Constructor Detail

UDPCommunicationHelper

public UDPCommunicationHelper()
Method Detail

insureCommunicationProtocolAndMulticasterSet

protected void insureCommunicationProtocolAndMulticasterSet()
                                                     throws java.net.UnknownHostException
Throws:
java.net.UnknownHostException

setDefaultMessageSignature

public void setDefaultMessageSignature(java.lang.String aDefaultMessageSignature)
Sets signature that will be set to CommunicationProtocol if one is created (and not specified explicitely)

Specified by:
setDefaultMessageSignature in interface CommunicationHelper<K extends java.io.Serializable,V extends java.io.Serializable>
Overrides:
setDefaultMessageSignature in class AbstractCommunicationHelper<byte[],K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aDefaultMessageSignature - signature that will be set to CommunicationProtocol if one is created (and not specified explicitely)

init

public void init()
          throws java.io.IOException
Method used for initialization of communication helper. This method SHOULD be called before actual usage of this class. If multicaster is not specified from outside, default multicaster will be created.

Specified by:
init in interface CommunicationHelper<K extends java.io.Serializable,V extends java.io.Serializable>
Throws:
java.io.IOException - if

initMulticaster

protected void initMulticaster()
                        throws java.io.IOException
Throws:
java.io.IOException

close

public void close()
Closing helper and multicaster (if one was not set from outside)

Specified by:
close in interface CommunicationHelper<K extends java.io.Serializable,V extends java.io.Serializable>

processUnexpectedMessage

protected void processUnexpectedMessage(byte aMessageType,
                                        ItemEntry<K,V> aEntry)
Description copied from class: AbstractCommunicationHelper
Method that is used to handle messages with unexpected type. Inherited classes may implement it to add processing of additional commands.

Specified by:
processUnexpectedMessage in class AbstractCommunicationHelper<byte[],K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aMessageType - type of message
aEntry - entry obtained with message

sendData

protected void sendData(byte[] aDataToSend)
Sends newtorking notification with provided data by delegating data provided to multicaster.

Specified by:
sendData in class AbstractCommunicationHelper<byte[],K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aDataToSend - data which should be sent

dataReceived

public void dataReceived(MulticastDataEvent aEvent)
Listens for incoming multicaster' events and calls AbstractCommunicationHelper.receiveData(D) for futher processing

Specified by:
dataReceived in interface MulticastListener
Parameters:
aEvent - event from multicaster

getMulticaster

public Multicaster getMulticaster()
Returns multicaster that is used to perfom UDP packets sending and receiving

Returns:
multicaster

setMulticaster

public void setMulticaster(Multicaster aMulticaster)
Sets multicaster that is used to perfom UDP packets sending and receiving. If multicaster is not set, default multicaster will be created.

Parameters:
aMulticaster - multicaster

setDefaultPort

public void setDefaultPort(int aDefaultPort)
Sets default port for datagram packets. Used for multicaster creation if one is not specified.

Parameters:
aDefaultPort - default port for datagram packets.

getDefaultTimeToLive

public int getDefaultTimeToLive()
Returns default time to live for datagram packets. Used for multicaster creation if one is not specified.

Returns:
default time to live for datagram packets.

setDefaultTimeToLive

public void setDefaultTimeToLive(int aDefaultTimeToLive)
Sets default time to live for datagram packets. Used for multicaster creation if one is not specified.

Parameters:
aDefaultTimeToLive - default time to live for datagram packets.

getDefaultPort

public int getDefaultPort()
Returns default port for datagram packets. Used for multicaster creation if one is not specified.

Returns:
default port for datagram packets.