-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.cs
161 lines (142 loc) · 4.11 KB
/
server.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
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.IO;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using WebSocketSharp;
using WebSocketSharp.Server;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace guardian_server {
class Settings {
public string server_name { get; set; }
public string browser_path { get; set; }
public string ui_path { get; set; }
public List<string> plugins { get; set; }
public int port { get; set; }
public int sensors_interval { get; set; }
public int devices_interval { get; set; }
public string keyboard { get; set; }
}
public class Message {
public string tag { get; set; }
public dynamic data { get; set; }
}
public class Service : WebSocketBehavior {
Server server;
void send_message(string tag, dynamic data) {
Send(server.make_message(tag, data));
}
protected override void OnOpen() {
Program.log.add_line("connection: " + Context.UserEndPoint);
}
protected override void OnMessage(MessageEventArgs e) {
Message message;
if (!e.IsText) {
return;
}
try {
message = JsonConvert.DeserializeObject<Message>(e.Data);
} catch {
return;
}
if (message.tag == "get_devices") {
send_message("devices", server.devices);
} else if (server.fifo_in.Count < 1024) {
server.fifo_in.Enqueue(message);
}
}
public Service(Server s) {
server = s;
}
}
public class Server {
public Dictionary<string, dynamic> sensors;
public Dictionary<string, dynamic> devices;
Stopwatch sensors_timer;
Stopwatch devices_timer;
public Hardware hw;
WebSocketServer wssv { get; set; }
public ConcurrentQueue<Message> fifo_in;
public ConcurrentQueue<string> fifo_out;
bool send_sensors_now = false;
public string make_message(string tag, dynamic data) {
var message = new Message();
message.tag = tag;
message.data = data;
return JsonConvert.SerializeObject(message);
}
void update() {
if (sensors_timer.ElapsedMilliseconds > Program.settings.sensors_interval) {
sensors_timer.Restart();
send_sensors_now = true;
}
if (devices_timer.ElapsedMilliseconds > (1000 * 60 * Program.settings.devices_interval)) {
devices_timer.Restart();
hw.update_devices();
}
}
void start_plugins() {
foreach (string file in Program.settings.plugins) {
Program.log.add_line("plugin: " + file);
Tools.run_process(file, "");
}
while (wssv.WebSocketServices["/"].Sessions.Count < Program.settings.plugins.Count) {
Thread.Sleep(10);
}
}
public void send_plugin_message(string tag, dynamic data) {
fifo_out.Enqueue(make_message(tag, data));
}
void send_sensors() {
if (send_sensors_now) {
hw.update_sensors();
sensors["time"] = DateTime.UtcNow;
wssv.WebSocketServices["/"].Sessions.Broadcast(make_message("sensors", sensors));
send_sensors_now = false;
}
}
void process_messages() {
Message msg_in;
string msg_out;
while (fifo_in.TryDequeue(out msg_in)) {
hw.get_message(msg_in.tag, msg_in.data);
send_sensors_now = true;
}
while (fifo_out.TryDequeue(out msg_out)) {
wssv.WebSocketServices["/"].Sessions.Broadcast(msg_out);
}
send_sensors();
}
public void run() {
wssv.AddWebSocketService<Service>("/", () => new Service(this));
wssv.Start();
start_plugins();
while (Program.is_running) {
update();
process_messages();
Thread.Sleep(100);
}
wssv.Stop();
}
void on_exit(object sender, EventArgs e) {
hw.save();
}
public Server() {
Application.ApplicationExit += new EventHandler(this.on_exit);
fifo_in = new ConcurrentQueue<Message>();
fifo_out = new ConcurrentQueue<string>();
sensors = new Dictionary<string, dynamic>();
devices = new Dictionary<string, dynamic>();
hw = new Hardware(this);
hw.update_devices();
hw.update_sensors();
sensors_timer = Stopwatch.StartNew();
devices_timer = Stopwatch.StartNew();
wssv = new WebSocketServer(Program.settings.port);
}
}
}