start using the uv thread pool forn work. add example.

This commit is contained in:
Christopher Jeffrey 2014-08-19 16:40:19 -07:00
parent 53b9d2224f
commit f6c05021d2
4 changed files with 85 additions and 22 deletions

View File

@ -1,2 +1 @@
var os = process.platform === 'win32' ? '_win' : ''; module.exports = require('./lib/bitcoind.js');
module.exports = require('./lib/pty'+ os +'.js');

View File

@ -15,14 +15,14 @@ var bitcoindjs = require('../build/Release/bitcoindjs.node');
function Bitcoin(flag) { function Bitcoin(flag) {
var self = this; var self = this;
if (!(this instanceof Bitcoind)) { if (!(this instanceof Bitcoin)) {
return new Bitcoind(flag); return new Bitcoin(flag);
} }
EventEmitter.call(this); EventEmitter.call(this);
var ret = bitcoindjs.start(function() { var ret = bitcoindjs.start(function(err, status) {
self.emit('open'); self.emit('open', status);
}); });
this.ret = ret; this.ret = ret;

View File

@ -9,11 +9,6 @@
"bugs": { "bugs": {
"url": "https://github.com/chjj/bitcoind.js/issues" "url": "https://github.com/chjj/bitcoind.js/issues"
}, },
"keywords": [
"pty",
"tty",
"terminal"
],
"scripts": { "scripts": {
"test": "NODE_ENV=test mocha -R spec" "test": "NODE_ENV=test mocha -R spec"
}, },

View File

@ -8,6 +8,9 @@
#include "nan.h" #include "nan.h"
#include <node.h>
#include <string>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -22,12 +25,22 @@ using namespace v8;
NAN_METHOD(StartBitcoind); NAN_METHOD(StartBitcoind);
static int void
misc_func(const char *); async_work(uv_work_t *req);
void
async_after(uv_work_t *req);
extern "C" void extern "C" void
init(Handle<Object>); init(Handle<Object>);
struct async_data {
Persistent<Function> callback;
bool err;
std::string err_msg;
char *result;
};
/** /**
* StartBitcoind * StartBitcoind
* bitcoind.start(callback) * bitcoind.start(callback)
@ -41,19 +54,75 @@ NAN_METHOD(StartBitcoind) {
"Usage: bitcoind.start(callback)"); "Usage: bitcoind.start(callback)");
} }
Local<Object> obj = NanNew<Object>(); Local<Function> callback = Local<Function>::Cast(args[0]);
obj->Set(NanNew<String>("foo"), NanNew<Number>(100));
NanReturnValue(obj); // Local<Value> err = Exception::Error(String::New("Bad input"));
// err->ToObject()->Set(NODE_PSYMBOL("errno"), Integer::New(0));
// const unsigned argc = 1;
// Local<Value> argv[1] = { err };
// callback->Call(Context::GetCurrent()->Global(), argc, argv);
// const unsigned argc = 2;
// Local<Value> argv[2] = {
// Local<Value>::New(Null()),
// Local<Value>::New(String::New("opened"))
// };
// callback->Call(Context::GetCurrent()->Global(), argc, argv);
async_data* data = new async_data();
data->err = false;
data->callback = Persistent<Function>::New(callback);
uv_work_t *req = new uv_work_t();
req->data = data;
int status = uv_queue_work(uv_default_loop(),
req, async_work, (uv_after_work_cb)async_after);
assert(status == 0);
NanReturnValue(Undefined());
} }
/** void async_work(uv_work_t *req) {
* misc_func async_data* data = static_cast<async_data*>(req->data);
*/ data->result = (char *)strdup("opened");
}
static int void async_after(uv_work_t *req) {
misc_func(const char *file) { NanScope();
return 0; async_data* data = static_cast<async_data*>(req->data);
if (data->err) {
Local<Value> err = Exception::Error(String::New(data->err_msg.c_str()));
const unsigned argc = 1;
Local<Value> argv[1] = { err };
TryCatch try_catch;
data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
} else {
const unsigned argc = 2;
Local<Value> argv[2] = {
Local<Value>::New(Null()),
Local<Value>::New(String::New(data->result))
};
TryCatch try_catch;
data->callback->Call(Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}
data->callback.Dispose();
if (data->result != NULL) {
free(data->result);
}
delete data;
delete req;
} }
/** /**