As we talked about in the meeting, rather than being embedded in
objects, the logic for things like parsing "/var/lib/dbus/machine-id"
needs to be in a C function that the objects call out to.
On Tue, Jun 1, 2010 at 8:12 PM, Darryl L. Pierce <dpierce(a)redhat.com> wrote:
Since the previous code was very Linux-specific, it had to move out
into
the platform layer to keep the code OS-agnostic.
Signed-off-by: Darryl L. Pierce <dpierce(a)redhat.com>
---
src/host.cpp | 62 +++++-----------------------------------------
src/host.h | 5 ----
src/linux_platform.cpp | 63 +++++++++++++++++++++++++++++++++++++++++++----
src/platform.h | 18 +++++++++++++
4 files changed, 82 insertions(+), 66 deletions(-)
diff --git a/src/host.cpp b/src/host.cpp
index ecaf082..aa44d49 100644
--- a/src/host.cpp
+++ b/src/host.cpp
@@ -29,57 +29,9 @@
using namespace std;
-const string UNKNOWN("Unknow");
-
Host::Host()
- :_uuid(UNKNOWN)
- ,_hostname(UNKNOWN)
- ,_hypervisor(UNKNOWN)
- ,_architecture(UNKNOWN)
- ,_memory(0)
- ,_beeping(false)
- ,_heartbeat_sequence(0)
-{
- struct utsname details;
- std::ifstream input("/var/lib/dbus/machine-id");
-
- if(input.is_open())
- {
- string uuid;
-
- getline(input, uuid);
- input.close();
- this->_uuid = uuid;
- }
-
- if(!uname(&details))
- {
- this->_hostname = string(details.nodename);
- this->_architecture = string(details.machine);
- }
- else
- {
- throw runtime_error("Unable to retrieve system details");
- }
-
- virConnectPtr lvconn = virConnectOpenReadOnly(NULL);
-
- if(lvconn)
- {
- this->_hypervisor = string(virConnectGetType(lvconn));
- virConnectClose(lvconn);
- }
-
- struct sysinfo sysinf;
- if(!sysinfo(&sysinf))
- {
- this->_memory = sysinf.totalram / 1024L;
- }
- else
- {
- throw runtime_error("Unable to retrieve system memory details.");
- }
-}
+ :_heartbeat_sequence(0)
+{ }
void
Host::update()
@@ -120,31 +72,31 @@ Host::removeHostListener(HostListener* listener)
string
Host:: getUUID() const
{
- return _uuid;
+ return Platform::instance()->getUUID();
}
string
Host::getHostname() const
{
- return _hostname;
+ return Platform::instance()->getHostname();
}
string
Host::getHypervisor() const
{
- return _hypervisor;
+ return Platform::instance()->getHypervisor();
}
string
Host::getArchitecture() const
{
- return _architecture;
+ return Platform::instance()->getArchitecture();
}
unsigned int
Host::getMemory() const
{
- return _memory;
+ return Platform::instance()->getMemory();
}
bool
diff --git a/src/host.h b/src/host.h
index ee70843..5a7eb2a 100644
--- a/src/host.h
+++ b/src/host.h
@@ -34,11 +34,6 @@ using namespace std;
class Host
{
private:
- string _uuid;
- string _hostname;
- string _hypervisor;
- string _architecture;
- unsigned int _memory;
bool _beeping;
unsigned int _heartbeat_sequence;
diff --git a/src/linux_platform.cpp b/src/linux_platform.cpp
index b05a327..0f71d23 100644
--- a/src/linux_platform.cpp
+++ b/src/linux_platform.cpp
@@ -21,11 +21,16 @@
#include <iomanip>
#include <iostream>
#include <dirent.h>
+#include <libvirt/libvirt.h>
#include <net/if.h>
#include <pcre.h>
#include <stdexcept>
#include <string.h>
#include <sys/ioctl.h>
+#include <sys/sysinfo.h>
+#include <sys/utsname.h>
+
+using namespace std;
// TODO remove this wrapper once rhbz#583747 is fixed
extern "C" {
@@ -34,10 +39,54 @@ extern "C" {
#include "linux_platform.h"
+using namespace std;
+
LinuxPlatform::LinuxPlatform()
{
+ struct utsname details;
+ ifstream *input;
+
+ input = new ifstream("/var/lib/dbus/machine-id");
+
+ if(input->is_open())
+ {
+ string uuid;
+
+ getline(*input, uuid);
+ input->close();
+ delete input;
+ this->setUUID(uuid);
+ }
+
+ if(!uname(&details))
+ {
+ this->setHostname(string(details.nodename));
+ this->setArchitecture(string(details.machine));
+ }
+ else
+ {
+ throw runtime_error("Unable to retrieve system details");
+ }
+
+ virConnectPtr lvconn = virConnectOpenReadOnly(NULL);
+
+ if(lvconn)
+ {
+ this->setHypervisor(string(virConnectGetType(lvconn)));
+ virConnectClose(lvconn);
+ }
+
+ struct sysinfo sysinf;
+ if(!sysinfo(&sysinf))
+ {
+ this->setMemory(sysinf.totalram / 1024L);
+ }
+ else
+ {
+ throw runtime_error("Unable to retrieve system memory details.");
+ }
+
int cpu_count = 0;
- string model = "unknown";
struct udev* udev = udev_new();
struct udev_enumerate* enumerator = udev_enumerate_new(udev);
@@ -58,8 +107,9 @@ LinuxPlatform::LinuxPlatform()
udev_enumerate_unref(enumerator);
udev_unref(udev);
- ifstream input("/proc/cpuinfo");
- if(input.is_open())
+ input = new ifstream("/proc/cpuinfo");
+
+ if(input->is_open())
{
string regexstr = "(.*\\S)\\s*:\\s*(\\S.*)";
int expected = 3;
@@ -73,11 +123,11 @@ LinuxPlatform::LinuxPlatform()
regex = pcre_compile(regexstr.c_str(), 0, &pcre_error, &pcre_error_offset,
NULL);
if(!regex) { throw runtime_error("Unable to compile regular
expression."); }
- while(!input.eof() && !done)
+ while(!input->eof() && !done)
{
string line;
- getline(input, line);
+ getline(*input, line);
int match = pcre_exec(regex, NULL, line.c_str(), line.length(),
0, PCRE_NOTEMPTY,found, expected * 3);
@@ -104,7 +154,8 @@ LinuxPlatform::LinuxPlatform()
}
}
}
- input.close();
+ input->close();
+ delete input;
}
}
diff --git a/src/platform.h b/src/platform.h
index ba70378..7d6968f 100644
--- a/src/platform.h
+++ b/src/platform.h
@@ -37,6 +37,12 @@ class Platform
private:
static Platform* _instance;
+ string _uuid;
+ string _hostname;
+ string _hypervisor;
+ string _architecture;
+ unsigned int _memory;
+
string _cpu_model;
unsigned int _cpu_cores;
@@ -44,6 +50,12 @@ class Platform
Platform() {}
virtual~ Platform() {}
+ void setUUID(const string uuid) { _uuid = uuid; }
+ void setHostname(const string hostname) { _hostname = hostname; }
+ void setHypervisor(const string hypervisor) { _hypervisor = hypervisor; }
+ void setArchitecture(const string arch) { _architecture = arch; }
+ void setMemory(unsigned int memory) { _memory = memory; }
+
void setCPUModel(const string model) { _cpu_model = model; }
void setNumberOfCPUCores(const int cores) { _cpu_cores = cores; }
@@ -51,6 +63,12 @@ class Platform
// the singleton instance
static Platform* instance();
+ string getUUID() const { return _uuid; }
+ string getHostname() const { return _hostname; }
+ string getHypervisor() const { return _hypervisor; }
+ string getArchitecture() const { return _architecture; }
+ unsigned int getMemory() const { return _memory; }
+
string getCPUModel() const { return _cpu_model; }
unsigned int getNumberOfCPUCores() const { return _cpu_cores; }
--
1.7.0.1
_______________________________________________
Matahari mailing list
Matahari(a)lists.fedorahosted.org
https://fedorahosted.org/mailman/listinfo/matahari