-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(libvirt-handler): refactored LibVirtHandler into own class
- Loading branch information
Showing
4 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#include <libvirt/libvirt.h> | ||
#include "libvirt_handle.h" | ||
|
||
namespace NodeLibvirt { | ||
|
||
|
||
|
||
struct LibVirtHandlePrivate | ||
{ | ||
LibVirtHandlePrivate() { | ||
data.connection = 0; | ||
data.interface = 0; | ||
data.network = 0; | ||
} | ||
|
||
union Data { | ||
virConnectPtr connection; | ||
virInterfacePtr interface; | ||
virNetworkPtr network; | ||
} data; | ||
}; | ||
|
||
LibVirtHandle::LibVirtHandle() | ||
: d(new LibVirtHandlePrivate) | ||
{ | ||
} | ||
|
||
LibVirtHandle::LibVirtHandle(virConnectPtr connection) | ||
: d(new LibVirtHandlePrivate) | ||
{ | ||
d->data.connection = connection; | ||
} | ||
|
||
LibVirtHandle::LibVirtHandle(virInterfacePtr interface) | ||
: d(new LibVirtHandlePrivate) | ||
{ | ||
d->data.interface = interface; | ||
} | ||
|
||
LibVirtHandle::LibVirtHandle(virNetworkPtr network) | ||
: d(new LibVirtHandlePrivate) | ||
{ | ||
d->data.network = network; | ||
} | ||
|
||
LibVirtHandle::~LibVirtHandle() | ||
{ | ||
} | ||
|
||
void LibVirtHandle::Clear() | ||
{ | ||
d->data.connection = 0; | ||
|
||
if (d->data.interface) | ||
virInterfaceFree(d->data.interface); | ||
d->data.interface = 0; | ||
|
||
if (d->data.network) | ||
virNetworkFree(d->data.network); | ||
d->data.network = 0; | ||
} | ||
|
||
virConnectPtr LibVirtHandle::ToConnection() const | ||
{ | ||
return d->data.connection; | ||
} | ||
|
||
virInterfacePtr LibVirtHandle::ToInterface() const | ||
{ | ||
return d->data.interface; | ||
} | ||
|
||
virNetworkPtr LibVirtHandle::ToNetwork() const | ||
{ | ||
return d->data.network; | ||
} | ||
|
||
} // namespace NodeLibvirt |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#ifndef LIBVIRT_HANDLE_H | ||
#define LIBVIRT_HANDLE_H | ||
|
||
#include <libvirt/libvirt.h> | ||
#include <memory> | ||
using namespace std; | ||
|
||
namespace NodeLibvirt { | ||
|
||
class LibVirtHandlePrivate; | ||
class LibVirtHandle | ||
{ | ||
public: | ||
LibVirtHandle(); | ||
~LibVirtHandle(); | ||
LibVirtHandle(virConnectPtr connection); | ||
LibVirtHandle(virInterfacePtr interface); | ||
LibVirtHandle(virNetworkPtr network); | ||
|
||
void Clear(); | ||
|
||
virConnectPtr ToConnection() const; | ||
virInterfacePtr ToInterface() const; | ||
virNetworkPtr ToNetwork() const; | ||
|
||
private: | ||
shared_ptr<LibVirtHandlePrivate> d; | ||
|
||
}; | ||
|
||
} // namespace NodeLibvirt | ||
|
||
#endif // LIBVIRT_HANDLE_H |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#ifndef WORKER_MACROS_H | ||
#define WORKER_MACROS_H | ||
|
||
// UTIL | ||
#define NLV_CATNX(A, B) A ## B | ||
#define NLV_CAT(A, B) NLV_CATNX(A, B) | ||
|
||
// ASSERTIONS | ||
#define NLV_WORKER_ASSERT_CONNECTION() \ | ||
if (Handle().ToConnection() == NULL) { \ | ||
SetErrorMessage("invalid connection"); \ | ||
return; \ | ||
} | ||
|
||
#define NLV_WORKER_ASSERT_INTERFACE() \ | ||
if (Handle().ToInterface() == NULL) { \ | ||
SetErrorMessage("invalid interface"); \ | ||
return; \ | ||
} | ||
|
||
#define NLV_WORKER_ASSERT_NETWORK() \ | ||
if (Handle().ToNetwork() == NULL) { \ | ||
SetErrorMessage("invalid network"); \ | ||
return; \ | ||
} | ||
|
||
|
||
// METHOD HELPERS | ||
#define NLV_WORKER_METHOD_NO_ARGS(Class, Method) \ | ||
NAN_METHOD(Class::Method) { \ | ||
NanScope(); \ | ||
if (args.Length() == 1 && !args[0]->IsFunction()) { \ | ||
NanThrowTypeError("You must specify a function as first argument"); \ | ||
NanReturnUndefined(); \ | ||
} \ | ||
NanCallback *callback = new NanCallback(args[0].As<Function>()); \ | ||
Class *object = ObjectWrap::Unwrap<Class>(args.This()); \ | ||
NanAsyncQueueWorker(new Method##Worker(callback, object->handle_)); \ | ||
NanReturnUndefined(); \ | ||
} | ||
|
||
// WORKER DEFINITIONS | ||
#define NLV_PRIMITIVE_RETURN_WORKER(Method, HandleType, Type) \ | ||
class Method##Worker : public PrimitiveReturnWorker<Type> { \ | ||
public: \ | ||
Method##Worker(NanCallback *callback, HandleType handle) \ | ||
: PrimitiveReturnWorker<Type>(callback, handle) {} \ | ||
void Execute(); \ | ||
}; | ||
|
||
#define NLV_LOOKUP_BY_VALUE_WORKER(Class, Method) \ | ||
class Method##Worker : public LookupInstanceByValueWorker<Class> { \ | ||
public: \ | ||
Method##Worker(NanCallback *callback, const LibVirtHandle &handle, const std::string &value) \ | ||
: LookupInstanceByValueWorker<Class>(callback, handle, value) {} \ | ||
void Execute(); \ | ||
}; | ||
|
||
// EXECUTE HELPERS | ||
#define NLV_INT_RETURN_EXECUTE(Class, Method, Accessor) \ | ||
void Class::Method##Worker::Execute() { \ | ||
HYPERVISOR_ASSERT_CONNECTION(); \ | ||
int result = Accessor(Handle().ToConnection()); \ | ||
if (result == -1) { \ | ||
SetVirError(virGetLastError()); \ | ||
return; \ | ||
} \ | ||
data_ = result; \ | ||
} | ||
|
||
#define NLV_BOOL_RETURN_EXECUTE(Class, Method, Accessor) \ | ||
void Class::Method##Worker::Execute() { \ | ||
HYPERVISOR_ASSERT_CONNECTION(); \ | ||
int result = Accessor(Handle().ToConnection()); \ | ||
if (result == -1) { \ | ||
SetVirError(virGetLastError()); \ | ||
return; \ | ||
} \ | ||
data_ = static_cast<bool>(result); \ | ||
} | ||
|
||
#define NLV_LOOKUP_BY_VALUE_EXECUTE(Class, Method, Accessor) \ | ||
void Class::Method##Worker::Execute() { \ | ||
lookupHandle_ = Accessor(Handle().ToConnection(), value_.c_str()); \ | ||
if (lookupHandle_.NLV_CAT(To, Class)() == NULL) { \ | ||
SetVirError(virGetLastError()); \ | ||
return; \ | ||
} \ | ||
} | ||
|
||
#endif // WORKER_MACROS_H |