Skip to content

Commit

Permalink
[fix] abstract out the creating of the reactor and allow server.add t…
Browse files Browse the repository at this point in the history
…o add the reactor to the running set
  • Loading branch information
jcrugzz committed Dec 1, 2013
1 parent f6dd769 commit eed25a5
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions lib/godot/net/server.js
Expand Up @@ -73,6 +73,17 @@ Server.prototype.add = function (reactor) {
reactor.on('error', this.emit.bind(this, 'error'));
reactor.on('reactor:error', this.emit.bind(this, 'reactor:error'));
this.reactors[reactor.id] = reactor;
//
// Add reactor to the running set
// Remark: there will only be one host
//
var keys = Object.keys(this.hosts);
if (keys.length) {
for(var i=0; i<keys.length; i++) {
this.hosts[keys[i]].push(this.createReactor(reactor.id));
}
}

};

//
Expand Down Expand Up @@ -198,6 +209,20 @@ Server.prototype.close = function (callback) {
: this.server.close();
};

//
// ### function createReactor
// #### @id {UUID} Reactor id
// Returns a source and dest stream object that
// the socket ends up writing to
//
Server.prototype.createReactor = function (id) {
var source = new ReadWriteStream();
return {
dest: this.reactors[id].createStream(source),
source: source
};
};

//
// ### function createReactors (id)
// #### @id {string} `host:port` id for these reactors.
Expand All @@ -213,14 +238,7 @@ Server.prototype.createReactors = function (id) {
}

this.hosts[id] = Object.keys(this.reactors)
.reduce(function (all, key) {
var source = new ReadWriteStream();
all.push({
dest: self.reactors[key].createStream(source),
source: source
});
return all;
}, []);
.map(this.createReactor.bind(this));
}
};

Expand Down

0 comments on commit eed25a5

Please sign in to comment.