Пример #1
0
        public JenpyObject Handle(JenpyObject req)
        {
            //TODO write a builder
            JenpyObject response = new JenpyObject(JenpyConstants.TERM, null);

            return(response);
        }
Пример #2
0
        public static string SerializeToString(JenpyObject res)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(res.Verb);

            sb.Append(" | ");

            // Key Value pairs

            foreach (KeyValuePair <string, string> entry in res.ObjectData)
            {
                sb.Append(entry.Key);
                sb.Append(":");
                sb.Append(entry.Value);
                sb.Append(" | ");
            }

            //remove the last |

            string bar = " | ";

            sb.Remove(sb.Length - bar.Length, bar.Length);
            sb.Append(" .");
            sb.Append('\n');

            return(sb.ToString());
        }
Пример #3
0
        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();
        }
Пример #4
0
        public JenpyObject Handle(JenpyObject req)
        {
            IDictionary <string, string> data = new Dictionary <string, string>();

            for (int i = 0; i < BlockStore.BlockChain.Count; i++)
            {
                Block block    = BlockStore.BlockChain[i];
                bool  verified = verify(block, i);
                if (verified)
                {
                    data.Add(block.Hash, block.Data);
                }
                else
                {
                    //TODO : shall I throw RTE instead? would netcat handle it
                    //data.Add(block.Hash, block.Data);
                    string ErrorString = String.Format("data verification failed at block with data: {0}, hash {1}", block.Data, block.Hash);
                    throw new JenpyBlockVerifiabilityException(ErrorString);
                }
            }

            JenpyObject res = new JenpyObject(JenpyConstants.OK, data);

            return(res);
        }
Пример #5
0
        public JenpyObject Handle(JenpyObject req)
        {
            IDictionary <string, string> data = new Dictionary <string, string>();

            foreach (var block in BlockStore.BlockChain)
            {
                data.Add(block.Hash, block.Data);
            }

            JenpyObject res = new JenpyObject(JenpyConstants.OK, data);

            return(res);
        }
Пример #6
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);
        }
Пример #7
0
        public JenpyObject Handle(JenpyObject req)
        {
            // serialize the string from req.ObjectData;
            StringBuilder sb = new StringBuilder();

            foreach (string key in req.ObjectData.Keys)
            {
                sb.Append(key);
                sb.Append(",");
                sb.Append(req.ObjectData[key]);
            }

            string input = sb.ToString();
            Block  b     = new Block(input);

            BlockStore.BlockChain.Add(b);
            JenpyObject res = new JenpyObject(JenpyConstants.OK, req.ObjectData);

            return(res);
        }
Пример #8
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);
        }
Пример #9
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);
        }
Пример #10
0
 private RequestHandler determineHandler(JenpyObject req)
 {
     return(handlers[req.Verb]);
 }
Пример #11
0
 public JenpyObject Handle(JenpyObject req)
 {
     req.Verb = JenpyConstants.OK;
     return(req);
 }