public void RollbackTest() { FBVersionBuilder vb = new FBVersionBuilder(rinfo.FullName); FolderBackup.Shared.FBVersion v = (FolderBackup.Shared.FBVersion)vb.generate(); SerializedVersion serV = new SerializedVersion(v.serialize()); Assert.IsTrue(server.newTransaction(serV)); byte[][] bfiles = server.getFilesToUpload(); foreach (byte[] bf in bfiles) { FBFile f = FBFile.deserialize(bf); Assert.IsTrue(v.fileList.Contains(f)); } FBFile file = (FBFile) new FBFileBuilder(@"asd\uno.txt").generate(); string[] lines = { "First line", "Second line", "Third line" }; System.IO.File.WriteAllLines(@"asd\uno.txt", lines); FileStream fstream = new FileStream(@"asd\uno.txt", FileMode.Open, FileAccess.Read); //Assert.AreEqual(server.uploadFile(fstream), file.hash); //fstream.Close(); UploadData credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); file = (FBFile) new FBFileBuilder(@"asd\due.txt").generate(); string[] lines1 = { "First line", "Second line", "Third lines" }; System.IO.File.WriteAllLines(@"asd\due.txt", lines1); fstream = new FileStream(@"asd\due.txt", FileMode.Open, FileAccess.Read); //Assert.AreEqual(server.uploadFile(fstream), file.hash); //fstream.Close(); credential = server.uploadFile(); UsefullMethods.SendFile(credential.ip, credential.port, credential.token, fstream); Assert.IsTrue(server.rollback()); Assert.IsTrue(server.newTransaction(serV)); bfiles = server.getFilesToUpload(); foreach (byte[] bf in bfiles) { FBFile f = FBFile.deserialize(bf); Assert.IsTrue(v.fileList.Contains(f)); } }
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(); } }