Exemplo n.º 1
0
        public bool Add(SubmitParams e, out bool newEntryCreated)
        {
            string hash = e.Address.GetHashString();

            newEntryCreated = false;

            if (nodeMap.ContainsKey(hash))
            {
                Log.Instance.Write("Node map entry updated");
                nodeMap[hash].LastAccessTime = e.LastAccessTime;
                nodeMap[hash].Version        = e.Version;
            }
            else
            {
                float  latitude = 0, longitude = 0;
                string continentCode, countryCode;
                if (GeoLocator.Get(e.Address, out latitude, out longitude, out continentCode, out countryCode))
                {
                    e.Address       = hash;
                    e.Latitude      = latitude;
                    e.Longitude     = longitude;
                    e.ContinentCode = continentCode;
                    e.CountryCode   = countryCode;
                    nodeMap.Add(hash, e);
                    Log.Instance.Write("Node map entry created");
                    newEntryCreated = true;
                }
                else
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
        private void HandleRequest(HttpListenerContext context)
        {
            HttpListenerRequest  request  = context.Request;
            HttpListenerResponse response = context.Response;

            response.ContentType = "application/json";

            string text;

            using (var reader = new StreamReader(request.InputStream, request.ContentEncoding))
            {
                text = reader.ReadToEnd();
            }

            string method = request.Url.Segments[1];

            dynamic json = JObject.Parse(text);

            Log.Instance.Write($"Received new request: {json.method}");
            switch (json.method.Value)
            {
            case "submit":
            {
                SubmitParams sp = JsonConvert.DeserializeObject <RpcRequest <SubmitParams> >(text).Params;
                bool         ne = false;
                bool         ok = dataStore.Add(sp, out ne);
                if (ok)
                {
                    if (ne)
                    {
                        MapDataChanged?.Invoke();
                    }
                }

                string result = ok ? RESULT_OK : RESULT_BAD;
                response.OutputStream.Write(Encoding.ASCII.GetBytes(result));
                context.Response.Close();
            }
            break;

            case "fetch":
            {
                FetchParams fp = JsonConvert.DeserializeObject <RpcRequest <FetchParams> >(text).Params;
                response.OutputStream.Write(Encoding.ASCII.GetBytes(dataStore.Fetch(fp)));
                context.Response.Close();
            }
            break;

            case "prune":
            {
                PruneParams pp = JsonConvert.DeserializeObject <RpcRequest <PruneParams> >(text).Params;

                if (string.IsNullOrEmpty(pp.Key))
                {
                    Log.Instance.Write(Log_Severity.Warning, "No access key provided");
                    return;
                }

                if (!Config.AllowedKeys.Contains(pp.Key))
                {
                    Log.Instance.Write(Log_Severity.Warning, "Invalid access key");
                    return;
                }

                int  pc = 0;
                bool ok = dataStore.Prune(pp, out pc);
                if (ok && !pp.DryRun)
                {
                    if (pc > 0)
                    {
                        MapDataChanged?.Invoke();
                    }
                }

                string result = ok ? RESULT_OK : RESULT_BAD;
                response.OutputStream.Write(Encoding.ASCII.GetBytes(result));
                context.Response.Close();
            }
            break;

            case "save":
            {
                Task.Run(() =>
                    {
                        new ObjectSerializer().Serialize(dataStore, "NodeMap.xml");
                        Log.Instance.Write("Node map data saved");

                        Log.Instance.Write("Saving node map data to json");
                        File.WriteAllText("/var/www/html/nodemap.json", $"{{\"status\":\"OK\",\"result\":{dataStore.FetchAll()}}}\r\n");
                    });
            }
            break;

            default:
                Log.Instance.Write($"Invalid request: {json.Method}");
                break;
            }
        }
Exemplo n.º 3
0
 private string NodeMapEntryToJson(SubmitParams n) =>
 $"{{\"version\":\"{n.Version}\",\"time\":\"{n.LastAccessTime}\",\"lat\":\"{n.Latitude}\",\"long\":\"{n.Longitude}\",\"cn\":\"{n.ContinentCode}\",\"cc\":\"{n.CountryCode}\"}}";