[Ovirt-devel] [PATCH matahari] Moving QMF functionality into a transport layer.
Darryl L. Pierce
dpierce at redhat.com
Thu May 13 21:05:45 UTC 2010
This is the first step towards separating the APIs from the means by
which they are carried between the remote and local system.
Added a few new classes and types:
* HostTransport - defines a type for talking to the Host
* Host - the public contract for the host APIs
* HostAgent - the QMF transport layer
Signed-off-by: Darryl L. Pierce <dpierce at redhat.com>
---
.gitignore | 2 +-
src/Makefile.am | 8 +++-
src/host.cpp | 133 +++++++++++++++++++----------------------------
src/host.h | 62 ++++++++++++++--------
src/host_transport.cpp | 33 ++++++++++++
src/host_transport.h | 37 +++++++++++++
src/hostimpl.cpp | 135 ++++++++++++++++++++++++++++++++++++++++++++++++
src/hostimpl.h | 47 +++++++++++++++++
src/main.cpp | 8 ++-
src/qmf/hostagent.cpp | 87 +++++++++++++++++++++++++++++++
src/qmf/hostagent.h | 55 +++++++++++++++++++
11 files changed, 502 insertions(+), 105 deletions(-)
create mode 100644 src/host_transport.cpp
create mode 100644 src/host_transport.h
create mode 100644 src/hostimpl.cpp
create mode 100644 src/hostimpl.h
create mode 100644 src/qmf/hostagent.cpp
create mode 100644 src/qmf/hostagent.h
diff --git a/.gitignore b/.gitignore
index ad6d70b..8e12062 100644
--- a/.gitignore
+++ b/.gitignore
@@ -17,4 +17,4 @@ stamp-h1
*~
src/matahari
-src/qmf
+src/qmf/com
diff --git a/src/Makefile.am b/src/Makefile.am
index aed9e19..530abd7 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -18,6 +18,10 @@ nodist_matahari_SOURCES = $(generated_file_list) $(first)
matahari_SOURCES = \
host.cpp \
host.h \
+ host_transport.cpp \
+ host_transport.h \
+ hostimpl.cpp \
+ hostimpl.h \
linux_platform.cpp \
linux_platform.h \
main.cpp \
@@ -26,7 +30,9 @@ matahari_SOURCES = \
platform.cpp \
platform.h \
processors.cpp \
- processors.h
+ processors.h \
+ qmf/hostagent.cpp \
+ qmf/hostagent.h
$(generated_file_list): $(generated_file_list)
diff --git a/src/host.cpp b/src/host.cpp
index 34d4550..e755ffa 100644
--- a/src/host.cpp
+++ b/src/host.cpp
@@ -17,103 +17,78 @@
* also available at http://www.gnu.org/copyleft/gpl.html.
*/
-#include <fstream>
-
-#include <libvirt/libvirt.h>
-#include <qpid/management/Manageable.h>
-#include <sys/sysinfo.h>
-#include <sys/utsname.h>
-
#include "host.h"
-#include "platform.h"
-#include "qmf/com/redhat/matahari/Host.h"
-using namespace qpid::management;
using namespace std;
-using qpid::management::Manageable;
-namespace _qmf = qmf::com::redhat::matahari;
-
void
-HostAgent::setup(ManagementAgent* agent)
+Host::set_uuid(const string uuid)
{
- management_object = new _qmf::Host(agent, this);
- agent->addObject(management_object);
-
- // discover the aspects of the host
- processors.setup(agent, this);
- networkdevices = Platform::instance()->get_network_devices();
-
- for(vector<NetworkDeviceAgent>::iterator iter = networkdevices.begin();
- iter != networkdevices.end();
- iter++)
- {
- iter->setup(agent, this);
- }
+ _uuid = uuid;
+}
- struct utsname details;
- string uuid = "Unknown";
- string hostname = "Unknown";
- string hypervisor = "Unknown";
- string architecture = "None";
- unsigned long memory = 0;
- bool beeping = false;
+string
+Host:: get_uuid() const
+{
+ return _uuid;
+}
- ifstream input("/var/lib/dbus/machine-id");
+void
+Host::set_hostname(const string hostname)
+{
+ _hostname = hostname;
+}
- if(input.is_open())
- {
- getline(input, uuid);
- input.close();
- }
+string
+Host::get_hostname() const
+{
+ return _hostname;
+}
- if(!uname(&details))
- {
- hostname = string(details.nodename);
- architecture = string(details.machine);
- }
- else
- {
- throw runtime_error("Unable to retrieve system details");
- }
+void
+Host::set_hypervisor(const string hypervisor)
+{
+ _hypervisor = hypervisor;
+}
- virConnectPtr lvconn = virConnectOpenReadOnly(NULL);
+string
+Host::get_hypervisor() const
+{
+ return _hypervisor;
+}
- if(lvconn)
- {
- hypervisor = string(virConnectGetType(lvconn));
- virConnectClose(lvconn);
- }
+void
+Host::set_architecture(const string architecture)
+{
+ _architecture = architecture;
+}
- struct sysinfo sysinf;
- if(!sysinfo(&sysinf))
- {
- memory = sysinf.totalram / 1024L;
- }
- else
- {
- throw runtime_error("Unable to retrieve system memory details.");
- }
+string
+Host::get_architecture() const
+{
+ return _architecture;
+}
- cout << "memory: " << memory << endl;
+void
+Host::set_memory(const unsigned int memory)
+{
+ _memory = memory;
+}
- management_object->set_uuid(uuid);
- management_object->set_hostname(hostname);
- management_object->set_hypervisor(hypervisor);
- management_object->set_arch(architecture);
- management_object->set_memory(memory);
- management_object->set_beeping(beeping);
+unsigned int
+Host::get_memory() const
+{
+ return _memory;
}
void
-HostAgent::update(void)
+Host::set_beeping(const bool beeping)
{
- processors.update();
+ _beeping = beeping;
+}
- for(vector<NetworkDeviceAgent>::iterator iter = networkdevices.begin();
- iter != networkdevices.end();
- iter++)
- {
- iter->update();
- }
+bool
+Host::is_beeping() const
+{
+ return _beeping;
}
diff --git a/src/host.h b/src/host.h
index d2da776..b2a4a30 100644
--- a/src/host.h
+++ b/src/host.h
@@ -20,39 +20,57 @@
* also available at http://www.gnu.org/copyleft/gpl.html.
*/
-#include <qpid/management/Manageable.h>
-#include <qpid/management/ManagementObject.h>
-#include <qpid/agent/ManagementAgent.h>
+#include <string>
-#include "qmf/com/redhat/matahari/Host.h"
-
-#include "networkdevice.h"
-#include "processors.h"
-
-using namespace qpid::management;
using namespace std;
-using qpid::management::Manageable;
+/*
+ HeartbeatListener defines a type which receives notification
+ when a heartbeat event occurs.
+ */
+class HeartbeatListener
+{
+ public:
+ virtual void heartbeat() = 0;
+};
-class HostAgent : public Manageable
+/*
+ Host represents the public contract for the set of host APIs.
+ */
+class Host
{
private:
- qmf::com::redhat::matahari::Host* management_object;
- ProcessorsAgent processors;
- vector<NetworkDeviceAgent> networkdevices;
+ string _uuid;
+ string _hostname;
+ string _hypervisor;
+ string _architecture;
+ unsigned int _memory;
+ bool _beeping;
public:
- HostAgent() {}
- virtual ~HostAgent() {}
+ void set_uuid(const string uuid);
+ string get_uuid() const;
+
+ void set_hostname(const string hostname);
+ string get_hostname() const;
+
+ void set_hypervisor(const string hypervisor);
+ string get_hypervisor() const;
+
+ void set_architecture(const string architecture);
+ string get_architecture() const;
- ManagementObject* GetManagementObject(void) const { return management_object; }
+ void set_memory(const unsigned int memory);
+ unsigned int get_memory() const;
- void setup(ManagementAgent* agent);
- void update(void);
+ void set_beeping(const bool beeping);
+ bool is_beeping() const;
- // agent methods
- void shutdown(void);
- void reboot(void);
+ virtual void identify(const int iterations) = 0;
+ virtual void subscribe_to_heartbeat(const HeartbeatListener& listener) = 0;
+ virtual void unsubscribe_from_heartbeat(const HeartbeatListener& listener) = 0;
+ virtual void shutdown() = 0;
+ virtual void reboot() = 0;
};
#endif // __HOST_H
diff --git a/src/host_transport.cpp b/src/host_transport.cpp
new file mode 100644
index 0000000..ab89912
--- /dev/null
+++ b/src/host_transport.cpp
@@ -0,0 +1,33 @@
+/* host_transport.cpp - Copyright (C) 2009 Red Hat, Inc.
+ * Written by Arjun Roy <arroy at redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA. A copy of the GNU General Public License is
+ * also available at http://www.gnu.org/copyleft/gpl.html.
+ */
+
+#include "host_transport.h"
+
+void
+HostTransport::addHostTransport(HostTransport* nextTransport)
+{
+ if(!_nextTransport)
+ {
+ _nextTransport = nextTransport;
+ }
+ else
+ {
+ this->_nextTransport->addHostTransport(nextTransport);
+ }
+}
diff --git a/src/host_transport.h b/src/host_transport.h
new file mode 100644
index 0000000..a4019ad
--- /dev/null
+++ b/src/host_transport.h
@@ -0,0 +1,37 @@
+#ifndef __HOST_TRANSPORT_H
+#define __HOST_TRANSPORT_H
+
+/* host_transport.h - Copyright (C) 2009 Red Hat, Inc.
+ * Written by Arjun Roy <arroy at redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA. A copy of the GNU General Public License is
+ * also available at http://www.gnu.org/copyleft/gpl.html.
+ */
+
+/*
+ HostTransport represents a type that handles transporting data to and from
+ an instance of Host.
+ */
+class HostTransport
+{
+ private:
+ HostTransport* _nextTransport;
+
+ public:
+ // Sets a reference to the next listener in a chain.
+ void addHostTransport(HostTransport* next);
+};
+
+#endif
diff --git a/src/hostimpl.cpp b/src/hostimpl.cpp
new file mode 100644
index 0000000..bb976b7
--- /dev/null
+++ b/src/hostimpl.cpp
@@ -0,0 +1,135 @@
+/* hostimpl.cpp - Copyright (C) 2009 Red Hat, Inc.
+ * Written by Arjun Roy <arroy at redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA. A copy of the GNU General Public License is
+ * also available at http://www.gnu.org/copyleft/gpl.html.
+ */
+
+#include <fstream>
+#include <string>
+
+#include <libvirt/libvirt.h>
+#include <sys/sysinfo.h>
+#include <sys/utsname.h>
+
+#include "hostimpl.h"
+#include "platform.h"
+
+using namespace std;
+
+HostImpl::HostImpl()
+{
+ struct utsname details;
+ this->set_uuid(string("Unknown"));
+ this->set_hostname(string("Unknown"));
+ this->set_hypervisor(string("Unknown"));
+ this->set_architecture(string("None"));
+ this->set_memory(0);
+ this->set_beeping(false);
+
+ std::ifstream input("/var/lib/dbus/machine-id");
+
+ if(input.is_open())
+ {
+ string uuid;
+
+ getline(input, uuid);
+ input.close();
+ this->set_uuid(uuid);
+ }
+
+ if(!uname(&details))
+ {
+ this->set_hostname(string(details.nodename));
+ this->set_architecture(string(details.machine));
+ }
+ else
+ {
+ throw runtime_error("Unable to retrieve system details");
+ }
+
+ virConnectPtr lvconn = virConnectOpenReadOnly(NULL);
+
+ if(lvconn)
+ {
+ this->set_hypervisor(string(virConnectGetType(lvconn)));
+ virConnectClose(lvconn);
+ }
+
+ struct sysinfo sysinf;
+ if(!sysinfo(&sysinf))
+ {
+ this->set_memory(sysinf.totalram / 1024L);
+ }
+ else
+ {
+ throw runtime_error("Unable to retrieve system memory details.");
+ }
+}
+
+void
+HostImpl::setup(ManagementAgent* agent, HostAgent* host)
+{
+ // discover the aspects of the host
+ processors.setup(agent, host);
+ networkdevices = Platform::instance()->get_network_devices();
+
+ for(vector<NetworkDeviceAgent>::iterator iter = networkdevices.begin();
+ iter != networkdevices.end();
+ iter++)
+ {
+ iter->setup(agent, host);
+ }
+}
+
+void
+HostImpl::update()
+{
+ processors.update();
+
+ for(vector<NetworkDeviceAgent>::iterator iter = networkdevices.begin();
+ iter != networkdevices.end();
+ iter++)
+ {
+ iter->update();
+ }
+}
+
+void
+HostImpl::identify(const int iterations)
+{
+}
+
+void
+HostImpl:: subscribe_to_heartbeat(const HeartbeatListener& listener)
+{
+}
+
+void
+HostImpl::unsubscribe_from_heartbeat(const HeartbeatListener& listener)
+{
+}
+
+void
+HostImpl::shutdown()
+{
+ // TODO implement an actual shutdown function
+}
+
+void
+HostImpl::reboot()
+{
+ // TODO implement an actual reboot function
+}
diff --git a/src/hostimpl.h b/src/hostimpl.h
new file mode 100644
index 0000000..f5a4e47
--- /dev/null
+++ b/src/hostimpl.h
@@ -0,0 +1,47 @@
+#ifndef __HOSTIMPL_H
+#define __HOSTIMPL_H
+
+/* hostimpl.h - Copyright (C) 2009 Red Hat, Inc.
+ * Written by Arjun Roy <arroy at redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA. A copy of the GNU General Public License is
+ * also available at http://www.gnu.org/copyleft/gpl.html.
+ */
+
+#include "host.h"
+#include "qmf/hostagent.h"
+#include "processors.h"
+#include "networkdevice.h"
+
+class HostImpl : public Host
+{
+ private:
+ ProcessorsAgent processors;
+ vector<NetworkDeviceAgent> networkdevices;
+ public:
+ HostImpl();
+ virtual ~HostImpl() {}
+
+ void setup(ManagementAgent* agent, HostAgent* host);
+ void update();
+
+ virtual void identify(const int iterations);
+ virtual void subscribe_to_heartbeat(const HeartbeatListener& listener);
+ virtual void unsubscribe_from_heartbeat(const HeartbeatListener& listener);
+ virtual void shutdown();
+ virtual void reboot();
+};
+
+#endif
diff --git a/src/main.cpp b/src/main.cpp
index 5ab3ac2..05b5ad6 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -32,6 +32,8 @@
#include <getopt.h>
#include "host.h"
+#include "hostimpl.h"
+
#include "qmf/com/redhat/matahari/Package.h"
using namespace qpid::management;
@@ -76,7 +78,8 @@ main(int argc, char **argv)
ConnectionSettings settings;
ManagementAgent *agent;
- HostAgent host;
+ HostImpl host;
+ HostAgent hostAgent(&host);
struct option opt[] = {
{"help", no_argument, NULL, 'h'},
@@ -178,7 +181,8 @@ main(int argc, char **argv)
agent->init(settings, 5, false, ".magentdata");
// Get the info and post it to the broker
- host.setup(agent);
+ hostAgent.setup(agent);
+ host.setup(agent, &hostAgent);
while(1)
{
diff --git a/src/qmf/hostagent.cpp b/src/qmf/hostagent.cpp
new file mode 100644
index 0000000..644679f
--- /dev/null
+++ b/src/qmf/hostagent.cpp
@@ -0,0 +1,87 @@
+/* hostagent.cpp - Copyright (C) 2009 Red Hat, Inc.
+ * Written by Arjun Roy <arroy at redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA. A copy of the GNU General Public License is
+ * also available at http://www.gnu.org/copyleft/gpl.html.
+ */
+
+#include "hostagent.h"
+#include <qpid/agent/ManagementAgent.h>
+
+namespace _qmf = qmf::com::redhat::matahari;
+
+HostAgent::HostAgent(Host* host)
+ :_host(host)
+{}
+
+void
+HostAgent::setup(ManagementAgent* agent)
+{
+ _management_object = new _qmf::Host(agent, this);
+ agent->addObject(_management_object);
+
+ _management_object->set_uuid(_host->get_uuid());
+ _management_object->set_hostname(_host->get_hostname());
+ _management_object->set_hypervisor(_host->get_hypervisor());
+ _management_object->set_arch(_host->get_architecture());
+ _management_object->set_memory(_host->get_memory());
+ _management_object->set_beeping(_host->is_beeping());
+}
+
+Manageable::status_t
+HostAgent::ManagementMethod(uint32_t method, Args& arguments, string& text)
+{
+ switch(method)
+ {
+ case _qmf::Host::METHOD_SHUTDOWN:
+ _host->shutdown();
+ return Manageable::STATUS_OK;
+ case _qmf::Host::METHOD_REBOOT:
+ _host->reboot();
+ return Manageable::STATUS_OK;
+ }
+
+ return Manageable::STATUS_NOT_IMPLEMENTED;
+}
+
+void
+HostAgent::identify(const int iterations)
+{
+ // TODO
+}
+
+void
+HostAgent::subscribe_to_heartbeat(const HeartbeatListener& listener)
+{
+ // TODO
+}
+
+void
+HostAgent::unsubscribe_from_heartbeat(const HeartbeatListener& listener)
+{
+ // TODO
+}
+
+void
+HostAgent::shutdown()
+{
+ _host->shutdown();
+}
+
+void
+HostAgent::reboot()
+{
+ _host->reboot();
+}
diff --git a/src/qmf/hostagent.h b/src/qmf/hostagent.h
new file mode 100644
index 0000000..1262e42
--- /dev/null
+++ b/src/qmf/hostagent.h
@@ -0,0 +1,55 @@
+#ifndef __HOSTAGENT_H
+#define __HOSTAGENT_H
+
+/* hostagent.h - Copyright (C) 2009 Red Hat, Inc.
+ * Written by Arjun Roy <arroy at redhat.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA. A copy of the GNU General Public License is
+ * also available at http://www.gnu.org/copyleft/gpl.html.
+ */
+
+#include <qpid/management/Manageable.h>
+
+#include "host.h"
+#include "host_transport.h"
+
+#include "qmf/com/redhat/matahari/Host.h"
+
+using namespace qpid::management;
+
+class HostAgent : public Manageable, public Host, public HostTransport, public HeartbeatListener
+{
+ private:
+ qmf::com::redhat::matahari::Host* _management_object;
+ Host* _host;
+
+ public:
+ HostAgent(Host* host);
+ virtual ~HostAgent() {}
+
+ void setup(ManagementAgent* agent);
+ ManagementObject* GetManagementObject() const { return _management_object; }
+ status_t ManagementMethod(uint32_t method, Args& arguments, string& text);
+
+ virtual void identify(const int iterations);
+ virtual void subscribe_to_heartbeat(const HeartbeatListener& listener);
+ virtual void unsubscribe_from_heartbeat(const HeartbeatListener& listener);
+ virtual void shutdown();
+ virtual void reboot();
+
+ virtual void heartbeat() {}
+};
+
+#endif
--
1.6.6.1
More information about the ovirt-devel
mailing list