コード例 #1
0
ファイル: Node.cs プロジェクト: opensourcer2/SharpLink
        public void relatedNodesStatusChanged(NodeId targetNode)
        {
            if (targetNode == nodeChangeLock.from)
            {
                nodeChangeLock.isLocked = false;
            }
            // child nodes offline
            NodeId childNodeToRemove = childNodes.Where(x => x.uuid == targetNode.uuid).DefaultIfEmpty(null).FirstOrDefault();

            if (childNodeToRemove != null)
            {
                childNodes.Remove(targetNode);
                Task.Run(async() =>
                {
                    ToxResponse res = await RequestProxy.sendRequest(mSkynet, new ToxRequest
                    {
                        uuid       = Guid.NewGuid().ToString(),
                        url        = "node/" + selfNode.uuid + "/childNodes",
                        method     = "put",
                        content    = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(childNodes)),
                        fromNodeId = selfNode.uuid,
                        fromToxId  = selfNode.toxid,
                        toNodeId   = selfNode.uuid,
                        toToxId    = selfNode.toxid,
                        time       = childNodesModifiedTime,
                    });
                });
                return;
            }
            // parent node offline
            if (targetNode.uuid == parent.uuid)
            {
                Task.Run(async() =>
                {
                    if (grandParents == null)
                    {
                        return;
                    }
                    bool isConnected = await joinNetByTargetParents(new List <NodeId> {
                        grandParents
                    });
                    // rejoin net might be failed, grandparents may also offline
                });
            }

            // grand parents and brothers will not be processed, just wait for request from parents
        }
コード例 #2
0
ファイル: Node.cs プロジェクト: ZhuJingquan/SharpLink
        public async Task <NodeResponse> sendRequest(NodeId target, string content, string method,
                                                     string url, long time = 0)
        {
            ToxResponse response = await RequestProxy.sendRequest(mSkynet, new ToxRequest {
                uuid       = Guid.NewGuid().ToString(),
                url        = url,
                method     = method,
                content    = Encoding.UTF8.GetBytes(content),
                fromNodeId = selfNode.uuid,
                fromToxId  = selfNode.toxid,
                toNodeId   = target.uuid,
                toToxId    = target.toxid,
                time       = time
            });

            if (response == null)
            {
                return(null); // request send failed
            }
            NodeResponse res = JsonConvert.DeserializeObject <NodeResponse>(Encoding.UTF8.GetString(response.content));

            return(res);
        }
コード例 #3
0
ファイル: Node.cs プロジェクト: ZhuJingquan/SharpLink
        /// <summary>
        /// method to join skynet by set target parents
        /// </summary>
        /// <returns>
        /// the target distributed
        /// </returns>
        public async Task <bool> joinNetByTargetParents(List <NodeId> parentsList)
        {
            List <NodeId> targetNodeList   = parentsList;
            List <NodeId> checkedNodesList = new List <NodeId>();
            NodeId        target           = null;

            while (targetNodeList.Count > 0 && target == null)
            {
                NodeId     parentNode   = targetNodeList[0];
                ToxRequest addParentReq = new ToxRequest
                {
                    url        = "node/" + parentNode.uuid + "/childNodes",
                    method     = "post",
                    uuid       = Guid.NewGuid().ToString(),
                    content    = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(selfNode)),
                    fromNodeId = selfNode.uuid,
                    fromToxId  = selfNode.toxid,
                    toNodeId   = parentNode.uuid,
                    toToxId    = parentNode.toxid,
                    time       = Utils.Utils.UnixTimeNow(),
                };
                ToxResponse mRes = await RequestProxy.sendRequest(mSkynet, addParentReq);

                // send req failed or target is currently locked, ie target is not avaliable right now. remove target node from nodelist
                NodeResponse addParentResponse = JsonConvert.DeserializeObject <NodeResponse>(Encoding.UTF8.GetString(mRes.content));

                if (addParentResponse.statusCode == NodeResponseCode.TargetLocked ||
                    addParentResponse.statusCode == NodeResponseCode.TargetIsFull)
                {
                    targetNodeList.Remove(parentNode);
                    if (!checkedNodesList.Contains <NodeId>(parentNode))
                    {
                        checkedNodesList.Add(parentNode);
                    }
                    // new nodes, not checked yet
                    List <NodeId> newTargetsList = JsonConvert.DeserializeObject <List <NodeId> >(addParentResponse.value).Where((mnode) =>
                    {
                        return(!checkedNodesList.Contains <NodeId>(mnode) && !targetNodeList.Contains <NodeId>(mnode));
                    }).ToList();
                    targetNodeList = targetNodeList.Concat(newTargetsList).ToList();
                    continue;
                }
                else if (addParentResponse.statusCode == NodeResponseCode.OK)
                {
                    // set parent and connect status
                    target = new NodeId
                    {
                        toxid = mRes.fromToxId,
                        uuid  = mRes.fromNodeId
                    };
                    isConnected = true;
                    break;
                }
                else
                {
                    // try to connect next target
                    targetNodeList.Remove(parentNode);
                    if (!checkedNodesList.Contains <NodeId>(parentNode))
                    {
                        checkedNodesList.Add(parentNode);
                    }
                }
            }

            if (target != null)
            {
                isConnected = true;
                // set parents, will boardcast grandparents change to child nodes, and set target grandparents
                ToxResponse setParentResponse = await RequestProxy.sendRequest(mSkynet, new ToxRequest {
                    url        = "node/" + selfNode.uuid + "/parent",
                    method     = "put",
                    content    = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(target)),
                    fromNodeId = selfNode.uuid,
                    fromToxId  = selfNode.toxid,
                    toNodeId   = selfNode.uuid,
                    toToxId    = selfNode.uuid,
                    uuid       = Guid.NewGuid().ToString(),
                    time       = Utils.Utils.UnixTimeNow(),
                });
            }
            else
            {
                parent      = null;
                isConnected = false;
            }
            return(isConnected);
        }