Skip to content
Browse files

[api] emit carapace ids with events

  • Loading branch information...
1 parent 8205b30 commit fda60acf31d3716a8fb611265cc2f60140a4541b @bmeck bmeck committed Aug 2, 2011
Showing with 31 additions and 37 deletions.
  1. +8 −22 bin/carapace-client
  2. +6 −6 lib/carapace.js
  3. +16 −8 lib/net.js
  4. +1 −1 lib/plugins/heartbeat.js
View
30 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)
});
View
12 lib/carapace.js
@@ -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) {
@@ -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);
}
});
@@ -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);
}
//
@@ -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();
View
24 lib/net.js
@@ -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;
@@ -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]);
+ }
@indexzero
nodejitsu member
indexzero added a note Aug 3, 2011

@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.

@bmeck
bmeck added a note Aug 3, 2011
@jamesonjlee
jamesonjlee added a note Aug 3, 2011

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.

@indexzero
nodejitsu member
indexzero added a note Aug 3, 2011

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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]);
@@ -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.
@@ -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();
@@ -108,4 +116,4 @@ function getDummyFD () {
dummyFD = null;
}
}
-}
+}
View
2 lib/plugins/heartbeat.js
@@ -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);
}

0 comments on commit fda60ac

Please sign in to comment.
Something went wrong with that request. Please try again.