using System;
using System.Net;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace grof{
/// <summary>
/// <c>Message</c> objects are sent between group
/// members in order to exchange information. There
/// are different kinds of messages:
/// <para>
/// <list type="bullet">
/// <item>
/// JOIN = Indicates that a group member wants to join a group.
/// </item>
/// <item>
/// LEAVE = Indicates that a group member wants to leave a group.
/// </item>
/// <item>
/// APP = Represents an application message which is used by the
/// framework user.
/// </item>
/// <item>
/// ALIVE = Group members sent this message to other group members
/// in order to signal that their alive.
/// </item>
/// </list>
/// </para>
/// <para>
/// <c>Message</c> objects have to be serializable (=stripped into
/// bytes) for sending over the network.
/// </para>
/// </summary>
[Serializable]
public class Message
{
/// <summary>
/// The type of the message.
/// </summary>
public enum MessageType { JOIN, LEAVE, JOINED, LEFT, APP, ALIVE };
/// <summary>
/// Messages sent by a specific group member are numbered
/// sequentially (0,1,2,3...)
/// </summary>
private long messageCounter;
/// <summary>
/// Encapsulates important data about the group member
/// like member name, host address etc.
/// </summary>
private GroupMemberInfo gmInfo;
/// <summary>
/// The user data which ought to be transmitted to
/// other group members.
/// </summary>
private Object data;
/// <summary>
/// The variable holding the message type.
/// </summary>
private MessageType type;
/// <summary>
/// The timestamp when the message was sent.
/// </summary>
private long timeStamp;
/// <summary>
/// Creates instances of class
/// <c>Message</c>.
/// </summary>
/// <param name="type">The message type (JOIN, LEAVE etc.).</param>
/// <param name="messageCounter">The message counter.</param>
/// <param name="timeStamp">Indicates when message was sent.</param>
/// <param name="data">The user data which is sent.</param>
/// <param name="gmInfo">Encapsulates important group member infos.</param>
public Message( MessageType type, long messageCounter, long timeStamp, Object data, GroupMemberInfo gmInfo )
{
this.type = type;
this.data = data;
this.gmInfo = gmInfo;
this.messageCounter = messageCounter;
this.timeStamp = timeStamp;
}
/// <summary>
/// Serializes the message object into a
/// byte array.
/// </summary>
/// <returns>The byte array containing the
/// serialized object.</returns>
public byte[] ToBytes()
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, this);
return ms.ToArray();
}
/// <summary>
/// Deserializes the byte stream into a
/// message object.
/// </summary>
/// <param name="bytes">The byte array which is
/// deserialized.</param>
/// <returns>The deserialized message object.</returns>
public static Message FromBytes( byte[] bytes )
{
MemoryStream ms = new MemoryStream( bytes );
BinaryFormatter bf = new BinaryFormatter();
ms.Position = 0;
return ( Message ) bf.Deserialize( ms );
}
/// <summary>
/// Prints a user friendly representation of the
/// message object.
/// </summary>
/// <returns>The message object as string.</returns>
public override String ToString()
{
return type + "-" + this.messageCounter + "-" +
DateTime.FromBinary( this.timeStamp ).ToString() + "-" +
gmInfo.ToString() + "-" + this.data;
}
/// <summary>
/// Returns the user data.
/// </summary>
/// <returns>The user data.</returns>
public object GetData()
{
return this.data;
}
/// <summary>
/// Returns the message type.
/// </summary>
/// <returns>The message type.</returns>
public MessageType GetType()
{
return this.type;
}
/// <summary>
/// Returns the group member info object.
/// </summary>
/// <returns>The group member info.</returns>
public GroupMemberInfo GetGroupMemberInfo()
{
return this.gmInfo;
}
/// <summary>
/// Returns the timestamp.
/// </summary>
/// <returns>The timestamp of the message
/// object.</returns>
public long GetTimeStamp()
{
return this.timeStamp;
}
/// <summary>
/// Returns the message counter.
/// </summary>
/// <returns>The message counter.</returns>
public long GetMessageCounter()
{
return this.messageCounter;
}
}
}
|