示例#1
0
 internal void initHttpServer()
 {
     m_HttpServer = new HttpServer(AgentHTTPServerPortNumber, this.Log);
     m_HttpServer.get("/blocks", (req, res) => res.send(Repository.stringify()));
     m_HttpServer.post("/mineBlock", (req, res) =>
     {
         string _newData = DataContract.Parse(req.body).data;
         Log($"adding new block using the data: {_newData}");
         IBlock _newBlock = Repository.generateNextBlock(_newData);
         //broadcast(responseLatestMsg());
         Log($"block added: {_newBlock.stringify()}");
         res.send();
     });
     m_HttpServer.get("/peers", (req, res) => { res.send(String.Join(", ", Network.Sockets)); });
     m_HttpServer.post("/addPeer", (req, res) =>
     {
         try
         {
             Uri _peer = PeerContract.Parse(req.body).PeerUri;
             Log($"Adding peer {_peer}");
             Network.ConnectToPeers(new Uri[] { _peer });
         }
         catch (Exception _ex)
         {
             Log($"Cannot add peer because of exception {_ex}");
         }
         res.send();
     });
     Task m_HTTPServerTask = m_HttpServer.Listen(() => Log($"Listening http on port: { AgentHTTPServerPortNumber}"));
 }
示例#2
0
        /// <summary>
        /// Parses the JSON string to recover an instance of <see cref="PeerContract"/>
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static PeerContract Parse(string data)
        {
            PeerContract _peer = data.Parse <PeerContract>();

            _peer.PeerUri = new Uri(_peer.peer);
            return(_peer);

            ;
        }