Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 4 commits
  • 9 files changed
  • 0 commit comments
  • 1 contributor
Showing with 86 additions and 65 deletions.
  1. +0 −14 hub/Event.js
  2. +0 −15 hub/EventManager.js
  3. +8 −23 hub/Hub.js
  4. +29 −1 hub/HubConfig.js
  5. +14 −0 hub/ItemManager.js
  6. +17 −0 hub/Petal.js
  7. +3 −3 hub/Server.js
  8. +4 −4 {hub → petal}/irc/Connection.js
  9. +11 −5 {hub → petal}/irc/Manager.js
View
14 hub/Event.js
@@ -1,14 +0,0 @@
-var Event = module.exports = function (item, type, data) {
- this.item = item;
- this.type = type;
-
- for (var key in data) {
- if (data.hasOwnProperty (key)) {
- this[key] = data[key];
- }
- }
-};
-
-Event.prototype.stringify = function () {
- return JSON.stringify (this);
-};
View
15 hub/EventManager.js
@@ -1,15 +0,0 @@
-
-/* EventManager:
- * This object is the main router for all of Diaptoval's events.
- * Managers like the IRC manager will register all of its events
- * with an EventManager instance. This is the main starting point
- * for all of the events.
- */
-var EventManager = module.exports = function () {
-};
-
-EventManager.prototype.send = function(event) {
- console.log (event.stringify ());
-};
-
-//EventManager
View
31 hub/Hub.js
@@ -4,14 +4,13 @@ var net = require ("net");
var util = require ("util");
var HubConfig = require ("./HubConfig.js");
-var IRCManager = require ("./irc/Manager.js");
var Server = require ("./Server.js");
-var EventManager = require ("./EventManager.js");
+var ItemManager = require ("./ItemManager.js");
var Hub = function () {
- this.managers = [];
- this.event_manager = new EventManager();
+ this.petals = [];
+ this.item_manager = new ItemManager();
};
/**
@@ -25,7 +24,6 @@ Hub.prototype.init = function () {
this.load_config (function (config) {
this.config = config;
this.start_server ();
- this.start_chat ();
});
};
@@ -50,7 +48,7 @@ Hub.prototype.load_config = function (callback) {
Hub.prototype.start_server = function () {
console.log ("Starting up hub server");
- this.server = new Server (this.config);
+ this.server = new Server (this);
this.server.listen ();
};
@@ -61,10 +59,10 @@ Hub.prototype.shutdown = function () {
// Close petal connections
this.server.close ();
- // Tell each manager to shut down
- var num = this.managers.length;
- this.managers.forEach (function (each) {
- each.disconnect (function() {
+ // Tell each petal to shut down
+ var num = this.petal.length;
+ this.petal.forEach (function (each) {
+ each.shutdown (function() {
num--;
if (!num) {
exit();
@@ -78,17 +76,4 @@ Hub.prototype.shutdown = function () {
}
};
-/**
- * Hub.start_chat():
- * This opens up the IRC connections from the config
- **/
-Hub.prototype.start_chat = function () {
- var irc;
-
- irc = new IRCManager (this.config, this.event_manager);
- irc.connect ();
-
- this.managers.push (irc);
-};
-
module.exports = Hub;
View
30 hub/HubConfig.js
@@ -2,9 +2,20 @@
var fs = require ("fs");
var path = require ("path");
+var util = require ("util");
+
+var Petal = require ("./Petal.js");
var FileUtils = require ("./FileUtils.js");
-var HubConfig = function (locations) {
+/**
+ * HubConfig:
+ * This is actually an internal petal which creates
+ * a special lic/config item. This item can be used by all
+ * petals to access, update, and modify global configuration.
+ **/
+
+var HubConfig = function () {
+ Petal.call (this);
this.path = null;
@@ -20,6 +31,8 @@ var HubConfig = function (locations) {
this.data.IRC.servers = [];
};
+util.inherits (HubConfig, Petal);
+
HubConfig.prototype.location = path.join (FileUtils.home (), ".lic", "config.json");
@@ -128,6 +141,8 @@ HubConfig.prototype.write_file = function(file, callback) {
data = JSON.stringify (this.data, null, 4);
+ console.log ("Saving configuration: %s", file);
+
fs.writeFile (file, data, 'utf8', function(error) {
callback.call (self, error);
});
@@ -154,4 +169,17 @@ HubConfig.prototype.load_config_data = function (data) {
};
+/**
+ * HubConfig#shutdown:
+ * This command is called when the hub begins to shutdown.
+ * It writes changes in the configuration to disk and calls the callback.
+ **/
+HubConfig.prototype.shutdown = function(callback) {
+ this.write_file (this.location, function(error) {
+ if (callback) {
+ callback.call (this, error);
+ }
+ });
+};
+
module.exports = HubConfig;
View
14 hub/ItemManager.js
@@ -0,0 +1,14 @@
+var util = require ("util");
+
+/* ItemManager:
+ * This object is the main router for all of lic's events.
+ * Petals like the IRC manager will register all of its events
+ * with the ItemManager instance belonging to the hub.
+ * This is the main starting point for all of the events.
+ */
+var ItemManager = module.exports = function () {
+};
+
+ItemManager.prototype.send = function(event) {
+ util.puts (util.inspect (event, false, 2, true));
+};
View
17 hub/Petal.js
@@ -0,0 +1,17 @@
+var Petal = function(item_manager) {
+ this.item_manager = item_manager;
+};
+
+Petal.prototype.item_manager = null;
+
+/** Petal#shutdown:
+ * This function is called by the hub when it is requested
+ * to shutdown. The hub will terminate once the callback is called.
+ *
+ * Default behavior is to immediately call callback.
+ **/
+Petal.prototype.shutdown = function (callback) {
+ if (callback) {
+ callback.call (this);
+ }
+};
View
6 hub/Server.js
@@ -2,8 +2,8 @@
var net = require ("net");
-var Server = function (config) {
- this.config = config;
+var Server = function (hub) {
+ this.hub = hub;
this.connections = [];
};
@@ -19,7 +19,7 @@ Server.prototype.listen = function () {
this.server.on ("connection", function(socket) { self.connection (socket); });
this.server.on ("listening", function() { self.listening (); });
- this.server.listen (this.config.data.Core.socket);
+ //this.server.listen (this.config.data.Core.socket);
};
/**
View
8 hub/irc/Connection.js → petal/irc/Connection.js
@@ -2,7 +2,7 @@ var util = require ("util");
var net = require ("net");
var tls = require ("tls");
-var Event = require ("../Event.js");
+var Message = require ("../../hub/Message.js");
/*
* A special connection for handling the IRC protocol.
@@ -15,10 +15,10 @@ var Event = require ("../Event.js");
* consult the README.
*/
-var IRCConnection = function (profile, event_manager) {
+var IRCConnection = function (profile, item_manager) {
this.profile = profile;
- this.event_manager = event_manager;
+ this.item_manager = item_manager;
/* profile.host: The hostname of the IRC server to connect to */
this.host = profile.host || "localhost";
@@ -92,7 +92,7 @@ var IRCConnection = function (profile, event_manager) {
data = this.parse_message (message);
if (data) {
this.emit (data.command, data);
- this.event_manager.send (new Event(this.get_item_name (data), data.command, data));
+ this.item_manager.send (new Message(this.get_item_name (data), data.command, data));
}
}).bind (this));
View
16 hub/irc/Manager.js → petal/irc/Manager.js
@@ -1,11 +1,17 @@
+var util = require ("util");
+
+var Petal = require ("../../hub/Petal.js");
var Connection = require ("./Connection.js");
-var IRCManager = function (config, event_manager) {
- this.config = config;
- this.event_manager = event_manager;
+var IRCManager = function (item_manager) {
+ Petal.call (this, item_manager);
+
this.connections = [];
+ this.connect ();
};
+util.inherits (IRCManager, Petal);
+
/* This function takes the server profile from the config and extends
* it with the defaults from the config so that when the defaults
* change or the server profile changes, it will updated as such
@@ -24,7 +30,7 @@ IRCManager.prototype.connect = function () {
for (var i = 0, len = servers.length; i < len; i++) {
profile = this.create_profile (servers[i], defaults);
- connection = new Connection (profile, this.event_manager);
+ connection = new Connection (profile, this.item_manager);
///* The following section is a massive hack, used temporarily as a testing interface.
connection.on("001", function (message) {
@@ -50,7 +56,7 @@ IRCManager.prototype.connect = function () {
}
};
-IRCManager.prototype.disconnect = function(callback) {
+IRCManager.prototype.shutdown = function(callback) {
var connections, self = this;
// First we clone the connections array to use as a queue

No commit comments for this range

Something went wrong with that request. Please try again.