Пример #1
0
        private void CommandREQServerConnect(Connection newConnection, Data dato)
        {
            string[] tmp          = dato.Payload.Message.Split(':');//viene name: ip : port : user count
            string   serverName   = tmp[0];
            string   serverIp     = tmp[1];
            int      serverPort   = int.Parse(tmp[2]);
            int      serverTxPort = int.Parse(tmp[3]);
            int      userCount    = int.Parse(tmp[4]);

            newConnection.IsServer     = true;
            newConnection.Ip           = serverIp;
            newConnection.Name         = serverName;
            newConnection.Port         = serverPort;
            newConnection.TransferPort = serverTxPort;
            newConnection.UserCount    = userCount;


            SingletonServerConnection ssc = SingletonServerConnection.GetInstance();
            Connection oldConnection      = ssc.GetServer(serverName);

            if (oldConnection != null)
            {
                ssc.RemoveServer(serverName);
                oldConnection.CloseConn();
            }

            ssc.AddServer(serverName, newConnection);
            log.InfoFormat("Agregado nuevo servidor: {0}:{2} , name:{1}, userCount:{3}",
                           newConnection.Ip, newConnection.Name, newConnection.Port, newConnection.UserCount);
            SendMessage(newConnection, Command.RES, OpCodeConstants.REQ_SERVER_CONNECT, new Payload("SUCCESS"));
        }
Пример #2
0
        private void CommandREQADDContact(Connection clientConnection, Data dato)
        {
            //en la trama viene: login que hace el request|contacto a agregar
            string login      = dato.Payload.Message.Split(ParseConstants.SEPARATOR_PIPE)[0];
            string contacto   = dato.Payload.Message.Split(ParseConstants.SEPARATOR_PIPE)[1];
            string serverName = UsersPersistenceHandler.GetInstance().GetServerName(login);

            if (serverName != null)
            {
                Connection serverConnection = SingletonServerConnection.GetInstance().GetServer(serverName);
                foreach (var item in dato.GetBytes())
                {
                    Console.WriteLine("Enviando peticion de agregar contacto al servidor");
                    serverConnection.WriteToStream(item);
                }
            }
            //agrega login a la lista de contacots de contacto
            serverName           = UsersPersistenceHandler.GetInstance().GetServerName(contacto);
            dato.Payload.Message = contacto + ParseConstants.SEPARATOR_PIPE + login;
            if (serverName != null)
            {
                Connection serverConnection = SingletonServerConnection.GetInstance().GetServer(serverName);
                foreach (var item in dato.GetBytes())
                {
                    Console.WriteLine("Enviando peticion de agregar contacto al servidor");
                    serverConnection.WriteToStream(item);
                }
            }
        }
Пример #3
0
        private void CommandREQContactList(Connection clientConnection, Data dato)
        {
            string login      = dato.Payload.Message;
            string serverName = UsersPersistenceHandler.GetInstance().GetServerName(login);

            if (serverName != null)
            {
                Connection serverConnection = SingletonServerConnection.GetInstance().GetServer(serverName);
                foreach (var item in dato.GetBytes())
                {
                    Console.WriteLine("Enviando peticion de lista de contactos al servidor");
                    serverConnection.WriteToStream(item);
                }
            }
        }
Пример #4
0
 public void Logout(Connection clientConnection)
 {
     if (clientConnection.IsServer)
     {
         log.InfoFormat("Desconectando el servidor {0}", clientConnection.Name);
         Console.WriteLine("Desconectando el servidor {0}", clientConnection.Name);
         SingletonServerConnection.GetInstance().RemoveServer(clientConnection.Name);
     }
     else
     {
         log.InfoFormat("Desconectando el cliente {0}", clientConnection.Name);
         Console.WriteLine("Desconectando el cliente {0}", clientConnection.Name);
         NotifyUserChangedStatus(clientConnection.Name, MessageConstants.STATUS_OFFLINE);
         SingletonClientConnection.GetInstance().RemoveClient(clientConnection.Name);
     }
 }
Пример #5
0
        private void CommandREQGetServers(Connection connection, Data dato)
        {
            //string login = dato.Payload.Message.Split('|')[0];
            List <ServerInfo> servers = SingletonServerConnection.GetInstance().GetServersWithUsers();
            StringBuilder     message = new StringBuilder();

            if (servers.Count == 0)
            {
                message.Append(MessageConstants.MESSAGE_ERROR).Append(ParseConstants.SEPARATOR_PIPE).Append("No hay Servidores en linea");
            }
            else
            {
                bool first = true;
                foreach (var item in servers)
                {
                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        message.Append(ParseConstants.SEPARATOR_PIPE);
                    }
                    message.Append(item.ToNetworkString());
                }
            }
            Data outData = new Data()
            {
                Command = Command.RES,
                OpCode  = OpCodeConstants.RES_GET_SERVERS,
                Payload = new MultiplePayload()
                {
                    Message = message.ToString(), Destination = dato.Payload.Message
                }
            };

            foreach (var item in outData.GetBytes())
            {
                connection.WriteToStream(item);
            }
        }
