Message.cs :  » Network-Clients » GROF » grof » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » GROF 
GROF » grof » Message.cs
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;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.