Beispiel #1
0
        /// <summary>
        /// Metodo che si occupa di ricevere i files dal server
        /// </summary>
        /// <param name="tr">Transazione attiva sulla directory</param>
        private void recvFiles(DirTransaction tr)
        {
            bool someError = false;
            /*Ricevo comando dal server*/
            Command c = Command.deserializeFrom(sock, netStream, ref _shouldStop);

            if (!_shouldStop && c == null)
            {
                sendErrFatal("Il Server non ha risposto, impossibile Ripristinare i Files in questo momento");
                tr.rollback();
                return;
            }
            else if (_shouldStop)
            {
                tr.rollback();
                return;
            }
            else if (c.cmd == Command.type.ERRPSW)
            {
                sendErrFatal("Impossibile Eseguire l'Autenticazione con successo, controlla username e password, e la directory di cui si vuole fare il ripristino.");
                return;
            }

            try
            {
                /*Ricevo files fino a quando non ricevo il comando END*/
                while (c.cmd != Command.type.END && !_shouldStop)
                {
                    if (c.cmd == Command.type.NEW)
                    {
                        if (!recvFileByte(c.file))
                        {
                            tr.rollback();
                            Command err = new Command(Command.type.ERR);
                            err.serializeTo(netStream);
                            if (!_shouldStop)
                            {
                                sendErrFatal("Impossibile Ripristinare i files in questo momento, problemi durante la comunicazione con il server");
                            }
                            someError = true;
                            break;
                        }

                        if (_shouldStop)
                        {
                            break;
                        }
                        prog += updForFile;
                        if (updProg != null)
                        {
                            updProg(prog);
                        }

                        /*Comando SUCC per confermare successo*/
                        Command succ = new Command(Command.type.SUCC);
                        succ.serializeTo(netStream);

                        /*Ricevo nuovo Comando*/
                        c = Command.deserializeFrom(sock, netStream, ref _shouldStop);
                        if (_shouldStop)
                        {
                            break;
                        }
                        else if (c == null)
                        {
                            sendErrFatal("Impossibile Ripristinare i files in questo momento, il server non risponde");
                            tr.rollback();
                            someError = true;
                            break;
                        }
                    }
                    else
                    {
                        tr.rollback();
                        Command err = new Command(Command.type.ERR);
                        err.serializeTo(netStream);
                        sendErrFatal("Impossibile Ripristinare i files in questo momento, il Server non risponde in modo corretto");
                        someError = true;
                        break;
                    }
                }

                if (_shouldStop)
                {
                    tr.rollback();
                }
                else if (!someError)
                {
                    tr.commit();
                }
            }
            catch (Exception)
            {
                sendErrFatal("Impossibile Ripristinare i files in questo momento.");
                tr.rollback();
                return;
            }
            if (_shouldStop)
            {
                sendErr("Ripristino Annullato");
                return;
            }

            if (updProg != null)
            {
                updProg(100);
            }
            if (stateWrite != null)
            {
                stateWrite("Files Ripristinati");
            }
            if (restored != null)
            {
                restored();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Metodo Eseguito dal Thread che si occupa di ripristinare i file ricevuti dal server
        /// </summary>
        /// <param name="o">Versione da ripristinare</param>
        private void restoreFiles(Object o)
        {
            Version v = (Version)o;

            if (v.numberOfFiles == 0)
            {
                if (restored != null)
                {
                    restored();
                }
                return;
            }

            if (!initConnection())
            {
                return;
            }

            /*Avvio Transizione su Directory*/
            DirTransaction tr = null;

            try
            {
                tr = new DirTransaction(settings.folder);
                if (v.isDirectory)
                {
                    DirTransaction.cleanDir(new DirectoryInfo(v.path));
                }
                else
                {
                    FileInfo f = new FileInfo(v.path);
                    if (f.Exists)
                    {
                        f.Delete();
                    }
                }
            }
            catch (Exception)
            {
                sendErrFatal("Impossibile Avviare il Ripristino dei file a causa di un errore imprevisto");
                return;
            }

            /*Invio Comando REST_REQ*/
            Command req = new Command(Command.type.REST_REQ);

            req.settings = settings;
            req.versions = new List <Version>();
            req.versions.Add(v);

            if (!req.serializeTo(netStream))
            {
                sendErrFatal("Impossibile Inviare la richiesta al server");
                netStream.Close();
                sock.Close();
                return;
            }


            if (_shouldStop)
            {
                netStream.Close();
                sock.Close();
                return;
            }

            if (updProg != null)
            {
                updProg(0);
            }
            updForFile = 100.0 / v.numberOfFiles;

            recvFiles(tr);

            /*Chiudo la Connessione*/
            netStream.Close();
            sock.Close();
        }