org.softamis.net.exchange.support
Class AbstractCommunicationProtocol<D extends java.io.Serializable,K extends java.io.Serializable,V extends java.io.Serializable,A extends java.io.Serializable>

java.lang.Object
  extended by org.softamis.net.exchange.support.AbstractCommunicationProtocol<D,K,V,A>
Type Parameters:
D - type of low level data that are sent via network
K - type of keys used to identify registry items
V - type of values stored in the registry
All Implemented Interfaces:
CommunicationProtocol<D,K,V>
Direct Known Subclasses:
JGCommunicationProtocol, UDPCommunicationProtocol

public abstract class AbstractCommunicationProtocol<D extends java.io.Serializable,K extends java.io.Serializable,V extends java.io.Serializable,A extends java.io.Serializable>
extends java.lang.Object
implements CommunicationProtocol<D,K,V>

Abstract implementation of CommunicationProtocol that contains base methods used for transformation application level data to/from underlying network form

Author:
Andrew Sazonov
See Also:
CommunicationProtocol

Field Summary
static boolean DEFAULT_COMPRESSION_MODE
          Default value of compression mode
protected  java.lang.String fDatagramSignature
          Signature used for notifications
protected static org.apache.commons.logging.Log fLog
           
protected  boolean fUseCompression
          Indicates whether data that are sent via network should be compressed
static java.lang.String MSG_EXCEPTION_CLOSING_STREAM
           
 
Constructor Summary
protected AbstractCommunicationProtocol()
           
 
Method Summary
protected static void closeStream(java.io.InputStream aStream, java.lang.String aMessage)
          Utility method used to close InputStream
protected static void closeStream(java.io.OutputStream aStream, java.lang.String aMessage)
          Utility method used to close OutputStream
 D createDataToSend(byte aType, java.lang.String aTo, ItemEntry<K,V> aEntry)
          Converts information stored in ItemEntry into low level data which could be sent in network communication.
protected abstract  MessageContent<K,V,A> createMessageContent(byte aType, java.lang.String aTo, ItemEntry<K,V> aEntry)
          Creates MessageContent which will be converted into form used by underlying network communication based on provided parameters.
protected  MessageContent<K,V,A> createMessageContentToRead()
           
protected abstract  D doCreateDataToSend(MessageContent<K,V,A> aContent)
          Converts given MessageContent to form used by underlying network communication
protected  void fillEntryByMessageContent(MessageContent<K,V,A> aDatagram, ItemEntry<K,V> aEntry)
           
 byte fillItemEntry(D aData, ItemEntry<K,V> aEntry)
          Extracts low level data obtained from network notification and wraps them into provided ItemEntry.
protected  MessageContent<K,V,A> getDatagram(byte[] aData)
          Constructs MessageContent from given to array of bytes by using deserialization and, if specified by corresponding option, previous uncompression of byte stream.
protected abstract  MessageContent<K,V,A> getDatagramFromReceivedData(D aReceivedData)
          Constructs MessageContent from underlying network data.
 java.lang.String getDatagramSignature()
          Returns signature used for notifications
protected  byte[] getDataToSend(MessageContent<K,V,A> aDatagram)
          Converts given MessageContent to array of bytes by using serialization and, if specified by corresponding option, further compression.
protected abstract  boolean isNotOwnDatagaram(MessageContent<K,V,A> aDatagram)
          Determines whether notification was obtained from external source
 boolean isUseCompression()
          Returns indicates whether data that are sent via network should be compressed
 void setDatagramSignature(java.lang.String aDatagramSignature)
          Sets signature used for notifications
 void setUseCompression(boolean aUseCompression)
          Allows to specify whether data sent via network should be compressed
 
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

fDatagramSignature

protected java.lang.String fDatagramSignature
Signature used for notifications


fUseCompression

protected boolean fUseCompression
Indicates whether data that are sent via network should be compressed


DEFAULT_COMPRESSION_MODE

public static final boolean DEFAULT_COMPRESSION_MODE
Default value of compression mode

See Also:
Constant Field Values

MSG_EXCEPTION_CLOSING_STREAM

