Deleted the files linux_platform.{cpp,h} and platform.{cpp,h}. All of that functionality was moved into the host.cpp file into conditionally compiled sections.
No Windows code was added, just the Linux code relocated.
Signed-off-by: Darryl L. Pierce dpierce@redhat.com --- configure.ac | 2 +- src/Makefile.am | 4 - src/host.cpp | 220 +++++++++++++++++++++++++++++++++++++++++--- src/linux_platform.cpp | 174 ---------------------------------- src/linux_platform.h | 34 ------- src/platform.cpp | 36 ------- src/platform.h | 77 --------------- src/tests/Makefile.am | 17 +--- src/tests/testhostapis.cpp | 130 -------------------------- src/tests/testhostapis.h | 54 ----------- 10 files changed, 208 insertions(+), 540 deletions(-) delete mode 100644 src/linux_platform.cpp delete mode 100644 src/linux_platform.h delete mode 100644 src/platform.cpp delete mode 100644 src/platform.h delete mode 100644 src/tests/testhostapis.cpp delete mode 100644 src/tests/testhostapis.h
diff --git a/configure.ac b/configure.ac index 9fda954..1dfdd80 100644 --- a/configure.ac +++ b/configure.ac @@ -4,7 +4,7 @@ dnl ---------------------------------------------------------------------
AC_PREREQ([2.65]) AC_INIT([Matahari], [0.1.0], [matahari@lists.fedorahosted.org]) -AC_CONFIG_SRCDIR([src/linux_platform.cpp]) +AC_CONFIG_SRCDIR([src/main.cpp]) AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_HEADERS([src/config.h]) AC_CONFIG_MACRO_DIR([m4]) diff --git a/src/Makefile.am b/src/Makefile.am index 20532b0..947367d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,11 +20,7 @@ matahari_SOURCES = \ host.cpp \ host.h \ hostlistener.h \ - linux_platform.cpp \ - linux_platform.h \ main.cpp \ - platform.cpp \ - platform.h \ qmf/hostagent.cpp \ qmf/hostagent.h \ virtio/multiplexer.cpp \ diff --git a/src/host.cpp b/src/host.cpp index dfb83f5..0b991fc 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -1,5 +1,5 @@ -/* host.cpp - Copyright (C) 2009 Red Hat, Inc. - * Written by Arjun Roy arroy@redhat.com +/* host.cpp - Copyright (C) 2010 Red Hat, Inc. + * Written by Darryl L. Pierce dpierce@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 @@ -18,14 +18,25 @@ */
#include <config.h> +#include <cstdlib> +#include <fstream> #include "host.h" - -#include "platform.h" +#include <libvirt/libvirt.h> +#include <pcre.h> #include <set> +#include <stdexcept> +#include <string> +#include <sys/sysinfo.h> +#include <sys/utsname.h> + +// TODO remove this wrapper once rhbz#583747 is fixed +extern "C" { +#include <libudev.h> +}
using namespace std;
-set<HostListener*> _listeners; +set<HostListener*> _listeners; unsigned int _heartbeat_sequence;
void @@ -53,7 +64,7 @@ host_update_event() iter++) { (*iter)->heartbeat((unsigned long)__time, - _heartbeat_sequence); + _heartbeat_sequence); }
for(set<HostListener*>::iterator iter = _listeners.begin(); @@ -67,49 +78,230 @@ host_update_event() string host_get_uuid() { - return Platform::instance()->getUUID(); + static string uuid; + + if(uuid.empty()) + { + +#ifdef __linux__ + ifstream input("/var/lib/dbus/machine-id"); + + if(input.is_open()) + { + getline(input, uuid); + input.close(); + } +#endif + + } + + return uuid; }
string host_get_hostname() { - return Platform::instance()->getHostname(); + static string hostname; + + if(hostname.empty()) + { + +#ifdef __linux__ + struct utsname details; + + if(!uname(&details)) + { + hostname = string(details.nodename); + } + #endif + + } + + return hostname; }
string host_get_hypervisor() { - return Platform::instance()->getHypervisor(); + static string hypervisor; + + if(hypervisor.empty()) + { + +#ifdef HAVE_LIBVIRT + virConnectPtr lvconn = virConnectOpenReadOnly(NULL); + + if(lvconn) + { + hypervisor = string(virConnectGetType(lvconn)); + virConnectClose(lvconn); + } +#endif + + } + + return hypervisor; }
string host_get_architecture() { - return Platform::instance()->getArchitecture(); + static string architecture; + + if(architecture.empty()) + { + +#ifdef __linux__ + struct utsname details; + + if(!uname(&details)) + { + architecture = string(details.machine); + } +#endif + + } + + return architecture; }
unsigned int host_get_memory() { - return Platform::instance()->getMemory(); + static unsigned int memory = 0; + + if(!memory) + { + +#ifdef __linux__ + struct sysinfo sysinf; + if(!sysinfo(&sysinf)) + { + memory = sysinf.totalram / 1024L; + } +#endif + + } + + return memory; }
string host_get_cpu_model() { - return Platform::instance()->getCPUModel(); + static string model; + + if(model.empty()) + { + +#ifdef __linux__ + ifstream* input = new ifstream("/proc/cpuinfo"); + + if(input->is_open()) + { + string regexstr = "(.*\S)\s*:\s*(\S.*)"; + int expected = 3; + int found[expected * 3]; + const char* pcre_error; + int pcre_error_offset; + pcre* regex; + bool done = false; + bool started = false; + + 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) + { + string line; + + getline(*input, line); + int match = pcre_exec(regex, NULL, line.c_str(), line.length(), + 0, PCRE_NOTEMPTY,found, expected * 3); + + if(match == expected) + { + string name = line.substr(found[2], found[3] - found[2]); + string value = line.substr(found[4], found[5] - found[4]); + + /* If we're at a second processor and we've already started, + then we're done. + */ + if (name == "processor") + { + if (started) + { + done = true; + } + else + { + started = true; + } + } + else + { + if(name == "model name") model = value; + } + } + } + input->close(); + delete input; + } +#endif + + } + + return model; }
unsigned int host_get_number_of_cpu_cores() { - return Platform::instance()->getNumberOfCPUCores(); + unsigned int cores = 0; + + if(!cores) + { + +#ifdef __linux__ + struct udev* udev = udev_new(); + struct udev_enumerate* enumerator = udev_enumerate_new(udev); + + udev_enumerate_add_match_property(enumerator, "DRIVER", "processor"); + if(!udev_enumerate_scan_devices(enumerator)) + { + struct udev_list_entry* entries = udev_enumerate_get_list_entry(enumerator); + struct udev_list_entry* entry; + + udev_list_entry_foreach(entry, entries) + { + cores++; + } + } + + udev_enumerate_unref(enumerator); + udev_unref(udev); +#endif + + } + + return cores; }
double host_get_load_average() { - return Platform::instance()->getLoadAverage(); + double result = 0.0; + +#ifdef __linux__ + double load_average; + ifstream input; + + input.open("/proc/loadavg", ios::in); + input >> result; + input.close(); +#endif + + return result; }
void diff --git a/src/linux_platform.cpp b/src/linux_platform.cpp deleted file mode 100644 index 8b70f37..0000000 --- a/src/linux_platform.cpp +++ /dev/null @@ -1,174 +0,0 @@ -/* linux_platform.cpp - Copyright (C) 2010 Red Hat, Inc. - * Written by Darryl L. Pierce dpierce@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 <config.h> -#include <fstream> -#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" { -#include <libudev.h> -} - -#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; - - struct udev* udev = udev_new(); - struct udev_enumerate* enumerator = udev_enumerate_new(udev); - - udev_enumerate_add_match_property(enumerator, "DRIVER", "processor"); - if(!udev_enumerate_scan_devices(enumerator)) - { - struct udev_list_entry* entries = udev_enumerate_get_list_entry(enumerator); - struct udev_list_entry* entry; - - udev_list_entry_foreach(entry, entries) - { - cpu_count++; - } - setNumberOfCPUCores(cpu_count); - } - - udev_enumerate_unref(enumerator); - udev_unref(udev); - - input = new ifstream("/proc/cpuinfo"); - - if(input->is_open()) - { - string regexstr = "(.*\S)\s*:\s*(\S.*)"; - int expected = 3; - int found[expected * 3]; - const char* pcre_error; - int pcre_error_offset; - pcre* regex; - bool done = false; - bool started = false; - - 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) - { - string line; - - getline(*input, line); - int match = pcre_exec(regex, NULL, line.c_str(), line.length(), - 0, PCRE_NOTEMPTY,found, expected * 3); - - if(match == expected) - { - string name = line.substr(found[2], found[3] - found[2]); - string value = line.substr(found[4], found[5] - found[4]); - - // if we're at a second processor and we've already started, then we're done - if (name == "processor") - { - if (started) - { - done = true; - } - else - { - started = true; - } - } - else - { - if(name == "model name") setCPUModel(value); - } - } - } - input->close(); - delete input; - } -} - -double -LinuxPlatform::getLoadAverage() const -{ - double load_average; - ifstream input; - - input.open("/proc/loadavg", ios::in); - input >> load_average; - input.close(); - - return load_average; -} diff --git a/src/linux_platform.h b/src/linux_platform.h deleted file mode 100644 index de9664a..0000000 --- a/src/linux_platform.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef __LINUX_PLATFORM_H -#define __LINUX_PLATFORM_H - -/* linux_platform.h - Copyright (C) 2010 Red Hat, Inc. - * Written by Darryl L. Pierce dpierce@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 "platform.h" - -class LinuxPlatform : public Platform -{ - public: - LinuxPlatform(); - virtual ~LinuxPlatform() {} - - virtual double getLoadAverage() const; -}; - -#endif diff --git a/src/platform.cpp b/src/platform.cpp deleted file mode 100644 index 087cf2a..0000000 --- a/src/platform.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* platform.cpp - Copyright (C) 2010 Red Hat, Inc. - * Written by Darryl L. Pierce dpierce@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 <config.h> -#include "platform.h" -#include "linux_platform.h" - -Platform* Platform::_instance = 0; - -Platform* -Platform::instance() -{ - // TODO this is where we decide which implementation to use - if(!_instance) - { - _instance = new LinuxPlatform; - } - - return _instance; -} diff --git a/src/platform.h b/src/platform.h deleted file mode 100644 index 7c0eb91..0000000 --- a/src/platform.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef __PLATFORM_H -#define __PLATFORM_H - -/* platform.h - Copyright (C) 2010 Red Hat, Inc. - * Written by Darryl L. Pierce dpierce@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 <string> -#include <vector> - -using namespace std; - -/* - * Platform defines a type that provides platform-specific details. - * - * Implementations provide the specific details needed by the - * various agents at runtime. - */ -class Platform -{ - private: - static Platform* _instance; - - string _uuid; - string _hostname; - string _hypervisor; - string _architecture; - unsigned int _memory; - - string _cpu_model; - unsigned int _cpu_cores; - - protected: - 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; } - - public: - // 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; } - - virtual double getLoadAverage() const = 0; -}; - -#endif diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 13fea3d..49df0c0 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -1,6 +1,5 @@
bin_PROGRAMS = \ - testhostapis \ testmultiplexer
INCLUDES = \ @@ -16,18 +15,4 @@ testmultiplexer_SOURCES = \ testmultiplexer.h
testmultiplexer_LDADD = -lcppunit -testmultiplexer_LDADD += ../../lib/libgnu.a - -testhostapis_SOURCES = \ - $(top_srcdir)/src/host.cpp \ - $(top_srcdir)/src/host.h \ - $(top_srcdir)/src/linux_platform.cpp \ - $(top_srcdir)/src/linux_platform.h \ - $(top_srcdir)/src/platform.cpp \ - $(top_srcdir)/src/platform.h \ - testhostapis.cpp \ - testhostapis.h - -testhostapis_LDFLAGS = -L/usr/local/lib -testhostapis_LDADD = -lcppunit $(LIBVIRT_LIBS) $(PCRE_LIBS) $(UDEV_LIBS) -testhostapis_LDADD += ../../lib/libmatahari.a +testmultiplexer_LDADD += ../../lib/libmatahari.a diff --git a/src/tests/testhostapis.cpp b/src/tests/testhostapis.cpp deleted file mode 100644 index 9710700..0000000 --- a/src/tests/testhostapis.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/* testhostapis.h - Copyright (C) 2010 Red Hat, Inc. - * Written by Darryl L. Pierce dpierce@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 <config.h> -#include "testhostapis.h" - -#include "host.h" -#include <cppunit/TestCaller.h> -#include <cppunit/ui/text/TestRunner.h> - -TestHostApis::TestHostApis() - :TestCase("TestHostApis") -{} - -TestHostApis::~TestHostApis() -{} - -TestSuite* -TestHostApis::suite() -{ - TestSuite* result = new TestSuite("Host APIs"); - - result->addTest(new TestCaller<TestHostApis> - ("testHostGetUUID", - &TestHostApis::testHostGetUUID)); - result->addTest(new TestCaller<TestHostApis> - ("testHostGetHostname", - &TestHostApis::testHostGetHostname)); - result->addTest(new TestCaller<TestHostApis> - ("testHostGetHypervisor", - &TestHostApis::testHostGetHypervisor)); - result->addTest(new TestCaller<TestHostApis> - ("testHostGetArchitecture", - &TestHostApis::testHostGetArchitecture)); - result->addTest(new TestCaller<TestHostApis> - ("testHostGetMemory", - &TestHostApis::testHostGetMemory)); - result->addTest(new TestCaller<TestHostApis> - ("testHostGetCpuModel", - &TestHostApis::testHostGetCpuModel)); - result->addTest(new TestCaller<TestHostApis> - ("testHostGetNumberOfCpuCores", - &TestHostApis::testHostGetNumberOfCpuCores)); - result->addTest(new TestCaller<TestHostApis> - ("testHostGetLoadAverage", - &TestHostApis::testHostGetLoadAverage)); - - return result; -} - -void -TestHostApis::setUp() -{ - this->platform = Platform::instance(); -} - -void -TestHostApis::testHostGetUUID() -{ - CPPUNIT_ASSERT(platform->getUUID() == host_get_uuid()); -} - -void -TestHostApis::testHostGetHostname() -{ - CPPUNIT_ASSERT(platform->getHostname() == host_get_hostname()); -} - -void -TestHostApis::testHostGetHypervisor() -{ - CPPUNIT_ASSERT(platform->getHypervisor() == host_get_hypervisor()); -} - -void -TestHostApis::testHostGetArchitecture() -{ - CPPUNIT_ASSERT(platform->getArchitecture() == host_get_architecture()); -} - -void -TestHostApis::testHostGetMemory() -{ - CPPUNIT_ASSERT(platform->getMemory() == host_get_memory()); -} - -void -TestHostApis::testHostGetCpuModel() -{ - CPPUNIT_ASSERT(platform->getCPUModel() == host_get_cpu_model()); -} - -void -TestHostApis::testHostGetNumberOfCpuCores() -{ - CPPUNIT_ASSERT(platform->getNumberOfCPUCores() == host_get_number_of_cpu_cores()); -} - -void -TestHostApis::testHostGetLoadAverage() -{ - CPPUNIT_ASSERT(platform->getLoadAverage() == host_get_load_average()); -} - -int -main(int argc, char** argv) -{ - TextUi::TestRunner runner; - - runner.addTest(TestHostApis::suite()); - runner.run(); - - return 0; -} diff --git a/src/tests/testhostapis.h b/src/tests/testhostapis.h deleted file mode 100644 index faebb22..0000000 --- a/src/tests/testhostapis.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef __TESTHOSTAPIS_H -#define __TESTHOSTAPIS_H - -/* testhostapis.h - Copyright (C) 2010 Red Hat, Inc. - * Written by Darryl L. Pierce dpierce@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 <cppunit/TestCase.h> -#include <cppunit/TestSuite.h> - -#include "platform.h" - -using namespace CppUnit; - -class TestHostApis : public TestCase -{ - private: - Platform* platform; - - public: - TestHostApis(); - virtual ~TestHostApis(); - - static TestSuite* suite(); - - void setUp(); - - void testHostGetUUID(); - void testHostGetHostname(); - void testHostGetHypervisor(); - void testHostGetArchitecture(); - void testHostGetMemory(); - void testHostGetCpuModel(); - void testHostGetNumberOfCpuCores(); - void testHostGetLoadAverage(); - void testHostIsBeeping(); -}; - -#endif
ACK .This just reverts an earlier patch doesn't it?
On Tue, Jul 6, 2010 at 9:40 PM, Darryl L. Pierce dpierce@redhat.com wrote:
Deleted the files linux_platform.{cpp,h} and platform.{cpp,h}. All of that functionality was moved into the host.cpp file into conditionally compiled sections.
No Windows code was added, just the Linux code relocated.
Signed-off-by: Darryl L. Pierce dpierce@redhat.com
configure.ac | 2 +- src/Makefile.am | 4 - src/host.cpp | 220 +++++++++++++++++++++++++++++++++++++++++--- src/linux_platform.cpp | 174 ---------------------------------- src/linux_platform.h | 34 ------- src/platform.cpp | 36 ------- src/platform.h | 77 --------------- src/tests/Makefile.am | 17 +--- src/tests/testhostapis.cpp | 130 -------------------------- src/tests/testhostapis.h | 54 ----------- 10 files changed, 208 insertions(+), 540 deletions(-) delete mode 100644 src/linux_platform.cpp delete mode 100644 src/linux_platform.h delete mode 100644 src/platform.cpp delete mode 100644 src/platform.h delete mode 100644 src/tests/testhostapis.cpp delete mode 100644 src/tests/testhostapis.h
diff --git a/configure.ac b/configure.ac index 9fda954..1dfdd80 100644 --- a/configure.ac +++ b/configure.ac @@ -4,7 +4,7 @@ dnl ---------------------------------------------------------------------
AC_PREREQ([2.65]) AC_INIT([Matahari], [0.1.0], [matahari@lists.fedorahosted.org]) -AC_CONFIG_SRCDIR([src/linux_platform.cpp]) +AC_CONFIG_SRCDIR([src/main.cpp]) AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_HEADERS([src/config.h]) AC_CONFIG_MACRO_DIR([m4]) diff --git a/src/Makefile.am b/src/Makefile.am index 20532b0..947367d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -20,11 +20,7 @@ matahari_SOURCES = \ host.cpp \ host.h \ hostlistener.h \
- linux_platform.cpp \
- linux_platform.h \
main.cpp \
- platform.cpp \
- platform.h \
qmf/hostagent.cpp \ qmf/hostagent.h \ virtio/multiplexer.cpp \ diff --git a/src/host.cpp b/src/host.cpp index dfb83f5..0b991fc 100644 --- a/src/host.cpp +++ b/src/host.cpp @@ -1,5 +1,5 @@ -/* host.cpp - Copyright (C) 2009 Red Hat, Inc.
- Written by Arjun Roy arroy@redhat.com
+/* host.cpp - Copyright (C) 2010 Red Hat, Inc.
- Written by Darryl L. Pierce dpierce@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 @@ -18,14 +18,25 @@ */
#include <config.h> +#include <cstdlib> +#include <fstream> #include "host.h"
-#include "platform.h" +#include <libvirt/libvirt.h> +#include <pcre.h> #include <set> +#include <stdexcept> +#include <string> +#include <sys/sysinfo.h> +#include <sys/utsname.h>
+// TODO remove this wrapper once rhbz#583747 is fixed +extern "C" { +#include <libudev.h> +}
using namespace std;
-set<HostListener*> _listeners; +set<HostListener*> _listeners; unsigned int _heartbeat_sequence;
void @@ -53,7 +64,7 @@ host_update_event() iter++) { (*iter)->heartbeat((unsigned long)__time,
- _heartbeat_sequence);
- _heartbeat_sequence);
}
for(set<HostListener*>::iterator iter = _listeners.begin(); @@ -67,49 +78,230 @@ host_update_event() string host_get_uuid() {
- return Platform::instance()->getUUID();
- static string uuid;
- if(uuid.empty())
- {
+#ifdef __linux__
- ifstream input("/var/lib/dbus/machine-id");
- if(input.is_open())
- {
- getline(input, uuid);
- input.close();
- }
+#endif
- }
- return uuid;
}
string host_get_hostname() {
- return Platform::instance()->getHostname();
- static string hostname;
- if(hostname.empty())
- {
+#ifdef __linux__
- struct utsname details;
- if(!uname(&details))
- {
- hostname = string(details.nodename);
- }
- #endif
- }
- return hostname;
}
string host_get_hypervisor() {
- return Platform::instance()->getHypervisor();
- static string hypervisor;
- if(hypervisor.empty())
- {
+#ifdef HAVE_LIBVIRT
- virConnectPtr lvconn = virConnectOpenReadOnly(NULL);
- if(lvconn)
- {
- hypervisor = string(virConnectGetType(lvconn));
- virConnectClose(lvconn);
- }
+#endif
- }
- return hypervisor;
}
string host_get_architecture() {
- return Platform::instance()->getArchitecture();
- static string architecture;
- if(architecture.empty())
- {
+#ifdef __linux__
- struct utsname details;
- if(!uname(&details))
- {
- architecture = string(details.machine);
- }
+#endif
- }
- return architecture;
}
unsigned int host_get_memory() {
- return Platform::instance()->getMemory();
- static unsigned int memory = 0;
- if(!memory)
- {
+#ifdef __linux__
- struct sysinfo sysinf;
- if(!sysinfo(&sysinf))
- {
- memory = sysinf.totalram / 1024L;
- }
+#endif
- }
- return memory;
}
string host_get_cpu_model() {
- return Platform::instance()->getCPUModel();
- static string model;
- if(model.empty())
- {
+#ifdef __linux__
- ifstream* input = new ifstream("/proc/cpuinfo");
- if(input->is_open())
- {
- string regexstr = "(.*\S)\s*:\s*(\S.*)";
- int expected = 3;
- int found[expected * 3];
- const char* pcre_error;
- int pcre_error_offset;
- pcre* regex;
- bool done = false;
- bool started = false;
- 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)
- {
- string line;
- getline(*input, line);
- int match = pcre_exec(regex, NULL, line.c_str(), line.length(),
- 0, PCRE_NOTEMPTY,found, expected * 3);
- if(match == expected)
- {
- string name = line.substr(found[2], found[3] - found[2]);
- string value = line.substr(found[4], found[5] - found[4]);
- /* If we're at a second processor and we've already started,
- then we're done.
- */
- if (name == "processor")
- {
- if (started)
- {
- done = true;
- }
- else
- {
- started = true;
- }
- }
- else
- {
- if(name == "model name") model = value;
- }
- }
- }
- input->close();
- delete input;
- }
+#endif
- }
- return model;
}
unsigned int host_get_number_of_cpu_cores() {
- return Platform::instance()->getNumberOfCPUCores();
- unsigned int cores = 0;
- if(!cores)
- {
+#ifdef __linux__
- struct udev* udev = udev_new();
- struct udev_enumerate* enumerator = udev_enumerate_new(udev);
- udev_enumerate_add_match_property(enumerator, "DRIVER", "processor");
- if(!udev_enumerate_scan_devices(enumerator))
- {
- struct udev_list_entry* entries = udev_enumerate_get_list_entry(enumerator);
- struct udev_list_entry* entry;
- udev_list_entry_foreach(entry, entries)
- {
- cores++;
- }
- }
- udev_enumerate_unref(enumerator);
- udev_unref(udev);
+#endif
- }
- return cores;
}
double host_get_load_average() {
- return Platform::instance()->getLoadAverage();
- double result = 0.0;
+#ifdef __linux__
- double load_average;
- ifstream input;
- input.open("/proc/loadavg", ios::in);
- input >> result;
- input.close();
+#endif
- return result;
}
void diff --git a/src/linux_platform.cpp b/src/linux_platform.cpp deleted file mode 100644 index 8b70f37..0000000 --- a/src/linux_platform.cpp +++ /dev/null @@ -1,174 +0,0 @@ -/* linux_platform.cpp - Copyright (C) 2010 Red Hat, Inc.
- Written by Darryl L. Pierce dpierce@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 <config.h> -#include <fstream> -#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" { -#include <libudev.h> -}
-#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;
- struct udev* udev = udev_new();
- struct udev_enumerate* enumerator = udev_enumerate_new(udev);
- udev_enumerate_add_match_property(enumerator, "DRIVER", "processor");
- if(!udev_enumerate_scan_devices(enumerator))
- {
- struct udev_list_entry* entries = udev_enumerate_get_list_entry(enumerator);
- struct udev_list_entry* entry;
- udev_list_entry_foreach(entry, entries)
- {
- cpu_count++;
- }
- setNumberOfCPUCores(cpu_count);
- }
- udev_enumerate_unref(enumerator);
- udev_unref(udev);
- input = new ifstream("/proc/cpuinfo");
- if(input->is_open())
- {
- string regexstr = "(.*\S)\s*:\s*(\S.*)";
- int expected = 3;
- int found[expected * 3];
- const char* pcre_error;
- int pcre_error_offset;
- pcre* regex;
- bool done = false;
- bool started = false;
- 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)
- {
- string line;
- getline(*input, line);
- int match = pcre_exec(regex, NULL, line.c_str(), line.length(),
- 0, PCRE_NOTEMPTY,found, expected * 3);
- if(match == expected)
- {
- string name = line.substr(found[2], found[3] - found[2]);
- string value = line.substr(found[4], found[5] - found[4]);
- // if we're at a second processor and we've already started, then we're done
- if (name == "processor")
- {
- if (started)
- {
- done = true;
- }
- else
- {
- started = true;
- }
- }
- else
- {
- if(name == "model name") setCPUModel(value);
- }
- }
- }
- input->close();
- delete input;
- }
-}
-double -LinuxPlatform::getLoadAverage() const -{
- double load_average;
- ifstream input;
- input.open("/proc/loadavg", ios::in);
- input >> load_average;
- input.close();
- return load_average;
-} diff --git a/src/linux_platform.h b/src/linux_platform.h deleted file mode 100644 index de9664a..0000000 --- a/src/linux_platform.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef __LINUX_PLATFORM_H -#define __LINUX_PLATFORM_H
-/* linux_platform.h - Copyright (C) 2010 Red Hat, Inc.
- Written by Darryl L. Pierce dpierce@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 "platform.h"
-class LinuxPlatform : public Platform -{
- public:
- LinuxPlatform();
- virtual ~LinuxPlatform() {}
- virtual double getLoadAverage() const;
-};
-#endif diff --git a/src/platform.cpp b/src/platform.cpp deleted file mode 100644 index 087cf2a..0000000 --- a/src/platform.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* platform.cpp - Copyright (C) 2010 Red Hat, Inc.
- Written by Darryl L. Pierce dpierce@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 <config.h> -#include "platform.h" -#include "linux_platform.h"
-Platform* Platform::_instance = 0;
-Platform* -Platform::instance() -{
- // TODO this is where we decide which implementation to use
- if(!_instance)
- {
- _instance = new LinuxPlatform;
- }
- return _instance;
-} diff --git a/src/platform.h b/src/platform.h deleted file mode 100644 index 7c0eb91..0000000 --- a/src/platform.h +++ /dev/null @@ -1,77 +0,0 @@ -#ifndef __PLATFORM_H -#define __PLATFORM_H
-/* platform.h - Copyright (C) 2010 Red Hat, Inc.
- Written by Darryl L. Pierce dpierce@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 <string> -#include <vector>
-using namespace std;
-/*
- Platform defines a type that provides platform-specific details.
- Implementations provide the specific details needed by the
- various agents at runtime.
- */
-class Platform -{
- private:
- static Platform* _instance;
- string _uuid;
- string _hostname;
- string _hypervisor;
- string _architecture;
- unsigned int _memory;
- string _cpu_model;
- unsigned int _cpu_cores;
- protected:
- 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; }
- public:
- // 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; }
- virtual double getLoadAverage() const = 0;
-};
-#endif diff --git a/src/tests/Makefile.am b/src/tests/Makefile.am index 13fea3d..49df0c0 100644 --- a/src/tests/Makefile.am +++ b/src/tests/Makefile.am @@ -1,6 +1,5 @@
bin_PROGRAMS = \
- testhostapis \
testmultiplexer
INCLUDES = \ @@ -16,18 +15,4 @@ testmultiplexer_SOURCES = \ testmultiplexer.h
testmultiplexer_LDADD = -lcppunit -testmultiplexer_LDADD += ../../lib/libgnu.a
-testhostapis_SOURCES = \
- $(top_srcdir)/src/host.cpp \
- $(top_srcdir)/src/host.h \
- $(top_srcdir)/src/linux_platform.cpp \
- $(top_srcdir)/src/linux_platform.h \
- $(top_srcdir)/src/platform.cpp \
- $(top_srcdir)/src/platform.h \
- testhostapis.cpp \
- testhostapis.h
-testhostapis_LDFLAGS = -L/usr/local/lib -testhostapis_LDADD = -lcppunit $(LIBVIRT_LIBS) $(PCRE_LIBS) $(UDEV_LIBS) -testhostapis_LDADD += ../../lib/libmatahari.a +testmultiplexer_LDADD += ../../lib/libmatahari.a diff --git a/src/tests/testhostapis.cpp b/src/tests/testhostapis.cpp deleted file mode 100644 index 9710700..0000000 --- a/src/tests/testhostapis.cpp +++ /dev/null @@ -1,130 +0,0 @@ -/* testhostapis.h - Copyright (C) 2010 Red Hat, Inc.
- Written by Darryl L. Pierce dpierce@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 <config.h> -#include "testhostapis.h"
-#include "host.h" -#include <cppunit/TestCaller.h> -#include <cppunit/ui/text/TestRunner.h>
-TestHostApis::TestHostApis()
- :TestCase("TestHostApis")
-{}
-TestHostApis::~TestHostApis() -{}
-TestSuite* -TestHostApis::suite() -{
- TestSuite* result = new TestSuite("Host APIs");
- result->addTest(new TestCaller<TestHostApis>
- ("testHostGetUUID",
- &TestHostApis::testHostGetUUID));
- result->addTest(new TestCaller<TestHostApis>
- ("testHostGetHostname",
- &TestHostApis::testHostGetHostname));
- result->addTest(new TestCaller<TestHostApis>
- ("testHostGetHypervisor",
- &TestHostApis::testHostGetHypervisor));
- result->addTest(new TestCaller<TestHostApis>
- ("testHostGetArchitecture",
- &TestHostApis::testHostGetArchitecture));
- result->addTest(new TestCaller<TestHostApis>
- ("testHostGetMemory",
- &TestHostApis::testHostGetMemory));
- result->addTest(new TestCaller<TestHostApis>
- ("testHostGetCpuModel",
- &TestHostApis::testHostGetCpuModel));
- result->addTest(new TestCaller<TestHostApis>
- ("testHostGetNumberOfCpuCores",
- &TestHostApis::testHostGetNumberOfCpuCores));
- result->addTest(new TestCaller<TestHostApis>
- ("testHostGetLoadAverage",
- &TestHostApis::testHostGetLoadAverage));
- return result;
-}
-void -TestHostApis::setUp() -{
- this->platform = Platform::instance();
-}
-void -TestHostApis::testHostGetUUID() -{
- CPPUNIT_ASSERT(platform->getUUID() == host_get_uuid());
-}
-void -TestHostApis::testHostGetHostname() -{
- CPPUNIT_ASSERT(platform->getHostname() == host_get_hostname());
-}
-void -TestHostApis::testHostGetHypervisor() -{
- CPPUNIT_ASSERT(platform->getHypervisor() == host_get_hypervisor());
-}
-void -TestHostApis::testHostGetArchitecture() -{
- CPPUNIT_ASSERT(platform->getArchitecture() == host_get_architecture());
-}
-void -TestHostApis::testHostGetMemory() -{
- CPPUNIT_ASSERT(platform->getMemory() == host_get_memory());
-}
-void -TestHostApis::testHostGetCpuModel() -{
- CPPUNIT_ASSERT(platform->getCPUModel() == host_get_cpu_model());
-}
-void -TestHostApis::testHostGetNumberOfCpuCores() -{
- CPPUNIT_ASSERT(platform->getNumberOfCPUCores() == host_get_number_of_cpu_cores());
-}
-void -TestHostApis::testHostGetLoadAverage() -{
- CPPUNIT_ASSERT(platform->getLoadAverage() == host_get_load_average());
-}
-int -main(int argc, char** argv) -{
- TextUi::TestRunner runner;
- runner.addTest(TestHostApis::suite());
- runner.run();
- return 0;
-} diff --git a/src/tests/testhostapis.h b/src/tests/testhostapis.h deleted file mode 100644 index faebb22..0000000 --- a/src/tests/testhostapis.h +++ /dev/null @@ -1,54 +0,0 @@ -#ifndef __TESTHOSTAPIS_H -#define __TESTHOSTAPIS_H
-/* testhostapis.h - Copyright (C) 2010 Red Hat, Inc.
- Written by Darryl L. Pierce dpierce@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 <cppunit/TestCase.h> -#include <cppunit/TestSuite.h>
-#include "platform.h"
-using namespace CppUnit;
-class TestHostApis : public TestCase -{
- private:
- Platform* platform;
- public:
- TestHostApis();
- virtual ~TestHostApis();
- static TestSuite* suite();
- void setUp();
- void testHostGetUUID();
- void testHostGetHostname();
- void testHostGetHypervisor();
- void testHostGetArchitecture();
- void testHostGetMemory();
- void testHostGetCpuModel();
- void testHostGetNumberOfCpuCores();
- void testHostGetLoadAverage();
- void testHostIsBeeping();
-};
-#endif
1.7.0.1
Matahari mailing list Matahari@lists.fedorahosted.org https://fedorahosted.org/mailman/listinfo/matahari
On Tue, Jul 13, 2010 at 10:19:08AM +0200, Andrew Beekhof wrote:
ACK .This just reverts an earlier patch doesn't it?
Sort of. For the POC work similar code was done to eliminate the C++ classes.
On Tue, Jul 13, 2010 at 10:19:08AM +0200, Andrew Beekhof wrote:
ACK .This just reverts an earlier patch doesn't it?
This is now pushed upstream.
matahari@lists.fedorahosted.org