eneter.messaging.messagingsystems.connectionprotocols
Class EasyProtocolFormatter

java.lang.Object
  extended by eneter.messaging.messagingsystems.connectionprotocols.EasyProtocolFormatter
All Implemented Interfaces:
IProtocolFormatter

public class EasyProtocolFormatter
extends java.lang.Object
implements IProtocolFormatter

Simple and very fast encoding/decoding for TCP, WebSocket and multicast/broadcast UDP. This protocol formatter is stripped to the bare minimum. It does not encode OpenConnection and CloseConnection messages but it encodes only data messages.
Therefore it can be used only with communication protocols which have own mechanisms to open and close the communication (TCP and WebSockets) or opening and closing the connection is not needed (multicast/broadcast UDP).

The simplicity of this formatter provides a high performance and easy portability to various platforms allowing so to communicate with Eneter even without having the Eneter framework.
Here is the list of limitation when using this protocol formatter:


Here is how this formatter encodes messages between channels: Open connection message:
N.A. - the open connection message is not used. The connection is considered open when the socket is open.

Close connection message:
N.A. = the close connection message is not used. The connection is considered closed then the socket is closed.

Data message:
1 byte - type of data: 10 string in UTF8, 40 bytes
4 bytes - length: 32 bit integer indicating the size (in bytes) of message data.
x bytes - message data

The 32 bit integer indicating the length of message data is encoded as little endian byte default. If big endian is needed it is possible to specify it in the constructor.

The following example shows how to use TCP messaging with EasyProtocolFormatter:
 // Instantiate protocol formatter. 
 IProtocolFormatter aFormatter = new EasyProtocolFormatter();
 
 // Provide the protocol formatter into the messaging constructor.
 // Note: It can be only TCP or WebSocket messaging.
 IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory(aFormatter);
 ...
 // Then use can use the messaging to create channels.
 IDuplexOutputChannel anOutputChannel = aMessaging.CreateDuplexOutputChannel("tcp://127.0.0.1:8084/");
 ...
 
 


Constructor Summary
EasyProtocolFormatter()
          Constructs the protocol formatter with default little endian encoding.
EasyProtocolFormatter(boolean isLittleEndian)
          Constructs the protocol formatter with specified endianess.
 
Method Summary
 ProtocolMessage decodeMessage(java.io.InputStream readStream)
          Decodes message from the stream.
 ProtocolMessage decodeMessage(java.lang.Object readMessage)
          Decodes message from the given object.
 java.lang.Object encodeCloseConnectionMessage(java.lang.String responseReceiverId)
          Returns null.
 void encodeCloseConnectionMessage(java.lang.String responseReceiverId, java.io.OutputStream outputSream)
          Does nothing.
 java.lang.Object encodeMessage(java.lang.String responseReceiverId, java.lang.Object message)
          Encodes the data message.
 void encodeMessage(java.lang.String responseReceiverId, java.lang.Object message, java.io.OutputStream outputSream)
          Encodes the data message into the stream.
 java.lang.Object encodeOpenConnectionMessage(java.lang.String responseReceiverId)
          Returns null.
 void encodeOpenConnectionMessage(java.lang.String responseReceiverId, java.io.OutputStream outputSream)
          Does nothing.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EasyProtocolFormatter

public EasyProtocolFormatter()
Constructs the protocol formatter with default little endian encoding.


EasyProtocolFormatter

public EasyProtocolFormatter(boolean isLittleEndian)
Constructs the protocol formatter with specified endianess.

Parameters:
isLittleEndian - true - little endian, false - big endian.
Method Detail

encodeOpenConnectionMessage

public java.lang.Object encodeOpenConnectionMessage(java.lang.String responseReceiverId)
                                             throws java.lang.Exception
Returns null. The open connection message is not used. Therefore it returns null.

Specified by:
encodeOpenConnectionMessage in interface IProtocolFormatter
Parameters:
responseReceiverId - id of the client opening the connection.
Returns:
encoded open connection message.
Throws:
java.lang.Exception

encodeOpenConnectionMessage

public void encodeOpenConnectionMessage(java.lang.String responseReceiverId,
                                        java.io.OutputStream outputSream)
                                 throws java.lang.Exception
Does nothing. The open connection message is not used. Therefore it does not write any data to the provided output stream.

Specified by:
encodeOpenConnectionMessage in interface IProtocolFormatter
Parameters:
responseReceiverId - id of the client opening the connection.
outputSream - output where the encoded open connection message is written
Throws:
java.lang.Exception

encodeCloseConnectionMessage

public java.lang.Object encodeCloseConnectionMessage(java.lang.String responseReceiverId)
                                              throws java.lang.Exception
Returns null. The close connection message is not used. Therefore it returns null.

Specified by:
encodeCloseConnectionMessage in interface IProtocolFormatter
Parameters:
responseReceiverId - id of the client that wants to disconnect or that will be disconnected
Returns:
encoded close connection message
Throws:
java.lang.Exception

encodeCloseConnectionMessage

public void encodeCloseConnectionMessage(java.lang.String responseReceiverId,
                                         java.io.OutputStream outputSream)
                                  throws java.lang.Exception
Does nothing. The close connection message is not used. Therefore it does not write any data to the provided output stream.

Specified by:
encodeCloseConnectionMessage in interface IProtocolFormatter
Parameters:
responseReceiverId - id of the client that wants to disconnect or that will be disconnected
outputSream - output where the encoded close connection message is written
Throws:
java.lang.Exception

encodeMessage

public java.lang.Object encodeMessage(java.lang.String responseReceiverId,
                                      java.lang.Object message)
                               throws java.lang.Exception
Description copied from interface: IProtocolFormatter
Encodes the data message. The message is used by the output as well as input channel to send the data message.

Specified by:
encodeMessage in interface IProtocolFormatter
Parameters:
responseReceiverId - client id.
message - serialized message to be sent.
Returns:
encoded data message
Throws:
java.lang.Exception

encodeMessage

public void encodeMessage(java.lang.String responseReceiverId,
                          java.lang.Object message,
                          java.io.OutputStream outputSream)
                   throws java.lang.Exception
Description copied from interface: IProtocolFormatter
Encodes the data message into the stream. The message is used by the output as well as input channel to send the data message.

Specified by:
encodeMessage in interface IProtocolFormatter
Parameters:
responseReceiverId - id of the client that wants to send the message.
message - serialized message to be sent.
outputSream - output where the encoded message is written
Throws:
java.lang.Exception

decodeMessage

public ProtocolMessage decodeMessage(java.lang.Object readMessage)
Description copied from interface: IProtocolFormatter
Decodes message from the given object.

Specified by:
decodeMessage in interface IProtocolFormatter
Parameters:
readMessage - reference to the object.
Returns:
decoded message

decodeMessage

public ProtocolMessage decodeMessage(java.io.InputStream readStream)
Description copied from interface: IProtocolFormatter
Decodes message from the stream.

Specified by:
decodeMessage in interface IProtocolFormatter
Parameters:
readStream - stream to be read
Returns:
decoded message