-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandManager.cs
137 lines (115 loc) · 4.32 KB
/
CommandManager.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
using System;
using System.Threading;
using WebSocketSharp;
using WebSocketSharp.Server;
namespace PAYMAP_BACKEND
{
public class CommandManager
{
private static CommandManager _instance;
private static Thread _monitorMasterThread;
private static bool _flagMonitorThread;
public static int IntervalMonitorThread = 1000;
private static WebSocket _masterWebSocket;
private static WebSocketServer _commandWebSocketServer;
public static bool IsModuleRemoteRunning = false;
public static bool IsModuleRemoteHealthy = false;
public static bool IsModuleDEVXRunning = false;
public static bool IsModuleDEVXHealthy = false;
private CommandManager()
{
}
public static CommandManager GetInstance()
{
return _instance ?? (_instance = new CommandManager());
}
public static void StartCommandServer()
{
if (_commandWebSocketServer == null)
{
_commandWebSocketServer = new WebSocketServer(9981);
_commandWebSocketServer.AddWebSocketService<RemoteCommand>("/RemoteCommand");
}
_commandWebSocketServer.Start();
}
public static void StopCommandServer()
{
_commandWebSocketServer?.Stop();
}
public static void ConnectMasterServer()
{
if (_masterWebSocket == null)
{
_masterWebSocket = new WebSocket("ws://localhost:8888/PHOENIXListener");
}
_masterWebSocket.Connect();
_flagMonitorThread = true;
if (_monitorMasterThread == null)
{
_monitorMasterThread = new Thread(MonitorMasterServer)
{
Priority = ThreadPriority.Lowest
};
}
if (!_monitorMasterThread.IsAlive)
{
_monitorMasterThread.Start();
}
}
public static void DisconnectMasterServer(bool hardOff = false)
{
_masterWebSocket?.Close();
_flagMonitorThread = true;
if (!hardOff || _monitorMasterThread == null || !_monitorMasterThread.IsAlive) return;
try
{
_monitorMasterThread.Interrupt();
}
catch (Exception exception)
{
LogManager.NewLog(LogType.CommandManager, LogLevel.Error, "DisconnectMasterServer", exception);
}
}
private static void MonitorMasterServer()
{
while (true)
{
if (!_flagMonitorThread)
{
LogManager.NewLog(LogType.CommandManager, LogLevel.Info, "MonitorMasterServer", "Master Monitor Thread Stop : Safe");
break;
}
if (_masterWebSocket == null)
{
LogManager.NewLog(LogType.CommandManager, LogLevel.Error, "MonitorMasterServer", "Master Monitor Thread Stop : _masterWebSocket null");
break;
}
if (!_masterWebSocket.IsAlive)
{
LogManager.NewLog(LogType.CommandManager, LogLevel.Info, "MonitorMasterServer", "Master Monitor Thread : trying to reconnect");
_masterWebSocket.Connect();
}
Thread.Sleep(IntervalMonitorThread);
}
}
public static void OnServerCommand(string command)
{
LogManager.NewLog(LogType.CommandManager, LogLevel.Info, "OnServerCommand", command);
}
private class RemoteCommand : WebSocketBehavior
{
protected override void OnMessage(MessageEventArgs e)
{
var commandRaw = e.Data;
try
{
LogManager.NewLog(LogType.CommandManager, LogLevel.Warn, "RemoteCommand : OnMessage", commandRaw);
}
catch (Exception exception)
{
LogManager.NewLog(LogType.CommandManager, LogLevel.Error, "RemoteCommand : OnMessage", exception);
}
}
}
}
}