Skip to content

Commit

Permalink
GetCapabilities now async
Browse files Browse the repository at this point in the history
  • Loading branch information
atoy40 committed Feb 26, 2015
1 parent 0a6d0f3 commit 725502f
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 12 deletions.
12 changes: 12 additions & 0 deletions src/baton.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ namespace NodeLibvirt {
: Baton(hypervisor, callback) {
}

StringBaton::StringBaton(Hypervisor *hypervisor, Persistent<Function> callback)
: Baton(hypervisor, callback), str_(NULL) {
}

void StringBaton::setString(char* str) {
this->str_ = str;
}

char* StringBaton::getString() {
return this->str_;
}

}
10 changes: 9 additions & 1 deletion src/baton.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ namespace NodeLibvirt {
class ConnectBaton : public Baton {
public:
ConnectBaton(Hypervisor *hypervisor, Persistent<Function> callback);
virConnectPtr conn_;
};

class StringBaton : public Baton {
public:
StringBaton(Hypervisor *hypervisor, Persistent<Function> callback);
void setString(char *);
char* getString();
private:
char *str_;
};
}

Expand Down
66 changes: 55 additions & 11 deletions src/hypervisor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,11 @@ namespace NodeLibvirt {
auth.cb = Hypervisor::auth_callback;
auth.cbdata = baton->getHypervisor();

baton->conn_ = virConnectOpenAuth( (const char*) hypervisor->uri_,
hypervisor->conn_ = virConnectOpenAuth( (const char*) hypervisor->uri_,
&auth,
hypervisor->readOnly_ ? VIR_CONNECT_RO : 0);

if(baton->conn_ == NULL) {
if(hypervisor->conn_ == NULL) {
baton->setError(virGetLastError());
}
}
Expand Down Expand Up @@ -568,23 +568,67 @@ namespace NodeLibvirt {
return scope.Close(Undefined());
}

Handle<Value> Hypervisor::GetCapabilities(const Arguments& args) {
HandleScope scope;
char* capabilities_ = NULL;
void Hypervisor::GetCapabilitiesWorker(uv_work_t* req) {
StringBaton *baton = static_cast<StringBaton*>(req->data);
Hypervisor *hypervisor = baton->getHypervisor();

Hypervisor *hypervisor = ObjectWrap::Unwrap<Hypervisor>(args.This());
char* capabilities_ = NULL;

capabilities_ = virConnectGetCapabilities(hypervisor->conn_);

if(capabilities_ == NULL) {
ThrowException(Error::New(virGetLastError()));
return Null();
baton->setError(virGetLastError());
return;
}

baton->setString(capabilities_);
}

void Hypervisor::GetCapabilitiesAfter(uv_work_t* req) {
HandleScope scope;

StringBaton *baton = static_cast<StringBaton*>(req->data);

if (baton->hasError()) {
Handle<Value> argv[] = { Error::New(baton->getError()) };

TryCatch try_catch;
baton->getCallback()->Call(Context::GetCurrent()->Global(), 1, argv);

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
} else {
TryCatch try_catch;
Local<Value> res = String::New((const char*)baton->getString());
Handle<Value> argv[] = { Undefined(), res };
baton->getCallback()->Call(Context::GetCurrent()->Global(), 2, argv);

if (try_catch.HasCaught()) {
node::FatalException(try_catch);
}
}

baton->getCallback().Dispose();
delete baton;
}

Handle<Value> Hypervisor::GetCapabilities(const Arguments& args) {
HandleScope scope;
Hypervisor *hypervisor = ObjectWrap::Unwrap<Hypervisor>(args.This());

if (args.Length() == 1 && !args[0]->IsFunction()) {
return ThrowException(Exception::TypeError(
String::New("You must specify a function as first argument")));
}

Local<String> capabilities = String::New((const char*)capabilities_);
free(capabilities_);
Local<Function> callback = Local<Function>::Cast(args[0]);

StringBaton *baton = new StringBaton(hypervisor, Persistent<Function>::New(callback));

uv_queue_work(uv_default_loop(), baton->getHandle(), Hypervisor::GetCapabilitiesWorker, (uv_after_work_cb)Hypervisor::GetCapabilitiesAfter);

return scope.Close(capabilities);
return scope.Close(Undefined());
}

Handle<Value> Hypervisor::GetHostname(const Arguments& args) {
Expand Down
2 changes: 2 additions & 0 deletions src/hypervisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ namespace NodeLibvirt {
static void ConnectWorker(uv_work_t* req);
static void ConnectAfter(uv_work_t* req);
static Handle<Value> GetCapabilities(const Arguments& args);
static void GetCapabilitiesWorker(uv_work_t* req);
static void GetCapabilitiesAfter(uv_work_t* req);
static Handle<Value> GetHostname(const Arguments& args);
static Handle<Value> GetSysinfo(const Arguments& args);
static Handle<Value> GetType(const Arguments& args);
Expand Down

0 comments on commit 725502f

Please sign in to comment.