* Listens on localhost:8888 by default\r
*/\r
\r
-var net = require('net'),\r
- kiwiModules = require('../server/modules'),\r
- rehash = require('../server/rehash.js'),\r
- config = require('../server/configuration.js'),\r
- _ = require('lodash');\r
+var net = require('net'),\r
+ kiwiModules = require('../server/modules'),\r
+ ControlInterface = require('../server/controlinterface.js'),\r
+ _ = require('lodash');\r
\r
var control_module = new kiwiModules.Module('Control');\r
\r
* The socket client\r
*/\r
function SocketClient (socket) {\r
+ var that = this;\r
+\r
this.socket = socket;\r
this.socket_closing = false;\r
\r
this.bindEvents();\r
\r
socket.write("\nHello, you are connected to the Kiwi server :)\n\n");\r
- this.displayPrompt();\r
+\r
+ this.control_interface = new ControlInterface(socket);\r
+ _.each(socket_commands, function(fn, command_name) {\r
+ that.control_interface.addCommand(command_name, fn.bind(that));\r
+ });\r
}\r
\r
SocketClient.prototype.bindEvents = function() {\r
var that = this;\r
\r
- this.socket.on('data', function() { that.onData.apply(that, arguments); });\r
this.socket.on('close', function() { that.onClose.apply(that, arguments); });\r
};\r
-SocketClient.prototype.unbindEvents = function() {\r
- this.socket.removeAllListeners();\r
-};\r
-\r
-\r
-\r
-SocketClient.prototype.write = function(data, append) {\r
- if (typeof append === 'undefined') append = '\n';\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
- this.write(prompt, '');\r
-};\r
-\r
-\r
-\r
-SocketClient.prototype.onData = function(data) {\r
- data = data.toString().trim();\r
-\r
-\r
\r
- try {\r
- var data_split = data.split(' ');\r
\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
-\r
- this.displayPrompt();\r
+SocketClient.prototype.unbindEvents = function() {\r
+ this.socket.removeAllListeners();\r
};\r
\r
\r
SocketClient.prototype.onClose = function() {\r
+ this.control_interface.dispose();\r
+ this.control_interface = null;\r
+\r
this.unbindEvents();\r
this.socket = null;\r
+\r
console.log('Control connection from ' + this.remoteAddress + ' closed');\r
};\r
\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