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 control_module
= new kiwiModules
.Module('Control');
19 function SocketClient (socket
) {
21 this.socket_closing
= false;
23 this.remoteAddress
= this.socket
.remoteAddress
;
24 console
.log('Control connection from ' + this.socket
.remoteAddress
+ ' opened');
28 socket
.write("\nHello, you are connected to the Kiwi server :)\n\n");
32 SocketClient
.prototype.bindEvents = function() {
35 this.socket
.on('data', function() { that
.onData
.apply(that
, arguments
); });
36 this.socket
.on('close', function() { that
.onClose
.apply(that
, arguments
); });
38 SocketClient
.prototype.unbindEvents = function() {
39 this.socket
.removeAllListeners();
44 SocketClient
.prototype.write = function(data
, append
) {
45 if (typeof append
=== 'undefined') append
= '\n';
46 if (this.socket
&& !this.socket_closing
)
47 this.socket
.write(data
+ append
);
49 SocketClient
.prototype.displayPrompt = function(prompt
) {
50 prompt
= prompt
|| 'Kiwi > ';
51 this.write(prompt
, '');
56 SocketClient
.prototype.onData = function(data
) {
57 data
= data
.toString().trim();
62 var data_split
= data
.split(' ');
64 if (typeof socket_commands
[data_split
[0]] === 'function') {
65 socket_commands
[data_split
[0]].call(this, data_split
.slice(1));
67 this.write('Unrecognised command: ' + data
);
71 console
.log('[Control error] ' + err
);
72 this.write('An error occured. Check the Kiwi server log for more details');
79 SocketClient
.prototype.onClose = function() {
82 console
.log('Control connection from ' + this.remoteAddress
+ ' closed');
89 * Each function is run in context of the SocketClient
91 var socket_commands
= {
92 module: function(data
) {
96 this.write('A module name must be specified');
100 if (!kiwiModules
.unload(data
[1])) {
101 this.write('Module ' + (data
[1] || '') + ' is not loaded');
105 if (!kiwiModules
.load(data
[1])) {
106 this.write('Error loading module ' + (data
[1] || ''));
108 this.write('Module ' + data
[1] + ' reloaded');
115 var module_names
= [];
116 kiwiModules
.getRegisteredModules().forEach(function(module
) {
117 module_names
.push(module
.module_name
);
119 this.write('Loaded modules: ' + module_names
.join(', '));
124 stats: function(data
) {
125 this.write('Connected clients: ' + _
.size(global
.clients
.clients
).toString());
126 this.write('Num. remote hosts: ' + _
.size(global
.clients
.addresses
).toString());
129 rehash: function(data
) {
131 this.write('Rehashed');
134 reconfig: function(data
) {
135 if (config
.loadConfig()) {
136 this.write('New config file loaded');
138 this.write("No new config file was loaded");
142 quit: function(data
) {
143 this.socket
.destroy();
144 this.socket_closing
= true;
146 exit: function(data
) {
147 this.socket
.destroy();
148 this.socket_closing
= true;
154 * Start the control socket server to serve connections
156 var server
= net
.createServer(function (socket
) {
157 new SocketClient(socket
);
161 control_module
.on('dispose', function() {