Skip to content

Commit

Permalink
feat(NLVObject): make all class inherit NLVObject
Browse files Browse the repository at this point in the history
This will soon aid in the process of properly cleaning up
lingering objects when e.g. a Hypervisor is disconnected
  • Loading branch information
mbroadst committed Aug 14, 2015
1 parent 0f57819 commit 9b09ea6
Show file tree
Hide file tree
Showing 20 changed files with 174 additions and 150 deletions.
9 changes: 1 addition & 8 deletions src/domain.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ void Domain::Initialize(Handle<Object> exports)

NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_SEND_KEY_MAX_KEYS);


// ETC
NODE_DEFINE_CONSTANT(exports, VIR_DOMAIN_EVENT_ID_LIFECYCLE);
}

Domain::Domain(virDomainPtr handle) : NLVObject(handle) {}
Local<Object> Domain::NewInstance(virDomainPtr handle)
{
NanEscapableScope();
Expand All @@ -201,13 +201,6 @@ Local<Object> Domain::NewInstance(virDomainPtr handle)
return NanEscapeScope(object);
}

Domain::~Domain()
{
if (handle_ != NULL)
virDomainFree(handle_);
handle_ = 0;
}

NLV_LOOKUP_BY_VALUE_EXECUTE_IMPL(Domain, LookupByName, virDomainLookupByName)
NAN_METHOD(Domain::LookupByName)
{
Expand Down
16 changes: 9 additions & 7 deletions src/domain.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@
#ifndef SRC_DOMAIN_H_
#define SRC_DOMAIN_H_

#include "node_libvirt.h"

#include "nlv_object.h"
#include "nlv_async_worker.h"
#include "worker_macros.h"

namespace NodeLibvirt {

class Domain : public ObjectWrap
struct DomainCleanupHandler {
static int cleanup(virDomainPtr handle) {
return virDomainFree(handle);
}
};

class Domain : public NLVObject<virDomainPtr, DomainCleanupHandler>
{
public:
static void Initialize(Handle<Object> exports);
static Local<Object> NewInstance(virDomainPtr handle);
virtual ~Domain();

private:
explicit Domain(virDomainPtr handle) : handle_(handle) {}
explicit Domain(virDomainPtr handle);
static Persistent<FunctionTemplate> constructor_template;
static Persistent<Function> constructor;
virDomainPtr handle_;

friend class Hypervisor;

private:
Expand Down
20 changes: 1 addition & 19 deletions src/hypervisor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,32 +164,14 @@ void Hypervisor::Initialize(Handle<Object> exports)
}

Hypervisor::Hypervisor(std::string uri, std::string username, std::string password, bool readonly)
: handle_(NULL),
: NLVObject(NULL),
uri_(uri),
username_(username),
password_(password),
readOnly_(readonly)
{
}

Hypervisor::~Hypervisor()
{
if (handle_ != NULL) {
int result = virConnectClose(handle_);
if (result == -1) {
fprintf(stderr, "unable to free connection handle\n");
return;
}

handle_ = NULL;
}
}

virConnectPtr Hypervisor::Connection() const
{
return handle_;
}

NAN_METHOD(Hypervisor::New)
{
NanScope();
Expand Down
17 changes: 8 additions & 9 deletions src/hypervisor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@

#include <string>

#include "node_libvirt.h"
#include "domain.h"

#include "nlv_object.h"
#include "nlv_async_worker.h"
#include "worker_macros.h"

namespace NodeLibvirt {

class Hypervisor : public ObjectWrap
struct HypervisorCleanupHandler {
static int cleanup(virConnectPtr handle) {
return virConnectClose(handle);
}
};

class Hypervisor : public NLVObject<virConnectPtr, HypervisorCleanupHandler>
{
public:
static void Initialize(Handle<Object> exports);

virConnectPtr Connection() const;

private:
explicit Hypervisor(std::string uri, std::string user, std::string pass, bool readOnly);
virtual ~Hypervisor();

static Persistent<Function> constructor;
static Persistent<FunctionTemplate> constructor_template;

virConnectPtr handle_;
std::string uri_;
std::string username_;
std::string password_;
Expand Down
13 changes: 1 addition & 12 deletions src/interface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void Interface::Initialize(Handle<Object> exports)
NODE_DEFINE_CONSTANT(exports, VIR_INTERFACE_XML_INACTIVE);
}

Interface::Interface(virInterfacePtr handle) : NLVObject(handle) {}
Local<Object> Interface::NewInstance(virInterfacePtr handle)
{
NanEscapableScope();
Expand All @@ -39,18 +40,6 @@ Local<Object> Interface::NewInstance(virInterfacePtr handle)
return NanEscapeScope(object);
}

Interface::~Interface()
{
if (handle_ != NULL)
virInterfaceFree(handle_);
handle_ = NULL;
}

virInterfacePtr Interface::GetInterface() const
{
return handle_;
}

NLV_WORKER_METHOD_NO_ARGS(Interface, Start)
NLV_WORKER_EXECUTE(Interface, Start)
{
Expand Down
18 changes: 9 additions & 9 deletions src/interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@
#ifndef SRC_INTERFACE_H_
#define SRC_INTERFACE_H_

#include "node_libvirt.h"

#include "nlv_object.h"
#include "nlv_async_worker.h"
#include "worker_macros.h"

namespace NodeLibvirt {

class Interface : public ObjectWrap
struct InterfaceCleanupHandler {
static int cleanup(virInterfacePtr handle) {
return virInterfaceFree(handle);
}
};

class Interface : public NLVObject<virInterfacePtr, InterfaceCleanupHandler>
{
public:
static void Initialize(Handle<Object> exports);
static Local<Object> NewInstance(virInterfacePtr handle);
virtual ~Interface();

virInterfacePtr GetInterface() const;

private:
static Persistent<Function> constructor;
virInterfacePtr handle_;

friend class Hypervisor;

private:
explicit Interface(virInterfacePtr handle) : handle_(handle) {}
explicit Interface(virInterfacePtr handle);

// HYPERVISOR METHODS
static NAN_METHOD(LookupByName);
Expand Down
8 changes: 1 addition & 7 deletions src/network.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void Network::Initialize(Handle<Object> exports)
exports->Set(NanNew("Network"), t->GetFunction());
}

Network::Network(virNetworkPtr handle) : NLVObject(handle) {}
Local<Object> Network::NewInstance(virNetworkPtr handle)
{
NanEscapableScope();
Expand All @@ -39,13 +40,6 @@ Local<Object> Network::NewInstance(virNetworkPtr handle)
return NanEscapeScope(object);
}

Network::~Network()
{
if (handle_ != NULL)
virNetworkFree(handle_);
handle_ = 0;
}

NLV_LOOKUP_BY_VALUE_EXECUTE_IMPL(Network, LookupByName, virNetworkLookupByName)
NAN_METHOD(Network::LookupByName)
{
Expand Down
16 changes: 9 additions & 7 deletions src/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
#ifndef SRC_NETWORK_H_
#define SRC_NETWORK_H_

#include "node_libvirt.h"

#include "nlv_object.h"
#include "nlv_async_worker.h"
#include "worker_macros.h"

namespace NodeLibvirt {

class Network : public ObjectWrap
struct NetworkCleanupHandler {
static int cleanup(virNetworkPtr handle) {
return virNetworkFree(handle);
}
};

class Network : public NLVObject<virNetworkPtr, NetworkCleanupHandler>
{
public:
static void Initialize(Handle<Object> exports);
static Local<Object> NewInstance(virNetworkPtr handle);
virtual ~Network();

private:
explicit Network(virNetworkPtr handle) : handle_(handle) {}
explicit Network(virNetworkPtr handle);
static Persistent<Function> constructor;
virNetworkPtr handle_;

friend class Hypervisor;

protected:
Expand Down
8 changes: 1 addition & 7 deletions src/network_filter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ void NetworkFilter::Initialize(Handle<Object> exports)
exports->Set(NanNew("NetworkFilter"), t->GetFunction());
}

NetworkFilter::NetworkFilter(virNWFilterPtr handle) : NLVObject(handle) {}
Local<Object> NetworkFilter::NewInstance(virNWFilterPtr handle)
{
NanEscapableScope();
Expand All @@ -33,13 +34,6 @@ Local<Object> NetworkFilter::NewInstance(virNWFilterPtr handle)
return NanEscapeScope(object);
}

NetworkFilter::~NetworkFilter()
{
if (handle_ != NULL)
virNWFilterFree(handle_);
handle_ = 0;
}

NLV_LOOKUP_BY_VALUE_EXECUTE_IMPL(NetworkFilter, LookupByName, virNWFilterLookupByName)
NAN_METHOD(NetworkFilter::LookupByName)
{
Expand Down
16 changes: 9 additions & 7 deletions src/network_filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@
#ifndef SRC_NETWORK_FILTER_H_
#define SRC_NETWORK_FILTER_H_

#include <nan.h>

#include "nlv_object.h"
#include "nlv_async_worker.h"
#include "worker_macros.h"

namespace NodeLibvirt {

class NetworkFilter : public ObjectWrap
struct NetworkFilterCleanupHandler {
static int cleanup(virNWFilterPtr handle) {
return virNWFilterFree(handle);
}
};

class NetworkFilter : public NLVObject<virNWFilterPtr, NetworkFilterCleanupHandler>
{
public:
static void Initialize(Handle<Object> exports);
static Local<Object> NewInstance(virNWFilterPtr handle);
virtual ~NetworkFilter();

private:
explicit NetworkFilter(virNWFilterPtr handle) : handle_(handle) {}
explicit NetworkFilter(virNWFilterPtr handle);
static Persistent<Function> constructor;
virNWFilterPtr handle_;

friend class Hypervisor;

protected:
Expand Down
38 changes: 37 additions & 1 deletion src/nlv_async_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ class NLVTypedParameterReturnWorker : public NLVAsyncWorker<HandleType>
} \
}


template <typename InstanceClass, typename ParentHandleType, typename InstanceHandleType>
class NLVLookupInstanceByValueWorker : public NLVAsyncWorker<ParentHandleType>
{
Expand All @@ -227,4 +226,41 @@ class NLVLookupInstanceByValueWorker : public NLVAsyncWorker<ParentHandleType>

};






/*
template <typename InstanceClass, typename ParentClass, typename InstanceHandleType>
class NLVLookupInstanceByValueWorkerV2 : public NLVAsyncWorkerBase
{
public:
explicit NLVLookupInstanceByValueWorkerV2(NanCallback *callback,
ParentClass *parent,
const std::string &value)
: NLVAsyncWorkerBase(callback), parent_(parent), value_(value), lookupHandle_(NULL) {}
protected:
using NLVAsyncWorkerBase::callback;
virtual void HandleOKCallback() {
NanScope();
v8::Local<v8::Value> argv[] = { NanNull(), InstanceClass::NewInstance(lookupHandle_) };
callback->Call(2, argv);
}
ParentClass *parent_;
std::string value_;
InstanceHandleType lookupHandle_;
};
*/








#endif // NLV_ASYNC_WORKER_H
Loading

0 comments on commit 9b09ea6

Please sign in to comment.