This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathapp.d
415 lines (379 loc) · 10.1 KB
/
app.d
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
module app;
import core.exception;
import core.sync.mutex;
import core.time;
import painlessjson;
import standardpaths;
import workspaced.api;
import workspaced.coms;
import std.algorithm;
import std.bitmanip;
import std.datetime.stopwatch : StopWatch;
import std.exception;
import std.functional;
import std.process;
import std.stdio : File, stderr;
import std.traits;
static import std.conv;
import std.json;
import std.meta;
import std.stdio;
import std.string;
__gshared File stdin, stdout;
shared static this()
{
stdin = std.stdio.stdin;
stdout = std.stdio.stdout;
version (Windows)
std.stdio.stdin = File("NUL", "r");
else version (Posix)
std.stdio.stdin = File("/dev/null", "r");
else
stderr.writeln("warning: no /dev/null implementation on this OS");
std.stdio.stdout = stderr;
}
__gshared Mutex writeMutex, commandMutex;
void sendResponse(int id, JSONValue message)
{
synchronized (writeMutex)
{
ubyte[] data = nativeToBigEndian(id) ~ (cast(ubyte[]) message.toString());
stdout.rawWrite(nativeToBigEndian(cast(int) data.length) ~ data);
stdout.flush();
}
}
void sendException(int id, Throwable t)
{
JSONValue[string] message;
message["error"] = JSONValue(true);
message["msg"] = JSONValue(t.msg);
message["exception"] = JSONValue(t.toString);
sendResponse(id, JSONValue(message));
}
void broadcast(WorkspaceD workspaced, WorkspaceD.Instance instance, JSONValue message)
{
sendResponse(0x7F000000, JSONValue([
"workspace": JSONValue(instance ? instance.cwd : null),
"data": message
]));
}
void bindFail(WorkspaceD.Instance instance, ComponentFactory component, Exception error)
{
sendResponse(0x7F000000, JSONValue([
"workspace": JSONValue(instance ? instance.cwd : null),
"data": JSONValue([
"component": JSONValue(component.info.name),
"type": JSONValue("bindfail"),
"msg": JSONValue(error.msg),
"trace": JSONValue(error.toString)
])
]));
}
WorkspaceD engine;
void handleRequest(int id, JSONValue request)
{
if (request.type != JSONType.object || "cmd" !in request
|| request["cmd"].type != JSONType.string)
{
goto printUsage;
}
else if (request["cmd"].str == "version")
{
version (unittest)
sendResponse(id, JSONValue(null));
else
{
import workspaced.info : getVersionInfoJson;
sendResponse(id, getVersionInfoJson);
}
}
else if (request["cmd"].str == "load")
{
if ("component" !in request || request["component"].type != JSONType.string)
{
sendException(id,
new Exception(
`Expected load message to be in format {"cmd":"load", "component":string, ("autoregister":bool)}`));
}
else
{
bool autoRegister = true;
if (auto v = "autoregister" in request)
autoRegister = v.type != JSONType.false_;
string[] allComponents;
static foreach (Component; AllComponents)
allComponents ~= getUDAs!(Component, ComponentInfoParams)[0].name;
ComponentSwitch:
switch (request["component"].str)
{
static foreach (Component; AllComponents)
{
case getUDAs!(Component, ComponentInfoParams)[0].name:
engine.register!Component(autoRegister);
break ComponentSwitch;
}
default:
sendException(id,
new Exception(
"Unknown Component '" ~ request["component"].str ~ "', built-in are " ~ allComponents.join(
", ")));
return;
}
sendResponse(id, JSONValue(true));
}
}
else if (request["cmd"].str == "new")
{
if ("cwd" !in request || request["cwd"].type != JSONType.string)
{
sendException(id,
new Exception(
`Expected new message to be in format {"cmd":"new", "cwd":string, ("config":object)}`));
}
else
{
string cwd = request["cwd"].str;
if ("config" in request)
engine.addInstance(cwd, Configuration(request["config"]));
else
engine.addInstance(cwd);
sendResponse(id, JSONValue(true));
}
}
else if (request["cmd"].str == "config-set")
{
if ("config" !in request || request["config"].type != JSONType.object)
{
configSetFail:
sendException(id,
new Exception(
`Expected new message to be in format {"cmd":"config-set", ("cwd":string), "config":object}`));
}
else
{
if ("cwd" in request)
{
if (request["cwd"].type != JSONType.string)
goto configSetFail;
else
engine.getInstance(request["cwd"].str).config.base = request["config"];
}
else
engine.globalConfiguration.base = request["config"];
sendResponse(id, JSONValue(true));
}
}
else if (request["cmd"].str == "config-get")
{
if ("cwd" in request)
{
if (request["cwd"].type != JSONType.string)
sendException(id,
new Exception(
`Expected new message to be in format {"cmd":"config-get", ("cwd":string)}`));
else
sendResponse(id, engine.getInstance(request["cwd"].str).config.base);
}
else
sendResponse(id, engine.globalConfiguration.base);
}
else if (request["cmd"].str == "call")
{
JSONValue[] params;
if ("params" in request)
{
if (request["params"].type != JSONType.array)
goto callFail;
params = request["params"].array;
}
if ("method" !in request || request["method"].type != JSONType.string
|| "component" !in request || request["component"].type != JSONType.string)
{
callFail:
sendException(id, new Exception(`Expected call message to be in format {"cmd":"call", "component":string, "method":string, ("cwd":string), ("params":object[])}`));
}
else
{
Future!JSONValue ret;
string component = request["component"].str;
string method = request["method"].str;
if ("cwd" in request)
{
if (request["cwd"].type != JSONType.string)
{
goto callFail;
}
else
{
string cwd = request["cwd"].str;
ret = engine.run(cwd, component, method, params);
}
}
else
ret = engine.run(component, method, params);
ret.onDone = {
if (ret.exception)
sendException(id, ret.exception);
else
sendResponse(id, ret.value);
};
}
}
else if (request["cmd"].str == "import-paths")
{
if ("cwd" !in request || request["cwd"].type != JSONType.string)
sendException(id,
new Exception(`Expected new message to be in format {"cmd":"import-paths", "cwd":string}`));
else
sendResponse(id, engine.getInstance(request["cwd"].str).importPaths.toJSON);
}
else if (request["cmd"].str == "import-files")
{
if ("cwd" !in request || request["cwd"].type != JSONType.string)
sendException(id,
new Exception(`Expected new message to be in format {"cmd":"import-files", "cwd":string}`));
else
sendResponse(id, engine.getInstance(request["cwd"].str).importFiles.toJSON);
}
else if (request["cmd"].str == "string-import-paths")
{
if ("cwd" !in request || request["cwd"].type != JSONType.string)
sendException(id,
new Exception(
`Expected new message to be in format {"cmd":"string-import-paths", "cwd":string}`));
else
sendResponse(id, engine.getInstance(request["cwd"].str).stringImportPaths.toJSON);
}
else
{
printUsage:
sendException(id, new Exception("Invalid request, must contain a cmd string key with one of the values [version, load, new, config-get, config-set, call, import-paths, import-files, string-import-paths]"));
}
}
void processException(int id, Throwable e)
{
stderr.writeln(e);
// dfmt off
sendResponse(id, JSONValue([
"error": JSONValue(true),
"msg": JSONValue(e.msg),
"exception": JSONValue(e.toString())
]));
// dfmt on
}
void processException(int id, JSONValue request, Throwable e)
{
stderr.writeln(e);
// dfmt off
sendResponse(id, JSONValue([
"error": JSONValue(true),
"msg": JSONValue(e.msg),
"exception": JSONValue(e.toString()),
"request": request
]));
// dfmt on
}
version (unittest)
{
}
else
{
int main(string[] args)
{
import workspaced.info;
import std.file;
import etc.linux.memoryerror;
version (DigitalMars)
static if (is(typeof(registerMemoryErrorHandler)))
registerMemoryErrorHandler();
if (args.length > 1 && (args[1] == "-v" || args[1] == "--version" || args[1] == "-version"))
{
stdout.writeln(getVersionInfoString);
return 0;
}
engine = new WorkspaceD();
engine.onBroadcast = (&broadcast).toDelegate;
engine.onBindFail = (&bindFail).toDelegate;
scope (exit)
engine.shutdown();
writeMutex = new Mutex;
commandMutex = new Mutex;
int length = 0;
int id = 0;
ubyte[4] intBuffer;
ubyte[] dataBuffer;
JSONValue data;
int gcCollects;
StopWatch gcInterval;
gcInterval.start();
scope (exit)
handleRequest(int.min, JSONValue(["cmd": "unload", "components": "*"]));
stderr.writeln("Config files stored in ", standardPaths(StandardPath.config, "workspace-d"));
while (stdin.isOpen && stdout.isOpen && !stdin.eof)
{
dataBuffer = stdin.rawRead(intBuffer);
if (dataBuffer.length == 0) break;
assert(dataBuffer.length == 4, "Unexpected buffer data");
length = bigEndianToNative!int(dataBuffer[0 .. 4]);
assert(length >= 4, "Invalid request");
dataBuffer = stdin.rawRead(intBuffer);
assert(dataBuffer.length == 4, "Unexpected buffer data");
id = bigEndianToNative!int(dataBuffer[0 .. 4]);
dataBuffer.length = length - 4;
dataBuffer = stdin.rawRead(dataBuffer);
try
{
data = parseJSON(cast(string) dataBuffer);
}
catch (Exception e)
{
processException(id, e);
}
catch (AssertError e)
{
processException(id, e);
}
try
{
handleRequest(id, data);
}
catch (Exception e)
{
processException(id, data, e);
}
catch (AssertError e)
{
processException(id, data, e);
}
stdout.flush();
if (gcInterval.peek >= 1.minutes)
{
import core.memory : GC;
auto before = GC.stats();
StopWatch gcSpeed;
gcSpeed.start();
GC.collect();
gcSpeed.stop();
auto after = GC.stats();
if (before != after)
stderr.writefln("GC run in %s. Freed %s bytes (%s bytes allocated, %s bytes available)", gcSpeed.peek,
cast(long) before.usedSize - cast(long) after.usedSize,
after.usedSize, after.freeSize);
else
stderr.writeln("GC run in ", gcSpeed.peek);
gcInterval.reset();
gcCollects++;
if (gcCollects > 5)
{
gcSpeed.reset();
gcSpeed.start();
GC.minimize();
gcSpeed.stop();
stderr.writeln("GC minimized in ", gcSpeed.peek);
gcCollects = 0;
}
}
}
return 0;
}
}