summaryrefslogtreecommitdiff
path: root/project2/console/consoleAppEngine.cpp
blob: e1267935e5e7f9571fe577653c36035fed33d0ac (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
#include "consoleAppEngine.h"
#include "consoleEnvironment.h"
#include "../iterate.h"
#include "../xmlObjectLoader.h"
#include "../xmlScriptParser.h"
#include <boost/foreach.hpp>

SimpleMessageException(UnknownPlatformAlias);

class ConsoleSession : public Session {
	public:
		ConsoleSession() : expiry(0)
		{
		}
		virtual ~ConsoleSession()
		{
		}

		virtual const VariableType & GetValue(const Glib::ustring & name) const
		{
			Values::const_iterator i = vars.find(name);
			if (i == vars.end()) {
				throw Session::VariableNotFound(name);
			}
			return i->second;
		}
		virtual Values GetValuesCopy() const
		{
			return vars;
		}
		virtual void SetValue(const Glib::ustring & name, const VariableType & value)
		{
			vars[name] = value;
		}
		virtual void ClearValue(const Glib::ustring & name)
		{
			vars.erase(name);
		}
		virtual time_t ExpiryTime() const
		{
			return expiry;
		}

	protected:
		virtual void ExpiryTime(time_t t)
		{
			expiry = t;
		}
	private:
		time_t expiry;
		Values vars;
};

ConsoleApplicationEngine::ConsoleApplicationEngine(const ConsoleEnvironment * env, const boost::filesystem::path & f) :
	ApplicationEngine("console/environment"),
	_env(env),
	indent(0),
	runtime(new ConsoleSession()),
	out(stdout, true)
{
	XmlScriptParser request(f.string(), false);

	LoaderBase loader("http://project2.randomdan.homeip.net", true);
	loader.supportedStorers.insert(Storer::into(&parameterChecks));
	loader.supportedStorers.insert(Storer::into(&tasks));
	loader.supportedStorers.insert(Storer::into(&rowSets));
	loader.supportedStorers.insert(Storer::into(&views));
	loader.collectAll(this, request.get_document()->get_root_node(), true);
}

ConsoleApplicationEngine::~ConsoleApplicationEngine()
{
}

void
ConsoleApplicationEngine::process() const
{
	BOOST_FOREACH(const ParamCheckers::value_type & pc, parameterChecks.get<bySOOrder>()) {
		if (!pc->performCheck()) {
			throw std::runtime_error("Check failed");
		}
	}
	try {
		BOOST_FOREACH(const Tasks::value_type & t, tasks.get<bySOOrder>()) {
			t->execute();
		}
		// Commit data source transactions (without invoking a connection)
		BOOST_FOREACH(const DataSources::value_type & ds, this->datasources) {
			ds->commit();
		}
	}
	catch (...) {
		// Do something about the error
		BOOST_FOREACH(const DataSources::value_type & ds, this->datasources) {
			ds->rollback();
		}
		throw;
	}
	BOOST_FOREACH(const Views::value_type & v, views) {
		v->execute(this);
	}
}

const Environment *
ConsoleApplicationEngine::env() const
{
	return _env;
}

SessionPtr
ConsoleApplicationEngine::session() const
{
	return runtime;
}

void
ConsoleApplicationEngine::addAppData(const Presenter *) const
{
}

void
ConsoleApplicationEngine::pushSub(const Glib::ustring & name, const Glib::ustring & ns) const
{
	fprintf(stdout, "%*s", indent, "");
	fprintf(stdout, ">>> ");
	if (!ns.empty()) {
		fprintf(stdout, "%s::", ns.c_str());
	}
	fprintf(stdout, "%s\n", name.c_str());
	indent += 2;
}

void
ConsoleApplicationEngine::addAttr(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const
{
	fprintf(stdout, "%*s", indent, "");
	if (!ns.empty()) {
		fprintf(stdout, "%s::", ns.c_str());
	}
	fprintf(stdout, "@%s = ", name.c_str());
	boost::apply_visitor(out, value);
	fprintf(stdout, "\n");
}

void
ConsoleApplicationEngine::addField(const Glib::ustring & name, const Glib::ustring & ns, const VariableType & value) const
{
	fprintf(stdout, "%*s", indent, "");
	if (!ns.empty()) {
		fprintf(stdout, "%s::", ns.c_str());
	}
	fprintf(stdout, "%s = ", name.c_str());
	boost::apply_visitor(out, value);
	fprintf(stdout, "\n");
}

void
ConsoleApplicationEngine::setText(const VariableType & value) const
{
	fprintf(stdout, "%*s<local> = ", indent, "");
	boost::apply_visitor(out, value);
	fprintf(stdout, "\n");
}

void
ConsoleApplicationEngine::popSub() const
{
	indent -= 2;
	fprintf(stdout, "%*s<<<\n", indent, "");
}

Glib::ustring
ConsoleApplicationEngine::resolveCurrentConfig() const
{
	ConsolePlatforms::const_iterator i = conplat.find(_env->getPlatform());
	if (i != conplat.end()) {
		return i->second;
	}
	throw UnknownPlatformAlias(_env->getPlatform());
}

void
ConsoleApplicationEngine::loadEngineSection(const xmlpp::Element * e) const
{
	conplat.insert(ConsolePlatforms::value_type(e->get_attribute_value("setting"), e->get_attribute_value("platform")));
}