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');
19 function SocketClient (socket
) {
22 this.remoteAddress
= this.socket
.remoteAddress
;
23 console
.log('Control connection from ' + this.socket
.remoteAddress
+ ' opened');
27 socket
.write("\nHello, you are connected to the Kiwi server :)\n\n");
31 SocketClient
.prototype.bindEvents = function() {
34 this.socket
.on('data', function() { that
.onData
.apply(that
, arguments
); });
35 this.socket
.on('close', function() { that
.onClose
.apply(that
, arguments
); });
37 SocketClient
.prototype.unbindEvents = function() {
38 this.socket
.removeAllListeners();
43 SocketClient
.prototype.write = function(data
, append
) {
44 if (typeof append
=== 'undefined') append
= '\n';
45 this.socket
.write(data
+ append
);
47 SocketClient
.prototype.displayPrompt = function(prompt
) {
48 prompt
= prompt
|| 'Kiwi > ';
49 this.write(prompt
, '');
54 SocketClient
.prototype.onData = function(data
) {
55 data
= data
.toString().trim();
60 var data_split
= data
.split(' ');
62 if (typeof socket_commands
[data_split
[0]] === 'function') {
63 socket_commands
[data_split
[0]].call(this, data_split
.slice(1));
65 this.write('Unrecognised command: ' + data
);
69 console
.log('[Control error] ' + err
);
70 this.write('An error occured. Check the Kiwi server log for more details');
77 SocketClient
.prototype.onClose = function() {
79 console
.log('Control connection from ' + this.remoteAddress
+ ' closed');
86 * Each function is run in context of the SocketClient
88 var socket_commands
= {
89 module: function(data
) {
93 this.write('A module name must be specified');
97 if (!kiwiModules
.unload(data
[1])) {
98 this.write('Module ' + (data
[1] || '') + ' was not found');
102 kiwiModules
.load(data
[1]);
103 this.write('Module ' + data
[1] + ' reloaded');
110 this.write('Loaded modules: ' + Object
.keys(kiwiModules
.getRegisteredModules()).join(', '));
115 stats: function(data
) {
116 this.write('Connected clients: ' + _
.size(global
.clients
.clients
).toString());
117 this.write('Num. remote hosts: ' + _
.size(global
.clients
.addresses
).toString());
120 rehash: function(data
) {
122 this.write('Rehashed');
125 reconfig: function(data
) {
126 if (config
.loadConfig()) {
127 this.write('New config file loaded');
129 this.write("No new config file was loaded");
133 quit: function(data
) {
134 this.socket
.destroy();
136 exit: function(data
) {
137 this.socket
.destroy();
143 * Start the control socket server to serve connections
145 var server
= net
.createServer(function (socket
) {
146 new SocketClient(socket
);