config = require('../server/configuration.js'),\r
_ = require('lodash');\r
\r
-var module = new kiwiModules.Module('Control');\r
+var control_module = new kiwiModules.Module('Control');\r
\r
\r
+/**\r
+ * The socket client\r
+ */\r
function SocketClient (socket) {\r
this.socket = socket;\r
+ this.socket_closing = false;\r
\r
this.remoteAddress = this.socket.remoteAddress;\r
console.log('Control connection from ' + this.socket.remoteAddress + ' opened');\r
\r
SocketClient.prototype.write = function(data, append) {\r
if (typeof append === 'undefined') append = '\n';\r
- this.socket.write(data + append);\r
+ if (this.socket && !this.socket_closing)\r
+ this.socket.write(data + append);\r
};\r
SocketClient.prototype.displayPrompt = function(prompt) {\r
prompt = prompt || 'Kiwi > ';\r
SocketClient.prototype.onData = function(data) {\r
data = data.toString().trim();\r
\r
- try {\r
- switch (data) {\r
- case 'modules':\r
- this.write('Loaded modules: ' + Object.keys(kiwiModules.getRegisteredModules()).join(', '));\r
- break;\r
-\r
- case 'stats':\r
- this.write('Connected clients: ' + _.size(global.clients.clients).toString());\r
- this.write('Num. remote hosts: ' + _.size(global.clients.addresses).toString());\r
- break;\r
-\r
- case 'rehash':\r
- rehash.rehashAll();\r
- this.write('Rehashed');\r
- break;\r
\r
- case 'reconfig':\r
- if (config.loadConfig()) {\r
- this.write('New config file loaded');\r
- } else {\r
- this.write("No new config file was loaded");\r
- }\r
- break;\r
\r
- case 'exit':\r
- case 'quit':\r
- this.socket.destroy();\r
- break;\r
+ try {\r
+ var data_split = data.split(' ');\r
\r
- default:\r
- this.write('Unrecognised command: ' + data);\r
+ if (typeof socket_commands[data_split[0]] === 'function') {\r
+ socket_commands[data_split[0]].call(this, data_split.slice(1));\r
+ } else {\r
+ this.write('Unrecognised command: ' + data);\r
}\r
+\r
} catch (err) {\r
console.log('[Control error] ' + err);\r
this.write('An error occured. Check the Kiwi server log for more details');\r
\r
SocketClient.prototype.onClose = function() {\r
this.unbindEvents();\r
+ this.socket = null;\r
console.log('Control connection from ' + this.remoteAddress + ' closed');\r
};\r
\r
\r
\r
+/**\r
+ * Available commands\r
+ * Each function is run in context of the SocketClient\r
+ */\r
+var socket_commands = {\r
+ module: function(data) {\r
+ switch(data[0]) {\r
+ case 'reload':\r
+ if (!data[1]) {\r
+ this.write('A module name must be specified');\r
+ return;\r
+ }\r
+\r
+ if (!kiwiModules.unload(data[1])) {\r
+ this.write('Module ' + (data[1] || '') + ' is not loaded');\r
+ return;\r
+ }\r
+\r
+ if (!kiwiModules.load(data[1])) {\r
+ this.write('Error loading module ' + (data[1] || ''));\r
+ }\r
+ this.write('Module ' + data[1] + ' reloaded');\r
+\r
+ break;\r
+\r
+ case 'list':\r
+ case 'ls':\r
+ default:\r
+ var module_names = [];\r
+ kiwiModules.getRegisteredModules().forEach(function(module) {\r
+ module_names.push(module.module_name);\r
+ });\r
+ this.write('Loaded modules: ' + module_names.join(', '));\r
+ }\r
+\r
+ },\r
+\r
+ stats: function(data) {\r
+ this.write('Connected clients: ' + _.size(global.clients.clients).toString());\r
+ this.write('Num. remote hosts: ' + _.size(global.clients.addresses).toString());\r
+ },\r
+\r
+ rehash: function(data) {\r
+ rehash.rehashAll();\r
+ this.write('Rehashed');\r
+ },\r
\r
+ reconfig: function(data) {\r
+ if (config.loadConfig()) {\r
+ this.write('New config file loaded');\r
+ } else {\r
+ this.write("No new config file was loaded");\r
+ }\r
+ },\r
+\r
+ quit: function(data) {\r
+ this.socket.destroy();\r
+ this.socket_closing = true;\r
+ },\r
+ exit: function(data) {\r
+ this.socket.destroy();\r
+ this.socket_closing = true;\r
+ }\r
+};\r
+\r
+\r
+/**\r
+ * Start the control socket server to serve connections\r
+ */\r
var server = net.createServer(function (socket) {\r
new SocketClient(socket);\r
});\r
-server.listen(8888);
\ No newline at end of file
+server.listen(8888);\r
+\r
+control_module.on('dispose', function() {\r
+ server.close();\r
+});\r