Ejemplo n.º 1
0
        /// <summary>
        /// delegato che inserisce il file in questione nell'appropriato hashSet
        /// </summary>
        /// <param name="fi"></param>
        private void checkFile(System.IO.FileInfo fi)
        {
            //MyLogger.add(fi.Name + "\n");
            RecordFile thisFile = new RecordFile(fi);

            if (fi.Length == 0)
            {
                return;
            }
            FileStatus status = dim.UpdateStatus(thisFile);

            switch (status)
            {
            case FileStatus.New:
                MyLogger.print("nuovo: ");
                newFiles.Add(thisFile);
                MyLogger.print(fi.Name + "\n");
                break;

            case FileStatus.Updated:
                MyLogger.print("aggiornato: ");
                updatedFiles.Add(thisFile);
                MyLogger.print(fi.Name + "\n");
                break;

            case FileStatus.Old:
                MyLogger.debug("vecchio: ");
                MyLogger.debug(fi.Name + "\n");
                //nothing to do
                break;
            }
            //MyLogger.add(thisFile);
        }
Ejemplo n.º 2
0
        //~DirImageManager()
        //{
        //    storeDirImage();
        //}
        /// <summary>
        /// Verify if the RecordFile passed is new/updated/old by comparing with the last calculated state of the directory.
        /// Can't return deleted files. Use getDeleted() at the end of all the updates.
        /// </summary>
        /// <param name="rf"></param>
        /// <returns></returns>
        public FileStatus UpdateStatus(RecordFile rf)
        {
            if (!Updating)
            {
                Updating = true;
                //crea elenco dei deleted pieno inizialmente
                //deletedRecord = new Dictionary<string, RecordFile>(dirImage);
                deletedFileNames = new HashSet<string>(dirImage.Keys);
            }

            //cerca tra i già presenti
            RecordFile match;
            if (dirImage.TryGetValue(rf.nameAndPath, out match))
            {
                //se lo trovi elimina la voce dalla lista dei deletedFiles
                deletedFileNames.Remove(rf.nameAndPath);
                //RecordFile newRf = new RecordFile(rf);
                if (rf.Equals(match))
                    //i due file sono identici, non cambio nulla
                    return FileStatus.Old;
                else
                    //è aggiornato
                    dirImage.Remove(rf.nameAndPath);
                    //dirImage.Add(rf.nameAndPath, rf); add è fatto solo tramite ConfirmSync (quando ho finito di sincronizzarlo con il server)
                    return FileStatus.Updated;
            }
            else
            {
                //se non lo trovi è new
                //aggiungilo a DirImage
                //dirImage.Add(rf.nameAndPath, rf); add è fatto solo tramite ConfirmSync (quando ho finito di sincronizzarlo con il server)
                return FileStatus.New;
            }
        }
Ejemplo n.º 3
0
 public RecordFile(RecordFile rf)
 {
     this.nameAndPath  = rf.nameAndPath;
     this.hash         = rf.hash;
     this.size         = rf.size;
     this.lastModified = rf.lastModified;
 }
Ejemplo n.º 4
0
 public void toSendFormatTest()
 {
     RecordFile rf = new RecordFile("C:\\ciao.txt", "abcdefghabcdefghabcdefghabcdefgh", 15, new DateTime(1970, 1, 1, 1, 0, 0));
     string s = rf.toSendFormat();
     string rs = "C:\\ciao.txt\r\n00000000000Fabcdefghabcdefghabcdefghabcdefgh0000000000000000\r\n";
     Assert.AreEqual(s, rs);
 }
Ejemplo n.º 5
0
 // aggiunge i recordFile e la sua versione alle RecoverInfos eliminando eventuali
 // ripetizioni di recordFile (stesso nome && stesso hash && stessa ultima mod).
 // poichè la stessa versione dello stesso file può comparire più volte sul server
 //con numero di backup diverso,
 // la addRecoverRecord mantiene una sola entry in questo caso, la prima che riceve.
 private void addRecoverRecord(RecordFile rf, int backupVersion)
 {
     lock (this)
     {
         recInfos.Add(new RecoverRecord(rf, backupVersion));
         BackupVersionNumbers.Add(backupVersion);
     }
 }
Ejemplo n.º 6
0
 internal void confirmSync(RecordFile rf, bool deleting)
 {
     if (!deleting)
     {
         dirImage.Add(rf.nameAndPath, rf);
     }
     else
     {
         dirImage.Remove(rf.nameAndPath);
     }
 }
