using System;
namespace grof{
/// <summary>
/// The <c>GroupMemberInfo</c> class caches all
/// important data of a group member like
/// the host address, the port number the
/// group member listens for incoming messages and
/// the group name. The <c>GroupMemberInfo</c> class
/// is declared as serializable because each sent message
/// contains a <c>GroupMemberInfo</c> object. Therefore for
/// each message the addresser can be detected.
/// </summary>
[Serializable]
public class GroupMemberInfo : IComparable
{
/// <summary>
/// The IP address of the machine where
/// the group member process runs.
/// </summary>
private string hostAddress;
/// <summary>
/// The port number where the
/// group member listens for incoming
/// requests.
/// </summary>
private int portNumber;
/// <summary>
/// The name of the group member.
/// </summary>
private string name;
/// <summary>
/// Creates instances of class
/// <c>GroupMemberInfo</c>.
/// </summary>
/// <param name="name">The name of the group member.</param>
/// <param name="host">The host address (IP) of the group member.</param>
/// <param name="port">The number of the port the group member
/// listens for incoming messages.</param>
public GroupMemberInfo( string name, string host, int port )
{
this.name = name;
this.hostAddress = host;
this.portNumber = port;
}
/// <summary>
/// Returns the name of the group member.
/// </summary>
/// <returns>The name of the group member.</returns>
public string GetName()
{
return this.name;
}
/// <summary>
/// Returns the host address of the
/// group member.
/// </summary>
/// <returns>The host address.</returns>
public string GetHostAddress()
{
return this.hostAddress;
}
/// <summary>
/// Returns the port number of the group
/// member.
/// </summary>
/// <returns>The port number.</returns>
public int GetPortNumber()
{
return this.portNumber;
}
/// <summary>
/// Returns the object in
/// string representation.
/// </summary>
/// <returns>The <c>GroupMemberInfo</c> object in string form.</returns>
public string ToString()
{
return this.name + "|" + this.hostAddress + "|" + this.portNumber;
}
/// <summary>
/// <code>GroupMemberInfo</code> instances are
/// cached in hashtables. Therefore the <code>Equals</code>
/// and <code>GetHashCode</code> methods have to be
/// overriden.
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
GroupMemberInfo gmInfo = (GroupMemberInfo) obj;
if ( !this.name.Equals( gmInfo.name ) )
return false;
if ( !this.hostAddress.Equals( gmInfo.hostAddress ) )
return false;
if ( this.portNumber != gmInfo.portNumber )
return false;
return true;
}
/// <summary>
/// <code>GroupMemberInfo</code> instances are
/// cached in hashtables. Therefore the <code>Equals</code>
/// and <code>GetHashCode</code> methods have to be
/// overriden.
/// TODO: This implementation is not very performant.
/// Think about better solution!
/// </summary>
/// <returns>The hash value.</returns>
public override int GetHashCode()
{
return ( this.name + this.hostAddress + this.portNumber ).GetHashCode() ;
}
/// <summary>
/// Compares with other <c>GroupMemberInfo</c> objects.
/// This method is called by the <c>Sort</c> and
/// <c>BinarySearch</c> methods of class
/// <c>Array</c>.
/// </summary>
/// <param name="obj">The object which is compared with
/// this instance.</param>
/// <returns>The result of the comparison</returns>
public int CompareTo( object obj ) {
if( obj is GroupMemberInfo ) {
GroupMemberInfo temp = ( GroupMemberInfo ) obj;
return this.ToString().CompareTo( temp.ToString() );
}
throw new ArgumentException( "object is not a GroupMemberInfo" );
}
}
}
|