예제 #1
0
        public void setParentNode()
        {
            Task.Run(() => {
                Skynet.Base.Skynet sender = new Skynet.Base.Skynet();
                Node mNode = new Node(new List <NodeId>(), mSkynet);
                Task.Run(async() => {
                    // create a node

                    Node parentNode = new Node(new List <NodeId>(), sender);

                    long timeStamp  = Skynet.Utils.Utils.UnixTimeNow();
                    ToxResponse res = await RequestProxy.sendRequest(mSkynet, new ToxRequest
                    {
                        uuid       = Guid.NewGuid().ToString(),
                        url        = "node/" + mNode.selfNode.uuid + "/parent",
                        method     = "put",
                        content    = JsonConvert.SerializeObject(parentNode.selfNode),
                        fromNodeId = parentNode.selfNode.uuid,
                        fromToxId  = sender.tox.Id.ToString(),
                        toNodeId   = mNode.selfNode.uuid,
                        toToxId    = mSkynet.tox.Id.ToString(),
                        time       = timeStamp,
                    });
                    NodeResponse mRes = JsonConvert.DeserializeObject <NodeResponse>(res.content);
                    Console.WriteLine("value: " + mRes.value);
                    Assert.AreEqual(mRes.statusCode, NodeResponseCode.OK);
                    Assert.AreEqual(timeStamp, mRes.time);
                    Assert.AreEqual(timeStamp, mNode.parentModifiedTime);
                }).GetAwaiter().GetResult();
            });
        }
예제 #2
0
 public void PostTest()
 {
     Task.Run(async() => {
         using (var client = new HttpClient()) {
             var data = new Dictionary <string, string> {
                 { "uuid", brotherNode.selfNode.uuid },
                 { "toxid", brotherNode.selfNode.toxid },
             };
             targetNode.parent = targetNode.selfNode;
             client.DefaultRequestHeaders.Add("Uuid", Guid.NewGuid().ToString());
             client.DefaultRequestHeaders.Add("From-Node-Id", targetNode.selfNode.uuid);
             client.DefaultRequestHeaders.Add("From-Tox-Id", targetNode.selfNode.toxid);
             client.DefaultRequestHeaders.Add("To-Node-Id", targetNode.selfNode.uuid);
             client.DefaultRequestHeaders.Add("To-Tox-Id", targetNode.selfNode.toxid);
             long timeStamp = Skynet.Utils.Utils.UnixTimeNow();
             client.DefaultRequestHeaders.Add("Skynet-Time", timeStamp + "");
             var postResponse = await client.PostAsync(baseUrl + "node/" + targetNode.selfNode.uuid
                                                       + "/brotherNodes", new FormUrlEncodedContent(data));
             string responseString = await postResponse.Content.ReadAsStringAsync();
             NodeResponse res      = JsonConvert.DeserializeObject <NodeResponse>(responseString);
             Assert.AreEqual(NodeResponseCode.OK, res.statusCode);
             List <NodeId> newBro = JsonConvert.DeserializeObject <List <NodeId> >(res.value);
             Assert.AreEqual(brotherNode.selfNode.uuid, newBro[0].uuid);
             Assert.AreEqual(timeStamp, targetNode.brotherModifiedTime);
             Assert.AreEqual(timeStamp, res.time);
         }
     }).GetAwaiter().GetResult();
 }
예제 #3
0
        public void PutTest()
        {
            Node node1 = new Node(mSkynet);
            Node node2 = new Node(mSkynet);
            Node node3 = new Node(mSkynet);

            node2.parent             = node1.selfNode;
            node2.parentModifiedTime = Skynet.Utils.Utils.UnixTimeNow();
            Task.Run(async() => {
                ToxResponse response = await RequestProxy.sendRequest(mSkynet, new ToxRequest {
                    uuid       = Guid.NewGuid().ToString(),
                    url        = "node/" + node3.selfNode.uuid + "/grandParents",
                    content    = JsonConvert.SerializeObject(node1.selfNode),
                    method     = "put",
                    fromNodeId = node2.selfNode.uuid,
                    fromToxId  = node2.selfNode.toxid,
                    toNodeId   = node3.selfNode.uuid,
                    toToxId    = node3.selfNode.toxid,
                    time       = node2.parentModifiedTime
                });

                NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(response.content);
                Assert.AreEqual(node1.selfNode.uuid, node3.grandParents.uuid);
                Assert.AreEqual(node2.parentModifiedTime, node3.grandParentsModifiedTime);
            }).GetAwaiter().GetResult();
        }
