Пример #1
0
        private void CommandGetFile(Connection connection, Data dato)
        {
            string[] payload  = dato.Payload.Message.Split(new string[] { PIPE_SEPARATOR }, StringSplitOptions.None);
            string   login    = payload[0];
            string   owner    = payload[1];
            string   hashfile = payload[2];

            FileInfo fi = FileOperationsSingleton.GetInstance().GetFile(hashfile, login);
            Data     retDato;

            if (fi == null)
            {
                retDato = new Data()
                {
                    Command = Command.RES,
                    OpCode  = OpCodeConstants.RES_SEARCH_FILES,
                    Payload = new Payload()
                    {
                        Message = "ERROR [wrong file or not found]"
                    }
                };
            }
            else
            {
                string port = Settings.GetInstance().GetProperty("server.transfers.port", "20001");
                string ip   = Settings.GetInstance().GetProperty("server.ip", "127.0.0.1");

                long size = fi.Length;

                string message = login + PIPE_SEPARATOR + owner + PIPE_SEPARATOR + hashfile + PIPE_SEPARATOR + ip + PIPE_SEPARATOR + port + PIPE_SEPARATOR + size;

                retDato = new Data()
                {
                    Command = Command.RES,
                    OpCode  = OpCodeConstants.RES_SEARCH_FILES,
                    Payload = new Payload()
                    {
                        Message = message
                    }
                };
            }

            foreach (var item in retDato.GetBytes())
            {
                Console.WriteLine("Envio :{0}", ConversionUtil.GetString(item));
                connection.WriteToStream(item);
            }
        }
        //solo se permite ageagar un usuario nuevo a la vez, crea el area compartida y lo da de alta en la lista de contactos con 0 contactos y salva el archivo
        private bool AddNewUser(string login)
        {
            if (!contacts.ContainsKey(login))
            {
                //ok si el directorio se cra o si ya existe
                bool ok = FileOperationsSingleton.GetInstance().CreateDiskSharedSpace(login);

                if (ok)
                {
                    contacts.Set(login, "");
                    contacts.Save();
                }
                return(ok);
            }
            return(false);
        }
Пример #3
0
        private void CommandSearchFiles(Connection connection, Data dato)
        {//REQ07
         // login + "|" + hashQuery + "|" + pattern;


            string[]          payload   = dato.Payload.Message.Split(new string[] { PIPE_SEPARATOR }, StringSplitOptions.None);
            string            login     = payload[0];
            string            queryHash = payload[1];
            string            pattern   = payload[2];
            List <FileObject> results   = FileOperationsSingleton.GetInstance().SearchFilesMatching(pattern);

            StringBuilder message     = new StringBuilder();
            string        destination = login + ARROBA_SEPARATOR + queryHash + ARROBA_SEPARATOR + Settings.GetInstance().GetProperty("server.name", "DEFAULT_SERVER");

            bool first = true;

            foreach (var item in results)
            {
                if (first)
                {
                    first = false;
                }
                else
                {
                    message.Append(PIPE_SEPARATOR);
                }
                message.Append(item.ToNetworkString());
            }

            string tmp = message.ToString();

            Data retDato = new Data()
            {
                Command = Command.RES,
                OpCode  = OpCodeConstants.RES_SEARCH_FILES,
                Payload = new MultiplePayload()
                {
                    Message = tmp, Destination = destination
                }
            };

            foreach (var item in retDato.GetBytes())
            {
                Console.WriteLine("Envio :{0}", ConversionUtil.GetString(item));
                connection.WriteToStream(item);
            }
        }
Пример #4
0
        private bool CommandDownloadFile(Connection connection, Data dato)
        {
            // login + "|" + owner + "|"+hashfile;
            string[] payload  = dato.Payload.Message.Split(ParseConstants.SEPARATOR_PIPE);
            string   login    = payload[0];
            string   owner    = payload[1];
            string   hashfile = payload[2];

            FileInfo fi = FileOperationsSingleton.GetInstance().GetFile(hashfile, owner);

            //   Data retDato;
            if (fi == null)
            {
                /*
                 * retDato = new Data()
                 * {
                 *  Command = Command.RES,
                 *  OpCode = OpCodeConstants.RES_DOWNLOAD_FILE,
                 *  Payload = new Payload() { Message = "ERROR [wrong file or not found]" }
                 * };
                 * foreach (var item in retDato.GetBytes())
                 * {
                 *  Console.WriteLine("Envio :{0}", ConversionUtil.GetString(item));
                 *  connection.WriteToStream(item);
                 * }
                 * */
                return(false);
            }
            else
            {
                long size = fi.Length;

                log.Info("Espero antes de mandar el binario");
                //  Thread.Sleep(2000);
                FileStream fileStream = fi.OpenRead();
                const int  BUFF_SIZE  = 10000;
                byte[]     buffer     = new byte[BUFF_SIZE];
                long       sentData   = 0;
                bool       done       = false;
                try
                {
                    while (!done)
                    {
                        int countRead = fileStream.Read(buffer, 0, BUFF_SIZE);
                        //   Thread.Sleep(20);
                        log.DebugFormat("countRead={0}", countRead);
                        sentData += countRead;
                        if (countRead > 0)
                        {
                            if (sentData == size)
                            {
                                done = true;
                            }
                            connection.WriteToNetworkStream(buffer, 0, countRead);
                            log.DebugFormat("Enviando {2} - {0}: {3}  - {1}", countRead, size, fi.FullName, sentData);
                            //   Thread.Sleep(2000);
                        }
                        else
                        {//no leyo nada de la entrada (cantidad de bytes justa, en la siguiente lectura)
                            done = true;
                        }
                    }
                    connection.FlushNetworkStream();
                }
                catch (Exception e)
                {
                    log.Error("descarga", e);
                }
                fileStream.Close();
                log.DebugFormat("Archivo puesto todo en el stream , y crerrado el file local");
            }
            return(true); //terminar la conexion
        }