2 * Server control via TCP socket
4 * Listens on localhost:8888 by default
7 var net
= require('net'),
8 kiwiModules
= require('../server/modules'),
9 rehash
= require('../server/rehash.js'),
10 config
= require('../server/configuration.js'),
11 _
= require('lodash');
13 var module
= new kiwiModules
.Module('Control');
16 function SocketClient (socket
) {
19 this.remoteAddress
= this.socket
.remoteAddress
;
20 console
.log('Control connection from ' + this.socket
.remoteAddress
+ ' opened');
24 socket
.write("\nHello, you are connected to the Kiwi server :)\n\n");
28 SocketClient
.prototype.bindEvents = function() {
31 this.socket
.on('data', function() { that
.onData
.apply(that
, arguments
); });
32 this.socket
.on('close', function() { that
.onClose
.apply(that
, arguments
); });
34 SocketClient
.prototype.unbindEvents = function() {
35 this.socket
.removeAllListeners();
40 SocketClient
.prototype.write = function(data
, append
) {
41 if (typeof append
=== 'undefined') append
= '\n';
42 this.socket
.write(data
+ append
);
44 SocketClient
.prototype.displayPrompt = function(prompt
) {
45 prompt
= prompt
|| 'Kiwi > ';
46 this.write(prompt
, '');
51 SocketClient
.prototype.onData = function(data
) {
52 data
= data
.toString().trim();
57 this.write('Loaded modules: ' + Object
.keys(kiwiModules
.getRegisteredModules()).join(', '));
61 this.write('Connected clients: ' + _
.size(global
.clients
.clients
).toString());
62 this.write('Num. remote hosts: ' + _
.size(global
.clients
.addresses
).toString());
67 this.write('Rehashed');
71 if (config
.loadConfig()) {
72 this.write('New config file loaded');
74 this.write("No new config file was loaded");
80 this.socket
.destroy();
84 this.write('Unrecognised command: ' + data
);
87 console
.log('[Control error] ' + err
);
88 this.write('An error occured. Check the Kiwi server log for more details');
95 SocketClient
.prototype.onClose = function() {
97 console
.log('Control connection from ' + this.remoteAddress
+ ' closed');
103 var server
= net
.createServer(function (socket
) {
104 new SocketClient(socket
);