예제 #4
0
        public void DeleteTest()
        {
            Task.Run(async() => {
                using (var client = new HttpClient()) {
                    // add childnode first
                    var values = new Dictionary <string, string> {
                        { "uuid", childNode.selfNode.uuid },
                        { "toxid", childNode.selfNode.toxid },
                    };
                    long timeStamp = Skynet.Utils.Utils.UnixTimeNow();
                    client.DefaultRequestHeaders.Add("Skynet-Time", timeStamp + "");
                    var response = await client.PostAsync(baseUrl + "node/" + testNode.selfNode.uuid
                                                          + "/childNodes", new FormUrlEncodedContent(values));
                    string responseString = await response.Content.ReadAsStringAsync();

                    var deleteResponse = await client.DeleteAsync(baseUrl + "node/" + testNode.selfNode.uuid
                                                                  + "/childNodes/" + childNode.selfNode.uuid);
                    string deleteResponseString = await deleteResponse.Content.ReadAsStringAsync();
                    NodeResponse res            = JsonConvert.DeserializeObject <NodeResponse>(deleteResponseString);
                    Assert.AreEqual(res.statusCode, NodeResponseCode.OK);
                    Assert.AreEqual(timeStamp, res.time);
                    Assert.AreEqual(timeStamp, testNode.childNodesModifiedTime);
                }
            }).GetAwaiter().GetResult();
        }
예제 #5
0
        public void TestSendMsgToSelf()
        {
            Node Node1  = new Node(mSkynet);
            Node Node2  = new Node(mSkynet);
            bool status = false;

            Task.Run(async() => {
                ToxResponse res = await mSkynet.sendRequest(mSkynet.tox.Id, new ToxRequest
                {
                    url        = "tox/" + mSkynet.tox.Id.ToString(),
                    uuid       = Guid.NewGuid().ToString(),
                    method     = "get",
                    content    = "",
                    fromNodeId = Node1.selfNode.uuid,
                    fromToxId  = mSkynet.tox.Id.ToString(),
                    toNodeId   = Node2.selfNode.uuid,
                    toToxId    = mSkynet.tox.Id.ToString(),
                }, out status);
                Console.WriteLine("status: " + status);
                Assert.AreEqual(true, status);
                if (status)
                {
                    NodeResponse nodeRes = JsonConvert.DeserializeObject <NodeResponse>(res.content);
                    Assert.AreEqual(nodeRes.statusCode, NodeResponseCode.OK);
                }
            }).GetAwaiter().GetResult();
        }
예제 #6
0
        static void Main(string[] args)
        {
            Base.Skynet mSkynet  = new Base.Skynet();
            Base.Skynet mSkynet2 = new Base.Skynet();
            Node        node1    = new Node(mSkynet);
            Node        node3    = new Node(mSkynet);
            Node        node2    = new Node(mSkynet2);

            Task.Run(async() =>
            {
                // add node1 to node2's childnodes
                NodeResponse res = await node1.sendRequest(node2.selfNode,
                                                           JsonConvert.SerializeObject(node1.selfNode), "post",
                                                           "node/" + node2.selfNode.uuid + "/childNodes", Utils.Utils.UnixTimeNow());
                NodeResponse setParentRes = await node1.sendRequest(node1.selfNode,
                                                                    JsonConvert.SerializeObject(node2.selfNode), "put",
                                                                    "node/" + node1.selfNode.uuid + "/parent", Utils.Utils.UnixTimeNow());
                NodeResponse addChildToNode3 = await node2.sendRequest(node3.selfNode,
                                                                       JsonConvert.SerializeObject(node2.selfNode), "post",
                                                                       "node/" + node3.selfNode.uuid + "/childNodes", Utils.Utils.UnixTimeNow());
                NodeResponse setParentNode2 = await node2.sendRequest(node2.selfNode,
                                                                      JsonConvert.SerializeObject(node3.selfNode), "put",
                                                                      "node/" + node2.selfNode.uuid + "/parent", Utils.Utils.UnixTimeNow());
                // stop mskynet2
                mSkynet2.stop();
                Console.ReadLine();
            }).GetAwaiter().GetResult();
        }