Пример #6
0
        private void CommandREQServerInfo(Connection clientConnection, Data dato)
        {
            //en el payload viene el login que solicita la info
            string        serverName       = UsersPersistenceHandler.GetInstance().GetServerName(dato.Payload.Message);
            Connection    serverConnection = SingletonServerConnection.GetInstance().GetServer(serverName);
            StringBuilder sb = new StringBuilder();

            if (serverConnection == null)
            {
                //el servidor no esta online, respondo con error
                sb.Append(MessageConstants.MESSAGE_ERROR);
            }
            else
            {
                //serverName|serverIp|serverPort|transfersPort
                sb.Append(serverConnection.Name).Append(ParseConstants.SEPARATOR_PIPE);
                sb.Append(serverConnection.Ip).Append(ParseConstants.SEPARATOR_PIPE);
                sb.Append(serverConnection.Port).Append(ParseConstants.SEPARATOR_PIPE);
                sb.Append(serverConnection.TransferPort);
            }
            SendMessage(clientConnection, Command.RES, OpCodeConstants.RES_SERVER_INFO, new Payload(sb.ToString()));
        }
Пример #7
0
        private bool AddUserToServer(string login, string serverName)
        {
            try
            {
                int count = SingletonServerConnection.GetInstance().IncUserCount(serverName);
                if (count > 0)
                {
                    //si se agrego en el dns, entonces pido al server que lo agregue, pero no espero confirmacion., las operaciones de un usuario sobe el server van a chequear que exista el usuario, y si no existe, lo va a crear.
                    SendMessage(SingletonServerConnection.GetInstance().GetServer(serverName), Command.REQ,
                                OpCodeConstants.REQ_CREATE_USER, new Payload(login));
                    Console.WriteLine("El server {0} queda con {1} usuarios registrados", serverName, count);
                }
                else
                {
                    throw new Exception("No se encontro el sevidor en la lista de Online");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error al agregar usuario al servidor: {0}", e.Message);
            }

            return(true);
        }
Пример #8
0
 private string FindAGoodServer()
 {
     return(SingletonServerConnection.GetInstance().FindBestServerForNewUser());
 }
Пример #9
0
        private void CommandREQLogin(Connection clientConnection, Data dato)
        {
            string login = dato.Payload.Message;
            bool   ret   = true;

            if (!UsersPersistenceHandler.GetInstance().IsLoginRegistered(login))
            {
                try
                {
                    string serverName = FindAGoodServer();
                    //agrega el usuario y el server al registro de usuario-server
                    bool ok = UsersPersistenceHandler.GetInstance().RegisterLoginServer(login, serverName);
                    if (ok)
                    {
                        //aumenta el contador de usuarios por servidor, luego que el server agreaga el usuario
                        //ret = UsersPersistenceHandler.GetInstance().RegisterLoginServer(login, serverName);
                        AddUserToServer(login, serverName);
                    }
                    else
                    {
                        ret = false;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error : {0}", e.Message);
                    ret = false;
                }
            }

            if (ret)//si esta registrado
            {
                SingletonClientConnection scc = SingletonClientConnection.GetInstance();
                Connection oldConnection      = scc.GetClient(login);
                if (oldConnection == null)
                {
                    if (SingletonServerConnection.GetInstance().GetServer(UsersPersistenceHandler.GetInstance().GetServerName(login)) != null)
                    {
                        scc.AddClient(login, clientConnection);
                        SendMessage(clientConnection, Command.RES, OpCodeConstants.REQ_LOGIN, new Payload("SUCCESS"));
                    }
                    else
                    {
                        SendMessage(clientConnection, Command.RES, OpCodeConstants.REQ_LOGIN, new Payload("ERROR Servidor Offline"));
                        clientConnection.CloseConn();
                        ret = false;
                    }
                }
                else
                {
                    SendMessage(clientConnection, Command.RES, OpCodeConstants.REQ_LOGIN, new Payload("ERROR Login en uso"));
                    clientConnection.CloseConn();
                    ret = false;
                }
            }
            else
            {
                SendMessage(clientConnection, Command.RES, OpCodeConstants.REQ_LOGIN, new Payload("ERROR No se pudo registrar el login nuevo (server offline??)"));
                clientConnection.CloseConn();
            }
        }