From bcf701b6d47bb0ae8f4864394508097775a2fb04 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 23 Aug 2015 03:10:49 +0100 Subject: Basic daemon implementation to host a Project2 daemon inside IceBox with unit test that (hopefully) emulates IceBox itself sufficiently closely --- project2/Jamfile.jam | 1 + project2/ice/Jamfile.jam | 24 +++++++++- project2/ice/iceboxDaemon.cpp | 72 +++++++++++++++++++++++++++++ project2/ice/unittests/Jamfile.jam | 8 ++++ project2/ice/unittests/testIceBoxDaemon.cpp | 58 +++++++++++++++++++++++ 5 files changed, 161 insertions(+), 2 deletions(-) create mode 100644 project2/ice/iceboxDaemon.cpp create mode 100644 project2/ice/unittests/testIceBoxDaemon.cpp diff --git a/project2/Jamfile.jam b/project2/Jamfile.jam index 7197c4b..8de52d5 100644 --- a/project2/Jamfile.jam +++ b/project2/Jamfile.jam @@ -25,6 +25,7 @@ alias p2daemonparts : : : : build-project console ; build-project cgi ; build-project daemon ; +build-project ice ; # Ensure tests are run (final targets don't reference projects, but specific libraries) build-project common//unittests ; diff --git a/project2/ice/Jamfile.jam b/project2/ice/Jamfile.jam index 9677e15..fc2b970 100644 --- a/project2/ice/Jamfile.jam +++ b/project2/ice/Jamfile.jam @@ -6,6 +6,7 @@ alias glibmm : : : : lib dl ; lib Slice ; lib Ice ; +lib IceBox ; lib IceUtil ; lib pthread ; lib boost_filesystem ; @@ -16,8 +17,8 @@ build-project unittests ; cpp-pch pch : pch.hpp : ..//adhocutil ../common//p2common - glibmm - slicer + glibmm + slicer ; lib p2iceclient : @@ -60,6 +61,25 @@ lib p2icedaemon : . ; +lib p2icebox : + pch iceboxDaemon.cpp + : + ../../libmisc + glibmm + ../daemon/lib//p2daemonlib + ..//p2parts + dl + Ice + IceBox + IceUtil + : : + ../daemon/lib//p2daemonlib + Ice + IceBox + IceUtil + . + ; + lib p2ice : pch iceConvert.cpp iceCompile.cpp sliceCompile.cpp buildComms.cpp slice2Common.cpp iceBase.cpp : diff --git a/project2/ice/iceboxDaemon.cpp b/project2/ice/iceboxDaemon.cpp new file mode 100644 index 0000000..ad7849a --- /dev/null +++ b/project2/ice/iceboxDaemon.cpp @@ -0,0 +1,72 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +class IceBoxDaemon : public IceBox::Service, public AppInstance { + public: + IceBoxDaemon() + { + Plugable::onAllComponents(boost::bind(&ComponentLoader::onBegin, _1)); + } + + ~IceBoxDaemon() + { + Plugable::onAllComponents(boost::bind(&ComponentLoader::onIdle, _1)); + } + + void start(const std::string &, const Ice::CommunicatorPtr & ic, const Ice::StringSeq &) + { + OptionsSource::loadSources([this] { return reqPlatform;} ); + int argc = 0; + char ** argv = nullptr; + ic->getLogger()->print("Creating daemon: " + daemonType); + daemon = DaemonLoader::createNew(daemonType, argc, argv); + daemon->setup(); + daemonThread = new std::thread(&Daemon::run, daemon); + } + + void stop() + { + daemon->shutdown(); + daemonThread->join(); + delete daemonThread; + daemonThread = nullptr; + daemon->teardown(); + daemon.reset(); + } + + INITOPTIONS; + static Glib::ustring daemonType; + static Glib::ustring reqPlatform; + static int periodicTimeout; + + private: + DaemonPtr daemon; + std::thread * daemonThread; +}; + +Glib::ustring IceBoxDaemon::daemonType; +Glib::ustring IceBoxDaemon::reqPlatform; +int IceBoxDaemon::periodicTimeout; + +DECLARE_OPTIONS(IceBoxDaemon, "Project2 IceBox options") +("daemon.type", Options::value(&daemonType), "The core daemon module to run") +("daemon.platform", Options::value(&reqPlatform), "Platform") +("daemon.periodicTimeout", Options::value(&periodicTimeout, 60), + "Delay between occurrences of component periodic calls (default 60s)") +END_OPTIONS(IceBoxDaemon); + + +extern "C" { + IceBox::Service * + createProject2Daemon(Ice::CommunicatorPtr) + { + return new IceBoxDaemon(); + } +} + diff --git a/project2/ice/unittests/Jamfile.jam b/project2/ice/unittests/Jamfile.jam index fe4ce51..8b7e766 100644 --- a/project2/ice/unittests/Jamfile.jam +++ b/project2/ice/unittests/Jamfile.jam @@ -120,3 +120,11 @@ run testDaemon : ; +run + testIceBoxDaemon.cpp + : : : + ..//p2icebox + ../../ut//p2ut + : + testIceBoxDaemon + ; diff --git a/project2/ice/unittests/testIceBoxDaemon.cpp b/project2/ice/unittests/testIceBoxDaemon.cpp new file mode 100644 index 0000000..b6a2dd5 --- /dev/null +++ b/project2/ice/unittests/testIceBoxDaemon.cpp @@ -0,0 +1,58 @@ +#define BOOST_TEST_MODULE IceBoxDaemon +#include +#include +#include +#include +#include + +extern "C" { + IceBox::Service * createProject2Daemon(Ice::CommunicatorPtr); +} + +BOOST_AUTO_TEST_CASE( test_icebox_daemon ) +{ + Ice::StringSeq adminOpts; + Ice::CommunicatorPtr adminComm = Ice::initialize(adminOpts); + BOOST_REQUIRE(adminComm); + + IceBox::Service * service = createProject2Daemon(adminComm); + BOOST_REQUIRE(service); + + Ice::StringSeq serviceOpts; + Ice::CommunicatorPtr serviceComm = Ice::initialize(serviceOpts); + BOOST_REQUIRE(serviceComm); + + service->start("test_icebox_daemon", serviceComm, { }); + + service->stop(); + + delete service; + + serviceComm->destroy(); + adminComm->destroy(); +} + +class TestDaemon : public Daemon { + public: + TestDaemon(int &, char **) : + stop(false) + { + } + + void run() const + { + while (!stop) sleep(1); + } + + void shutdown() const + { + stop = true; + } + + private: + mutable bool stop; +}; +// DECLARE_COMPONENT_LOADER("TestDaemon", TestDaemon, DaemonLoader); +// This doesn't have a name yet because there's no current way to inject settings for daemon.type +DECLARE_COMPONENT_LOADER("", TestDaemon, DaemonLoader); + -- cgit v1.2.3