summaryrefslogtreecommitdiff
path: root/project2/ice/iceDaemon.cpp
blob: 7dcf29887748fd893370380672b1bbbc5d264db3 (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
#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);

	IceCompile::CPtr bc(new BuildComms(slice));
	IceCompile::CPtr bds(new BuildShared(slice, { bc }));
	IceCompile::CPtr bd(new BuildDaemon(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);
}