Purple Code

com.purpletech.message
Class Message

java.lang.Object
  |
  +--com.purpletech.message.Message
All Implemented Interfaces:
Writeable

public class Message
extends java.lang.Object
implements Writeable

A Message is passed between peers

Version:
$Id: Message.java,v 1.2 2001/10/30 12:20:52 alex Exp $
Author:
Alex

Field Summary
static int ALL
          peer id number meaning "all clients"
protected  java.lang.String classname
           
protected  java.lang.Object data
          contents of message
protected  int from
          transient id for source of message
static int MAGIC
           
static int NONE
          peer id number meaning "no client"
protected  int to
          transient id for destination of message
protected  short type
          type of message (determines which class to use to deserialize it)
static short TYPE_BOOLEAN
          the data object contains a Boolean
static short TYPE_BYTE
          the data object contains a Byte
static short TYPE_BYTES
          the data object contains a byte array
static short TYPE_CHAR
          the data object contains a Character
static short TYPE_DOUBLE
          the data object contains a Double
static short TYPE_FLOAT
          the data object contains a Float
static short TYPE_HASHED
          the data object contains a Writeable, but the name of the writeable is encoded for efficiency (not yet implemented)
static short TYPE_INT
          the data object contains a Integer
static short TYPE_LONG
          the data object contains a Long
static short TYPE_SHORT
          the data object contains a Short
static short TYPE_STRING
          the data object contains a String
static short TYPE_SYSTEM
          the data object contains a SystemData object
static short TYPE_WRITEABLE
          the data object contains a Writeable
protected  long whenCreated
          msec since epoch when it was created
 
Constructor Summary
  Message(java.io.DataInputStream in)
           
  Message(java.io.InputStream in)
           
  Message(int from, int to, byte[] data)
          Initialize this object.
  Message(int from, int to, java.lang.Number data)
           
  Message(int from, int to, java.lang.String data)
           
  Message(int from, int to, SystemData data)
           
  Message(int from, int to, Writeable data)
           
  Message(Message m)
          copy constructor
  Message(Message m, boolean copyWhenCreated)
          copy constructor
protected Message(short type, int from, int to, java.lang.String classname, java.lang.Object data)
          standard constructor
 
Method Summary
 java.lang.String getClassName()
           
 java.lang.Object getData()
           
 java.io.DataInputStream getDataInputStream()
          only makes sense with TYPE_BYTES
 java.io.DataOutputStream getDataOutputStream()
          returns a DataOutputStream that stores its bytes into this Message's data property.
 int getFrom()
           
 java.io.InputStream getInputStream()
           
 int getInt()
          Alternate accessor -- get data as int
 java.io.OutputStream getOutputStream()
          returns an OutputStream that stores its bytes into this Message's data property.
 java.lang.String getString()
          Alternate accessor -- get data as string
 int getTo()
           
 short getType()
           
 long getWhenCreated()
          returns the time the message was constructed, in the local time of the local machine
protected static short numberToType(java.lang.Number data)
           
 void read(java.io.DataInputStream in)
          Read a message from a stream.
protected  void read(java.io.InputStream in)
          Read a message from a stream.
static Message readMessage(java.io.DataInputStream in)
          Factory method.
 void setData(int x)
          Alternate mutator -- set data as int
 void setData(java.lang.Object data)
           
 void setFrom(int from)
           
 void setTo(int to)
           
 java.lang.String toString()
           
 void write(java.io.DataOutputStream out)
          Write this message to a stream
 void write(java.io.OutputStream out)
          Write this message to a stream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ALL

public static final int ALL
peer id number meaning "all clients"

See Also:
Constant Field Values

NONE

public static final int NONE
peer id number meaning "no client"

See Also:
Constant Field Values

MAGIC

public static final int MAGIC
See Also:
Constant Field Values

TYPE_BYTES

public static final short TYPE_BYTES
the data object contains a byte array

See Also:
Constant Field Values

TYPE_BOOLEAN

public static final short TYPE_BOOLEAN
the data object contains a Boolean

See Also:
Constant Field Values

TYPE_BYTE

public static final short TYPE_BYTE
the data object contains a Byte

See Also:
Constant Field Values

TYPE_SHORT

public static final short TYPE_SHORT
the data object contains a Short

See Also:
Constant Field Values

TYPE_CHAR

public static final short TYPE_CHAR
the data object contains a Character

See Also:
Constant Field Values

TYPE_INT

public static final short TYPE_INT
the data object contains a Integer

See Also:
Constant Field Values

TYPE_LONG

public static final short TYPE_LONG
the data object contains a Long

See Also:
Constant Field Values

TYPE_FLOAT

