summaryrefslogtreecommitdiff
path: root/project2/ice/iceDaemon.cpp
blob: 0f56a33a0c9ad5f9b7304d590c66bcb214a248c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include <pch.hpp>
#include "iceDaemon.h"
#include "buildComms.h"
#include "buildDaemon.h"
#include "iceViewSerializer.h"
#include <scripts.h>
#include <scriptLoader.h>
#include <options.h>
#include <sys/stat.h>
#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <commonObjects.h>
#include <logger.h>
#include <checkHost.h>
#include <views/flatView.h>
#include <taskHost.h>
#include <execContext.h>
#include <exceptions.h>
#include "appInstance.h"

std::string IceDaemon::adapterName;
std::string IceDaemon::adapterEndpoint;
std::string IceDaemon::viewRoot;
std::string IceDaemon::taskRoot;
IceBase::Libs IceDaemon::libs;

class IceDaemonFactory : public DaemonFactory::For<IceDaemon>, public ComponentLoader {
	public:
		void onConfigLoad() override {
			IceBase::FinaliseLoad(IceDaemon::libs);
		}
};

NAMEDPLUGIN("ice", IceDaemonFactory, DaemonFactory);
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::functions(boost::bind(&IceBase::SetSlice, boost::ref(IceDaemon::libs), &IceDaemon::GetComponentCompiler, _1, true, true), boost::bind(&IceDaemon::ClearSlice)),
 "The ICE Slice file(s) to compile")
("ice.daemon.slicedaemon", Options::functions(boost::bind(&IceDaemon::SetSlice, boost::ref(IceDaemon::libs), &IceDaemon::GetComponentCompiler, _1, false, true), boost::bind(&IceDaemon::ClearSlice)),
 "The ICE Slice file(s) to compile (daemon only, assumes comms library referenced)")
("ice.daemon.slicerdaemon", Options::functions(boost::bind(&IceDaemon::SetSlice, boost::ref(IceDaemon::libs), &IceDaemon::GetComponentCompiler, _1, false, false), boost::bind(&IceDaemon::ClearSlice)),
 "The ICE Slice file(s) to compile (daemon only, assumes comms library referenced and built with Slicer support)")
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::setup() const
{
	Logger()->messagebf(LOG_DEBUG, "  %s creating adapter %s [%s]...", __PRETTY_FUNCTION__, adapterName, adapterEndpoint);
	Ice::ObjectAdapterPtr adapter = ic->createObjectAdapterWithEndpoints(adapterName, adapterEndpoint);

	Logger()->messagebf(LOG_DEBUG, "  %s installing servants...", __PRETTY_FUNCTION__);
	for(auto p : AdHoc::PluginManager::getDefault()->getAll<IceDaemonAdapterHandlerFactory>()) {
		p->implementation()->create(this)->add(adapter, ic);
	}

	Logger()->messagebf(LOG_DEBUG, "  %s starting...", __PRETTY_FUNCTION__);
	adapter->activate();
}


void
IceDaemon::run() const
{
	Logger()->messagebf(LOG_INFO, "  %s running...", __PRETTY_FUNCTION__);
	ic->waitForShutdown();

	Logger()->messagebf(LOG_INFO, "  %s stopped", __PRETTY_FUNCTION__);
}

class IceDaemonViewHost : public virtual CommonObjects, public virtual CheckHost {
	public:
		IceDaemonViewHost(ScriptNodePtr s) :
			CommonObjects(s),
			CheckHost(s)
		{
			s->script->loader.addLoadTarget(s, Storer::into<ElementLoader>(&view));
		}
		void executeView(RowSetPresenterPtr presenter, ExecContext * ec) const
		{
			loadScriptComponents();
			Logger()->messagebf(LOG_DEBUG, "%s: run %d checks", __PRETTY_FUNCTION__, CheckHost::checks.size());
			runChecks(ec);
			Logger()->messagebf(LOG_DEBUG, "%s: execute view", __PRETTY_FUNCTION__);
			view->execute(presenter.get(), ec);
			// Caches might open transactions
			for (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 AdHoc::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, Slicer::ModelPartPtr p, const ParamMap & pm) const
{
	ScriptNodePtr s = ScriptReader::resolveScript(IceDaemon::viewRoot, name, false)->root();
	boost::intrusive_ptr<IceDaemonViewHost> v = new IceDaemonViewHost(s);
	IceCallContext icc(pm);
	v->executeView(new IceViewSerializer(p), &icc);
}

class IceDaemonTaskHost : public TaskHost {
	public:
		IceDaemonTaskHost(ScriptNodePtr s) :
			SourceObject(s),
			CommonObjects(s),
			CheckHost(s),
			TaskHost(s)
		{
		}
		void executeTask(ExecContext * ec) const
		{
			Logger()->messagebf(LOG_DEBUG, "%s: run %d checks", __PRETTY_FUNCTION__, CheckHost::checks.size());
			runChecks(ec);
			Logger()->messagebf(LOG_DEBUG, "%s: execute %d tasks", __PRETTY_FUNCTION__, TaskHost::tasks.size());
			execute(ec);
		}
};

void
IceDaemon::executeTask(const std::string & name, const ParamMap & pm) const
{
	ScriptNodePtr s = ScriptReader::resolveScript(IceDaemon::taskRoot, name, false)->root();
	boost::intrusive_ptr<IceDaemonTaskHost> t = new IceDaemonTaskHost(s);
	IceCallContext icc(pm);
	t->executeTask(&icc);
}

IceCompile::Ptr
IceDaemon::GetComponentCompiler(const std::string & slice, const IceCompile::Deps & deps)
{
	return IceCompile::Ptr(new BuildDaemon(slice, deps));
}

void
IceDaemon::ClearSlice()
{
	AppInstance::current().clearScriptCache();
	libs.clear();
}