示例#1
0
        private void sync()
        {
            String dirPath = conf.targetPath.get();

            if (dirPath == null || !Directory.Exists(dirPath))
            {
                throw new DirectoryNotFoundException("Directory in configuration is not valid");
            }
            try
            {
                cv = (FBVersion)vb.generate();
            }catch (Exception e)
            {
                MessageBox.Show(e.Message + "\nThe application will be closed", "Unexpected error");
                Environment.Exit(0);
            }
            SerializedVersion serV = new SerializedVersion();

            serV.encodedVersion = cv.serialize();
            threadCallback.Invoke(TypeThread.SYNC, StatusCode.WORKING, "Start syncing");
            FileStream fs = null;

            try {
                if (server.newTransaction(serV))
                {
                    this.status = "Syncing";
                    byte[][]      bfiles     = server.getFilesToUpload();
                    List <FBFile> fileToSync = new List <FBFile>();
                    foreach (byte[] bf in bfiles)
                    {
                        fileToSync.Add(FBFile.deserialize(bf));
                    }

                    int i = 0;

                    foreach (FBFile f in fileToSync)
                    {
                        if (!this.stopSync)
                        {
                            threadCallback.Invoke(TypeThread.SYNC, StatusCode.WORKING, "Syncing file " + i + " of " + fileToSync.Count);
                            i++;
                            FBFileClient cf        = FBFileClient.generate(f);
                            UploadData   cedential = server.uploadFile();
                            UsefullMethods.SendFile(cedential.ip, cedential.port, cedential.token, new FileStream(cf.FullName, FileMode.Open));
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (!this.stopSync)
                    {
                        server.commit();
                        threadCallback.Invoke(TypeThread.SYNC, StatusCode.SUCCESS, "Sync completed");
                    }
                    else
                    {
                        threadCallback.Invoke(TypeThread.SYNC, StatusCode.ABORTED, "Syncing Stopped");
                        server.rollback();
                    }
                    this.status = "Idle";
                }
                else
                {
                    threadCallback.Invoke(TypeThread.SYNC, StatusCode.IDLE, "Nothing to be done");
                }
            }
            catch (System.ServiceModel.CommunicationException e)
            {
                MessageBox.Show("There is a problem with connection, please retry to login!", "Error in connection");
                threadCallback.Invoke(TypeThread.SYNC, StatusCode.ABORTED, "Connection error");
            }
            catch
            {
                server.rollback();
            }
        }