/// <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; } }
private int versionNumber; // Keep track of this spreadsheet's version number #endregion Fields #region Constructors /// <summary> /// Constructs a new spreadsheet /// </summary> public Form1() { InitializeComponent(); //Creates new socket for this specific spreadsheet. client = new Client(); time = new System.Timers.Timer(); client.IncomingLineEvent += MessageReceived; //Creates new login window. login = new Login.Form1(client); login.ClickedCancel += CancelSpreadSheet; // Will close this spreadsheet if user clicks cancel. login.ShowDialog(); //Save the IpAddress and the password so user will not have to input every // time a new spreadsheet is opened. usrPassword = login.getPassword(); usrIpAddrss = login.getIp(); SSEdit = false; //Hide spreadsheet until the login has been verified. spreadsheetPanel1.Hide(); time.Interval = 30000; // Set to 30000 for 30 seconds!! time.Elapsed += new ElapsedEventHandler(time_elapsed); // This could also be done graphically in the designer, as has been // demonstrated in class. spreadsheetPanel1.SelectionChanged += DisplaySelectedCell; spreadsheet = new SS.Spreadsheet(x => true, (string s) => s.ToUpper(), "ps6"); titleFile = "Sheet " + totalSpreadsheets; saveFile = ""; SetTitle(); InsertMode = true; }