summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.cpp
blob: 9ac86217e91ef2542f16d85eb03fb0631dd4865d (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/**
 *  @file
 *  @author     2012-2013 Stefan Radomski (stefan.radomski@cs.tu-darmstadt.de)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#include "USCXMLInvoker.h"

#include "uscxml/config.h"

#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif

#ifdef UNIX
#include <pthread.h>
#endif

namespace uscxml {

// msxml.h should die in a fire for polluting the global namespace
// using namespace XERCESC_NS;

#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool pluginConnect(pluma::Host& host) {
	host.add( new USCXMLInvokerProvider() );
	return true;
}
#endif

USCXMLInvoker::USCXMLInvoker() {
	_parentQueue = EventQueue(std::shared_ptr<ParentQueueImpl>(new ParentQueueImpl(this)));
	_thread = NULL;
	_isActive = false;
	_isStarted = false;
}


USCXMLInvoker::~USCXMLInvoker() {
	stop();
};

void USCXMLInvoker::start() {
	_isStarted = true;
	_thread = new std::thread(USCXMLInvoker::run, this);
}

void USCXMLInvoker::stop() {
	_isStarted = false;
	_isActive = false;

	if (_thread) {
		/**
		 * We cannot join the invoked thread if it is blocking at an external
		 * receive. Cancel will finalize and unblock.
		 */
		_invokedInterpreter.cancel();
		_thread->join();
		delete _thread;
		_thread = NULL;
	}
}

void USCXMLInvoker::deserialize(const Data& encodedState) {
	_invokedInterpreter.deserialize(encodedState["intepreter"]);
}

Data USCXMLInvoker::serialize() {
	Data encodedState;

	std::lock_guard<std::recursive_mutex> lock(_mutex);

	InterpreterState state = USCXML_UNDEF;
	while((state = _invokedInterpreter.getState())) {
		if (state != USCXML_IDLE && state != USCXML_MACROSTEPPED && state != USCXML_FINISHED) {
			_cond.wait(_mutex);
		} else {
			break;
		}
	}

	encodedState["intepreter"] = Data(_invokedInterpreter.serialize());

	return encodedState;
}

void USCXMLInvoker::uninvoke() {
	_isActive = false;
	stop();
}

void USCXMLInvoker::eventFromSCXML(const Event& event) {
	if (_isActive) {
		_invokedInterpreter.receive(event);
	}
}

void USCXMLInvoker::run(void* instance) {
	USCXMLInvoker* INSTANCE = (USCXMLInvoker*)instance;

#ifdef APPLE
	std::string threadName;
	threadName += "uscxml::";
	threadName += (INSTANCE->_invokedInterpreter.getImpl()->_name.size() > 0 ? INSTANCE->_invokedInterpreter.getImpl()->_name : "anon");
	threadName += ".scxml";

	pthread_setname_np(threadName.c_str());
#endif

	InterpreterState state = USCXML_UNDEF;
	while(state != USCXML_FINISHED) {
		std::lock_guard<std::recursive_mutex> lock(INSTANCE->_mutex);
		state = INSTANCE->_invokedInterpreter.step(200);
		INSTANCE->_cond.notify_all();
	}

	if (INSTANCE->_isActive) {
		// we finished on our own and were not cancelled
		Event e;
		e.eventType = Event::PLATFORM;
		e.invokeid = INSTANCE->_invokedInterpreter.getImpl()->getInvokeId();
		e.name = "done.invoke." + e.invokeid;
		INSTANCE->_callbacks->enqueueExternal(e);
	}

	INSTANCE->_isActive = false;
}

std::shared_ptr<InvokerImpl> USCXMLInvoker::create(InterpreterImpl* interpreter) {
	std::shared_ptr<USCXMLInvoker> invoker(new USCXMLInvoker());
	invoker->_callbacks = interpreter;
	return invoker;
}

Data USCXMLInvoker::getDataModelVariables() {
	Data data;
	return data;
}

void USCXMLInvoker::invoke(const std::string& source, const Event& invokeEvent) {
	if (source.length() > 0) {
		_invokedInterpreter = Interpreter::fromURL(source);
	} else if (invokeEvent.data.node) {
		XERCESC_NS::DOMImplementation* implementation = XERCESC_NS::DOMImplementationRegistry::getDOMImplementation(X("core"));
		XERCESC_NS::DOMDocument* document = implementation->createDocument();

		// we need to import the parent - to support xpath test150
		XERCESC_NS::DOMNode* newNode = document->importNode(invokeEvent.data.node, true);
		document->appendChild(newNode);

		// TODO: where do we get the namespace from?
		_invokedInterpreter = Interpreter::fromDocument(document, _callbacks->getBaseURL(), false);
	} else if (invokeEvent.data.atom.size() > 0) {
		// test530 when deserializing
		_invokedInterpreter = Interpreter::fromXML(invokeEvent.data.atom, _callbacks->getBaseURL());

	} else {
		_isActive = false;
		ERROR_PLATFORM_THROW("Cannot invoke nested SCXML interpreter, neither src attribute nor content nor DOM is given");
	}

	if (_invokedInterpreter) {
		_invokedInterpreter.getImpl()->_parentQueue = _parentQueue;
		_invokedInterpreter.getImpl()->_invokeId = invokeEvent.invokeid;
		_invokedInterpreter.getImpl()->_invokeReq = invokeEvent;

		// create new instances from the parent's ActionLanguage
#if 1
		InterpreterImpl* invoked = _invokedInterpreter.getImpl().get();
		invoked->_execContent = _callbacks->_execContent.getImpl()->create(invoked);
		invoked->_delayQueue = _callbacks->_delayQueue.getImplDelayed()->create(invoked);
		invoked->_internalQueue = _callbacks->_internalQueue.getImplBase()->create();
		invoked->_externalQueue = _callbacks->_externalQueue.getImplBase()->create();
		invoked->_microStepper = _callbacks->_microStepper.getImpl()->create(invoked);

		// TODO: setup invokers dom, check datamodel attribute and create new instance from parent if matching?
#endif
		// copy monitors
		std::set<InterpreterMonitor*>::const_iterator monIter = _callbacks->_monitors.begin();
		while(monIter != _callbacks->_monitors.end()) {
			if ((*monIter)->copyToInvokers()) {
				_invokedInterpreter.getImpl()->_monitors.insert(*monIter);
			}
			monIter++;
		}

		/**
		 * test240 assumes that invoke request params will carry over to the datamodel
		 * This is solved by passing the invoke request above
		 */
//		_invokedInterpreter.getImpl()->setInvokeRequest(req);
		_isActive = true;

		// we need to make sure it is at least setup to receive data!
		_invokedInterpreter.getImpl()->init();

		start();

	} else {
		// test 530
		// TODO: Is this the correct thing/place to do?
		Event e("done.invoke." + invokeEvent.invokeid, Event::PLATFORM);
		eventToSCXML(e, USCXML_INVOKER_SCXML_TYPE, _invokeId);
		_isActive = false;
	}
}

void USCXMLInvoker::ParentQueueImpl::enqueue(const Event& event) {
	// test 252
	if (!_invoker->_isActive)
		return;
	Event copy(event); // TODO: can we get around a copy?
	_invoker->eventToSCXML(copy, USCXML_INVOKER_SCXML_TYPE, _invoker->_invokeId);
}

}