Example #1
0
        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);
        }
Example #2
0
        public JenpyObject Handle(JenpyObject req)
        {
            IDictionary <string, string> data = new Dictionary <string, string>();


            foreach (KeyValuePair <string, string> entry in req.ObjectData)
            {
                String keyString = entry.Key;
                String valString = JenpyConstants.FAIL;
                if (DataStore.DataValues.ContainsKey(keyString))
                {
                    valString = DataStore.DataValues[entry.Key];
                }
                data.Add(entry.Key, valString);
            }

            JenpyObject resp = new JenpyObjectBuilder()
                               .WithVerb(JenpyConstants.OK)
                               .WithObjectData(data)
                               .Build();

            return(resp);
        }
Example #3
0
        public JenpyObject Handle(JenpyObject req)
        {
            IDictionary <string, string> DataBody = req.ObjectData;
            IDictionary <string, string> resBody  = new Dictionary <string, string>();

            foreach (var key in DataBody.Keys)

            {
                String regPeer = String.Format("peer data {0}:{1}", key, DataBody[key]);
                Console.WriteLine(regPeer);
                string    ip         = key;
                int       port       = Int32.Parse(DataBody[key]);
                TcpClient PeerClient = new TcpClient();
                PeerClient.Connect(ip, port);
                TcpServer.peersList.Add(PeerClient);

                resBody.Add(regPeer, JenpyConstants.SUCCESS);
            }
            JenpyObject res = new JenpyObjectBuilder()
                              .WithVerb(JenpyConstants.OK)
                              .WithObjectData(resBody).Build();

            return(res);
        }