Package com.mdfromhtml.core
Class MDfromHTMLIPCMessage
- java.lang.Object
-
- com.mdfromhtml.core.MDfromHTMLIPCMessage
-
- All Implemented Interfaces:
IMDfromHTMLIPCMessage,Serializable
public class MDfromHTMLIPCMessage extends Object implements IMDfromHTMLIPCMessage, Serializable
Implementation of the MDfromHTML IPC Message to be sent (or received) across the MDfromHTML Interprocess Communications Infrastructure. Messages may carry JSONObject or byte[] content. Messages are identified by a topic (used for subscription), a type (used for identifying how a message should be parsed and interpreted), a create time, a sequence number where 0 implies the only message, 1 or greater implies a chunk of a larger message (whereInteger.MAX_VALUEacks as a semaphore to signal the last of a sequence), and a flag describing if the message content is a JSONObject or byte[].- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static intIPC_MAX_MESSAGE_SIZEBelow overridden by a MDfromHTMLIPC.properties value
-
Constructor Summary
Constructors Constructor Description MDfromHTMLIPCMessage(com.api.json.JSONObject message)MDfromHTMLIPCMessage(String publisherName, MDfromHTMLID sessionID, MDfromHTMLIPCApplications appl, String topic, String type, MDfromHTMLIPCVerbs verb, MDfromHTMLID userID, byte[] message)Constructor.MDfromHTMLIPCMessage(String publisherName, MDfromHTMLID sessionID, MDfromHTMLIPCApplications appl, String topic, String type, MDfromHTMLIPCVerbs verb, MDfromHTMLID userID, com.api.json.JSONObject message)Constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MDfromHTMLIPCMessagefromByteArray(byte[] bytes)Converts the message format sent across the wire into a MDfromHTMLIPCMessage objectMDfromHTMLIPCApplicationsgetApplication()intgetApplicationValue()static MDfromHTMLIPCMessagegetAssembledMessage(MDfromHTMLIPCMessage[] msgs)Reassembles an array of sequenced messages into a single messagestatic intgetIPCMaxMessageSize()static PropertiesgetMDfromHTMLIPCProps()com.api.json.JSONObjectgetMessage()byte[]getMessageBytes()protected intgetMessageSize()Retrieve the length (in bytes) of the message content.StringgetPublisherName()intgetSequence()MDfromHTMLIPCMessage[]getSequencedMessages(int chunkSize)Splits a single message into a sequence of smaller messages based on the chunkSize.MDfromHTMLIDgetSessionID()longgetTime()StringgetTopic()StringgetType()MDfromHTMLIDgetUserID()Retrieve the user identification associated with this messageMDfromHTMLIPCVerbsgetVerb()Retrieve the enumerated value associated with this messageintgetVerbValue()Retrieve the numeric identity of the verbbooleanisJSON()booleanisLastMessage()booleanisOnlyMessage()booleanisSequence()static voidmain(String[] args)Test rig for various methodsvoidsetMessage(com.api.json.JSONObject message)Updates the message when disassembling or reassembling a chunked messagestatic byte[]toByteArray(MDfromHTMLIPCMessage msg)Transforms a message into the byte array used for transmission on the wireStringtoHdrString()com.api.json.JSONObjecttoJson()StringtoString()StringtoStringFull()StringtoStringFull(boolean formatJSON)StringtoStringHeader()
-
-
-
Constructor Detail
-
MDfromHTMLIPCMessage
public MDfromHTMLIPCMessage(String publisherName, MDfromHTMLID sessionID, MDfromHTMLIPCApplications appl, String topic, String type, MDfromHTMLIPCVerbs verb, MDfromHTMLID userID, byte[] message)
Constructor.- Parameters:
publisherName- if null, theMDfromHTMLID.UNDEFINED_IDis used to create a unique namesessionID- if null, theMDfromHTMLID.UNDEFINED_IDis usedappl- the application id, if null, theMDfromHTMLIPCApplications.UNDEFINEDis usedtopic- if null, theMDfromHTMLConstants.IPC_DEFAULT_TOPICis usedtype- if null, theMDfromHTMLConstants.UNDEFINED_Stringis usedverb- the verb id, if null, theMDfromHTMLIPCVerbs.UNDEFINEDis useduserID- the user id within the session, if null, theMDfromHTMLID.UNDEFINED_IDis usedmessage- if null, an empty byte array is used
-
MDfromHTMLIPCMessage
public MDfromHTMLIPCMessage(String publisherName, MDfromHTMLID sessionID, MDfromHTMLIPCApplications appl, String topic, String type, MDfromHTMLIPCVerbs verb, MDfromHTMLID userID, com.api.json.JSONObject message)
Constructor.- Parameters:
publisherName- if null, theMDfromHTMLID.UNDEFINED_IDis used to create a unique namesessionID- if null, theMDfromHTMLID.UNDEFINED_IDis usedappl- the application id, if null, theMDfromHTMLIPCApplications.UNDEFINEDis usedtopic- if null, theMDfromHTMLConstants.IPC_DEFAULT_TOPICis usedtype- if null, theMDfromHTMLConstants.UNDEFINED_Stringis usedverb- the verb id, if null, theMDfromHTMLIPCVerbs.UNDEFINEDis useduserID- the user id within the session, if null, theMDfromHTMLID.UNDEFINED_IDis usedmessage- if null, an empty JSONObject is sent. If for some reason the object can not be serialized, rather than throwing an exception, we are using the toString() method that sends a non-JSON String. This should never happen as we are always using a valid JSON object so throwing the exception and forcing people to catch it everywhere didn't make any sense
-
-
Method Detail
-
getIPCMaxMessageSize
public static int getIPCMaxMessageSize()
-
fromByteArray
public static MDfromHTMLIPCMessage fromByteArray(byte[] bytes) throws Exception
Converts the message format sent across the wire into a MDfromHTMLIPCMessage object- Parameters:
bytes- byte array carrying the message- Returns:
- transformation of the passed byte array into a message
- Throws:
Exception- (Exception) if the bytes are null or not a valid message format (ExceptionParse) if the message content can not be transformed into a JSONObject
-
getAssembledMessage
public static MDfromHTMLIPCMessage getAssembledMessage(MDfromHTMLIPCMessage[] msgs) throws Exception
Reassembles an array of sequenced messages into a single message- Parameters:
msgs- array of message chunks to be reassembled into a single message- Returns:
- the single message built from the pieces passed
- Throws:
Exception- if the message array is null or incomplete (e.g., missing a sequence number)
-
main
public static void main(String[] args)
Test rig for various methods- Parameters:
args- No arguments are required, but if a positive integer between 1 and 99 is passed, it is used to split up the default 100 character message into chunks.
-
toByteArray
public static byte[] toByteArray(MDfromHTMLIPCMessage msg) throws Exception
Transforms a message into the byte array used for transmission on the wire- Parameters:
msg- the message to be transformed into a byte array- Returns:
- the byte array representing the content of the passed message
- Throws:
Exception- if the message is null
-
getMDfromHTMLIPCProps
public static Properties getMDfromHTMLIPCProps() throws Exception
- Returns:
- the properties file used to define MDfromHTML interprocess communications
- Throws:
Exception- See Also:
MDfromHTMLUtils.loadMDfromHTMLProperties(String),MDfromHTMLConstants.MDfromHTML_IPC_PropertiesFileName
-
getApplication
public MDfromHTMLIPCApplications getApplication()
- Specified by:
getApplicationin interfaceIMDfromHTMLIPCMessage- Returns:
- the enumerated value associated with this message
- See Also:
MDfromHTMLIPCVerbs
-
getApplicationValue
public int getApplicationValue()
- Specified by:
getApplicationValuein interfaceIMDfromHTMLIPCMessage- Returns:
- the numeric identity of the application
-
getMessage
public com.api.json.JSONObject getMessage() throws Exception- Specified by:
getMessagein interfaceIMDfromHTMLIPCMessage- Returns:
- the message content as a JSONObject
- Throws:
Exception
-
getMessageBytes
public byte[] getMessageBytes()
- Specified by:
getMessageBytesin interfaceIMDfromHTMLIPCMessage- Returns:
- the message content as a byte[]
-
getMessageSize
protected int getMessageSize()
Retrieve the length (in bytes) of the message content.- Returns:
- the length (in bytes) of the message content.
-
getPublisherName
public String getPublisherName()
- Specified by:
getPublisherNamein interfaceIMDfromHTMLIPCMessage- Returns:
- the publisher (creator) of this message
-
getSequence
public int getSequence()
- Specified by:
getSequencein interfaceIMDfromHTMLIPCMessage- Returns:
- the sequence number of this message. Note: zero implies the only message, non-zero implies a part of a chunked message, with 65768 signifying the last of the sequence of messages.
-
getSequencedMessages
public MDfromHTMLIPCMessage[] getSequencedMessages(int chunkSize) throws Exception
Splits a single message into a sequence of smaller messages based on the chunkSize. The original message's time is preserved, as is the isJSON status- Parameters:
chunkSize-- Returns:
- array of messages based on the passed chunkSize
- Throws:
Exception
-
getSessionID
public MDfromHTMLID getSessionID()
- Specified by:
getSessionIDin interfaceIMDfromHTMLIPCMessage- Returns:
- the session id related to the message content. If the content is
not associated with a particular session, the ID returned is
MDfromHTMLID.UNDEFINED_IDand corresponds with the String value inMDfromHTMLConstants.UNDEFINED_ID
-
getTime
public long getTime()
- Specified by:
getTimein interfaceIMDfromHTMLIPCMessage- Returns:
- the original message creation time (not necessarily the same as the time it was published)
-
getTopic
public String getTopic()
- Specified by:
getTopicin interfaceIMDfromHTMLIPCMessage- Returns:
- the message topic
-
getType
public String getType()
- Specified by:
getTypein interfaceIMDfromHTMLIPCMessage- Returns:
- the message type. If the original was undefined, the type will
return the
MDfromHTMLConstants.UNDEFINED_String
-
getVerb
public MDfromHTMLIPCVerbs getVerb()
Description copied from interface:IMDfromHTMLIPCMessageRetrieve the enumerated value associated with this message- Specified by:
getVerbin interfaceIMDfromHTMLIPCMessage- Returns:
- the enumerated value associated with this message
- See Also:
MDfromHTMLIPCVerbs
-
getUserID
public MDfromHTMLID getUserID()
Description copied from interface:IMDfromHTMLIPCMessageRetrieve the user identification associated with this message- Specified by:
getUserIDin interfaceIMDfromHTMLIPCMessage- Returns:
- the user identification associated with this message
-
getVerbValue
public int getVerbValue()
Description copied from interface:IMDfromHTMLIPCMessageRetrieve the numeric identity of the verb- Specified by:
getVerbValuein interfaceIMDfromHTMLIPCMessage- Returns:
- the numeric identity of the verb
-
isJSON
public boolean isJSON()
- Specified by:
isJSONin interfaceIMDfromHTMLIPCMessage- Returns:
- true if the original message was a String (as opposed to a byte[])
-
isLastMessage
public boolean isLastMessage()
- Specified by:
isLastMessagein interfaceIMDfromHTMLIPCMessage- Returns:
- true if the message sequence is
Integer.MAX_VALUEsignaling the end of a chunked sequence of messages
-
isOnlyMessage
public boolean isOnlyMessage()
- Specified by:
isOnlyMessagein interfaceIMDfromHTMLIPCMessage- Returns:
- true if the message sequence is zero signaling the message was not chunked
-
isSequence
public boolean isSequence()
- Specified by:
isSequencein interfaceIMDfromHTMLIPCMessage- Returns:
- true if the message sequence is not zero (for an "chunked" message).
-
setMessage
public void setMessage(com.api.json.JSONObject message)
Updates the message when disassembling or reassembling a chunked message- Parameters:
message- the payload of this message
-
toStringFull
public String toStringFull()
- Specified by:
toStringFullin interfaceIMDfromHTMLIPCMessage- Returns:
- formatted complete version of this message
- See Also:
that is more useful for debugging
-
toStringFull
public String toStringFull(boolean formatJSON)
- Specified by:
toStringFullin interfaceIMDfromHTMLIPCMessage- Parameters:
formatJSON- true if the message is to be formatted (if the message contains JSON), false if not.- Returns:
- formatted complete version of this message
- See Also:
that is more useful for debugging
-
toHdrString
public String toHdrString()
-
toStringHeader
public String toStringHeader()
-
toString
public String toString()
- Specified by:
toStringin interfaceIMDfromHTMLIPCMessage- Overrides:
toStringin classObject- Returns:
- an abbreviated formatted version of the message (contains only the first 100 bytes of the payload)
-
toJson
public com.api.json.JSONObject toJson()
-
-