modules.registerPublisher(global.modules);
// Load any modules in the config
-(global.config.modules || []).forEach(function (module_name) {
- if (modules.load('../server_modules/' + module_name + '.js')) {
- console.log('Module ' + module_name + ' loaded successfuly');
- } else {
- console.log('Module ' + module_name + ' failed to load');
- }
-});
-
+if (global.config.module_dir) {
+ (global.config.modules || []).forEach(function (module_name) {
+ if (modules.load(global.config.module_dir + module_name + '.js')) {
+ console.log('Module ' + module_name + ' loaded successfuly');
+ } else {
+ console.log('Module ' + module_name + ' failed to load');
+ }
+ });
+}
--- /dev/null
+/**\r
+ * Server control via TCP socket\r
+ *\r
+ * 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
+\r
+var module = new kiwiModules.Module('Control');\r
+\r
+\r
+function SocketClient (socket) {\r
+ this.socket = socket;\r
+\r
+ this.remoteAddress = this.socket.remoteAddress;\r
+ console.log('Control connection from ' + this.socket.remoteAddress + ' opened');\r
+\r
+ this.bindEvents();\r
+\r
+ socket.write("\nHello, you are connected to the Kiwi server :)\n\n");\r
+ this.displayPrompt();\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
+ 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
+ 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
+\r
+ default:\r
+ this.write('Unrecognised command: ' + data);\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
+};\r
+\r
+\r
+SocketClient.prototype.onClose = function() {\r
+ this.unbindEvents();\r
+ console.log('Control connection from ' + this.remoteAddress + ' closed');\r
+};\r
+\r
+\r
+\r
+\r
+var server = net.createServer(function (socket) {\r
+ new SocketClient(socket);\r
+});\r
+server.listen(8888);
\ No newline at end of file