Created
February 8, 2011 19:12
-
-
Save polaris/817000 to your computer and use it in GitHub Desktop.
nodekeeper.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <v8.h> | |
#include <node.h> | |
#include <node_events.h> | |
#include <c-client-src/zookeeper.h> | |
#include <iostream> | |
using namespace node; | |
using namespace v8; | |
class NodeKeeper : public EventEmitter | |
{ | |
private: | |
ev_async * completed_; | |
public: | |
static void Init(Handle<Object> target) | |
{ | |
HandleScope scope; | |
Local<FunctionTemplate> t = FunctionTemplate::New(New); | |
t->Inherit(EventEmitter::constructor_template); | |
t->InstanceTemplate()->SetInternalFieldCount(1); | |
NODE_SET_PROTOTYPE_METHOD(t, "trigger", trigger); | |
target->Set(String::NewSymbol("NodeKeeper"), t->GetFunction()); | |
} | |
static void Complete(EV_P_ ev_async * w, int revents) | |
{ | |
NodeKeeper * hw = static_cast<NodeKeeper *>(w->data); | |
hw->Emit(String::New("ping"), 0, NULL); | |
} | |
NodeKeeper() | |
{ | |
completed_ = new ev_async(); | |
} | |
~NodeKeeper() | |
{ | |
ev_async_stop(EV_DEFAULT_UC_ completed_); | |
delete completed_; | |
} | |
private: | |
static Handle<Value> New(const Arguments& args) | |
{ | |
HandleScope scope; | |
NodeKeeper * hw = new NodeKeeper(); | |
hw->completed_->data = hw; | |
ev_async_init(hw->completed_, NodeKeeper::Complete); | |
ev_async_start(EV_DEFAULT_UC_ hw->completed_); | |
ev_unref(EV_DEFAULT_UC); | |
hw->Wrap(args.This()); | |
return args.This(); | |
} | |
static Handle<Value> trigger(const Arguments & args) | |
{ | |
HandleScope scope; | |
NodeKeeper * hw = ObjectWrap::Unwrap<NodeKeeper>(args.This()); | |
if (args.Length() < 2 || !args[0]->IsString() || !args[1]->IsInt32()) { | |
return ThrowException(Exception::Error(String::New("Must give a connection string (host:port) and a timeout (integer) to initialize ZooKeeper"))); | |
} | |
String::AsciiValue connectionString(args[0]->ToString()); | |
zookeeper_init(* connectionString, watcherCallback, args[1]->ToInt32()->Value(), 0, hw, 0); | |
return Undefined(); | |
} | |
static void watcherCallback(zhandle_t * zh, int type, int state, const char * path, void * watcherCtx) | |
{ | |
HandleScope scope; | |
NodeKeeper * that = (NodeKeeper *)watcherCtx; | |
ev_async_send(EV_DEFAULT_UC_ that->completed_); | |
ev_ref(EV_DEFAULT_UC); | |
} | |
}; | |
extern "C" { | |
static void init(Handle<Object> target) | |
{ | |
HandleScope scope; | |
NodeKeeper::Init(target); | |
} | |
NODE_MODULE(nodekeeper, init); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hello = require('./build/default/nodekeeper'); | |
var hi = new hello.NodeKeeper(); | |
hi.addListener('ping', function () { | |
console.log('pong'); | |
}); | |
hi.trigger('localhost:2181', 5000); | |
var http = require('http'); | |
http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello World\n'); | |
}).listen(8124, "127.0.0.1"); | |
console.log('Server running at http://127.0.0.1:8124/'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment