예제 #1
0
 /// <summary>
 /// send msg to all the other nodes
 /// </summary>
 /// <param name="req"></param>
 /// <returns></returns>
 public Task boardCastAll(ToxRequest req)
 {
     return(Task.Factory.StartNew(() =>
     {
         // boardcast to all nodes
     }));
 }
예제 #2
0
        public static async Task<ToxResponse> sendRequest(Skynet host, ToxRequest req) {

            // if req is not send to local node
            if (host.tox.Id.ToString() != req.toToxId) {
                bool mResStatus = false;
                return await host.sendRequest(new ToxId(req.toToxId), req, out mResStatus);
            }

            string baseUrl = "http://localhost:" + host.httpPort + "/";
            var request = (HttpWebRequest)WebRequest.Create(baseUrl + "api/" + req.url);
            request.Headers.Add("Uuid", req.uuid);
            request.Headers.Add("From-Node-Id", req.fromNodeId);
            request.Headers.Add("From-Tox-Id", req.fromToxId);
            request.Headers.Add("To-Node-Id", req.toNodeId);
            request.Headers.Add("To-Tox-Id", req.toToxId);
            request.Headers.Add("Skynet-Time", req.time + "");
            request.Method = req.method.ToUpper();
            request.ContentType = "application/json";

            List<string> allowedMethods = new List<string> { "POST", "PUT", "PATCH" };
            if (allowedMethods.Any(x => x == req.method.ToUpper())) {
                // only the above methods are allowed to add body data
                using (var streamWriter = new StreamWriter(request.GetRequestStream()))
                {
                    streamWriter.Write(req.content);
                }
            }
            var response = await request.GetResponseAsync();
            var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
            return req.createResponse(responseString);
        }
예제 #3
0
        /// <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);
        }