Exemplo n.º 1
0
        static void Main(string[] args)
        {
            Masternode.StartMonitoring();

            JsonRpc.OnReceivedHttpRequest(c =>
            {
                c.Response.AddHeader("Access-Control-Allow-Origin", "*");

                if (c.Request.HttpMethod == "OPTIONS")
                {
                    c.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
                    c.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
                    c.Response.Close();
                }

                return(Task.CompletedTask);
            });

            JsonRpc.OnReceivedRequest(c =>
            {
                Console.WriteLine($"method: {c.Request?.Method} params: {c.Request?.Params}");

                return(Task.CompletedTask);
            });

            var address = "http://*:5000";

#if DEBUG
            address = "http://localhost:5000/";
#endif

            JsonRpc.Start(address);

            Console.CancelKeyPress += (sender, eventArgs) => ResetEvent.Set();
            ResetEvent.WaitOne();

            JsonRpc.Stop();
        }
Exemplo n.º 2
0
        public static async void StartMonitoring()
        {
            while (true)
            {
                try
                {
                    //full
                    var fullResult = await BitSendCli.RunAsync("masternodelist full");

                    var fullReader = new JsonTextReader(new StringReader(fullResult));

                    //rank
                    var rankResult = await BitSendCli.RunAsync("masternodelist rank");

                    var rankReader     = new JsonTextReader(new StringReader(rankResult));
                    var ranks          = new List <(string address, int rank)>();
                    var vinIndexes     = new Dictionary <string, int>();
                    var addressIndexes = new Dictionary <string, int>();
                    var nodeIds        = new HashSet <string>();

                    while (rankReader.Read())
                    {
                        if (rankReader.TokenType == JsonToken.PropertyName)
                        {
                            var address = (string)rankReader.Value;
                            var rank    = rankReader.ReadAsInt32() ?? 0;
                            ranks.Add((address, rank));
                        }
                    }

                    while (fullReader.Read())
                    {
                        if (fullReader.TokenType == JsonToken.PropertyName)
                        {
                            var vin = ((string)fullReader.Value).Trim();

                            if (vinIndexes.TryGetValue(vin, out var vinIndex))
                            {
                                vinIndex++;
                            }

                            var nodeId = $"{vin}-{vinIndex}";
                            nodeIds.Add(nodeId);

                            if (!All.TryGetValue(nodeId, out var masternode))
                            {
                                masternode = new Masternode
                                {
                                    NodeId   = nodeId,
                                    Vin      = vin,
                                    VinIndex = vinIndex
                                };
                            }

                            var info   = fullReader.ReadAsString();
                            var values = info.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                            masternode.Status   = values[0];
                            masternode.Protocol = int.Parse(values[1]);
                            masternode.PubKey   = values[2];

                            var address = values[3];
                            masternode.Address = address;
                            var portIndex = address.LastIndexOf(':');

                            if (portIndex > -1)
                            {
                                masternode.IP   = address.Substring(0, portIndex);
                                masternode.Port = int.Parse(address.Substring(portIndex + 1));
                            }

                            var lastSeenSeconds = int.Parse(values[4]);
                            masternode.LastSeen = Unix.GetTimestampFromSeconds(lastSeenSeconds);

                            masternode.ActiveSeconds = int.Parse(values[5]);
                            var activeDuration = TimeSpan.FromSeconds(masternode.ActiveSeconds);
                            masternode.ActiveDuration = $"{activeDuration:%d} days {activeDuration:hh} hours {activeDuration:mm} minutes {activeDuration:ss} seconds";

                            vinIndexes[masternode.Vin] = vinIndex;
                            masternode.VinIndex        = vinIndex;

                            if (addressIndexes.TryGetValue(masternode.Address, out var addressIndex))
                            {
                                addressIndex++;
                            }

                            addressIndexes[masternode.Address] = addressIndex;
                            var addressRanks = ranks.Where(r => r.address == masternode.Address).ToList();
                            if (addressRanks.Count > addressIndex)
                            {
                                masternode.Rank = addressRanks[addressIndex].rank;
                            }

                            masternode.LastRefresh = DateTime.UtcNow;

                            All[masternode.NodeId] = masternode;
                        }
                    }

                    var missingIds = All.Keys.Where(k => !nodeIds.Contains(k)).ToList();
                    foreach (var missingId in missingIds)
                    {
                        All.TryRemove(missingId, out var _);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }

                if (!IsTestingConnections)
                {
                    StartTestingConnections();
                }
                if (!IsRetreivingLocations)
                {
                    StartRetreivingLocations();
                }

                await Task.Delay(TimeSpan.FromMinutes(2));
            }
        }