/// <summary> /// When the user clicks cancel this method will be called and will terminate /// the socket and this spreadsheet. /// </summary> /// <param name="clickedCancel"></param> private void CancelSpreadSheet(bool clickedCancel) { if (clickedCancel) { if (this.InvokeRequired) { ShowSSCallback d = new ShowSSCallback(CloseSS); this.Invoke(d, new object[] { }); } else { CloseServerSocket(); Close(); } } return; }
/// <summary> /// Method called when a new message has been received by the server. /// </summary> /// <param name="line"></param> /// <param name="e"></param> private void MessageReceived(String line, Exception e) { if (ReferenceEquals(line, null) || line == "") { if (e is SocketException) { serverClosed = true; MessageBox.Show("Server has closed. Please try again later.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); if (this.InvokeRequired) { CloseSSCallback d = new CloseSSCallback(CloseSS); this.Invoke(d, new object[] { }); } else { CloseSS(); } return; } return; } string[] lineReceived = Regex.Split(line, esc.ToString()); string protocol = lineReceived[0]; //INVALID\n if (protocol.Equals("INVALID")) { login.LoginError(); login = new Login.Form1(client); login.ShowDialog(); } //FILELIST[esc]list_of_existing_filenames\n ( each name delimited by [esc]) else if (protocol.Equals("FILELIST")) { availableFile = new AvailableFiles.Form1(client); availableFile.ClickedCancel += CancelSpreadSheet; //Add the items to the list of items you can open availableFile.AddSS(lineReceived); //Run the files available prompt window. availableFile.ShowDialog(); NameOfSS = availableFile.getSSName(); //Shows the spreadsheet again after the user has picked to open // a new spreadsheet or to open an exsisting spreadsheet. if (this.InvokeRequired) { ShowSSCallback d = new ShowSSCallback(ShowSS); this.Invoke(d, new object[] { }); } else { spreadsheetPanel1.Show(); } availableFile.Focus(); time.Start(); SetTitle(); } //UPDATE[esc]current_version[esc]cell_name1[esc]cell_content1[esc]cell_name2[esc]…\n //SYNC[esc]current_version[esc]cell_name[esc]cell_content…\n else if (protocol.Equals("UPDATE") || protocol.Equals("SYNC")) { Dictionary<string, string> cells = new Dictionary<string, string>(); String cellName = ""; String cellCont = ""; try { int currentVersion = Convert.ToInt32(lineReceived[1]); //If the last command sent to server was an edit to the spreadsheet and // the version + 1 doesn't equal to the version send back by the server demand a Resync to server. if (SSEdit && (versionNumber + 1) != currentVersion) { //Must resync with the server. client.SendMessage("RESYNC\n"); SSEdit = false; return; } versionNumber = currentVersion; } catch { //TODO:Handle this properly. MessageBox.Show( "Error", "Protocol was not sent properly. No version number was sent", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } //Go through the array and place the cellName in the correct container // and do the same with the content of that cell. for (int i = 2; i < lineReceived.Length; i++) { if (i % 2 == 0) { //It is a cell name cellName = lineReceived[i]; i++; } if ((i % 2) == 1 && i < lineReceived.Length) { //It is the contents of the cell. cellCont = lineReceived[i]; } cells.Add(cellName, cellCont); } Update(cells); SSEdit = false; } else if (protocol.Equals("SAVED")) { //TODO: what to do when the saved command is received. MessageBox.Show("File Successfully Saved", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //ERROR[esc]error_message\n else if (protocol.Equals("ERROR")) { MessageBox.Show(lineReceived[1], "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } }
/************************** THREAD SAFE METHODS **************************/ /// <summary> /// If user closes available file window has been closed and closes this spreadsheet as well. /// </summary> private void availableFileClosed(object sender, FormClosedEventArgs e) { if (this.InvokeRequired) { ShowSSCallback d = new ShowSSCallback(CloseSS); this.Invoke(d, new object[] { }); } else { this.Close(); } }