diff --git a/src/hypervisor.cc b/src/hypervisor.cc index 87b35e6..3e47db9 100644 --- a/src/hypervisor.cc +++ b/src/hypervisor.cc @@ -1,5 +1,7 @@ // Copyright 2010, Camilo Aguilar. Cloudescape, LLC. +#include + #include "worker_macros.h" #include "domain.h" @@ -162,7 +164,7 @@ void Hypervisor::Initialize(Handle exports) } Hypervisor::Hypervisor(string uri, string username, string password, bool readonly) - : ObjectWrap(), + : handle_(NULL), uri_(uri), username_(username), password_(password), @@ -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) \ @@ -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; } @@ -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; diff --git a/src/hypervisor.h b/src/hypervisor.h index 55a724a..86445f0 100644 --- a/src/hypervisor.h +++ b/src/hypervisor.h @@ -8,6 +8,8 @@ #include "worker_macros.h" #include "worker.h" +#include "nlv_async_worker.h" + namespace NodeLibvirt { class Hypervisor : public ObjectWrap @@ -95,10 +97,10 @@ class Hypervisor : public ObjectWrap private: // ACTION WORKERS - class ConnectWorker : public LibVirtWorker { + class ConnectWorker : public NLVAsyncWorker { 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); @@ -106,10 +108,10 @@ class Hypervisor : public ObjectWrap Hypervisor *hypervisor_; }; - class DisconnectWorker : public LibVirtWorker { + class DisconnectWorker : public NLVAsyncWorker { public: DisconnectWorker(NanCallback *callback, Hypervisor *hypervisor) - : LibVirtWorker(callback, hypervisor->handle_), hypervisor_(hypervisor) {} + : NLVAsyncWorker(callback, hypervisor->handle_), hypervisor_(hypervisor) {} void Execute(); private: Hypervisor *hypervisor_; @@ -237,10 +239,10 @@ class Hypervisor : public ObjectWrap int flags_; }; - class GetNodeInfoWorker : public LibVirtWorker { + class GetNodeInfoWorker : public NLVAsyncWorker { public: - GetNodeInfoWorker(NanCallback *callback, const LibVirtHandle &handle) - : LibVirtWorker(callback, handle) {} + GetNodeInfoWorker(NanCallback *callback, virConnectPtr handle) + : NLVAsyncWorker(callback, handle) {} void Execute(); protected: void HandleOKCallback(); diff --git a/src/nlv_async_worker.h b/src/nlv_async_worker.h new file mode 100644 index 0000000..dd7dac8 --- /dev/null +++ b/src/nlv_async_worker.h @@ -0,0 +1,61 @@ +#ifndef NLV_ASYNC_WORKER_H +#define NLV_ASYNC_WORKER_H + +#include + +#include +#include + +#include "error.h" + +using namespace NodeLibvirt; + +template +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 argv[] = { Error::New(VirError()) }; + callback->Call(1, argv); + } else { + v8::Local argv[] = { + v8::Exception::Error(NanNew(ErrorMessage())) + }; + callback->Call(1, argv); + } + } + +private: + T handle_; + virErrorPtr virError_; + +}; + +#endif // NLV_ASYNC_WORKER_H diff --git a/src/worker_macros.h b/src/worker_macros.h index 5792f65..8472add 100644 --- a/src/worker_macros.h +++ b/src/worker_macros.h @@ -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"); \