-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·70 lines (56 loc) · 1.97 KB
/
server.js
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
#!/usr/bin/gjs --include-path=.
'use strict';
imports.searchPath.unshift('.');
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Utils = imports.utils;
// globals
let loop = new GLib.MainLoop(null, false);
let service = new Gio.SocketService();
let socket_address = Utils.new_socket_address();
// add a port to the service
service.add_address(socket_address, Gio.SocketType.STREAM, Gio.SocketProtocol.DEFAULT, null);
// add a handler to listen for incoming requests
service.connect('incoming', function(socket_service, connection, channel) {
// get the input and output streams
let input = connection.get_input_stream();
let output = connection.get_output_stream();
// read the first byte of the request and convert it to a string
let request = String.fromCharCode.apply(null, input.read_bytes(Utils.NUM_BYTES, null).get_data());
// this is the string that the service will respond with
let response = '1'; // exit code 1 - error
// handle the request
switch (request) {
// kill the server, respond with exit code 0
case '9':
response = '0';
loop.quit();
break;
// known requests, handle them and respond with exit code 0
case '1':
case '2':
response = '0';
break;
// unknown request, leave exit code as 1
default:
break;
}
print('Received request: '+request+', responding with '+response);
// send back the response on the output stream
output.write_bytes(new GLib.Bytes(response), null);
// close the connection
connection.close(null);
});
// start the service
service.start();
if (Utils.EXAMPLE_INET) {
print('Accepting connections on TCP port '+Utils.INET_PORT);
} else {
print('Accepting connections on Unix socket '+Utils.UNIX_SOCK);
}
// run the main loop, waiting for connections
loop.run();
// close the service
service.close();
// clean up the socket, if it exists
Utils.close_socket();