Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Console.SetWindowSize(100, 10);

            ChatLog.AddChannel(new ChatLogConsoleChannel());
            ChatLog.Info("Server running.");
            ChatServer server = new ChatServer();

            //server.CommandExecutor.Add("print_hello", () => ChatLog.Info("Hello")); //test

            ////print all the active rooms
            //server.CommandExecutor.Add("print rooms", () => {
            //    int i = 0;
            //    foreach (var room in server.GetActiveRooms())
            //    {
            //        ChatLog.Info(string.Format("{0}) Name -> {1} --- Host -> {2}", i, room.Name, room.Host));
            //        i++;
            //    }
            //});

            ////specific room members
            //server.CommandExecutor.Add("members", () => {

            //    ChatLog.Info("type room name:");
            //    string room = Console.ReadLine();
            //    if (!server.RoomExistsByName(room))
            //    {
            //        ChatLog.Error("room does not exists");
            //        return;
            //    }

            //    int i = 0;
            //    foreach (var members in server.GetRoomMembers(room))
            //    {
            //        ChatLog.Info(string.Format("---> " + members));
            //        i++;
            //    }
            //});

            server.Start(); //implicit args
        }
Ejemplo n.º 2
0
        static void Main(string[] _args)
        {
            ChatLog.AddChannel(new ChatLogConsoleChannel());
            bool running = true;

            ChatLog.Info("type name:");
            string name = Console.ReadLine();

            ChatClient client  = new ChatClient("127.0.0.1", 2001, name, true);
            bool       connect = false;

            //set the client name
            client.CommandExecutor.Add("setname", new string[] { "-name" }, "Allow to change the name of the client if it's not joined", (args) =>
            {
                if (args.Length != 1)
                {
                    ChatLog.Error("Only argument number accepted is 1");
                }
                else
                {
                    bool success = client.ChangeClientName(args[0].value);
                    if (!success)
                    {
                        ChatLog.Warning("Operation not allowed when the client is joined");
                    }
                }
            });

            //connect to server
            client.CommandExecutor.Add("connect", new string[] { "-join" }, "Connect to the server", (args) =>
            {
                if (args.Length == 1)
                {
                    if (args[0].value == "true")
                    {
                        connect = client.Connect(true);
                    }
                    else if (args[0].value == "false")
                    {
                        connect = client.Connect(false);
                    }
                    else
                    {
                        ChatLog.Error("Argument not valid");
                    }
                }
                else if (args.Length == 0)
                {
                    connect = client.Connect(false);
                }
                else
                {
                    ChatLog.Error("Arguments number error. Should type join if you want autojoin or leave just connect command");
                }
            });

            //client.CommandExecutor.Execute("setname -name pippo", null);
            //just join the server
            client.CommandExecutor.Add("join", new string[] { }, "Join the server", (args) =>
            {
                client.Join();
            });

            //just quit the server
            client.CommandExecutor.Add("quit", new string[] { }, "Quit the server", (args) =>
            {
                client.Quit();
            });

            //ask for the joined clients
            client.CommandExecutor.Add("askjoined", new string[] { }, "Ask the server for the connected clients", (args) =>
            {
                client.AskForConnectedUsers();
            });

            //message a specific client
            client.CommandExecutor.Add("message", new string[] { "-text", "-dest" }, "Message a specific client specified with -dest, the message must be specified with -text argument", (args) =>
            {
                if (args.Length != 2)
                {
                    ChatLog.Error("Only argument number accepted is 2");
                    return;
                }

                string dest    = "";
                string message = "";

                foreach (var arg in args)
                {
                    if (arg.key == "dest")
                    {
                        dest = arg.value;
                    }
                    else
                    {
                        message = arg.value;
                    }
                }
                client.SendMessage(dest, message, false);
            });


            //message a specific room
            client.CommandExecutor.Add("messageroom", new string[] { "-text", "-dest" }, "Message a specific room specified with -dest, the message must be specified with -text argument", (args) =>
            {
                if (args.Length != 2)
                {
                    ChatLog.Error("Only argument number accepted is 2");
                    return;
                }

                string dest    = "";
                string message = "";

                foreach (var arg in args)
                {
                    if (arg.key == "dest")
                    {
                        dest = arg.value;
                    }
                    else
                    {
                        message = arg.value;
                    }
                }
                client.SendMessage(dest, message, true);
            });

            //message a specific room
            client.CommandExecutor.Add("createroom", new string[] { "-name" }, "create a new room with a specified name with -name argument", (args) =>
            {
                if (args.Length != 1)
                {
                    ChatLog.Error("Only argument number accepted is 1");
                    return;
                }

                client.CreateRoom(args[0].value);
            });

            client.CommandExecutor.Add("closeroom", new string[] { "-name" }, "close the room (if you're the host) with a specified name with -name argument", (args) =>
            {
                if (args.Length != 1)
                {
                    ChatLog.Error("Only argument number accepted is 1");
                    return;
                }

                client.CloseRoom(args[0].value);
            });

            client.CommandExecutor.Add("members", new string[] { "-name" }, "locally print the room members", (args) =>
            {
                if (args.Length != 1)
                {
                    ChatLog.Error("Only argument number accepted is 1");
                    return;
                }

                if (client.JoinedRooms.ContainsKey(args[0].value))
                {
                    foreach (var m in client.JoinedRooms[args[0].value].Members)
                    {
                        ChatLog.Info(m);
                    }
                }
                else
                {
                    ChatLog.Warning("Room not joined");
                }
            });

            client.CommandExecutor.Add("invite", new string[] { "-name", "-room" }, "invite a user to a room", (args) =>
            {
                if (args.Length != 2)
                {
                    ChatLog.Error("Only argument number accepted is 2");
                    return;
                }

                string user = "";
                string room = "";

                foreach (var arg in args)
                {
                    if (arg.key == "name")
                    {
                        user = arg.value;
                    }
                    else
                    {
                        room = arg.value;
                    }
                }
                client.Invite(room, user);
            });

            client.CommandExecutor.Add("leave", new string[] { "-room" }, "leave the room", (args) =>
            {
                if (args.Length != 1)
                {
                    ChatLog.Error("Only argument number accepted is 1");
                    return;
                }

                client.LeaveRoom(args[0].value);
            });
            //finish commands implementation

            client.StartCommandsThread();

            while (running)
            {
                client.UpdateScheduler();
            }
        }