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); }
/// <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); }