public void handleRequest(StreamReader sReader, StreamWriter sWriter) { String sData = sReader.ReadLine(); JenpyObject req = JenpyObjectParser.toJenpy(sData); RequestHandler hander = determineHandler(req); JenpyObject res = hander.Handle(req); if (res.Verb == JenpyConstants.TERM) { sWriter.Write("Terminating\n"); sWriter.Flush(); sWriter.Dispose(); sReader.Dispose(); return; } // shows content on the console. Console.WriteLine("Client > " + sData); foreach (KeyValuePair <string, string> entry in req.ObjectData) { Console.WriteLine("key {0}, val {1}", entry.Key, entry.Value); } String JenpyData = JenpyObjectParser.SerializeToString(res); sWriter.WriteLine(JenpyData); sWriter.Flush(); }
public JenpyObject Handle(JenpyObject req) { IDictionary <string, string> data = new Dictionary <string, string>(); foreach (KeyValuePair <string, string> entry in req.ObjectData) { String value = JenpyConstants.SUCCESS; if (DataStore.DataValues.ContainsKey(entry.Key)) { value = JenpyConstants.FAIL; } else { // TODO: make this async... // Also write to peer... // TODO: conflict resolution for multiple read / write transactions foreach (TcpClient peer in TcpServer.peersList) { StreamWriter sWriter = new StreamWriter(peer.GetStream(), Encoding.ASCII); //StreamReader sReader = new StreamReader(peer.GetStream(), Encoding.ASCII); var input = JenpyObjectParser.SerializeToString(req); Console.WriteLine("Jenpy request to pass to peer - serialized to {0}", input); sWriter.Write(input); sWriter.Flush(); } // Peer ended try { DataStore.DataValues.Add(entry.Key, entry.Value); writeToDisk(entry); } catch (Exception e) { Console.WriteLine("Issues with PUT"); Console.WriteLine(e.StackTrace); } } data.Add(entry.Key, value); } JenpyObject resp = new JenpyObjectBuilder() .WithVerb(JenpyConstants.OK) .WithObjectData(data) .Build(); return(resp); }