From 5a18896d550df70197a5b6c6600ec31482f7f699 Mon Sep 17 00:00:00 2001 From: Darren Date: Wed, 10 Jul 2013 21:36:23 +0100 Subject: [PATCH] Server module refactor; Control module refactor; --- server/kiwi.js | 2 +- server/modules.js | 21 +++++--- server_modules/control.js | 100 +++++++++++++++++++++++++++----------- 3 files changed, 86 insertions(+), 37 deletions(-) diff --git a/server/kiwi.js b/server/kiwi.js index 35a3482..75b3dde 100755 --- a/server/kiwi.js +++ b/server/kiwi.js @@ -67,7 +67,7 @@ modules.registerPublisher(global.modules); // Load any modules in the config if (global.config.module_dir) { (global.config.modules || []).forEach(function (module_name) { - if (modules.load(global.config.module_dir + module_name + '.js')) { + if (modules.load(module_name)) { console.log('Module ' + module_name + ' loaded successfuly'); } else { console.log('Module ' + module_name + ' failed to load'); diff --git a/server/modules.js b/server/modules.js index 0553974..cfba650 100644 --- a/server/modules.js +++ b/server/modules.js @@ -38,15 +38,15 @@ function registerPublisher (obj) { var registered_modules = {}; function loadModule (module_file) { - var module; + var module, + full_module_filename = global.config.module_dir + module_file; // Get an instance of the module and remove it from the cache try { - module = require(module_file); - delete require.cache[require.resolve(module_file)]; + module = require(full_module_filename); + delete require.cache[require.resolve(full_module_filename)]; } catch (err) { // Module was not found - console.log(err); return false; } @@ -56,12 +56,18 @@ function loadModule (module_file) { // Find a registered collection, .dispose() of it and remove it function unloadModule (module) { + var found_module = false; + registered_modules = _.reject(registered_modules, function (registered_module) { - if (module === registered_module) { - module.dispose(); + if (module.toLowerCase() === registered_module.module_name.toLowerCase()) { + found_module = true; + + registered_module.dispose(); return true; } }); + + return found_module; } @@ -75,6 +81,7 @@ function unloadModule (module) { */ function Module (module_name) { registered_modules[module_name] = this; + this.module_name = module_name; } @@ -126,7 +133,7 @@ Module.prototype.off = function (event_name, fn) { } } - active_publisher.removeListener(event_name, fn); + active_publisher.off(event_name, fn); }; diff --git a/server_modules/control.js b/server_modules/control.js index ac1a3e7..15c2bc4 100644 --- a/server_modules/control.js +++ b/server_modules/control.js @@ -13,6 +13,9 @@ var net = require('net'), var module = new kiwiModules.Module('Control'); +/** + * The socket client + */ function SocketClient (socket) { this.socket = socket; @@ -51,38 +54,17 @@ SocketClient.prototype.displayPrompt = function(prompt) { SocketClient.prototype.onData = function(data) { data = data.toString().trim(); - try { - switch (data) { - case 'modules': - this.write('Loaded modules: ' + Object.keys(kiwiModules.getRegisteredModules()).join(', ')); - break; - - case 'stats': - this.write('Connected clients: ' + _.size(global.clients.clients).toString()); - this.write('Num. remote hosts: ' + _.size(global.clients.addresses).toString()); - break; - case 'rehash': - rehash.rehashAll(); - this.write('Rehashed'); - break; - case 'reconfig': - if (config.loadConfig()) { - this.write('New config file loaded'); - } else { - this.write("No new config file was loaded"); - } - break; - - case 'exit': - case 'quit': - this.socket.destroy(); - break; + try { + var data_split = data.split(' '); - default: - this.write('Unrecognised command: ' + data); + if (typeof socket_commands[data_split[0]] === 'function') { + socket_commands[data_split[0]].call(this, data_split.slice(1)); + } else { + this.write('Unrecognised command: ' + data); } + } catch (err) { console.log('[Control error] ' + err); this.write('An error occured. Check the Kiwi server log for more details'); @@ -99,8 +81,68 @@ SocketClient.prototype.onClose = function() { +/** + * Available commands + * Each function is run in context of the SocketClient + */ +var socket_commands = { + module: function(data) { + switch(data[0]) { + case 'reload': + if (!data[1]) { + this.write('A module name must be specified'); + return; + } + + if (!kiwiModules.unload(data[1])) { + this.write('Module ' + (data[1] || '') + ' was not found'); + return; + } + + kiwiModules.load(data[1]); + this.write('Module ' + data[1] + ' reloaded'); + + break; + + case 'list': + case 'ls': + default: + this.write('Loaded modules: ' + Object.keys(kiwiModules.getRegisteredModules()).join(', ')); + } + + }, + + stats: function(data) { + this.write('Connected clients: ' + _.size(global.clients.clients).toString()); + this.write('Num. remote hosts: ' + _.size(global.clients.addresses).toString()); + }, + rehash: function(data) { + rehash.rehashAll(); + this.write('Rehashed'); + }, + + reconfig: function(data) { + if (config.loadConfig()) { + this.write('New config file loaded'); + } else { + this.write("No new config file was loaded"); + } + }, + + quit: function(data) { + this.socket.destroy(); + }, + exit: function(data) { + this.socket.destroy(); + } +}; + + +/** + * Start the control socket server to serve connections + */ var server = net.createServer(function (socket) { new SocketClient(socket); }); -server.listen(8888); \ No newline at end of file +server.listen(8888); -- 2.25.1