Example #1
0
        public static void Main()
        {
            Chatter       bob = new TextChatter("Bob");
            Chatter       joe = new TextChatter("Joe");
            TopicsManager gt  = new TextGestTopics();

            gt.createTopic("java");
            gt.createTopic("UML");
            gt.listTopics();
            gt.createTopic("jeux");
            gt.listTopics();
            Chatroom cr = gt.joinTopic("jeux");

            cr.join(bob);
            cr.post("Je suis seul ou quoi ?", bob);
            cr.join(joe);
            cr.post("Tiens, salut Joe !", bob);
            cr.post("Toi aussi tu chat sur les forums de jeux pendant les TP, Bob ? ", joe);
        }
Example #2
0
        static void Main(string[] args)
        {
            Chatter       bob = new TextChatter("Bob");
            Chatter       joe = new TextChatter("Joe");
            TopicsManager gt  = new TextGestTopics();

            gt.CreateTopic("java");
            gt.CreateTopic("UML");
            gt.ListTopics();
            gt.CreateTopic("jeux");
            gt.ListTopics();
            Chatroom cr = gt.JoinTopic("jeux");

            cr.Join(bob);
            cr.Post("Je suis seul ou quoi ?", bob);
            cr.Join(joe);
            cr.Post("Tiens, salut Joe !", bob);
            cr.Post("Toi aussi tu chat sur les forums de jeux pendant les TP,Bob ? ", joe);

            AuthentificationManager am = new Authentification();

            // users management

            try
            {
                am.AddUser("bob", "123");
                Console.WriteLine("Bob has been added !");
                am.RemoveUser("bob");
                Console.WriteLine("Bob has been removed !");
                am.RemoveUser("bob");
                Console.WriteLine("Bob has been removes twice !");
            }
            catch (UserUnknownException e)
            {
                Console.WriteLine(e.Login + " : user unknown (enable to remove)!");
            }
            catch (UserExistsException e)
            {
                Console.WriteLine(e.Login + " has already been added !");
            }

            // authentification
            try
            {
                am.AddUser("bob", "123");
                Console.WriteLine("Bob has been added !");
                am.Authentify("bob", "123");
                Console.WriteLine("Authentification OK !");
                am.Authentify("bob", "456");
                Console.WriteLine("Invalid password !");
            }
            catch (WrongPasswordException e)
            {
                Console.WriteLine(e.Login + " has provided an invalid password !");
            }
            catch (UserExistsException e)
            {
                Console.WriteLine(e.Login + " has already been added !");
            }
            catch (UserUnknownException e)
            {
                Console.WriteLine(e.Login + " : user unknown (enable to remove)!");
            }

            // persistance
            try
            {
                am.Save("users.txt");
                AuthentificationManager am1 = new Authentification();
                am1.Load("users.txt");
                am1.Authentify("bob", "123");
                Console.WriteLine("Loading complete !");
            }
            catch (UserUnknownException e)
            {
                Console.WriteLine(e.Login + " is unknown ! error during the saving / loading.");
            }
            catch (WrongPasswordException e)
            {
                Console.WriteLine(e.Login + " has provided an invalid password!error during the saving / loading.");
            }
            catch (IOException e)
            {
                Console.WriteLine(e);
            }


            Console.ReadLine();
        }
        public void startServer()
        {
            TcpListener l = new TcpListener(new IPAddress(new byte[] { 127, 0, 0, 1 }), port);

            l.Start();

            TopicsManager   gt            = new TextGestTopics();
            List <ChatRoom> ChatRoomsList = new List <ChatRoom>();

            while (true) // Exchanges with the clients
            {
                try
                {
                    TcpClient comm = l.AcceptTcpClient(); // Connection with client
                    Console.WriteLine("Connection established @" + comm);
                    Console.WriteLine("Computing operation");

                    string  response;
                    Message mes = new Message(gt.listTopics());
                    do // Send : Join or Create a Chatroom
                    {  // Send : Names of opened chatrooms
                        MessageConnection.sendMessage(comm.GetStream(), new Message("server", "1 Join a chatroom \n2 Create a chatroom"));
                        MessageConnection.sendMessage(comm.GetStream(), mes);

                        mes      = MessageConnection.getMessage(comm.GetStream());
                        response = mes.msg;
                    } while (!response.Equals("1") && !response.Equals("2"));

                    Chatter chatter = new TextChatter(mes.name); // New user of a chatroom
                    chatterList.Add(chatter);
                    TcpClientList.Add(comm);

                    if (response.Equals("1")) // Join an existing chatroom
                    {
                        do
                        {
                            MessageConnection.sendMessage(comm.GetStream(), new Message("serveur", "Please, choose a chatroom: "));
                            mes = MessageConnection.getMessage(comm.GetStream());
                            if ((!(gt.IsInside(mes.msg))))
                            {
                                Console.WriteLine("This chatroom does not exist.");
                            }
                        } while (mes.msg.Length == 0 || (!(gt.IsInside(mes.msg))));
                    }
                    else // Create a chatroom
                    {
                        do
                        {
                            MessageConnection.sendMessage(comm.GetStream(), new Message("serveur", "Please, give a name to your topic: "));
                            mes = MessageConnection.getMessage(comm.GetStream());
                        } while (mes.msg.Length == 0 || gt.IsInside(mes.msg));

                        gt.createTopic(mes.msg);
                        ChatRoomsList.Add(gt.joinTopic(mes.msg));
                    }

                    foreach (ChatRoom cr in ChatRoomsList)
                    {
                        if (mes.msg.Equals(cr.getTopic()))
                        {
                            cr.join(chatter);                               // Add the chatter in the chatroom
                            new Thread(new Receiver(comm, cr).run).Start(); // Create Threads to listen and send messages
                            TcpClient commGet = l.AcceptTcpClient();

                            chatterList.Add(chatter);
                            TcpClientList.Add(commGet);

                            new Thread(new Receiver(commGet, cr).run).Start();

                            Message join = new Message("server", "Welcome to " + mes.name + " in the " + mes.msg + " chatroom");
                            broadcast(join, cr);//send welcome message
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
        }