//Calls the server and ask for a document. public void openFile(string nameOfFile) { if (hasLock == false) { try { if (nameOfFile != "") { //Network call. openDocument = network.openFile(nameOfFile, myInfo); //Clears the text box. DocumentText.Document.Blocks.Clear(); //Appends the grabbed document. DocumentText.AppendText(openDocument.FileContents); //Sets current textbox string for the thread to send and recieve changes. currentTextboxText = openDocument.FileContents; //Unlocks the lock request and drop buttons. lockRequest.IsEnabled = true; releaseLock.IsEnabled = true; } } catch (Exception err) { onDisconnect(err); } } else { releaseLock_Click(null, null); openFile(nameOfFile); //System.Windows.MessageBox.Show("Please release the lock before opening a new file.", "Error"); } }
public void changes() { while (true) { if (openDocument != null) { List <Patch> patches; Dictionary <int, List <Patch> > returnedPatches; Object[] temp; while (true) { try { if (hasLock == true) { Thread.Sleep(1000); //Copies the document Textbox // TextRange range = new TextRange(DocumentText.Document.ContentStart, DocumentText.Document.ContentEnd); string myText2 = currentTextboxText; patches = diffMatch.patch_make(diffMatch.diff_main(openDocument.FileContents, myText2)); temp = diffMatch.patch_apply(patches, openDocument.FileContents); openDocument.FileContents = temp[0].ToString(); network.sendDocChanges(openDocument.FileName, patches, myInfo); openDocument.SaveID = network.getLastPatchID(openDocument.FileName) + 1; } else { Thread.Sleep(1000); returnedPatches = network.getDocChanges(openDocument.FileName, myInfo, openDocument.SaveID); if (returnedPatches.Count > 0) { foreach (List <Patch> item in returnedPatches.Values) { temp = diffMatch.patch_apply(item, openDocument.FileContents); openDocument.FileContents = temp[0].ToString(); this.Dispatcher.Invoke(new Action(() => { DocumentText.Document.Blocks.Clear(); DocumentText.AppendText(openDocument.FileContents); })); } openDocument.SaveID = returnedPatches.Last().Key + 1; } } } catch (Exception) { //MessageBoxResult result = System.Windows.MessageBox.Show("Error: Connection to the server was lost. Would you like to reconnect?", "Error", MessageBoxButton.YesNo); //if (result == MessageBoxResult.No) //{ // this.Close(); // Environment.Exit(1); //} } } } } }