예제 #7
0
        public void TestPut()
        {
            Node node3 = new Node(mSkynet);

            node3.parent       = targetNode.selfNode;
            brotherNode.parent = targetNode.selfNode;
            targetNode.childNodes.Add(node3.selfNode);
            targetNode.childNodes.Add(brotherNode.selfNode);
            Task.Run(() => {
                var request = WebRequest.Create(baseUrl + "node/" + brotherNode.selfNode.uuid
                                                + "/brotherNodes");
                request.Method = "PUT";
                request.Headers.Add("Uuid", Guid.NewGuid().ToString());
                request.Headers.Add("From-Node-Id", targetNode.selfNode.uuid);
                request.Headers.Add("From-Tox-Id", targetNode.selfNode.toxid);
                request.Headers.Add("To-Node-Id", brotherNode.selfNode.uuid);
                request.Headers.Add("To-Tox-Id", brotherNode.selfNode.toxid);
                long timestamp = Skynet.Utils.Utils.UnixTimeNow();
                request.Headers.Add("Skynet-Time", timestamp + "");
                request.ContentType = "application/json";
                using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) {
                    writer.Write(
                        JsonConvert.SerializeObject(
                            targetNode.childNodes
                            .Where(x => x.uuid != brotherNode.selfNode.uuid).ToList()));
                }
                var response     = request.GetResponse();
                string resString = new StreamReader(response.GetResponseStream()).ReadToEnd();
                NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(resString);
                Assert.AreEqual(NodeResponseCode.OK, res.statusCode);
                Assert.AreEqual(timestamp, res.time);
                Assert.AreEqual(timestamp, brotherNode.brotherModifiedTime);
            }).GetAwaiter().GetResult();
        }
예제 #8
0
        public void TestHandShake()
        {
            while (!mSkynet.tox.IsConnected)
            {
                Thread.Sleep(10);
            }
            Skynet.Base.Skynet mSkynet2 = new Skynet.Base.Skynet();
            Node mNode1 = new Node(new List <NodeId>(), mSkynet);
            Node mNode2 = new Node(new List <NodeId>(), mSkynet2);

            bool status = false;

            Task.Run(async() =>
            {
                ToxResponse res = await mSkynet2.sendRequest(mSkynet.tox.Id, new ToxRequest
                {
                    url        = "tox/" + mSkynet.tox.Id.ToString(),
                    uuid       = Guid.NewGuid().ToString(),
                    method     = "get",
                    content    = "",
                    fromNodeId = mNode2.selfNode.uuid,
                    fromToxId  = mNode2.selfNode.toxid,
                    toNodeId   = mNode1.selfNode.uuid,
                    toToxId    = mNode1.selfNode.toxid,
                }, out status);
                Console.WriteLine("status " + status);
                Assert.AreEqual(status, true);
                if (status)
                {
                    NodeResponse nodeRes = JsonConvert.DeserializeObject <NodeResponse>(res.content);
                    Assert.AreEqual(nodeRes.statusCode, NodeResponseCode.OK);
                }
            }).GetAwaiter().GetResult();
        }
예제 #9
0
        public async Task <NodeKeysDto> GetNodePublicKeyAsync(NodeConnection nodeConnection, long?keyId = null)
        {
            NodeRequest nodeRequest = keyId == null ? new GetPublicKeyNodeRequest() : new GetPublicKeyNodeRequest(keyId.Value);

            SendRequest(nodeConnection, nodeRequest);
            try
            {
                NodeResponse response = await GetResponseAsync(nodeRequest).ConfigureAwait(false);

                switch (response.ResponseType)
                {
                case Enums.NodeResponseType.PublicKey:
                {
                    PublicKeyNodeResponse nodeKeysResponse = (PublicKeyNodeResponse)response;
                    return(new NodeKeysDto
                        {
                            NodeId = (nodeConnection.Node?.Id).GetValueOrDefault(),
                            KeyId = nodeKeysResponse.KeyId,
                            PublicKey = nodeKeysResponse.PublicKey,
                            ExpirationTime = nodeKeysResponse.ExpirationTime,
                            SignPublicKey = nodeKeysResponse.SignPublicKey
                        });
                }

                default:
                    return(null);
                }
            }
            catch (ResponseException ex)
            {
                Logger.WriteLog(ex);
                return(null);
            }
        }
