/* $Id$ * * Copyright 2007-2008 Cisco Systems Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. */ using System; using System.Collections.Generic; using System.Globalization; using Etch.Support; using Etch.Util; namespace etch.examples.chat { public class MainChatClient : ChatClient, ObjSession { private readonly List _whoIsOnline = new List(); private readonly DateTimeFormatInfo myDTFI = new CultureInfo("en-US", false).DateTimeFormat; public MainChatClient() { } public static void Main( String[] args ) { string uri; if ( args.Length == 1 ) { uri = args[0]; } else if ( args.Length == 0 ) { uri = "tcp://localhost:4005?TcpTransport.reconnectDelay=4000"; } else { Console.WriteLine("usage: MainChatClient []"); Environment.Exit( 1 ); return; } Console.WriteLine("Connecting to chat server at " + uri); MainChatClient client = new MainChatClient(); RemoteChatServer server = ChatHelper.NewServer( uri, null, new MyChatClientFactory( client )); server._StartAndWaitUp( 4000 ); Console.WriteLine( "( connected, enter commands or help)" ); while ( true ) { Console.Write( "cmd : " ); Console.Out.Flush(); String s = Console.ReadLine(); if (s == null) break; if ( s.Length == 0 ) continue; char[] seps = { ' ', '\t', '\r', '\n' }; String[] cmds = s.Split( seps ); if ( cmds.Length == 0 ) continue; if ( cmds[ 0 ].Equals( "help" ) ) { Do_help(); continue; } if ( cmds[0].Equals( "login") ) { Do_login( server, cmds ); continue; } if ( cmds[0].Equals( "logout") ) { server.logout(); continue; } if ( cmds[0].Equals( "quit") ) { break; } if ( cmds[ 0 ].Equals( "send" ) ) { // extract message // note: cmds[2] = message to be extracted if ( cmds.Length < 3 ) { Console.WriteLine("usage : send "); continue; } char[] seps2 = {'\n', '\r'}; String msg = ""; for ( int i = 2; i < cmds.Length; i++ ) msg = msg + " " + cmds[ i ]; String[] cmds2 = msg.Split( seps2 ); Do_send( server, cmds[ 1 ], cmds2 ); continue; } if ( cmds[ 0 ].Equals( "who" ) ) { Do_who( client ); continue; } Console.WriteLine( "unknown command" ); Do_help(); } //server.logout(); //( ( RemoteChatServer ) server )._StopAndWaitDown( 4000 ); server._Stop(); } private static void Do_help() { Console.WriteLine( "commands: " ); Console.WriteLine( " help" ); Console.WriteLine( " login " ); Console.WriteLine( " logout" ); Console.WriteLine( " quit" ); Console.WriteLine( " send " ); Console.WriteLine( " who" ); } private static void Do_login( ChatServer server, String[] st ) { try { String name = st[ 1 ]; String pwd = st[ 2 ]; server.login( name, pwd ); Console.WriteLine( "login successful" ); } catch ( types.Chat.Failure e ) { Console.WriteLine( "failed : " + e.GetMessage() ); } catch ( IndexOutOfRangeException ) { Console.WriteLine( "usage : login " ); } catch ( Exception e ) { Console.WriteLine( e.Message ); } } /// /// Send message /// /// /// username /// message to be sent /// private static void Do_send( ChatServer server, String user, String[] st ) { try { String who = user; String msg = st[ 0 ]; server.send( who, msg ); } catch ( types.Chat.Failure e ) { Console.WriteLine( "failed : " + e.GetMessage() ); } catch ( _Etch_AuthException ) { Console.WriteLine( "you must login first" ); } catch ( IndexOutOfRangeException ) { Console.WriteLine( "usage: send " ); } catch ( Exception e ) { Console.WriteLine( e.Message ); } } private static void Do_who(MainChatClient client) { String[] names = client.GetWhoIsOnline(); foreach ( String name in names ) { Console.WriteLine( " " + name ); } } /// /// /// /// a list of who is currently online public String[] GetWhoIsOnline() { return _whoIsOnline.ToArray(); } #region ChatClient Members public void whoIsOnline( string[] names ) { foreach ( String name in names ) { Console.WriteLine( name + " is online" ); _whoIsOnline.Add( name ); } } public void statusChange( string name, bool? online ) { if ( ( bool ) online ) { Console.WriteLine( name + " is online" ); _whoIsOnline.Add( name ); } else { Console.WriteLine( name + " is offline" ); _whoIsOnline.Remove( name ); } } #endregion #region Chat Members public bool? isLoggedIn() { // server is always logged in to the client return true; } public void send( string who, string msg ) { lock (myDTFI) { Console.WriteLine( "[ {0}@{1} ] {2}\n", who, DateTime.Now.ToString("HH:mm:ss",myDTFI ), msg ); } } #endregion #region Session Members public void _SessionControl( object control, object value ) { throw new Exception( "The method or operation is not implemented." ); } public void _SessionNotify( object eventObj ) { if ( eventObj.Equals( SessionConsts.UP ) ) { Console.WriteLine( " (server opened connection)" ); return; } if ( eventObj.Equals( SessionConsts.DOWN ) ) { Console.WriteLine( " (server closed connection)" ); return; } if ( eventObj is Exception ) { Console.WriteLine( eventObj ); return; } } public object _SessionQuery( object query ) { throw new Exception( "The method or operation is not implemented." ); } #endregion private class MyChatClientFactory : ChatHelper.ChatClientFactory { public MyChatClientFactory(MainChatClient client) { this.client = client; } private readonly MainChatClient client; public ChatClient NewChatClient(RemoteChatServer server) { return client; } } } }