Ejemplo n.º 7
0
 internal void syncNewFiles(RecordFile rf)
 {
     MyLogger.debug("newing file: " + rf.nameAndPath);
     sendToServer(commNewFile);
     waitForAck(commCmdAckFromServer);
     //aspettare eventuale MISS_BCK o BACKUPOK
     if (strRecCommFromServer() == commBackupOkFromServer)
     {
         SendWholeFileToServer(rf);
         MyLogger.debug("newed\n");
     }
     else
     {
         throw new AckErrorException();
     }
 }
Ejemplo n.º 8
0
        //però se ho tutto uguale e solo la lastModified diversa dovrei considerarla la stessa????
        //forse non dovrei avere la lastModified in memoria?
        public override bool Equals(object obj)
        {
            RecordFile r = obj as RecordFile;

            if (r == null)
            {
                return(false);
            }
            if (nameAndPath.Equals(r.nameAndPath) &&
                hash.Equals(r.hash) &&
                //size.Equals(r.size) && il size può essere a -1
                lastModified.Equals(r.lastModified))
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 9
0
        private void SendWholeFileToServer(RecordFile rf)
        {
            int tryes = 0;

            while (tryes < 6)
            {
                //invio info del file
                sendToServer(rf.toSendFormat());
                //check ack
                string resp = strRecCommFromServer();
                if (resp != commInfoAckFromServer)
                {
                    if (resp == commSndAgain)
                    {
                        tryes++;
                        continue;
                    }
                    else
                    {
                        throw new AckErrorException();
                    }
                }

                //invio contenuto del file
                sendFileContent(rf);

                //check ack
                resp = strRecCommFromServer();
                if (resp != commDataAck)
                {
                    if (resp == commSndAgain)
                    {
                        tryes++;
                        continue;
                    }
                    else
                    {
                        throw new AckErrorException();
                    }
                }
                break;
            }
        }
Ejemplo n.º 10
0
        private void sendFileContent(RecordFile f)
        {
            try
            {
                const int bufsize      = 1024;
                var       buffer       = new byte[bufsize];
                int       actuallyRead = 0;

                long sizeFile       = 0;
                long totalBytesRead = 0;
                long nextStep       = 0;
                long stepSize;

                using (var s = File.OpenRead(f.nameAndPath))
                {
                    sizeFile = s.Length;
                    stepSize = (long)Math.Ceiling((double)sizeFile / 10);

                    while ((actuallyRead = s.Read(buffer, 0, bufsize)) > 0)
                    {
                        serverStream.Write(buffer, 0, actuallyRead);

                        //aggiorna progress bar
                        totalBytesRead += actuallyRead;
                        if (totalBytesRead > nextStep)
                        {
                            mainWindow.Dispatcher.BeginInvoke(mainWindow.DelSetProgressValues, sizeFile, 0, totalBytesRead);
                            nextStep += stepSize;
                        }
                    }
                }
                serverStream.Flush();
                mainWindow.Dispatcher.BeginInvoke(mainWindow.DelSetProgressValues, sizeFile, 0, sizeFile);
            }
            catch (Exception ex)
            {
                MyLogger.debug(ex.ToString());
                MyLogger.print("errore leggendo il file");
                throw;
            }
        }
Ejemplo n.º 11
0
        internal void syncDeletedFile(RecordFile rf)
        {
            MyLogger.debug("deleting file: " + rf.nameAndPath);
            sendToServer(commDeleteFile);
            waitForAck(commCmdAckFromServer);

            sendToServer(rf.nameAndPath);

            waitForAck(commInfoAckFromServer);
            MyLogger.debug("deleted\n");
            string res = strRecCommFromServer();

            if (res == commDELETED)
            {
                MyLogger.debug("deleted\n");
            }
            if (res == commNOTDEL)
            {
                MyLogger.debug("file inesistente.\n");
            }
        }
Ejemplo n.º 12
0
        //~DirImageManager()
        //{
        //    storeDirImage();
        //}

        /// <summary>
        /// Verify if the RecordFile passed is new/updated/old by comparing with the last calculated state of the directory.
        /// Can't return deleted files. Use getDeleted() at the end of all the updates.
        /// </summary>
        /// <param name="rf"></param>
        /// <returns></returns>
        public FileStatus UpdateStatus(RecordFile rf)
        {
            if (!Updating)
            {
                Updating = true;
                //crea elenco dei deleted pieno inizialmente
                //deletedRecord = new Dictionary<string, RecordFile>(dirImage);
                deletedFileNames = new HashSet <string>(dirImage.Keys);
            }

            //cerca tra i già presenti
            RecordFile match;

            if (dirImage.TryGetValue(rf.nameAndPath, out match))
            {
                //se lo trovi elimina la voce dalla lista dei deletedFiles
                deletedFileNames.Remove(rf.nameAndPath);
                //RecordFile newRf = new RecordFile(rf);
                if (rf.Equals(match))
                {
                    //i due file sono identici, non cambio nulla
                    return(FileStatus.Old);
                }
                else
                {
                    //è aggiornato
                    dirImage.Remove(rf.nameAndPath);
                }
                //dirImage.Add(rf.nameAndPath, rf); add è fatto solo tramite ConfirmSync (quando ho finito di sincronizzarlo con il server)
                return(FileStatus.Updated);
            }
            else
            {
                //se non lo trovi è new
                //aggiungilo a DirImage
                //dirImage.Add(rf.nameAndPath, rf); add è fatto solo tramite ConfirmSync (quando ho finito di sincronizzarlo con il server)
                return(FileStatus.New);
            }
        }
Ejemplo n.º 13
0
 internal void syncUpdatedFile(RecordFile rf)
 {
     MyLogger.debug("updating file: " + rf.nameAndPath);
     sendToServer(commUpdFile);
     waitForAck(commCmdAckFromServer);
     //aspettare eventuale MISS_BCK o BACKUPOK
     if (strRecCommFromServer() == commBackupOkFromServer)
     {
         SendWholeFileToServer(rf);
         MyLogger.debug("updated\n");
     }
     else
         throw new AckErrorException();
 }
Ejemplo n.º 14
0
        internal void syncDeletedFile(RecordFile rf)
        {
            MyLogger.debug("deleting file: " + rf.nameAndPath);
            sendToServer(commDeleteFile);
            waitForAck(commCmdAckFromServer);

            sendToServer(rf.nameAndPath);

            waitForAck(commInfoAckFromServer);
            MyLogger.debug("deleted\n");
            string res = strRecCommFromServer();
            if (res == commDELETED)
            {
                MyLogger.debug("deleted\n");
            }
            if (res == commNOTDEL)
            {
                MyLogger.debug("file inesistente.\n");
            }
        }
Ejemplo n.º 15
0
        /// <summary>
        /// return true se ricezione corretta, false altrimenti
        /// </summary>
        /// <param name="fout">already opened output fileStream</param>
        /// <returns>last modify date of the file</returns>
        private System.DateTime RecFileContent(string FileNameAndPath, FileStream fout)
        {
            //ricezione hash
            byte[] hashReceived = new byte[32];
            var    count        = serverStream.Read(hashReceived, 0, 32);
            string strHash      = System.Text.Encoding.UTF8.GetString(hashReceived);

            //remove \r\n
            socketReadline();

            //legge dimensione del file
            long sizeFile = Convert.ToInt64(socketReadline(), 16);

            //legge data_ultima_modifica file
            var lmfile = MyConverter.UnixTimestampToDateTime(Convert.ToInt64(socketReadline(), 16));

            //data_rec
            sendToServer(commDataRec);

            int attempt = 0;

            do
            {
                //ricezione e salvataggio su disco.
                var  buffer = new byte[1024];
                int  bytesRead;
                long totalBytesRead = 0;
                fout.Seek(0, SeekOrigin.Begin);

                //step della progress bar
                long stepSize = (long)Math.Ceiling((double)sizeFile / 10);
                long nextStep = 0;

                do
                {
                    bytesRead = serverStream.Read(buffer, 0, buffer.Length);
                    fout.Write(buffer, 0, bytesRead);
                    totalBytesRead += bytesRead;

                    //aggiorna progress bar
                    if (totalBytesRead > nextStep)
                    {
                        mainWindow.recoverW.Dispatcher.BeginInvoke(mainWindow.recoverW.DelSetRecProgressValues, sizeFile, 0, totalBytesRead);
                        nextStep += stepSize;
                    }
                }while (totalBytesRead < sizeFile);

                mainWindow.recoverW.Dispatcher.BeginInvoke(mainWindow.recoverW.DelSetRecProgressValues, sizeFile, 0, sizeFile);

                if (totalBytesRead != sizeFile)
                {
                    sendToServer(commSndAgain);
                    attempt++;
                    if (attempt < 5)
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }

                //calcolo e confronto hash
                var computedHash = RecordFile.calcHash(FileNameAndPath, fout);
                if (computedHash == strHash)
                {
                    sendToServer(commDataAck);
                    return(lmfile);
                }
                else
                {
                    sendToServer(commSndAgain);
                    attempt++;
                }
            }while (attempt < 5);

            MyLogger.print("errore nel download del file. impossibile effettuare il ripristino.\n");
            //todo: catchare questa eccezione nelle funzioni chiamanti
            throw new CancelFileRequestException();
        }
Ejemplo n.º 16
0
 public RecoverRecord(RecordFile rf, int backupVersion)
 {
     this.rf            = rf;
     this.backupVersion = backupVersion;
 }
Ejemplo n.º 17
0
 public RecordFile(RecordFile rf)
 {
     this.nameAndPath = rf.nameAndPath;
     this.hash = rf.hash;
     this.size = rf.size;
     this.lastModified = rf.lastModified;
 }
Ejemplo n.º 18
0
 internal void confirmSync(RecordFile rf, bool deleting)
 {
     if (!deleting)
         dirImage.Add(rf.nameAndPath, rf);
     else
         dirImage.Remove(rf.nameAndPath);
 }
Ejemplo n.º 19
0
 internal void confirmSync(RecordFile f, bool deleting = false)
 {
     dim.confirmSync(f, deleting);
     //dim.storeDirImage(); non salvo + niente online
 }
Ejemplo n.º 20
0
 /// <summary>
 /// delegato che inserisce il file in questione nell'appropriato hashSet 
 /// </summary>
 /// <param name="fi"></param>
 private void checkFile(System.IO.FileInfo fi)
 {
     //MyLogger.add(fi.Name + "\n");
     RecordFile thisFile = new RecordFile(fi);
     if (fi.Length == 0)
         return;
     FileStatus status = dim.UpdateStatus(thisFile);
     switch (status)
     {
         case FileStatus.New:
             MyLogger.print("nuovo: ");
             newFiles.Add(thisFile);
             MyLogger.print(fi.Name + "\n");
             break;
         case FileStatus.Updated:
             MyLogger.print("aggiornato: ");
             updatedFiles.Add(thisFile);
             MyLogger.print(fi.Name + "\n");
             break;
         case FileStatus.Old:
             MyLogger.debug("vecchio: ");
             MyLogger.debug(fi.Name + "\n");
             //nothing to do
             break;
     }
     //MyLogger.add(thisFile);
 }
Ejemplo n.º 21
0
 internal void confirmSync(RecordFile f, bool deleting = false)
 {
     dim.confirmSync(f, deleting);
     //dim.storeDirImage(); non salvo + niente online
 }
Ejemplo n.º 22
0
        private void sendFileContent(RecordFile f)
        {
            try
            {

                const int bufsize = 1024;
                var buffer = new byte[bufsize];
                int actuallyRead = 0;

                long sizeFile = 0;
                long totalBytesRead = 0;
                long nextStep = 0;
                long stepSize;

                using (var s = File.OpenRead(f.nameAndPath))
                {
                    sizeFile = s.Length;
                    stepSize = (long)Math.Ceiling((double)sizeFile / 10);

                    while ((actuallyRead = s.Read(buffer, 0, bufsize)) > 0)
                    {
                        serverStream.Write(buffer, 0, actuallyRead);

                        //aggiorna progress bar
                        totalBytesRead += actuallyRead;
                        if (totalBytesRead > nextStep)
                        {
                            mainWindow.Dispatcher.BeginInvoke(mainWindow.DelSetProgressValues, sizeFile, 0, totalBytesRead);
                            nextStep += stepSize;
                        }
                    }
                }
                serverStream.Flush();
                mainWindow.Dispatcher.BeginInvoke(mainWindow.DelSetProgressValues, sizeFile, 0, sizeFile);
            }
            catch (Exception ex)
            {
                MyLogger.debug(ex.ToString());
                MyLogger.print("errore leggendo il file");
                throw;
            }
        }
Ejemplo n.º 23
0
        private void SendWholeFileToServer(RecordFile rf)
        {
            int tryes = 0;
            while (tryes < 6)
            {
                //invio info del file
                sendToServer(rf.toSendFormat());
                //check ack
                string resp = strRecCommFromServer();
                if (resp != commInfoAckFromServer)
                {
                    if (resp == commSndAgain)
                    {
                        tryes++;
                        continue;
                    }
                    else
                    {
                        throw new AckErrorException();
                    }
                }

                //invio contenuto del file
                sendFileContent(rf);

                //check ack
                resp = strRecCommFromServer();
                if (resp != commDataAck)
                {
                    if (resp == commSndAgain)
                    {
                        tryes++;
                        continue;
                    }
                    else
                    {
                        throw new AckErrorException();
                    }
                }
                break;
            }
        }
Ejemplo n.º 24
0
 public void getJustNameTest()
 {
     RecordFile rf = new RecordFile("C:\\cartella\\prova\\ciao.txt", "abcdefghabcdefghabcdefghabcdefgh", 15, new DateTime(1970, 1, 1, 1, 0, 0));
     Assert.AreEqual("ciao.txt", rf.getJustName());
 }