Browse Source

* Program.cs:
* clsNode.cs:
* clsListNodes.cs: Add version number & display column adjustment.

* .gitignore:

Logic Ethos Ltd 1 year ago
parent
commit
b122ffef6f
4 changed files with 91 additions and 33 deletions
  1. 1 0
      .gitignore
  2. 72 6
      TFA-Bot/DataClasses/clsNode.cs
  3. 1 1
      TFA-Bot/DiscordBot/Commands/clsListNodes.cs
  4. 17 26
      TFA-Bot/Program.cs

+ 1 - 0
.gitignore

@@ -1,5 +1,6 @@
 sipp-*
 vs/
+.vs/
 
 ## Ignore Visual Studio temporary files, build results, and
 ## files generated by popular Visual Studio add-ons.

+ 72 - 6
TFA-Bot/DataClasses/clsNode.cs

@@ -17,7 +17,6 @@ namespace TFABot
         {
         }
         
-        
         public clsNodeGroup NodeGroup;
         
         public uint LatencyLowest { get; private set;}
@@ -32,6 +31,7 @@ namespace TFABot
         public bool Monitor {get;set;}
         
         public String ErrorMsg {get; private set;}
+        public String NodeVersion {get; private set;}
         
         public int Latency
         {
@@ -51,6 +51,7 @@ namespace TFABot
                 if (_leaderHeight < value)
                 {
                     _leaderHeight = value;
+                    if (!LastLeaderHeight.HasValue) GetVersionAsync();  //Get version no if leader now known.
                     LastLeaderHeight = DateTime.UtcNow;
                 }
             }
@@ -106,8 +107,8 @@ namespace TFABot
                 Program.AlarmManager.New(AlarmLatencyLow);
              }
            }
-        }        
-        
+        }
+
 
         clsAlarm AlarmRequestFail = null;
         uint _requestFailCount;
@@ -126,6 +127,7 @@ namespace TFABot
                 {
                     Program.AlarmManager.Clear(AlarmRequestFail,$"CLEARED: {Name} now responding.");
                     AlarmRequestFail = null;
+                    GetVersionAsync();
                 }
                 if (!String.IsNullOrEmpty(ErrorMsg)) ErrorMsg="";
              }
@@ -166,13 +168,11 @@ namespace TFABot
                 
                 if(response.ResponseStatus == ResponseStatus.Completed)
                 {                    
-                    
                    var content = response.Content; // raw content as string
                    
                    var pos1 = 0;
                    var pos2= 0;
                    
-                   
                     if (!string.IsNullOrEmpty(content))
                     {
                         pos1 = content.IndexOf("leaderheight\":");
@@ -217,7 +217,63 @@ namespace TFABot
                 RequestFailCount++;
             }
         }
-               
+
+        public async Task GetVersionAsync(int timeout = 2000)
+        {
+             await Task.Run(() => {GetVersion(timeout);});
+        }
+
+        public void GetVersion(int timeout = 2000)
+        {
+            try {
+                var client = new RestClient($"http://{Host}:8088");
+                        
+                client.Timeout = timeout;                    
+                        
+                var request = new RestRequest("v2", Method.POST);
+                request.AddHeader("Content-type", "application/json");
+                request.AddHeader("header", "value");
+                request.AddJsonBody(
+                    new { jsonrpc = "2.0", id = 0, method = "properties" }
+                );
+                  
+                // execute the request
+                IRestResponse response = client.Execute(request);
+                
+                if(response.ResponseStatus == ResponseStatus.Completed)
+                {                    
+                   var content = response.Content; // raw content as string
+                   
+                   var pos1 = 0;
+                   var pos2= 0;
+                   
+                    if (!string.IsNullOrEmpty(content))
+                    {
+                        pos1 = content.IndexOf("factomdversion\":");
+                        pos1+=17;
+                        pos2 = content.IndexOf("\"",pos1);
+                        if (pos2>pos1) NodeVersion = content.Substring(pos1,pos2-pos1);
+                    }
+                    else
+                    {
+                        ErrorMsg="Empty version data";
+                    }
+                    
+               } else if(response.ResponseStatus == ResponseStatus.Error || response.ResponseStatus == ResponseStatus.TimedOut)
+               {
+                    ErrorMsg=response.ErrorMessage;
+               }
+               else if(response.ResponseStatus == ResponseStatus.None)
+               {
+                    ErrorMsg="Empty data";
+               }
+            }
+            catch (Exception ex)
+            {
+                Console.WriteLine(ex.Message);
+            }
+        }
+      
         async public void PingHostAsync()
         {
             try
@@ -323,5 +379,15 @@ namespace TFABot
             return $"{Name}\t{Host}\t{LeaderHeight}\t{LatencyList.CurrentAverage.ToString().PadLeft(3)} ms ({(100-PacketLoss.CurrentAverage):0.#}%) {ErrorMsg}";
         }
         
+        public void AppendDisplayColumns(ref clsColumnDisplay columnDisplay)
+        {
+            columnDisplay.AppendCol(Name);
+            columnDisplay.AppendCol(Host);
+            columnDisplay.AppendCol(NodeVersion);
+            columnDisplay.AppendCol($"{LeaderHeight}");
+            columnDisplay.AppendCol($"{LatencyList.CurrentAverage.ToString().PadLeft(3)} ms ({(100-PacketLoss.CurrentAverage):0.#}%) {ErrorMsg}");
+            
+        }
+        
     }
 }

+ 1 - 1
TFA-Bot/DiscordBot/Commands/clsListNodes.cs

@@ -18,7 +18,7 @@ namespace TFABot.DiscordBot.Commands
         
         public void Run(MessageCreateEventArgs e)
         {
-            e.Channel.SendMessageAsync(TFABot.Program.GetNodes());
+            e.Channel.SendMessageAsync($"```{TFABot.Program.GetNodes()}```");
         }
         
         public String HelpString

+ 17 - 26
TFA-Bot/Program.cs

@@ -190,34 +190,25 @@ namespace TFABot
 
         static public String GetNodes()
         {
-                var sb = new StringBuilder();
-                sb.Append("```");
-                
-                sb.AppendLine("Node        | Host                 |   Height   | Ave reply");
-                sb.AppendLine("------------|----------------------|------------|-----------");
-                
-                foreach (var group in NodeGroupList)
+            var cd = new clsColumnDisplay();
+             
+            cd.AppendCol("Node");
+            cd.AppendCol("Host");
+            cd.AppendCol("Ver");
+            cd.AppendCol("Height");
+            cd.AppendCol("Ave reply");
+             
+            cd.AppendCharLine('-');
+
+            foreach (var group in NodeGroupList)
+            {
+                foreach (var node in NodesList.Values.Where(x=>x.Group == group.Key))
                 {
-                    foreach (var node in NodesList.Values.Where(x=>x.Group == group.Key))
-                    {
-                        var nodetext = node.ToString().Split('\t');
-                        if (nodetext.Length==4)
-                        {
-                            sb.Append($"{nodetext[0].PadRight(12)}");  //Node
-                            sb.Append($"| {nodetext[1].Replace("http://","").PadRight(21)}"); //URL
-                            sb.Append($"| {nodetext[2].PadRight(11)}"); //Height
-                            sb.Append($"| {nodetext[3]}"); //Ave reply
-                        }else
-                        {
-                            sb.Append(node);
-                        }
-                        sb.AppendLine();
-                    }
+                    node.AppendDisplayColumns(ref cd);
+                    cd.NewLine();
                 }
-
-                sb.Append("```");
-                return sb.ToString();
+            }
+            return cd.ToString();
         }
-       
     }
 }