public static final short TYPE_FLOAT
the data object contains a Float

See Also:
Constant Field Values

TYPE_DOUBLE

public static final short TYPE_DOUBLE
the data object contains a Double

See Also:
Constant Field Values

TYPE_STRING

public static final short TYPE_STRING
the data object contains a String

See Also:
Constant Field Values

TYPE_WRITEABLE

public static final short TYPE_WRITEABLE
the data object contains a Writeable

See Also:
Constant Field Values

TYPE_HASHED

public static final short TYPE_HASHED
the data object contains a Writeable, but the name of the writeable is encoded for efficiency (not yet implemented)

See Also:
Constant Field Values

TYPE_SYSTEM

public static final short TYPE_SYSTEM
the data object contains a SystemData object

See Also:
Constant Field Values

type

protected short type
type of message (determines which class to use to deserialize it)


classname

protected java.lang.String classname

from

protected int from
transient id for source of message


to

protected int to
transient id for destination of message


data

protected java.lang.Object data
contents of message


whenCreated

protected long whenCreated
msec since epoch when it was created

Constructor Detail

Message

protected Message(short type,
                  int from,
                  int to,
                  java.lang.String classname,
                  java.lang.Object data)
standard constructor


Message

public Message(Message m)
copy constructor


Message

public Message(Message m,
               boolean copyWhenCreated)
copy constructor


Message

public Message(int from,
               int to,
               byte[] data)
Initialize this object.


Message

public Message(int from,
               int to,
               java.lang.String data)

Message

public Message(int from,
               int to,
               java.lang.Number data)

Message

public Message(int from,
               int to,
               SystemData data)

Message

public Message(int from,
               int to,
               Writeable data)

Message

public Message(java.io.DataInputStream in)
        throws java.io.IOException

Message

public Message(java.io.InputStream in)
        throws java.io.IOException
Method Detail

numberToType

protected static short numberToType(java.lang.Number data)

getType

public short getType()
Returns:
the type of message

getFrom

public int getFrom()
Returns:
which (transient, server-specific) peer created this message

setFrom

public void setFrom(int from)
Parameters:
from - which (transient, server-specific) peer created this message

getTo

public int getTo()
Returns:
which (transient, server-specific) peer will receive this message

setTo

public void setTo(int to)
Parameters:
to - which (transient, server-specific) peer will receive this message

getData

public java.lang.Object getData()
Returns:
the data attached to this message

setData

public void setData(java.lang.Object data)

getWhenCreated

public long getWhenCreated()
returns the time the message was constructed, in the local time of the local machine


getString

public java.lang.String getString()
Alternate accessor -- get data as string


getInt

public int getInt()
Alternate accessor -- get data as int


setData

public void setData(int x)
Alternate mutator -- set data as int


getInputStream

public java.io.InputStream getInputStream()
Returns:
an InputStream that returns this message's data bytes.

getDataInputStream

public java.io.DataInputStream getDataInputStream()
only makes sense with TYPE_BYTES

Returns:
a DataInputStream that returns this message's data bytes

getDataOutputStream

public java.io.DataOutputStream getDataOutputStream()
returns a DataOutputStream that stores its bytes into this Message's data property. Calling close() on the stream finishes the process. (Before you call close, the data has not been set.) This forces the type to be TYPE_BYTES.


getOutputStream

public java.io.OutputStream getOutputStream()
returns an OutputStream that stores its bytes into this Message's data property. Calling close() on the stream finishes the process. (Before you call close, the data has not been set.) This forces the type to be TYPE_BYTES.


toString

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

write

public void write(java.io.OutputStream out)
           throws java.io.IOException
Write this message to a stream

java.io.IOException

write

public void write(java.io.DataOutputStream out)
           throws java.io.IOException
Write this message to a stream

Specified by:
write in interface Writeable
java.io.IOException

read

protected void read(java.io.InputStream in)
             throws java.io.IOException
Read a message from a stream. This method is protected; use the factory method readMessage() instead.

java.io.IOException
See Also:
readMessage(java.io.DataInputStream)

read

public void read(java.io.DataInputStream in)
          throws java.io.IOException
Read a message from a stream. This method should be protected; use the factory method readMessage() instead. (It can't be protected because it's public in the interface.)

Specified by:
read in interface Writeable
java.io.IOException
See Also:
readMessage(java.io.DataInputStream)

getClassName

public java.lang.String getClassName()
Specified by:
getClassName in interface Writeable

readMessage

public static Message readMessage(java.io.DataInputStream in)
                           throws java.io.IOException
Factory method. This is the preferred way of reading the next message from a stream.

java.io.IOException

Documentation generated March 5 2003