using System;
namespace grof{
/// <summary>
/// The <c>MessageCreator</c> class is a helper class for
/// creating <c>Message</c> objects. Each group member has one
/// <c>MessageCreator</c> instance which is able to create
/// join, leave, alive and application messages.
/// </summary>
public class MessageCreator
{
/// <summary>
/// The name of the group member.
/// </summary>
private String name;
/// <summary>
/// The IP address of the group member.
/// </summary>
private String hostAddress;
/// <summary>
/// The port number of the group member.
/// </summary>
private int port;
/// <summary>
/// The message counter.
/// </summary>
private MessageCounter msgCounter;
/// <summary>
/// The timestamp of the message which indicates when
/// the message was created and sent.
/// </summary>
private TimeStamp timeStamp;
/// <summary>
/// Creates instances of class <c>MessageCreator</c>. Each
/// message holds information (IP address, port number and name)
/// of the sender object.
/// </summary>
/// <param name="name">The name of the sender object.</param>
/// <param name="hostAddress">The IP address of the sender object.</param>
/// <param name="port">The port number the sender object
/// listens for incoming messages.</param>
public MessageCreator( String name, String hostAddress, int port )
{
this.name = name;
this.hostAddress = hostAddress;
this.port = port;
this.msgCounter = new MessageCounter();
this.timeStamp = new TimeStamp();
}
/// <summary>
/// Creates join messages. When a group member wants to join
/// a group it sends a JOIN message to all group members of
/// this group.
/// </summary>
/// <param name="data">Further data the JOIN message holds</param>
/// <returns>The created message object.</returns>
public Message CreateJoinMessage( Object data )
{
GroupMemberInfo gmInfo = new GroupMemberInfo(
this.name, this.hostAddress, this.port );
return new Message( Message.MessageType.JOIN, this.msgCounter.GetCounter(),
this.timeStamp.GetTimeStamp(), data, gmInfo );
}
/// <summary>
/// Creates leave messages. When a group member wants to leave
/// a group it sends a LEAVE message to all group members of
/// this group.
/// </summary>
/// <param name="data">Further data the LEAVE message holds</param>
/// <returns>The created message object.</returns>
public Message CreateLeaveMessage(Object data)
{
GroupMemberInfo gmInfo = new GroupMemberInfo(
this.name, this.hostAddress, this.port );
return new Message( Message.MessageType.LEAVE, this.msgCounter.GetCounter(),
this.timeStamp.GetTimeStamp(), data, gmInfo );
}
/// <summary>
/// When a group member receives a JOIN message from another
/// group member, it sends an ALIVE message to the joined group
/// member. On this way the joined group member is able to build
/// a cache of all group members.
/// </summary>
/// <param name="data">Further data the ALIVE message holds</param>
/// <returns>The created message object.</returns>
public Message CreateAliveMessage( Object data )
{
GroupMemberInfo gmInfo = new GroupMemberInfo(
this.name, this.hostAddress, this.port );
return new Message( Message.MessageType.ALIVE, this.msgCounter.GetCounter(),
this.timeStamp.GetTimeStamp(), data, gmInfo );
}
/// <summary>
/// This method is not used by the <c>GROF</c> framework itself but
/// implicitly by the user of the <c>GroupMember</c> class. Everytime
/// when the user sends an object the <c>GroupMember</c> instance wraps
/// this object in an application message.
/// </summary>
/// <param name="data">The user data.</param>
/// <returns>The message object.</returns>
public Message CreateApplicationMessage( Object data )
{
GroupMemberInfo gmInfo = new GroupMemberInfo(
this.name, this.hostAddress, this.port );
return new Message( Message.MessageType.APP, this.msgCounter.GetAppCounter(),
this.timeStamp.GetTimeStamp(), data, gmInfo );
}
/// <summary>
/// In order to be able to sort incoming messages from
/// different group members by time all group members must
/// work with the same time. For synchronizing the times this method
/// compares the timestamp of JOIN and ALIVE messages with
/// the local time value and updates it if necessary.
/// </summary>
/// <param name="time">The timestamp of incoming messages</param>
public void CheckAndSetTimeStamp( long time )
{
this.timeStamp.CheckAndSetTimeStamp( time );
}
}
}
|