-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBXCNode.cs
149 lines (136 loc) · 2.95 KB
/
BXCNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Created by SharpDevelop.
* User: allen
* Date: 05/06/2019
* Time: 09:38
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Net;
using System.Web.Script.Serialization;
using System.Windows.Media;
using System.Net.Sockets;
namespace bxc_bound
{
/// <summary>
/// Description of BXCNode.
/// </summary>
public class BXCNode : IComparable<BXCNode>
{
public string api_ip;
public api_discovery Discovery;
public api_status Status;
public api_version Version;
public bool IsOK;
public bool IsSelected;
public bool IsBoundOK;
public BXCNode(string ip, TimeSpan timeout)
{
this.api_ip = ip;
this.IsOK = false;
this.IsSelected = false;
this.IsBoundOK = false;
try
{
if(BXCNode.IsPortOpen(ip, 9017, timeout))
{
WebClient wc = new WebClient();
this.Discovery = new JavaScriptSerializer().Deserialize<api_discovery>(wc.DownloadString("http://" + ip + ":9017/discovery"));
this.Status = new JavaScriptSerializer().Deserialize<api_status>(wc.DownloadString("http://" + ip + ":9017/status"));
this.Version = new JavaScriptSerializer().Deserialize<api_version>(wc.DownloadString("http://" + ip + ":9017/version"));
if(this.Discovery.cksum == "BonusCloud")
{
this.IsOK = true;
}
}
}
catch
{
}
}
public SolidColorBrush ToBrush()
{
if(this.IsOK)
{
if(this.Status.status.bound)
{
return Brushes.Green;
}
else
{
return Brushes.Black;
}
}
else
{
return Brushes.Black;
}
}
public override string ToString()
{
try{
string process = "";
if(this.Status.status.bound && !this.Status.status.process)
{
process = " [节点离线]";
}
return this.api_ip + process + System.Environment.NewLine + this.Discovery.mac + " - " + this.Version.version ;
}
catch
{
return this.api_ip + System.Environment.NewLine;
}
}
public class api_discovery
{
public string cksum { get; set; }
public string ip { get; set; }
public string mac { get; set; }
}
public class key_status
{
public bool bound { get; set; }
public bool process { get; set; }
}
public class api_status
{
public key_status status { get; set; }
}
public class api_version
{
public string version { get; set; }
}
static bool IsPortOpen(string host, int port, TimeSpan timeout)
{
try
{
using(var client = new TcpClient())
{
var result = client.BeginConnect(host, port, null, null);
var success = result.AsyncWaitHandle.WaitOne(timeout);
if (!success)
{
return false;
}
client.EndConnect(result);
}
}
catch
{
return false;
}
return true;
}
public int CompareTo(BXCNode other)
{
try{
return Convert.ToInt32(this.api_ip.Split('.')[3]).CompareTo(Convert.ToInt32(other.api_ip.Split('.')[3]));
}
catch
{
return 0;
}
}
}
}