///
/// Yo !!
/// Chat program using .net remoting
/// developed using C# and the .NET Framework.
/// 
/// author: J Edvar 
/// 
/// URL: http://www.superedge.com 
/// email: edvarp@yahoo.com
///
/// Remarks: csc /debug+ /r:system.runtime.remoting.dll YoServer.cs
///
/// History
/// feb-25-2003	Developed the first tests 
///
///
///


using System;

// Import the Remoting API support
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Http;

// Import Collections support
using System.Collections;

namespace YoServer 
  {
  /// <summary>
  /// A Bulletin Board Server that buffers messages
  /// </summary>
  public class TYoServer : MarshalByRefObject 
  {
    /// <summary>
    /// Buffers the messages on the Bulletin Board Server
    /// </summary>
    ArrayList messageList_ = new ArrayList();

    /// <summary>
    /// Adds a new message to the Bulletin Board Server
    /// </summary>
    /// <param name="message">
    /// New message to be buffered
    /// </param>
    public void addMessage( string clientID, string message ) 
    {
      messageList_.Add( message );
      Console.WriteLine( "User :" + clientID + ", said :" + message );
    }

    /// <summary>
    /// Retrieves all messages stored on the Server
    /// </summary>
    /// <returns>
    /// All messages, each concatenated by new line characters
    /// </returns>
    public string retrieveAllMessages(string clientID) 
    {
      string messages = null;
      foreach ( string message in messageList_ ) 
      {
        messages += message+"\n";
      }
      return messages;
    }

    /// <summary>
    /// Displays on console any string thats pased as a parameter
    /// </summary>
    /// <param name="info">
    /// string to be displayed
    /// </param>
    /// <returns>
    /// string that was displayed
    /// </returns>
    public string displayMessage( string clientID, string info ) 
    {
      Console.WriteLine( info );
      return info;
    }
  }

  /// <summary>
  /// The Server class implementation
  /// </summary>

  public class TheYoServer 
  {

    /// <summary>
    /// Entry Point into this application
    /// </summary>
    public static void Main() 
    {
      int listeningChannel = 1099;
      // Create a New HTTP Channel that listens on Port 
listeningChannel
      // TcpChannel channel = new TcpChannel( listeningChannel );
      HttpChannel channel = new HttpChannel( listeningChannel );
      // Register the channel with the runtime
      ChannelServices.RegisterChannel( channel );
      // Expose the Calculator Object from this Server
      RemotingConfiguration.RegisterWellKnownServiceType( typeof( 
TYoServer), "YoServer", WellKnownObjectMode.Singleton );

      // Keep the Server running until the user presses enter
      Console.WriteLine( "The Yo! Server is up and running on port 
{0}", listeningChannel);
      Console.WriteLine( "Press enter to stop the server..." );
      Console.ReadLine();
    }
  }
}

