Skip to content

Commit

Permalink
[api] emit carapace ids with events
Browse files Browse the repository at this point in the history
  • Loading branch information
bmeck committed Aug 2, 2011
1 parent 8205b30 commit fda60ac
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
30 changes: 8 additions & 22 deletions bin/carapace-client
@@ -1,26 +1,12 @@
#!/usr/local/bin/node

var path = require('path');
var bridgePath = path.resolve(process.argv[2]);

console.log('connecting to ' + bridgePath)
require('dnode').connect(bridgePath, function (client, conn) {
console.log('connected');

carapace = client;
plugins = client.plugins;
emit = client.emit.bind(client);
on = client.on.bind(client);

console.log('');
console.log('REPL started');
console.log(' use carapace.*, plugins.*, on(..), and emit(..) to communicate with the carapace');
console.log('');
require('repl').start('carapace>');

conn.on('end', function () {
console.log('carapace connection had ended.');
process.exit(0);
});
var hookio = require('hook.io');
var Hook = hookio.Hook;

new Hook().connect({
name: "carapace-client",
"hook-port": process.argv[2]
}, function () {
console.dir(arguments)
});

12 changes: 6 additions & 6 deletions lib/carapace.js
Expand Up @@ -72,10 +72,10 @@ carapace.listen = function listen (options, callback) {

conn.on('close', function () {
evref.ref();
carapace.emit('carapace::connection::close');
carapace.emit('carapace::connection::close', carapace.id);
});

carapace.emit('carapace::connection::open', conn);
carapace.emit('carapace::connection::open', carapace.id, conn);
});

_listen.call(this, options, function (err) {
Expand Down Expand Up @@ -113,10 +113,10 @@ carapace.use = function (plugins, callback) {
// this requires absolute path or node_module
//
require(plugin)(carapace);
carapace.emit('carapace::plugin::loaded', plugin);
carapace.emit('carapace::plugin::loaded', carapace.id, plugin);
}
catch (ex) {
carapace.emit('carapace::plugin::error', plugin, ex);
carapace.emit('carapace::plugin::error', carapace.id, plugin, ex);
}
});

Expand Down Expand Up @@ -157,7 +157,7 @@ carapace.run = function (script, argv, override, callback) {
if (error) {
return callback
? callback(error)
: carapace.emit('error', error);
: carapace.emit('carapace::error', carapace.id, error);
}

//
Expand Down Expand Up @@ -190,7 +190,7 @@ carapace.run = function (script, argv, override, callback) {
carapace._module = process.mainModule;

carapace.running = true;
carapace.emit('carapace::running');
carapace.emit('carapace::running', carapace.id);

if (callback) {
callback();
Expand Down
24 changes: 16 additions & 8 deletions lib/net.js
Expand Up @@ -43,7 +43,10 @@ carapace.servers = {}
// done(carapace.proxy.ports);
// });

net.Server.prototype._doListen = function() {
//
// Separate since net.Server uses a cached bind function
//
net.Server.prototype._doListen = function () {
var self = this,
desired = toPort(arguments[0]),
actual;
Expand All @@ -52,13 +55,18 @@ net.Server.prototype._doListen = function() {
getDummyFD();

try {
//
// Don't allow listening on the hook-port, hook may be restarting
//
if(desired === carapace['hook-port']) {
return this._doListen.call(this, nextPort(desired), arguments[1]);
}

This comment has been minimized.

Copy link
@indexzero

indexzero Aug 3, 2011

Member

@bmeck This is wrong. We should allow listening on hook-port, but we should not auto-increment the port on error. This is used by hook.io in .start() to determine if it can start a hook server or not. With this here, the carapace will always be a hook server, and not a hook-client.

This comment has been minimized.

Copy link
@bmeck

bmeck via email Aug 3, 2011

Author Contributor

This comment has been minimized.

Copy link
@jamesonjlee

jamesonjlee Aug 3, 2011

Contributor

when carapace.listen(port) is called, carapace.hook-port is the same as desired, so this will always bump the port up.

since carapace <-> plugins/other carapie(?) does require the hook-server to come up.

This comment has been minimized.

Copy link
@indexzero

indexzero Aug 3, 2011

Member

In short-term, this is a convenience for tests, but in the long term I could see a carapace spawning another carpace. It's a somewhat unorthodox use-case I agree, but it's too early to be limiting our options imho.

binding.bind(self.fd, arguments[0], arguments[1]);
}
catch (err) {
if (err.code !== 'EADDRINUSE' || desired === carapace['hook-port']) {
catch (err) {
if (err.code !== 'EADDRINUSE') {
self.close();
self.emit('error', err);
return;
return self.emit('error', err);
}

return this._doListen.call(this, nextPort(desired), arguments[1]);
Expand All @@ -73,7 +81,7 @@ net.Server.prototype._doListen = function() {
// Need to the listening in the nextTick so that people potentially have
// time to register 'listening' listeners.
//
process.nextTick(function() {
return process.nextTick(function() {
//
// It could be that server.close() was called between the time the
// original listen command was issued and this. Bail if that's the case.
Expand All @@ -88,7 +96,7 @@ net.Server.prototype._doListen = function() {
// Remark: How does this work with multi-server programs?
//
carapace.servers[desired] = self;
carapace.emit('*::port', desired);
carapace.emit('*::carapace::port', carapace.id, desired, actual);
}
catch (err) {
self.close();
Expand All @@ -108,4 +116,4 @@ function getDummyFD () {
dummyFD = null;
}
}
}
}
2 changes: 1 addition & 1 deletion lib/plugins/heartbeat.js
Expand Up @@ -24,7 +24,7 @@ module.exports = function (carapace) {
// start the timer
//
this.interval = setInterval(function () {
carapace.emit('carapace::heartbeat');
carapace.emit('carapace::heartbeat', carapace.id);
return done ? done() : null;
}, interval);
}
Expand Down

0 comments on commit fda60ac

Please sign in to comment.