Example #1
0
        //Método que descompone el comando, lo analiza sintácticamente y si es correcto lo ejecuta.
        private static int ExecuteCommand(string comando)
        {
            //Cadena con el comando
            string [] emptyOptions = { " " };
            string[]  Command      = comando.Split(emptyOptions, StringSplitOptions.RemoveEmptyEntries);

            if (Command.Length == 0)
            {
                throw new Exception("No se ingresó ninguna instrucción.");
            }

            //Según la primer palabra del comando, así se validará el resto de la cadena.
            switch (Command[0].ToUpper())
            {
            //CONNECT "IP" ":" "PUERTO" "USUARIO"
            case "CONNECT":
                Regex rgx = new Regex(@"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$");
                if (rgx.IsMatch(Command[1]))
                {
                    int Puerto;
                    //Se verifica que sea un puerto correcto
                    try
                    {
                        if (Command[1] == LocalIPAddress() && Convert.ToInt32(Command[3]) == port)
                        {
                            throw new Exception("No es posible conectarse a tu misma sesión de chat.");
                        }

                        Puerto = Convert.ToInt32(Command[3]);
                        //Se genera la suscripción al puerto, con la ip especificada.
                        Subscribe1(Puerto, Command[1]);
                        //Ejecuta el método del servidor para suscribirse (+1 FOLLOWER).
                        //Clave USUARIO, Valor: IP, Puerto

                        string Answer = remoteObject.Connect(Command[4], LocalIPAddress() + "," + port);

                        if (Answer.Contains("Ya existe"))
                        {
                            throw new Exception(Answer);
                        }

                        Console.WriteLine(Answer);

                        //Guarda la suscripción en la lista propia (+1 FOLLOWING).
                        //Clave USUARIO, Valor: IP, Puerto
                        Subscribe1(port, LocalIPAddress());
                        remoteObject.AddFollowing(Command[4], Command[1] + "," + Puerto);
                    }

                    catch (Exception ex)
                    {
                        throw new Exception(ex.Message);
                    }
                }

                else
                {
                    throw new Exception("La IP ingresada no contiene el formato correcto");
                }
                break;

            case "LIST":

                //Mostrar la lista de chats suscritos a mi.
                if (Command[1].ToUpper() == "CONNECTED")
                {
                    Subscribe1(port, LocalIPAddress());
                    Console.WriteLine(remoteObject.Connected());
                }

                //Mostrar la lista de chats a los que estoy suscrito
                else if (Command[1].ToUpper() == "SENDER")
                {
                    Subscribe1(port, LocalIPAddress());
                    ShowFollowing(remoteObject.Sender());
                }

                else if (Command[1].ToUpper() == "DIR")
                {
                    //Obtiene la lista de chats a los que yo sigo.
                    Subscribe1(port, LocalIPAddress());
                    Dictionary <string, string> Suscripciones = new Dictionary <string, string>();
                    Suscripciones = remoteObject.Sender();

                    if (Suscripciones.Count == 0)
                    {
                        throw new Exception("No estás conectado a ningún chat para solicitar rutas.");
                    }

                    //Manda a solicitar la información de cada path, de cada uno de los chats obtenidos.
                    foreach (KeyValuePair <string, string> Pair in Suscripciones)
                    {
                        try
                        {
                            string[] UserData = Pair.Value.Split(',');
                            Subscribe1(Convert.ToInt32(UserData[1]), UserData[0]);
                            Console.WriteLine("\t" + Pair.Key + ": " + remoteObject.Path(Command[2]));
                        }

                        catch (Exception ex)
                        {
                            Console.WriteLine("Error al procesar el comando: " + ex.Message);
                        }
                    }
                }

                else
                {
                    throw new Exception("No se reconoce la cadena '" + Command[1] + "'");
                }
                break;

            case "KILL":

                if (Command[1].ToUpper() == "USER")
                {
                    //Obtengo el objeto de MI máquina server.
                    Subscribe1(port, LocalIPAddress());
                    string   Answer    = remoteObject.KillUser(Command[2]);
                    string[] Respuesta = Answer.Split(',');
                    Subscribe1(Convert.ToInt32(Respuesta[1]), Respuesta[0]);
                    remoteObject.SendMessage("Usted ha sido removido de mi lista de suscriptores. No recibirá más mensajes de este servidor", "Servidor con Ip: " + LocalIPAddress() + ", Puerto: " + port);

                    Console.WriteLine(remoteObject.DeleteFollowing(Command[2]));
                }

                else
                {
                    throw new Exception("Se esperaba 'USER'");
                }
                break;

            case "SEND":

                Subscribe1(port, LocalIPAddress());
                Dictionary <string, string> Dummy = new Dictionary <string, string>();

                //Cambio
                Dummy = remoteObject.Sender();

                if (Dummy.Count == 0)
                {
                    throw new Exception("No estás conectado a ningún chat para hacer un broadcast.");
                }

                foreach (KeyValuePair <string, string> Pair in Dummy)
                {
                    try
                    {
                        string[] UserData = Pair.Value.Split(',');
                        Subscribe1(Convert.ToInt32(UserData[1]), UserData[0]);
                        Command[0] = "";
                        Console.WriteLine(remoteObject.SendMessage(String.Join(" ", Command), Pair.Key.ToString()));
                    }

                    catch (Exception ex)
                    {
                        Console.WriteLine("Error al procesar el comando: " + ex.Message);
                    }
                }

                break;

            case "EXIT":
                bandera = false;

                //Se suscribe a su servidor
                Subscribe1(port, LocalIPAddress());

                //Obtiene su lista de followers para matarlos
                Dictionary <string, string> Sus = remoteObject.getFollowers();
                foreach (KeyValuePair <string, string> Pair in Sus)
                {
                    try
                    {
                        //Mata a sus followers para que se des-suscriban
                        string Ans = "";
                        Ans = remoteObject.KillUser(Pair.Key);
                    }

                    catch (Exception ex)
                    {
                        Console.WriteLine("Error al procesar el comando: " + ex.Message);
                    }
                }

                //Obtiene la lista de sus suscripciones
                Dictionary <string, string> Sen = remoteObject.Sender();

                foreach (KeyValuePair <string, string> Pair in Sen)
                {
                    try
                    {
                        //Obtiene la ip y puerto de la suscripcion
                        string[] UserData = Pair.Value.Split(',');

                        //Se conecta al servidor de esa suscripción
                        Subscribe1(Convert.ToInt32(UserData[1]), UserData[0]);
                        string Ans = "";

                        //Solicita a la suscripción que lo mate.
                        Ans = remoteObject.KillUser(Pair.Key);
                    }

                    catch (Exception ex)
                    {
                        Console.WriteLine("Error al procesar el comando: " + ex.Message);
                    }
                }

                break;

            case "CLEAR":
                Console.Clear();
                Console.WriteLine("PUERTO SERVER {0}", port);
                Console.WriteLine("**INGRESE UN COMANDO:");
                break;

            default:
                throw new Exception("No se reconoce el comando '" + Command[0] + "' ");
            }

            return(1);
        }