diff options
Diffstat (limited to 'project2/ice/iceDaemon.cpp')
-rw-r--r-- | project2/ice/iceDaemon.cpp | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/project2/ice/iceDaemon.cpp b/project2/ice/iceDaemon.cpp new file mode 100644 index 0000000..01acb36 --- /dev/null +++ b/project2/ice/iceDaemon.cpp @@ -0,0 +1,161 @@ +#include <pch.hpp> +#include "iceDaemon.h" +#include "buildComms.h" +#include "buildShared.h" +#include "buildDaemon.h" +#include <scriptLoader.h> +#include <options.h> +#include <sys/stat.h> +#include <boost/foreach.hpp> +#include <boost/filesystem.hpp> +#include <boost/lexical_cast.hpp> +#include <commonObjects.h> +#include <logger.h> +#include <checkHost.h> +#include <flatView.h> +#include <taskHost.h> +#include <execContext.h> +#include <misc.h> +#include <exceptions.h> + +std::string IceDaemon::adapterName; +std::string IceDaemon::adapterEndpoint; +std::string IceDaemon::interface; +std::string IceDaemon::slice; +std::string IceDaemon::viewRoot; +std::string IceDaemon::taskRoot; + +DECLARE_GENERIC_LOADER("ice", DaemonLoader, IceDaemon); +DECLARE_OPTIONS(IceDaemon, "ICE Daemon Options") +("ice.daemon.viewRoot", Options::value(&viewRoot, "views"), + "The folder in which to find view scripts") +("ice.daemon.taskRoot", Options::value(&taskRoot, "tasks"), + "The folder in which to find task scripts") +("ice.daemon.adapterName", Options::value(&adapterName, "DefaultAdapter"), + "The name of the ICE adapter created") +("ice.daemon.adapterEndpoint", Options::value(&adapterEndpoint), + "The ICE endpoint string for the ICE adapter") +("ice.daemon.slice", Options::value(&slice), + "The ICE Slice file to compile") +("ice.daemon.interface", Options::value(&interface), + "The ICE interface to wrap") +END_OPTIONS(IceDaemon); + +IceDaemon::IceDaemon(int & argc, char ** argv) : + ic(Ice::initialize(argc, argv)) +{ +} + +IceDaemon::~IceDaemon() +{ + ic->destroy(); +} + +void +IceDaemon::shutdown() const +{ + ic->shutdown(); +} + +void +IceDaemon::run() const +{ + Logger()->messagebf(LOG_INFO, ">>> %s compiling slice '%s'...", __PRETTY_FUNCTION__, slice); + + BuildComms bc(slice); + BuildShared bds(slice, { &bc }); + BuildDaemon bd(slice, { &bds }); + bd.Update(); + + auto library = bd.Open(); + + Logger()->messagebf(LOG_INFO, " %s starting...", __PRETTY_FUNCTION__); + Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints(adapterName, adapterEndpoint); + IceDaemonAdapterHandlerPtr interfacePtr = IceDaemonAdapterHandlerLoader::createNew(interface); + interfacePtr->add(adapter, this, ic); + adapter->activate(); + Logger()->messagebf(LOG_INFO, " %s running...", __PRETTY_FUNCTION__); + interfacePtr->remove(adapter, ic); + ic->waitForShutdown(); + Logger()->messagebf(LOG_INFO, " %s stopped...", __PRETTY_FUNCTION__); + + Logger()->messagebf(LOG_INFO, "<<< %s", __PRETTY_FUNCTION__); +} + +class IceDaemonFlatViewHost : public virtual CommonObjects, public virtual CheckHost { + public: + IceDaemonFlatViewHost(ScriptNodePtr s) : + CommonObjects(s), + CheckHost(s) + { + s->script->loader.addLoadTarget(s, Storer::into<ElementLoader>(&view)); + } + void executeView(RowSetPresenterPtr presenter, ExecContext * ec) const + { + loadScriptComponents(); + view->execute(presenter.get(), ec); + // Caches might open transactions + BOOST_FOREACH(const CommonObjects::DataSources::value_type & ds, CommonObjects::datasources) { + ds.second->commit(); + } + } + private: + typedef boost::intrusive_ptr<FlatView> ViewPtr; + ViewPtr view; +}; + +class IceCallContext : public ExecContext { + public: + IceCallContext(const ParamMap & p) : + params(p) + { + } + + VariableType getParameter(const VariableType & key) const + { + return safeMapLookup<ParamNotFound>(params, key); + } + + SessionPtr getSession() const + { + // There's no way to tell clients apart + throw NotSupported(__FUNCTION__); + } + + private: + const ParamMap & params; +}; + +void +IceDaemon::executeView(const std::string & name, RowSetPresenterPtr p, const ParamMap & pm) const +{ + ScriptNodePtr s = ScriptReader::resolveScript(IceDaemon::viewRoot, name, false)->root(); + IceDaemonFlatViewHost f(s); + IceCallContext icc(pm); + f.executeView(p, &icc); +} + +class IceDaemonTaskHost : public TaskHost { + public: + IceDaemonTaskHost(ScriptNodePtr s) : + SourceObject(s), + CheckHost(s), + TaskHost(s) + { + } + void executeTask(ExecContext * ec) const + { + runChecks(ec); + execute(ec); + } +}; + +void +IceDaemon::executeTask(const std::string & name, const ParamMap & pm) const +{ + ScriptNodePtr s = ScriptReader::resolveScript(IceDaemon::taskRoot, name, false)->root(); + IceDaemonTaskHost t(s); + IceCallContext icc(pm); + t.executeTask(&icc); +} + |