public static final java.lang.String MSG_EXCEPTION_CLOSING_STREAM
See Also:
Constant Field Values
Constructor Detail

AbstractCommunicationProtocol

protected AbstractCommunicationProtocol()
Method Detail

fillItemEntry

public byte fillItemEntry(D aData,
                          ItemEntry<K,V> aEntry)
Extracts low level data obtained from network notification and wraps them into provided ItemEntry.

Specified by:
fillItemEntry in interface CommunicationProtocol<D extends java.io.Serializable,K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aData - low level data received with external notification
aEntry - entry that will be filled based on received data
Returns:
type of command received with data.
See Also:
CommunicationHelper

fillEntryByMessageContent

protected void fillEntryByMessageContent(MessageContent<K,V,A> aDatagram,
                                         ItemEntry<K,V> aEntry)

createDataToSend

public D createDataToSend(byte aType,
                          java.lang.String aTo,
                          ItemEntry<K,V> aEntry)
Converts information stored in ItemEntry into low level data which could be sent in network communication.

Specified by:
createDataToSend in interface CommunicationProtocol<D extends java.io.Serializable,K extends java.io.Serializable,V extends java.io.Serializable>
Parameters:
aType - type of notification
aTo -
aEntry - entry to be converted
Returns:
low level data that are ready to be sent via network notification

createMessageContent

protected abstract MessageContent<K,V,A> createMessageContent(byte aType,
                                                              java.lang.String aTo,
                                                              ItemEntry<K,V> aEntry)
Creates MessageContent which will be converted into form used by underlying network communication based on provided parameters.

Parameters:
aType - type of notification
aTo - destination of notification
aEntry - Item data
Returns:
created message content

doCreateDataToSend

protected abstract D doCreateDataToSend(MessageContent<K,V,A> aContent)
Converts given MessageContent to form used by underlying network communication

Parameters:
aContent - given message content
Returns:
data in form ready to send via network by CommunicationHelper

isNotOwnDatagaram

protected abstract boolean isNotOwnDatagaram(MessageContent<K,V,A> aDatagram)
Determines whether notification was obtained from external source

Parameters:
aDatagram - data to receive
Returns:
true if data was obtained from external source

getDatagramFromReceivedData

protected abstract MessageContent<K,V,A> getDatagramFromReceivedData(D aReceivedData)
Constructs MessageContent from underlying network data.

Parameters:
aReceivedData - raw data obtained in network notification
Returns:
constructed content

getDatagram

protected MessageContent<K,V,A> getDatagram(byte[] aData)
Constructs MessageContent from given to array of bytes by using deserialization and, if specified by corresponding option, previous uncompression of byte stream.

Parameters:
aData - data to be converted
Returns:
constructed message content

createMessageContentToRead

protected MessageContent<K,V,A> createMessageContentToRead()

getDataToSend

protected byte[] getDataToSend(MessageContent<K,V,A> aDatagram)
Converts given MessageContent to array of bytes by using serialization and, if specified by corresponding option, further compression.

Parameters:
aDatagram - data to be converted
Returns:
serialized form of message content

isUseCompression

public boolean isUseCompression()
Returns indicates whether data that are sent via network should be compressed

Returns:
true if data sent via network should be compressed

setUseCompression

public void setUseCompression(boolean aUseCompression)
Allows to specify whether data sent via network should be compressed

Parameters:
aUseCompression - true if compression should be enabled

closeStream

protected static void closeStream(java.io.OutputStream aStream,
                                  java.lang.String aMessage)
Utility method used to close OutputStream

Parameters:
aStream - stream to close
aMessage - message to write to log (name of stream)

closeStream

protected static void closeStream(java.io.InputStream aStream,
                                  java.lang.String aMessage)
Utility method used to close InputStream

Parameters:
aStream - stream to close
aMessage - message to write to log (name of stream)

getDatagramSignature

public java.lang.String getDatagramSignature()
Returns signature used for notifications

Returns:
signature

setDatagramSignature

public void setDatagramSignature(java.lang.String aDatagramSignature)
Sets signature used for notifications

Parameters:
aDatagramSignature - signature used for notifications