Exemple #1
0
        private static void leaveDHT(Container container)
        {
            if (string.IsNullOrEmpty(DHTServerCtx.DHT.Parent))
            {
                Console.WriteLine("Left with code 200 - No parent");
                return;
            }

            var client      = new RestClient(DHTServerCtx.DHT.Parent);
            var requestPath = (RouteAttribute)Attribute.GetCustomAttribute(typeof(LeaveChildDto), typeof(RouteAttribute));
            var request     = new RestRequest(requestPath.Path, Method.POST);

            request.RequestFormat = DataFormat.Json;
            var data = new LeaveChildDto {
                Child    = DHTServerCtx.DHT.Child,
                RangeMin = DHTServerCtx.DHT.HashRange.Min.ToString(),
                RangeMax = DHTServerCtx.DHT.HashRange.Max.ToString(),
                Data     = removeFromDb(container)
            };

            request.AddJsonBody(data);

            Console.WriteLine("Sending leave as child request to {0} {1}", client.BaseUrl, requestPath.Path);
            RestResponse response = (RestResponse)client.Execute(request);

            if (DHTServerCtx.DHT.Parent != DHTServerCtx.DHT.Child)
            {
                //notify child
                leaveMyChild();
            }

            Console.WriteLine("Left as child with code {0}", response.StatusCode);
        }
        //server want to leave DHT
        public object Post(LeaveChildDto req)
        {
            DHTServerCtx.DHT.mergeRange (new DHT {
                Child = req.Child,
                HashRange = new HashRange {
                    Min = BigInteger.Parse (req.RangeMin),
                    Max = BigInteger.Parse (req.RangeMax)
                }
            });

            var valueList = req.Data.Select (v => new Value {
                Key = v.Key,
                Data = v.Data,
                Hash = DHTServerCtx.HashFunction.apply (v.Key)
            }).ToList ();
            Db.InsertAll<Value> (valueList);

            log.Info (string.Format ("Old child left (new child {0}), new max range={1}", DHTServerCtx.DHT.Child, DHTServerCtx.DHT.HashRange.Max));
            log.Debug (DHTServerCtx.DHT);
            return new HttpResult { StatusCode = HttpStatusCode.NoContent };
        }