/// <summary> /// Handle input signals from the client /// </summary> /// <param name="signal"></param> private void handleInput(Signal signal) { Console.WriteLine("Handling input for signal " + signal); switch (signal) { case Signal.c2s: MyFile newFile = Common.ReceiveFile(socket, dataDir); if (newFile != null) { server.serverDB.UpdateFile(User, newFile); } server.SpanCatchupOperation(handle, User.id, signal, newFile.name); break; //case Signal.clientWantsToSend: // String relPath = Common.ReceiveString(socket); // long timestamp = Common.ReceiveTimestamp(socket); // sendCommandToClient(Signal.clientWantsToSend_response); // Common.SendString(socket, relPath); // // reply 'yes' if it refers to a file that does not exist or if the times do not match // if (File.Exists(dataDir + relPath) && Common.GetModTime(dataDir + relPath) == timestamp) { // Common.SendString(socket, "no"); // } // else { // Common.SendString(socket, "yes"); // } // break; case Signal.clientWants: String relPath = Common.ReceiveString(socket); if (File.Exists(dataDir + relPath)) { outQueue.Enqueue(relPath); processOutQueue(); } break; case Signal.deleteOnServer: relPath = Common.ReceiveString(socket); if (Common.DeleteLocal(dataDir + relPath)) { server.serverDB.RemoveFile(User, relPath); } // index.Remove(relPath); // TODO: check return value server.SpanCatchupOperation(handle, User.id, signal, relPath); break; case Signal.createDirectoryOnServer: relPath = Common.ReceiveString(socket); if (Common.CreateLocalDirectory(dataDir + relPath)) { server.serverDB.UpdateFile(User, new MyFile(relPath, 'd', Common.GetModTime(dataDir + relPath) , 0, "0")); } server.SpanCatchupOperation(handle, User.id, signal, relPath); break; case Signal.requestServerFileList: List <List <string> > fileListToSerialize = server.serverDB.GetFileListSerializable(User); String jsonOutStringFiles = JsonConvert.SerializeObject(fileListToSerialize); Console.WriteLine("sending json file list: " + jsonOutStringFiles); try { sendCommandToClient(Signal.requestServerFileList_response); Common.SendString(socket, jsonOutStringFiles); } catch (Exception e) { Console.WriteLine("Error during " + Signal.requestServerFileList_response + e.Message); Common.ExitError(); } break; case Signal.attachaccount: String args = Common.ReceiveString(socket); Console.WriteLine("received " + args); List <string> attachInput = JsonConvert.DeserializeObject <List <string> >(args); String userName = attachInput[0]; String password = attachInput[1]; Dictionary <string, string> jsonOut = new Dictionary <string, string>(); jsonOut.Add("serverMyboxVersion", Common.AppVersion); if (attachUser(userName, password)) { jsonOut.Add("status", "success"); //jsonOut.Add("quota", Account.quota.ToString()); //jsonOut.Add("salt", Account.salt); server.AddToMultiMap(User.id, handle); } else { jsonOut.Add("status", "failed"); jsonOut.Add("error", "login invalid"); close(); // TODO: disconnect the client here } String jsonOutString = JsonConvert.SerializeObject(jsonOut); try { sendCommandToClient(Signal.attachaccount_response); Common.SendString(socket, jsonOutString); } catch (Exception e) { Console.WriteLine("Error during " + Signal.attachaccount_response + e.Message); Common.ExitError(); } Console.WriteLine("attachaccount_response: " + jsonOutString); break; default: Console.WriteLine("Unknown command"); break; } }
/// <summary> /// Deal with incoming signal from server /// </summary> /// <param name="signal"></param> private void handleInput(Signal signal) { if (paused) { return; } writeMessage("Handling input for signal " + signal); // TODO: make sure these all update the index setStatus(ClientStatus.SYNCING); switch (signal) { case Signal.s2c: MyFile newFile = Common.ReceiveFile(socket, dataDir); if (newFile != null) { fileIndex.Update(newFile); incommingFiles.Remove(newFile.name); setOverlay(true); } break; case Signal.deleteOnClient: // catchup operation String relPath = Common.ReceiveString(socket); if (Common.DeleteLocal(dataDir + relPath)) { fileIndex.Remove(relPath); } break; case Signal.createDirectoryOnClient: // catchup operation relPath = Common.ReceiveString(socket); if (Common.CreateLocalDirectory(dataDir + relPath)) { fileIndex.Update(new MyFile(relPath, 'd', Common.GetModTime(dataDir + relPath), 0, "0")); } break; case Signal.requestServerFileList_response: String jsonStringFiles = Common.ReceiveString(socket); List <List <string> > fileDict = JsonConvert.DeserializeObject <List <List <string> > >(jsonStringFiles); S.Clear(); foreach (List <string> fileItem in fileDict) { // TODO: try, catch for parse errors etc S.Add(fileItem[0], new MyFile(fileItem[0], char.Parse(fileItem[1]), long.Parse(fileItem[2]), long.Parse(fileItem[3]), fileItem[4])); } break; case Signal.attachaccount_response: // TODO: replace JSON parser with simple text parsing so we dont have to lug around the dependency Dictionary <string, string> jsonMap = JsonConvert.DeserializeObject <Dictionary <string, string> >(Common.ReceiveString(socket)); if (jsonMap["status"] != "success")// TODO: change to signal { writeMessage("Unable to attach account. Server response: " + jsonMap["error"]); // TODO: catch these exceptions above somewhere //throw new Exception("Unable to attach account. Server response: " + jsonMap["error"]); //socket.Close(); Stop(); } else { //writeMessage("set account salt to: " + account.Salt); if (Common.AppVersion != jsonMap["serverMyboxVersion"]) { writeMessage("Client and Server Mybox versions do not match"); } } break; default: writeMessage("Unknown command from server: " + signal); break; } lastReceivedOperation = signal; if (incommingFiles.Count == 0 && outQueue.Count == 0) { setStatus(ClientStatus.READY); } }