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
*/\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
\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
if (!kiwiModules.unload(data[1])) {\r
- this.write('Module ' + (data[1] || '') + ' was not found');\r
+ this.write('Module ' + (data[1] || '') + ' is not loaded');\r
return;\r
}\r
\r
- kiwiModules.load(data[1]);\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
case 'list':\r
case 'ls':\r
default:\r
- this.write('Loaded modules: ' + Object.keys(kiwiModules.getRegisteredModules()).join(', '));\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
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
new SocketClient(socket);\r
});\r
server.listen(8888);\r
+\r
+control_module.on('dispose', function() {\r
+ server.close();\r
+});\r