Commit | Line | Data |
---|---|---|
d8002ae0 D |
1 | /**\r |
2 | * Server control via TCP socket\r | |
3 | *\r | |
4 | * Listens on localhost:8888 by default\r | |
5 | */\r | |
6 | \r | |
4eba7612 D |
7 | var net = require('net'),\r |
8 | kiwiModules = require('../server/modules'),\r | |
9 | ControlInterface = require('../server/controlinterface.js'),\r | |
94b00b2d JA |
10 | _ = require('lodash'),\r |
11 | winston = require('winston');\r | |
d8002ae0 | 12 | \r |
ab3043ad | 13 | var control_module = new kiwiModules.Module('Control');\r |
d8002ae0 D |
14 | \r |
15 | \r | |
5a18896d D |
16 | /**\r |
17 | * The socket client\r | |
18 | */\r | |
d8002ae0 | 19 | function SocketClient (socket) {\r |
4eba7612 D |
20 | var that = this;\r |
21 | \r | |
d8002ae0 | 22 | this.socket = socket;\r |
ab3043ad | 23 | this.socket_closing = false;\r |
d8002ae0 D |
24 | \r |
25 | this.remoteAddress = this.socket.remoteAddress;\r | |
94b00b2d | 26 | winston.info('Control connection from %s opened', this.socket.remoteAddress);\r |
d8002ae0 D |
27 | \r |
28 | this.bindEvents();\r | |
29 | \r | |
30 | socket.write("\nHello, you are connected to the Kiwi server :)\n\n");\r | |
4eba7612 D |
31 | \r |
32 | this.control_interface = new ControlInterface(socket);\r | |
33 | _.each(socket_commands, function(fn, command_name) {\r | |
4eba7612 D |
34 | that.control_interface.addCommand(command_name, fn.bind(that));\r |
35 | });\r | |
d8002ae0 D |
36 | }\r |
37 | \r | |
38 | SocketClient.prototype.bindEvents = function() {\r | |
39 | var that = this;\r | |
40 | \r | |
d8002ae0 D |
41 | this.socket.on('close', function() { that.onClose.apply(that, arguments); });\r |
42 | };\r | |
d8002ae0 | 43 | \r |
d8002ae0 | 44 | \r |
4eba7612 D |
45 | SocketClient.prototype.unbindEvents = function() {\r |
46 | this.socket.removeAllListeners();\r | |
d8002ae0 D |
47 | };\r |
48 | \r | |
49 | \r | |
50 | SocketClient.prototype.onClose = function() {\r | |
4eba7612 D |
51 | this.control_interface.dispose();\r |
52 | this.control_interface = null;\r | |
53 | \r | |
d8002ae0 | 54 | this.unbindEvents();\r |
ab3043ad | 55 | this.socket = null;\r |
4eba7612 | 56 | \r |
94b00b2d | 57 | winston.info('Control connection from %s closed', this.remoteAddress);\r |
d8002ae0 D |
58 | };\r |
59 | \r | |
60 | \r | |
61 | \r | |
5a18896d D |
62 | /**\r |
63 | * Available commands\r | |
64 | * Each function is run in context of the SocketClient\r | |
65 | */\r | |
66 | var socket_commands = {\r | |
5a18896d D |
67 | quit: function(data) {\r |
68 | this.socket.destroy();\r | |
ab3043ad | 69 | this.socket_closing = true;\r |
5a18896d D |
70 | },\r |
71 | exit: function(data) {\r | |
72 | this.socket.destroy();\r | |
ab3043ad | 73 | this.socket_closing = true;\r |
5a18896d D |
74 | }\r |
75 | };\r | |
76 | \r | |
77 | \r | |
78 | /**\r | |
79 | * Start the control socket server to serve connections\r | |
80 | */\r | |
d8002ae0 D |
81 | var server = net.createServer(function (socket) {\r |
82 | new SocketClient(socket);\r | |
83 | });\r | |
5a18896d | 84 | server.listen(8888);\r |
ab3043ad D |
85 | \r |
86 | control_module.on('dispose', function() {\r | |
87 | server.close();\r | |
88 | });\r |