Example #1
0
        public static void Main(string[] args)
        {
            Log.CreateInstance(true);
            AngryWasp.Serializer.Serializer.Initialize();

            CommandLineParser cmd = CommandLineParser.Parse(args);

            NodeMapDataStore ds = new NodeMapDataStore();

            if (File.Exists("NodeMap.xml"))
            {
                Log.Instance.Write("Loading node map info");
                ds = new ObjectSerializer().Deserialize <NodeMapDataStore>(XDocument.Load("NodeMap.xml"));
                Log.Instance.Write($"Node map loaded {ds.NodeMap.Count} items from file");

                if (!File.Exists("/var/www/html/nodemap.json"))
                {
                    File.WriteAllText("/var/www/html/nodemap.json", $"{{\"status\":\"OK\",\"result\":{ds.FetchAll()}}}\r\n");
                }
            }

            string        apiKey      = null;
            List <string> allowedKeys = new List <string>();

            if (cmd["geo-api-key"] != null)
            {
                Log.Instance.Write("Geolocation API key loaded from command line");
                apiKey = cmd["geo-api-key"].Value;
            }

            if (apiKey == null && cmd["geo-api-key-file"] != null)
            {
                //todo: check file exists
                Log.Instance.Write("Geolocation API key loaded from file");
                apiKey = File.ReadAllText(cmd["geo-api-key-file"].Value);
            }

            if (apiKey == null)
            {
                Log.Instance.Write(Log_Severity.Fatal, "Geolocation API key not provided!");
                Environment.Exit(0);
            }
            else
            {
                Log.Instance.Write($"Loaded Geolocation API key {apiKey}");
            }

            if (cmd["access-keys"] != null)
            {
                //todo: check file exists
                Log.Instance.Write("Access keys loaded from file");
                Config.AllowedKeys = File.ReadAllLines(cmd["access-keys"].Value).ToList();
            }

            GeoLocator.ApiKey = apiKey;

            int port = DEFAULT_PORT;

            if (cmd["port"] != null)
            {
                port = new IntSerializer().Deserialize(cmd["port"].Value);
            }

            Log.Instance.Write($"Listening on port {port}");

            RpcListener r = new RpcListener();

            bool mapDataChanged = false;

            r.MapDataChanged += () => {
                mapDataChanged = true;
            };

            //run once every 5 minutes
            Timer t = new Timer(1000 * 60 * 5);

            t.Elapsed += (s, e) =>
            {
                if (mapDataChanged)
                {
                    Task.Run(() =>
                    {
                        new ObjectSerializer().Serialize(r.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\":{r.DataStore.FetchAll()}}}\r\n");

                        mapDataChanged = false;
                    });
                }
            };

            t.Start();

            r.Start(ds, port);

            Application.Start();
        }