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');
12 var control_module
= new kiwiModules
.Module('Control');
18 function SocketClient (socket
) {
22 this.socket_closing
= false;
24 this.remoteAddress
= this.socket
.remoteAddress
;
25 console
.log('Control connection from ' + this.socket
.remoteAddress
+ ' opened');
29 socket
.write("\nHello, you are connected to the Kiwi server :)\n\n");
31 this.control_interface
= new ControlInterface(socket
);
32 _
.each(socket_commands
, function(fn
, command_name
) {
33 that
.control_interface
.addCommand(command_name
, fn
.bind(that
));
37 SocketClient
.prototype.bindEvents = function() {
40 this.socket
.on('close', function() { that
.onClose
.apply(that
, arguments
); });
44 SocketClient
.prototype.unbindEvents = function() {
45 this.socket
.removeAllListeners();
49 SocketClient
.prototype.onClose = function() {
50 this.control_interface
.dispose();
51 this.control_interface
= null;
56 console
.log('Control connection from ' + this.remoteAddress
+ ' closed');
63 * Each function is run in context of the SocketClient
65 var socket_commands
= {
66 quit: function(data
) {
67 this.socket
.destroy();
68 this.socket_closing
= true;
70 exit: function(data
) {
71 this.socket
.destroy();
72 this.socket_closing
= true;
78 * Start the control socket server to serve connections
80 var server
= net
.createServer(function (socket
) {
81 new SocketClient(socket
);
85 control_module
.on('dispose', function() {