예제 #10
0
        public async Task <ValuePair <TokenVm, UserVm> > CheckTokenAsync(long userId, TokenVm token, long nodeId)
        {
            var nodeConnection = connectionsService.GetNodeConnection(nodeId);

            if (nodeConnection != null)
            {
                CheckTokenNodeRequest request = new CheckTokenNodeRequest(userId, token);
                SendRequest(nodeConnection, request);
                NodeResponse nodeResponse = await GetResponseAsync(request).ConfigureAwait(false);

                switch (nodeResponse.ResponseType)
                {
                case NodeResponseType.UserTokens:
                {
                    UserTokensNodeResponse tokensResponse = (UserTokensNodeResponse)nodeResponse;
                    return(new ValuePair <TokenVm, UserVm>(tokensResponse.Token, tokensResponse.User));
                }

                default:
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
예제 #11
0
        public void TestDelete()
        {
            Node node3 = new Node(mSkynet);

            node3.parent       = targetNode.selfNode;
            brotherNode.parent = targetNode.selfNode;

            Task.Run(async() => {
                using (var client = new HttpClient()) {
                    client.DefaultRequestHeaders.Add("Uuid", Guid.NewGuid().ToString());
                    client.DefaultRequestHeaders.Add("From-Node-Id", targetNode.selfNode.uuid);
                    client.DefaultRequestHeaders.Add("From-Tox-Id", targetNode.selfNode.toxid);
                    client.DefaultRequestHeaders.Add("To-Node-Id", brotherNode.selfNode.uuid);
                    client.DefaultRequestHeaders.Add("To-Tox-Id", brotherNode.selfNode.toxid);
                    long timeStamp = Skynet.Utils.Utils.UnixTimeNow();
                    client.DefaultRequestHeaders.Add("Skynet-Time", timeStamp + "");
                    // test not found
                    var responseNotFound = await client.DeleteAsync(baseUrl + "node/" + brotherNode.selfNode.uuid
                                                                    + "/brotherNodes/" + node3.selfNode.uuid);
                    string resNotFound = await responseNotFound.Content.ReadAsStringAsync();
                    NodeResponse res   = JsonConvert.DeserializeObject <NodeResponse>(resNotFound);
                    Assert.AreEqual(NodeResponseCode.NotFound, res.statusCode);
                    // test delete
                    brotherNode.brotherNodes.Add(node3.selfNode);
                    var response = await client.DeleteAsync(baseUrl + "node/" + brotherNode.selfNode.uuid
                                                            + "/brotherNodes/" + node3.selfNode.uuid);
                    string resString  = await response.Content.ReadAsStringAsync();
                    NodeResponse res1 = JsonConvert.DeserializeObject <NodeResponse>(resString);
                    Assert.AreEqual(NodeResponseCode.OK, res1.statusCode);
                    Assert.AreEqual(timeStamp, res1.time);
                    Assert.AreEqual(timeStamp, brotherNode.brotherModifiedTime);
                }
            }).GetAwaiter().GetResult();
        }
        public async Task HandleAsync()
        {
            NodeResponse response = default;

            try
            {
                TokenVm token = await tokensService.CheckTokenAsync(request.Token, NodeSettings.Configs.Node.Id).ConfigureAwait(false);

                UserVm user = await loadUsersService.GetUserAsync(token.UserId).ConfigureAwait(false);

                response = new UserTokensNodeResponse(request.RequestId, token, user);
            }
            catch (InvalidTokenException)
            {
                response = new ResultNodeResponse(request.RequestId, ErrorCode.InvalidAccessToken);
            }
            catch (TokensTimeoutException)
            {
                response = new ResultNodeResponse(request.RequestId, ErrorCode.AccessTokenTimeout);
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
                response = new ResultNodeResponse(request.RequestId, ErrorCode.UnknownError);
            }
            finally
            {
                NodeWebSocketCommunicationManager.SendResponse(response, current);
            }
        }
예제 #13
0
 /// <summary>
 /// WCF wrapper around getNodes
 /// </summary>
 public static void getNodes(ICswResources CswResources, NodeResponse Response, NodeSelect.Request Request)
 {
     if (null != CswResources)
     {
         CswNbtResources NbtResources = (CswNbtResources)CswResources;
         CswNbtSdNode    sd           = new CswNbtSdNode(NbtResources);
         Response.Data = sd.getNodes(Request);
     }
 }
    private void OnUserLoginOK(NodeResponse res)
    {
        trace("PIERRE: ---------------------- ARE WE HERE?");

        UpdateUserUI();
        ShowHeader();

        //var curMan = CurrencyManager.Instance;

        //curMan.ParseCurrencyData(res["game"]["currency"]);
    }
예제 #15
0
 public void GetToxInfoWithoutId()
 {
     Task.Run(async() => {
         using (var client = new HttpClient())
         {
             string res        = await client.GetStringAsync(baseUrl + "api/tox");
             NodeResponse mRes = JsonConvert.DeserializeObject <NodeResponse>(res);
             Assert.AreEqual(mRes.statusCode, NodeResponseCode.InvalidRequest);
         }
     });
 }
 public static async void SendResponse(NodeResponse response, NodeConnection nodeConnection)
 {
     try
     {
         await SendResponseAsync(response, nodeConnection).ConfigureAwait(false);
     }
     catch (Exception ex)
     {
         Logger.WriteLog(ex, "Error send response.");
     }
 }
예제 #17
0
 public void GetNotFoundTest()
 {
     Task.Run(async() => {
         using (var client = new HttpClient()) {
             string response = await client.GetStringAsync(baseUrl + "node/" + testNode.selfNode.uuid
                                                           + "/childNodes/" + Guid.NewGuid().ToString());
             NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(response);
             Assert.AreEqual(res.statusCode, NodeResponseCode.NotFound);
         }
     }).GetAwaiter().GetResult();
 }
예제 #18
0
 public void GetLocalToxInfo()
 {
     Task.Run(async() =>
     {
         using (var client = new HttpClient())
         {
             string res        = await client.GetStringAsync(baseUrl + "api/tox/" + mSkynet.tox.Id.ToString());
             NodeResponse mRes = JsonConvert.DeserializeObject <NodeResponse>(res);
             Assert.AreEqual(mRes.statusCode, NodeResponseCode.OK);
         }
     }).GetAwaiter().GetResult();
 }
예제 #19
0
 public void GetTest()
 {
     Task.Run(async() => {
         using (var client = new HttpClient()) {
             string response  = await client.GetStringAsync(baseUrl + "node/" + Node1.selfNode.uuid);
             NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(response);
             Assert.AreEqual(NodeResponseCode.OK, res.statusCode);
             NodeInfo nodeInfo = JsonConvert.DeserializeObject <NodeInfo>(res.value);
             Assert.AreEqual(Node1.selfNode.uuid, nodeInfo.selfNode.uuid);
         }
     }).GetAwaiter().GetResult();
 }
예제 #20
0
 public void GetAllTest()
 {
     Task.Run(async() => {
         using (var client = new HttpClient()) {
             string response  = await client.GetStringAsync(baseUrl + "node");
             NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(response);
             Assert.AreEqual(NodeResponseCode.OK, res.statusCode);
             List <NodeId> nodeList = JsonConvert.DeserializeObject <List <NodeId> >(res.value);
             Assert.AreEqual(2, nodeList.Count);
         }
     }).GetAwaiter().GetResult();
 }
예제 #21
0
 public void PostTest()
 {
     Task.Run(async() => {
         using (var client = new HttpClient()) {
             int currentNodeCount  = Node.AllLocalNodes.Count;
             var response          = await client.PostAsync(baseUrl + "node", null);
             string responseString = await response.Content.ReadAsStringAsync();
             NodeResponse res      = JsonConvert.DeserializeObject <NodeResponse>(responseString);
             Assert.AreEqual(NodeResponseCode.OK, res.statusCode);
             Assert.AreEqual(currentNodeCount + 1, Node.AllLocalNodes.Count);
         }
     }).GetAwaiter().GetResult();
 }
예제 #22
0
 public void GetTest()
 {
     Task.Run(async() => {
         using (var client = new HttpClient()) {
             testNode.childNodes.Add(childNode.selfNode);
             string response = await client.GetStringAsync(baseUrl + "node/" + testNode.selfNode.uuid
                                                           + "/childNodes/" + childNode.selfNode.uuid);
             NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(response);
             Assert.AreEqual(res.statusCode, NodeResponseCode.OK);
             NodeId value = JsonConvert.DeserializeObject <NodeId>(res.value);
             Assert.AreEqual(value.uuid, childNode.selfNode.uuid);
         }
     }).GetAwaiter().GetResult();
 }
예제 #23
0
 public void GetAllTest()
 {
     Task.Run(async() => {
         using (var client = new HttpClient()) {
             // add brothers
             targetNode.brotherNodes.Add(brotherNode.selfNode);
             string response = await client.GetStringAsync(baseUrl + "node/" + targetNode.selfNode.uuid
                                                           + "/brotherNodes");
             NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(response);
             Assert.AreEqual(NodeResponseCode.OK, res.statusCode);
             List <NodeId> resBros = JsonConvert.DeserializeObject <List <NodeId> >(res.value);
             Assert.AreEqual(brotherNode.selfNode.uuid, resBros[0].uuid);
         }
     }).GetAwaiter().GetResult();
 }
예제 #24
0
        public async Task <ChannelDto> GetChannelInformationAsync(long channelId, NodeConnection nodeConnection)
        {
            try
            {
                GetObjectsInfoNodeRequest request = new GetObjectsInfoNodeRequest(new List <long> {
                    channelId
                }, null, Enums.NodeRequestType.GetChannels);
                SendRequest(nodeConnection, request);
                NodeResponse response = await GetResponseAsync(request).ConfigureAwait(false);

                return(((ChannelsNodeResponse)response).Channels.FirstOrDefault());
            }
            catch
            {
                return(null);
            }
        }
예제 #25
0
        /// <inheritdoc/>
        public override Task <NodeResponse> Read(KeyRequest request, ServerCallContext context)
        {
            var getResult = this.service.Read(request.Key);
            var value     = getResult.Item2 == null
                ? ByteString.Empty
                : ByteString.CopyFrom(getResult.Item2);
            var response = new NodeResponse
            {
                Node = new Node
                {
                    Key   = request.Key,
                    Value = value,
                },
                Succeeded = getResult.Item1,
            };

            return(Task.FromResult(response));
        }
예제 #26
0
        public void GetTest()
        {
            Node node1 = new Node(mSkynet);
            Node node2 = new Node(mSkynet);

            node1.grandParents             = node2.selfNode;
            node1.grandParentsModifiedTime = Skynet.Utils.Utils.UnixTimeNow();
            Task.Run(async() => {
                using (var client = new HttpClient()) {
                    string response = await client.GetStringAsync(baseUrl + "node/" + node1.selfNode.uuid
                                                                  + "/grandParents");
                    NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(response);
                    Assert.AreEqual(NodeResponseCode.OK, res.statusCode);
                    NodeId grandParents = JsonConvert.DeserializeObject <NodeId>(res.value);
                    Assert.AreEqual(node2.selfNode.uuid, grandParents.uuid);
                    Assert.AreEqual(node1.grandParentsModifiedTime, res.time);
                }
            }).GetAwaiter().GetResult();
        }
예제 #27
0
        private NodeResponse RegisterNewNode()
        {
            // TODO Validation of host?
            var nodeResponse = new NodeResponse {
                DataRows = new Dictionary <string, string>()
            };

            var     jsonString = Request.Body.AsString();
            JObject jsonObject;

            try
            {
                jsonObject = JObject.Parse(jsonString);
            }
            catch (JsonReaderException)
            {
                nodeResponse.HttpCode       = HttpStatusCode.BadRequest;
                nodeResponse.ResponseString = "Missing host parameter";

                return(nodeResponse);
            }

            var host = (string)jsonObject["host"];

            if (string.IsNullOrEmpty(host))
            {
                nodeResponse.HttpCode       = HttpStatusCode.BadRequest;
                nodeResponse.ResponseString = "No host provided";

                return(nodeResponse);
            }

            NodeBalance.NodeSet.Add(host);
            nodeResponse.HttpCode       = HttpStatusCode.OK;
            nodeResponse.ResponseString = "New host added, full host list returned";
            nodeResponse.DataRows.Add("Nodes", JsonConvert.SerializeObject(NodeBalance.NodeSet));

            Logger.Log.Info($"Добавлен новый хост {host}");
            Logger.Log.Debug($"Новое количество узлов: {NodeBalance.NodeSet.Count}");

            return(nodeResponse);
        }
예제 #28
0
        private static NodeResponse GetNodeInfo()
        {
            Logger.Log.Debug($"Адрес хоста: {Common.HostName}\n" +
                             $"Количество узлов: {NodeBalance.NodeSet.Count}");

            var nodeResponse = new NodeResponse
            {
                HttpCode       = HttpStatusCode.OK,
                ResponseString = "Node info returned",
                DataRows       = new Dictionary <string, string>
                {
                    ["Host"]                 = Common.HostName,
                    ["Chain Length"]         = OperationModule.Machine.Chain.Count.ToString(),
                    ["Visible Hosts"]        = NodeBalance.NodeSet.Count.ToString(),
                    ["Pending Transactions"] = OperationModule.Machine.Pending.ToString()
                }
            };

            return(nodeResponse);
        }
        private async void HandleResponse(NodeResponse response)
        {
            try
            {
                ICommunicationHandler handler = null;
                switch (response.ResponseType)
                {
                case NodeResponseType.Chats:
                    handler = new ChatsNodeResponseHandler(response, nodeConnection, appServiceProvider.CrossNodeService);
                    break;

                case NodeResponseType.Users:
                    handler = new UsersNodeResponseHandler(response, nodeConnection, appServiceProvider.CrossNodeService);
                    break;

                case NodeResponseType.Channels:
                    handler = new ChannelsNodeResponseHandler(response, nodeConnection, appServiceProvider.CreateChannelsService);
                    break;

                case NodeResponseType.Proxy:
                    handler = new ProxyUsersCommunicationsNodeResponseHandler(response, appServiceProvider.ConnectionsService);
                    break;

                case NodeResponseType.Files:
                    handler = new FilesInformationNodeResponseHandler(response, nodeConnection, appServiceProvider.FilesService);
                    break;
                }
                if (handler != null && handler.IsObjectValid())
                {
                    await handler.HandleAsync().ConfigureAwait(false);
                }
                if (NodeDataReceiver.ResponseTasks.TryGetValue(response.RequestId, out var taskCompletionSource))
                {
                    taskCompletionSource.TrySetResult(response);
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }
예제 #30
0
 public void GetParnentNode()
 {
     Task.Run(async() =>
     {
         using (var client = new HttpClient())
         {
             client.Timeout = TimeSpan.FromSeconds(200); // This process may take up to 2mins
             // create a node
             Node mNode = new Node(new List <NodeId>(), mSkynet);
             // set http headers
             client.DefaultRequestHeaders.Add("Uuid", Guid.NewGuid().ToString());
             client.DefaultRequestHeaders.Add("From-Node-Id", mNode.selfNode.uuid);
             client.DefaultRequestHeaders.Add("From-Tox-Id", mNode.selfNode.toxid);
             client.DefaultRequestHeaders.Add("To-Node-Id", mNode.selfNode.uuid);
             client.DefaultRequestHeaders.Add("To-Tox-Id", mSkynet.tox.Id.ToString());
             string res        = await client.GetStringAsync(baseUrl + "api/tox/" + ""); // the tox id does not exist
             NodeResponse mRes = JsonConvert.DeserializeObject <NodeResponse>(res);
             Assert.AreEqual(mRes.statusCode, NodeResponseCode.NotFound);
         }
     });
 }