/*
* Created by SharpDevelop.
* User: magr2419
* Date: 15.03.2006
* Time: 12:13
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using grof;
namespace grof{
/// <summary>
/// Description of GroupMemberConsole.
/// </summary>
public class GroupMemberConsole
{
private GroupMember member = null;
private static readonly String[,] COMMANDS = {
{ "help", "Lists all possible commands" },
{ "quit", "Quit the console application (group member leaves the group" },
{ "send <message>", "Sends a message to all group members" },
{ "getgm", "Lists all group members" }
};
// TODO: doc
public GroupMemberConsole( String groupName, String memberName,
String groupAddress, int groupPort,
String listenerAddress, int listenerPort )
{
Debug.Listeners.Add( new TextWriterTraceListener( Console.Out ) );
Initializer init = new Initializer();
init.GroupAddress = groupAddress;
init.GroupName = groupName;
init.GroupPort = groupPort;
init.ListenerAddress = listenerAddress;
init.ListenerPort = listenerPort;
init.MemberName = memberName;
this.member = new GroupMember( init );
this.member.SetMessageReceiver(this.Received);
this.member.JoinGroup();
Thread.Sleep( 2000 );
this.startConsole();
}
public void Received(Message msg)
{
Console.WriteLine("> received message: " + msg);
}
// group ernie 224.100.0.1 9050 127.0.0.1 9011
public static void Main( string[] args )
{
if ( args.Length == 0 ||
( args.Length == 1 && args[0] == "help" ) )
{
Console.WriteLine( "Parameter:" );
Console.WriteLine( " group name - The group name." );
Console.WriteLine( " member name - The name of the group member." );
Console.WriteLine( " group address - The IP address of the multicast group." );
Console.WriteLine( " group port - The port number of the multicast group." );
Console.WriteLine( " listener address - The IP address where the group member listens for TCP requests." );
Console.WriteLine( " listener port - The port number where the group member listens fro TCP requests." );
} else if ( args.Length == 6 )
{
try
{
int portNumber = Int32.Parse( args[3] );
int listenerPort = Int32.Parse( args[5] );
new GroupMemberConsole( args[0], args[1],
args[2], portNumber,
args[4], listenerPort );
} catch( Exception e )
{
Console.WriteLine( "!listener port/group port are not valid numbers! " + e.ToString() );
}
} else
{
Console.WriteLine( "!wrong parameters!" );
}
}
private void startConsole()
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine( "-----------------------------------------------------" );
Console.WriteLine( "| |" );
Console.WriteLine( "| Group Member Console |" );
Console.WriteLine( "| |" );
Console.WriteLine( "-----------------------------------------------------" );
Console.WriteLine();
Console.Write( "> " );
string input;
while ( !( input = Console.ReadLine() ).Equals( COMMANDS[1,0] ) )
{
string[] strs = input.Split( ' ' );
switch( strs[0] )
{
// It is not possible to write here
// case COMMANDS[2,0]
case "send":
if ( strs.Length == 2 )
{
this.member.Send( strs[1] );
}
break;
case "getgm":
GroupMemberInfo[] gmInfos = this.member.GetGroupMembers();
for ( int i=0; i<gmInfos.Length; i++ )
{
Console.WriteLine( "-> Group member #" + i + ": " + gmInfos[i].ToString() );
}
break;
case "help":
for ( int i=0; i<COMMANDS.Length/2; i++ )
{
Console.WriteLine( "-> " + COMMANDS[i,0] + " - " + COMMANDS[i,1] );
}
break;
case "":
Console.Write( "\n> " );
break;
default:
Console.WriteLine( "!command not valid!" );
break;
}
Console.Write( "\n> " );
}
this.member.LeaveGroup();
Console.WriteLine( "\nbye.\n\n" );
}
}
}
|