Skip to content

Commit

Permalink
feat(NLVAsyncWorker): create new async worker, migrate (dis)connect
Browse files Browse the repository at this point in the history
LibVirtHandle was a mistake, this is the first step towards
correcting that mistake. NLVAsyncWorker will be the base class for
all future workers in node-libvirt.
  • Loading branch information
mbroadst committed Aug 13, 2015
1 parent c843ed1 commit 87d2036
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/hypervisor.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2010, Camilo Aguilar. Cloudescape, LLC.

#include <assert.h>

#include "worker_macros.h"

#include "domain.h"
Expand Down Expand Up @@ -162,7 +164,7 @@ void Hypervisor::Initialize(Handle<Object> exports)
}

Hypervisor::Hypervisor(string uri, string username, string password, bool readonly)
: ObjectWrap(),
: handle_(NULL),
uri_(uri),
username_(username),
password_(password),
Expand Down Expand Up @@ -319,14 +321,15 @@ NAN_METHOD(Hypervisor::Disconnect)

NLV_WORKER_EXECUTE(Hypervisor, Disconnect)
{
NLV_WORKER_ASSERT_CONNECTION();
int result = virConnectClose(Handle().ToConnection());
NLV_WORKER_ASSERT_CONNECTION2();
int result = virConnectClose(Handle());
if (result == -1) {
SetVirError(virGetLastError());
return;
}

Handle().Clear();
assert(result == 0);
hypervisor_->handle_ = NULL;
}

#define HYPERVISOR_STRING_RETURN_EXECUTE(MethodName, Accessor) \
Expand Down Expand Up @@ -421,7 +424,6 @@ NLV_WORKER_EXECUTE(Hypervisor, GetLibVirtVersion)

char versionString[10];
sprintf(versionString, "%d.%d.%d", major, minor, patch);
fprintf(stderr, "%s", versionString);
data_ = version;
}

Expand Down Expand Up @@ -813,8 +815,8 @@ NLV_WORKER_OKCALLBACK(Hypervisor, GetNodeSecurityModel)
NLV_WORKER_METHOD_NO_ARGS(Hypervisor, GetNodeInfo)
NLV_WORKER_EXECUTE(Hypervisor, GetNodeInfo)
{
NLV_WORKER_ASSERT_CONNECTION();
int result = virNodeGetInfo(Handle().ToConnection(), &info_);
NLV_WORKER_ASSERT_CONNECTION2();
int result = virNodeGetInfo(Handle(), &info_);
if (result == -1) {
SetVirError(virGetLastError());
return;
Expand Down
16 changes: 9 additions & 7 deletions src/hypervisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include "worker_macros.h"
#include "worker.h"

#include "nlv_async_worker.h"

namespace NodeLibvirt {

class Hypervisor : public ObjectWrap
Expand Down Expand Up @@ -95,21 +97,21 @@ class Hypervisor : public ObjectWrap

private:
// ACTION WORKERS
class ConnectWorker : public LibVirtWorker {
class ConnectWorker : public NLVAsyncWorker<virConnectPtr> {
public:
ConnectWorker(NanCallback *callback, Hypervisor *hypervisor)
: LibVirtWorker(callback, LibVirtHandle()), hypervisor_(hypervisor) {}
: NLVAsyncWorker(callback, NULL), hypervisor_(hypervisor) {}

void Execute();
static int auth_callback(virConnectCredentialPtr cred, unsigned int ncred, void *data);
private:
Hypervisor *hypervisor_;
};

class DisconnectWorker : public LibVirtWorker {
class DisconnectWorker : public NLVAsyncWorker<virConnectPtr> {
public:
DisconnectWorker(NanCallback *callback, Hypervisor *hypervisor)
: LibVirtWorker(callback, hypervisor->handle_), hypervisor_(hypervisor) {}
: NLVAsyncWorker(callback, hypervisor->handle_), hypervisor_(hypervisor) {}
void Execute();
private:
Hypervisor *hypervisor_;
Expand Down Expand Up @@ -237,10 +239,10 @@ class Hypervisor : public ObjectWrap
int flags_;
};

class GetNodeInfoWorker : public LibVirtWorker {
class GetNodeInfoWorker : public NLVAsyncWorker<virConnectPtr> {
public:
GetNodeInfoWorker(NanCallback *callback, const LibVirtHandle &handle)
: LibVirtWorker(callback, handle) {}
GetNodeInfoWorker(NanCallback *callback, virConnectPtr handle)
: NLVAsyncWorker(callback, handle) {}
void Execute();
protected:
void HandleOKCallback();
Expand Down
61 changes: 61 additions & 0 deletions src/nlv_async_worker.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef NLV_ASYNC_WORKER_H
#define NLV_ASYNC_WORKER_H

#include <nan.h>

#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>

#include "error.h"

using namespace NodeLibvirt;

template <typename T>
class NLVAsyncWorker : public NanAsyncWorker
{
public:
explicit NLVAsyncWorker(NanCallback *callback, T handle)
: NanAsyncWorker(callback), handle_(handle), virError_(NULL) {}

T Handle() const { return handle_; }

virErrorPtr VirError() const { return virError_; }
void SetVirError(virErrorPtr error) { virError_ = error; }

virtual void WorkComplete()
{
NanScope();

if (virError_ == NULL && ErrorMessage() == NULL) {
HandleOKCallback();
} else {
HandleErrorCallback();
}

delete callback;
callback = NULL;
}

protected:
virtual void HandleErrorCallback()
{
NanScope();

if (virError_ != NULL) {
v8::Handle<v8::Value> argv[] = { Error::New(VirError()) };
callback->Call(1, argv);
} else {
v8::Local<v8::Value> argv[] = {
v8::Exception::Error(NanNew<v8::String>(ErrorMessage()))
};
callback->Call(1, argv);
}
}

private:
T handle_;
virErrorPtr virError_;

};

#endif // NLV_ASYNC_WORKER_H
10 changes: 10 additions & 0 deletions src/worker_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
#define NLV_CAT(A, B) NLV_CATNX(A, B)

// ASSERTIONS
#define NLV_WORKER_ASSERT_HANDLE(Type) \
if (Handle() == NULL) { \
SetErrorMessage("invalid "#Type); \
return; \
}

#define NLV_WORKER_ASSERT_CONNECTION2() \
NLV_WORKER_ASSERT_HANDLE("connection")


#define NLV_WORKER_ASSERT_CONNECTION() \
if (Handle().ToConnection() == NULL) { \
SetErrorMessage("invalid connection"); \
Expand Down

0 comments on commit 87d2036

Please sign in to comment.