Exemplo n.º 1
0
        private void CommandGetContactList(Connection Connection, Data dato)
        {
            string        login    = dato.Payload.Message;
            List <string> contacts = UsersContactsPersistenceHandler.GetInstance().GetContacts(login);

            StringBuilder message = new StringBuilder();
            bool          first   = true;

            foreach (var item in contacts)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    message.Append(CONTACT_DELIMITER);
                }
                message.Append(item).Append(STATUS_DELIMITER).Append("0");
            }
            Data retDato = new Data()
            {
                Command = Command.RES, OpCode = OpCodeConstants.RES_CONTACT_LIST, Payload = new MultiplePayload()
                {
                    Message = message.ToString(), Destination = login
                }
            };

            foreach (var item in retDato.GetBytes())
            {
                Console.WriteLine("Envio :{0}", ConversionUtil.GetString(item));
                Connection.WriteToStream(item);
            }
            Console.WriteLine("termina :CommandGetContactList");
        }
Exemplo n.º 2
0
        private void CommandAddContact(Connection Connection, Data dato)
        {
            string[] payloadSplitted = dato.Payload.Message.Split('|');
            string   login           = payloadSplitted[0];
            string   contactToAdd    = payloadSplitted[1];

            bool ok = UsersContactsPersistenceHandler.GetInstance().AddContact(login, contactToAdd);

            string statusMessage = ok ? "SUCCESS" : "ERROR";
            string message       = contactToAdd + STATUS_DELIMITER + "0" + CONTACT_DELIMITER + statusMessage;

            Data retDato = new Data()
            {
                Command = Command.RES,
                OpCode  = OpCodeConstants.RES_ADD_CONTACT,
                Payload = new MultiplePayload()
                {
                    Message = message, Destination = login
                }
            };

            foreach (var item in retDato.GetBytes())
            {
                Console.WriteLine("Envio :{0}", ConversionUtil.GetString(item));
                Connection.WriteToStream(item);
            }
        }
Exemplo n.º 3
0
        public void SetupConn()  // Setup connection and login
        {
            connection = new Connection("DNS", new TcpClient(DNSServer, DNSPort), new ReceiveEventHandler(), delegado);


            string payload = Settings.GetInstance().GetProperty("server.name", "rodrigo-nb")
                             + ":" + Settings.GetInstance().GetProperty("server.ip", "127.0.0.1")
                             + ":" + Settings.GetInstance().GetProperty("server.port", "2001")
                             + ":" + Settings.GetInstance().GetProperty("server.transfers.port", "20001")
                             + ":" + UsersContactsPersistenceHandler.GetInstance().Count;

            Data data = new Data()
            {
                Command = Command.REQ,
                OpCode  = 3,//REQ_SERVER_CONNECT
                Payload = new Payload(payload)
            };

            int cont = 0;

            foreach (var item in data.GetBytes())
            {
                Console.WriteLine("line " + cont++ + "   --->" + ConversionUtil.GetString(item));
                log.Info("Enviando linea " + cont++ + "   --->" + ConversionUtil.GetString(item));
                connection.WriteToStream(item);
            }

            log.Info("End Register Server");
        }
Exemplo n.º 4
0
        Connection.ConnectionDroppedDelegate dnsCaeDelegado;//delegado = new Connection.ConnectionDroppedDelegate(OneConnectionDroppedEvent);

        public Program()
        {
            log4net.Config.XmlConfigurator.Configure(new FileInfo("log4net.config"));
            log4net.GlobalContext.Properties["serverName"] = Settings.GetInstance().GetProperty("server.name", "rodrigo-nb");
            log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

            Console.Title = "Servidor De Contenidos: " + Settings.GetInstance().GetProperty("server.name", "rodrigo-nb").ToUpper();
            Console.WriteLine("----- Servidor De Contenidos -----");
            UsersContactsPersistenceHandler.GetInstance().LoadContacts();
            log.Info("contactos cargados");
            Console.WriteLine("contactos cargados");

            Console.WriteLine("[{0}] Starting server...", DateTime.Now);

            string listenAddressStr = Settings.GetInstance().GetProperty("listen.ip", "ANY");

            IPAddress ip            = "ANY".Equals(listenAddressStr) ? IPAddress.Any : IPAddress.Parse(listenAddressStr);
            int       port          = int.Parse(Settings.GetInstance().GetProperty("server.port", "2001"));
            int       portTransfers = int.Parse(Settings.GetInstance().GetProperty("server.transfers.port", "20001"));


            transferServer = new ListeningServer(true, "Transfers", ip, portTransfers);
            controlServer  = new ListeningServer(false, "Control", ip, port);


            Console.WriteLine("[{0}] Server is running properly!", DateTime.Now);
            log.Info("Server is running properly!");



            if (!DEBUG)
            {
                dnsCaeDelegado = new Connection.ConnectionDroppedDelegate(DisconnectServers);
                Console.WriteLine("[{0}] Connecting Dns...", DateTime.Now);
                dns = new DNSConnection(dnsCaeDelegado);
                try
                {
                    dns.SetupConn();
                }
                catch (System.Net.Sockets.SocketException e)
                {
                    log.ErrorFormat("No se pudo registrar con el DNS, terminando", e);
                    running  = false;
                    dnsError = true;
                    //  throw new Exception("No se pudo registrar con el DNS, terminando", e);
                }
                Console.WriteLine("[{0}] DNS connection ready...", DateTime.Now);
            }
            else
            {
                Console.WriteLine("[{0}] Modo DEBUG Sin conezion al Dns...", DateTime.Now);
            }
        }
Exemplo n.º 5
0
        private void CommandCreateNewUser(Connection Connection, Data dato)
        {
            string login   = dato.Payload.Message;
            bool   ok      = UsersContactsPersistenceHandler.GetInstance().RegisterNewUser(login);
            Data   retDato = new Data()
            {
                Command = Command.RES,
                OpCode  = OpCodeConstants.RES_CREATE_USER,
                Payload = new MultiplePayload()
                {
                    Message = (ok? "SUCCESS" : "ERROR"), Destination = login
                }
            };

            foreach (var item in retDato.GetBytes())
            {
                Console.WriteLine("Envio :{0}", ConversionUtil.GetString(item));
                Connection.WriteToStream(item);
            }
        }