2 * Server control via TCP socket
4 * Listens on localhost:8888 by default
7 var net
= require('net'),
8 kiwiModules
= require('../server/modules'),
9 ControlInterface
= require('../server/controlinterface.js'),
10 _
= require('lodash'),
11 winston
= require('winston');
13 var control_module
= new kiwiModules
.Module('Control');
19 function SocketClient (socket
) {
23 this.socket_closing
= false;
25 this.remoteAddress
= this.socket
.remoteAddress
;
26 winston
.info('Control connection from %s opened', this.socket
.remoteAddress
);
30 socket
.write("\nHello, you are connected to the Kiwi server :)\n\n");
32 this.control_interface
= new ControlInterface(socket
);
33 _
.each(socket_commands
, function(fn
, command_name
) {
34 that
.control_interface
.addCommand(command_name
, fn
.bind(that
));
38 SocketClient
.prototype.bindEvents = function() {
41 this.socket
.on('close', function() { that
.onClose
.apply(that
, arguments
); });
45 SocketClient
.prototype.unbindEvents = function() {
46 this.socket
.removeAllListeners();
50 SocketClient
.prototype.onClose = function() {
51 this.control_interface
.dispose();
52 this.control_interface
= null;
57 winston
.info('Control connection from %s closed', this.remoteAddress
);
64 * Each function is run in context of the SocketClient
66 var socket_commands
= {
67 quit: function(data
) {
68 this.socket
.destroy();
69 this.socket_closing
= true;
71 exit: function(data
) {
72 this.socket
.destroy();
73 this.socket_closing
= true;
79 * Start the control socket server to serve connections
81 var server
= net
.createServer(function (socket
) {
82 new SocketClient(socket
);
86 control_module
.on('dispose', function() {