Server module refactor; Control module refactor;
authorDarren <darren@darrenwhitlen.com>
Wed, 10 Jul 2013 20:36:23 +0000 (21:36 +0100)
committerDarren <darren@darrenwhitlen.com>
Wed, 10 Jul 2013 20:36:23 +0000 (21:36 +0100)
server/kiwi.js
server/modules.js
server_modules/control.js

index 35a3482c7cd02a3c07050dbabe695594175c7b55..75b3dde24e515ac89801791274c55895e421b527 100755 (executable)
@@ -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');
index 0553974931a3d2e7ec6a55595e39bc93cb0bbc48..cfba650f2ad412ca1799e0be3b187d162792d43e 100644 (file)
@@ -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);
 };
 
 
index ac1a3e79b469aeb83eb0cb02d29b633bf911dd1f..15c2bc471e17e066c8f927ac2b0bc546464bb608 100644 (file)
@@ -13,6 +13,9 @@ var net         = require('net'),
 var module = new kiwiModules.Module('Control');\r
 \r
 \r
+/**\r
+ * The socket client\r
+ */\r
 function SocketClient (socket) {\r
     this.socket = socket;\r
 \r
@@ -51,38 +54,17 @@ SocketClient.prototype.displayPrompt = function(prompt) {
 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
@@ -99,8 +81,68 @@ SocketClient.prototype.onClose = function() {
 \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] || '') + ' was not found');\r
+                    return;\r
+                }\r
+\r
+                kiwiModules.load(data[1]);\r
+                this.write('Module ' + data[1] + ' reloaded');\r
+\r
+                break;\r
+\r
+            case 'list':\r
+            case 'ls':\r
+            default:\r
+                this.write('Loaded modules: ' + Object.keys(kiwiModules.getRegisteredModules()).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
+    },\r
+    exit: function(data) {\r
+        this.socket.destroy();\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