net.zortrium.p2proto
Class Msg

java.lang.Object
  extended by net.zortrium.p2proto.Msg

public final class Msg
extends Object

The main class representing a message to be sent over the network. Every message contains a numeric type (defined by the user) and optionally arbitrary binary content. The suggested usage pattern is to encapsulate a user-defined protocol buffer message in the content, in which the encapsulated message class is indicated by the message type. The encapsulated message can then be extracted by checking the message type. See PeerListener for an example of this.

Author:
Sean Barker - www.zortrium.net

Constructor Summary
Msg(int type)
          Construct a new message with the given type and no content.
Msg(int type, byte[] content)
          Construct a new message with the given type and binary content.
Msg(int type, byte[] content, int offset, int size)
          Construct a new message with the given type and binary content (of the given size at the given offset).
Msg(int type, com.google.protobuf.ByteString content)
          Construct a new message with the given type and content.
Msg(int type, com.google.protobuf.Message.Builder content)
          Convenience constructor that builds the given protocol buffer message, then calls Msg(int, Message).
Msg(int type, com.google.protobuf.Message content)
          Construct a new message with the given type and the given protocol buffer message as content.
 
Method Summary
 boolean equals(Object obj)
          Two messages are equal if their type and content are identical.
 com.google.protobuf.ByteString getContent()
          Get the binary content of the message (or null if the message has no content).
 int getType()
          Get the type of the message.
 boolean hasContent()
          Check whether this message contains any content.
 int hashCode()
           
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Msg

public Msg(int type,
           com.google.protobuf.ByteString content)
Construct a new message with the given type and content. If the given content is null, this is equivalent to calling Msg(int). The given type must be nonnegative.

Parameters:
type - The type of the message.
content - The content of the message (or null).
Throws:
IllegalArgumentException - If the given type is negative.

Msg

public Msg(int type)
Construct a new message with the given type and no content. The type must be nonnegative.

Parameters:
type - The type of the message.
Throws:
IllegalArgumentException - If the given type is negative.

Msg

public Msg(int type,
           byte[] content)
Construct a new message with the given type and binary content. The type must be nonnegative.

Parameters:
type - The type of the message.
content - The content of the message.
Throws:
IllegalArgumentException - If the given type is negative.

Msg

public Msg(int type,
           byte[] content,
           int offset,
           int size)
Construct a new message with the given type and binary content (of the given size at the given offset). The type must be nonnegative.

Parameters:
type - The type of the message.
content - The content byte array.
offset - Offset into the content array.
size - Number of bytes to read in the content array.

Msg

public Msg(int type,
           com.google.protobuf.Message content)
Construct a new message with the given type and the given protocol buffer message as content. The type must be nonnegative.

Parameters:
type - The type of the message.
content - The content of the message.

Msg

public Msg(int type,
           com.google.protobuf.Message.Builder content)
Convenience constructor that builds the given protocol buffer message, then calls Msg(int, Message).

Parameters:
type - The type of the message.
content - The content of the message.
Method Detail

getType

public int getType()
Get the type of the message.

Returns:
The message type.

getContent

public com.google.protobuf.ByteString getContent()
Get the binary content of the message (or null if the message has no content). If your messages are encapsulating other protocol buffer messages, this content should be immediately passed to parseFrom in the appropriate protocol buffer class (as determined by the value of getType()).

Returns:
The message content or null.

hasContent

public boolean hasContent()
Check whether this message contains any content.

Returns:
False if the content is null and true otherwise.

toString

public String toString()
Overrides:
toString in class Object

equals

public boolean equals(Object obj)
Two messages are equal if their type and content are identical.

Overrides:
equals in class Object

hashCode

public int hashCode()
Overrides:
hashCode in class Object