public bool SendAllHashes()
        {
            if (!RegistrationProcessStarted)
            {
                return(false);
            }
            else
            {
                RegistrationProcessStarted = true;
                sendAllInProgress          = true;
                int count = Sizes.GetChunksNumber(fd.FileSize);

                for (int i = 0; i < count - 1; i++)
                {
                    if (i != count - 1)
                    {
                        if (ReadChunk(0) == null)
                        {
                            return(false);
                        }
                        SendHash(i, MakeHash(ReadChunk(i)));
                    }
                }
                SendHash(count - 1, MakeHash(ReadLastChunk(count - 1)));
                sendAllInProgress = false;
                return(true);
            }
        }
Ejemplo n.º 2
0
        public void InitReceiver(FileDescription fd)
        {
            this.fd = fd;
            hm      = new HashManager(-1, Sizes.GetChunksNumber(fd.FileSize), fd.FileId, this);
            DataMessageProvider dmp = new DataMessageProvider(SocketResourcesManager.srm.ProvideBuffer());
            OutputMessage       om  = dmp.ProvideFileHashTransferRequestMessage(fd.FileId);

            ClientCommunicationCenter.commCenter.SendMessageToServer(om);
        }
Ejemplo n.º 3
0
        public bool CheckChunkHash(Chunk chunk)
        {
            int chunkCount = Sizes.GetChunksNumber(this.FileDescription.FileSize);

            byte[] correctHash    = fileManager.ReadHashFromDescriptor(chunk.ChunkId, chunkCount);
            byte[] calculatedHash = new SHA256Managed().ComputeHash(chunk.Data);
            for (int i = 0; i < Sizes.HashSizeByte; i++)
            {
                if (correctHash[i] != calculatedHash[i])
                {
                    return(false);
                }
            }
            return(true);
        }
 public void ResendHash(int chunkId)
 {
     if (sendAllInProgress)
     {
         return;
     }
     if (chunkId != Sizes.GetChunksNumber(fd.FileSize) + 1)
     {
         SendHash(chunkId, MakeHash(ReadChunk(chunkId)));
     }
     else
     {
         SendHash(chunkId, MakeHash(ReadLastChunk(chunkId)));
     }
 }
Ejemplo n.º 5
0
        public FileState ProvideFileState(int fileId)
        {
            FileDescription fd = FileDescriptions[fileId];

            string fullName = fd.FileName;

            if (fd.FileExtension != null && fd.FileExtension != "")
            {
                fullName += "." + fd.FileExtension;
            }
            FileStream mainFileStream = new FileStream(fd.Path + "\\" + fullName, FileMode.Open, FileAccess.ReadWrite);

            if (fd.Fts != FileTransferState.Deleted && fd.Fts != FileTransferState.Finished)
            {
                FileStream descriptorFile = new FileStream(".\\descriptors\\descriptor_" + fullName + ".desc", FileMode.Open, FileAccess.ReadWrite);
                FileState  fs             = new FileState();
                fs.FileDescription = fd;

                byte[] chunksState = new byte[Sizes.GetChunksNumber(fd.FileSize)];
                descriptorFile.Seek(0, SeekOrigin.Begin);
                int numberOfChunks = Sizes.GetChunksNumber(fd.FileSize);
                descriptorFile.Read(chunksState, 0, numberOfChunks);

                fs.ChunksState = Utils.GetBitForByte(chunksState);
                int downloaded = 0;
                foreach (bool b in fs.ChunksState)
                {
                    if (b == true)
                    {
                        downloaded++;
                    }
                }
                fs.FinishedChunksNumber = downloaded;

                fs.FileManager = new FileIOManager(mainFileStream, descriptorFile);
                return(fs);
            }
            else
            {
                FileState fs = new FileState();
                fs.FileDescription      = fd;
                fs.FinishedChunksNumber = fs.NumberOfChunks;
                fs.FileManager          = new FileIOManager(mainFileStream);
                return(fs);
            }
        }
        private void CreateDescriptorFile(FileDescription fd, ChunkHash[] hashes)
        {
            FileStream fileStream = new FileStream(".\\descriptors\\descriptor_" + fd.FileName + "." + fd.FileExtension + ".desc", FileMode.Create, FileAccess.Write);

            byte[] data = new byte[Sizes.GetChunksNumber(fd.FileSize)];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = 0;
            }
            fileStream.Write(data, 0, data.Length);

            foreach (ChunkHash h in hashes)
            {
                fileStream.Write(h.Hash, 0, h.Hash.Length);
            }
            fileStream.Flush();
            fileStream.Close();
        }
        public void HandleRequestFileUpload(C2SMessageReader mr)
        {
            if (!ServerCommunicationCenter.commCenter.ClientsEndPoint.ContainsKey(mr.ClientEP()))
            {
                return;
            }
            Debug.WriteLine("SR: Received request for registration");
            ServerMessageProvider smp = new ServerMessageProvider(SocketResourcesManager.srm.ProvideBuffer());
            OutputMessage         om;
            string fileFullName = mr.ReadRegisteringFileFullName();
            string ext          = Utils.GetFileExtensionFromPath(fileFullName);
            string name         = Utils.GetFileNameFromPath(fileFullName);

            bool duplikat = File.Exists(name, ext, mr.ReadRegisteringFileSize());

            if (!duplikat)
            {
                File f = new File();
                f.ApplicationId = mr.ClientEP();
                f.FileExtension = ext;
                f.FileName      = name;
                f.FileSize      = mr.ReadRegisteringFileSize();
                int fileId = f.Add();
                om = smp.ProvidePositiveUploadFileMessage(fileId);
                HashManager shr = new HashManager(mr.ClientEP(), Sizes.GetChunksNumber(mr.ReadRegisteringFileSize()), fileId, new ServerHashManagerListener());
                Debug.WriteLine("File: " + fileId);
                Debug.WriteLine("FileSize: " + f.FileSize);
                ServerObjectGraph.sog.FileRegistrations.Add(fileId, shr);
                Debug.WriteLine("SR: Registration approved");
            }
            else
            {
                om = smp.ProvideNegativeUploadFileMessage();
            }
            ServerCommunicationCenter.commCenter.SendMessageTo(mr.ClientEP(), om);
        }