Class MessageImpl

  • All Implemented Interfaces:
    Message, java.io.Serializable

    public class MessageImpl
    extends java.lang.Object
    implements Message
    This is a default Message's implementation The byte array or ByteBuffer which represent this message's low level data will be cached if this message is not modified Here are this message's structure ---- [packet] magic(4) + version(4) + type(4) + messages_length(4) + messages(message_length) [messages] message_count(4) + message_key1 + message_value1 + message_key2 + message_value2 + ...(message_count) ----
    Author:
    Bongjae Chang
    See Also:
    Serialized Form
    • Constructor Detail

      • MessageImpl

        public MessageImpl()
      • MessageImpl

        public MessageImpl​(int type)
      • MessageImpl

        public MessageImpl​(int type,
                           java.util.Map<java.lang.String,​java.io.Serializable> messages)
    • Method Detail

      • getMaxMessageLength

        public static int getMaxMessageLength()
      • setMaxMessageLength

        public static void setMaxMessageLength​(int maxMsgLength)
      • initialize

        public void initialize​(int type,
                               java.util.Map<java.lang.String,​java.io.Serializable> messages)
                        throws java.lang.IllegalArgumentException
        Initializes this message with initial type and key-value pair's map
        Specified by:
        initialize in interface Message
        Parameters:
        type - message type
        messages - key-value pair's message
        Throws:
        java.lang.IllegalArgumentException - if the argument is not valid
      • parseHeader

        public int parseHeader​(byte[] bytes,
                               int offset)
                        throws java.lang.IllegalArgumentException
        Parses the message's header from given byte array
        Specified by:
        parseHeader in interface Message
        Parameters:
        bytes - byte array which should be parsed
        offset - offset from which the message should be parsed
        Returns:
        the message's length(body length) which this message contains
        Throws:
        java.lang.IllegalArgumentException - if the argument is not valid or an unexpected error occurs
      • parseHeader

        public int parseHeader​(Buffer buffer,
                               int offset)
                        throws java.lang.IllegalArgumentException
        Parses the message's header from given ByteBuffer
        Specified by:
        parseHeader in interface Message
        Parameters:
        buffer - ByteBuffer which should be parsed
        offset - offset from which the message should be parsed
        Returns:
        the message's length(body length) which this message contains
        Throws:
        java.lang.IllegalArgumentException - if the argument is not valid or an unexpected error occurs
      • parseMessage

        public void parseMessage​(byte[] bytes,
                                 int offset,
                                 int length)
                          throws java.lang.IllegalArgumentException,
                                 MessageIOException
        Parses the message's body from given byte array
        Specified by:
        parseMessage in interface Message
        Parameters:
        bytes - byte array which should be parsed
        offset - offset from which the message should be parsed
        length - the message's length(body length)
        Throws:
        java.lang.IllegalArgumentException - if the argument is not valid or an unexpected error occurs
        MessageIOException - if an I/O error occurs
      • parseMessage

        public void parseMessage​(Buffer buffer,
                                 int offset,
                                 int length)
                          throws java.lang.IllegalArgumentException,
                                 MessageIOException
        Parses the message's body from given ByteBuffer
        Specified by:
        parseMessage in interface Message
        Parameters:
        buffer - ByteBuffer which should be parsed
        offset - offset from which the message should be parsed
        length - the message's length(body length)
        Throws:
        java.lang.IllegalArgumentException - if the argument is not valid or an unexpected error occurs
        MessageIOException - if an I/O error occurs
      • getVersion

        public int getVersion()
        Returns the message's version
        Specified by:
        getVersion in interface Message
        Returns:
        message version
      • getType

        public int getType()
        Returns the message's type i.g. TYPE_CLUSTER_MANAGER_MESSAGE or TYPE_HEALTH_MONITOR_MESSAGE's integer value or etc...
        Specified by:
        getType in interface Message
        Returns:
        message type
      • addMessageElement

        public java.lang.Object addMessageElement​(java.lang.String key,
                                                  java.io.Serializable value)
        Adds a special element to this message with key-value pair
        Specified by:
        addMessageElement in interface Message
        Parameters:
        key - key with which the specified value is to be associated
        value - serializable value to be associated with the specified key
        Returns:
        the previous value associated with key, or null if there was no mapping for key
      • getMessageElement

        public java.lang.Object getMessageElement​(java.lang.String key)
        Returns the value to which the specified key is mapped, or null if this message contains no mapping for the key
        Specified by:
        getMessageElement in interface Message
        Parameters:
        key - the key whose associated value is to be returned
        Returns:
        the value to which the specified key is mapped, or null if this message contains no mapping for the key
      • removeMessageElement

        public java.lang.Object removeMessageElement​(java.lang.String key)
        Removes the mapping for a key from this message if it is present
        Specified by:
        removeMessageElement in interface Message
        Parameters:
        key - key whose mapping is to be removed from the message
        Returns:
        the previous value associated with key, or null if there was no mapping for key
      • getMessageElements

        public java.util.Set<java.util.Map.Entry<java.lang.String,​java.io.Serializable>> getMessageElements()
        Returns a Set element view of the mappings contained in this message
        Specified by:
        getMessageElements in interface Message
        Returns:
        a set element view of the mappings contained in this message
      • getStringType

        public static java.lang.String getStringType​(int type)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object