summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.cpp
blob: 4d3c579b1153ec13d63941a61c6488c44212736c (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
/**
 *  @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;

#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::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) {
		state = INSTANCE->_invokedInterpreter.step(true);

//		if (!INSTANCE->_isStarted) {
//			// we have been cancelled
//			INSTANCE->_isActive = false;
//			return;
//		}
	}

	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->_interpreter->enqueueExternal(e);
	}

	INSTANCE->_isActive = false;
}

std::shared_ptr<InvokerImpl> USCXMLInvoker::create(InterpreterImpl* interpreter) {
	std::shared_ptr<USCXMLInvoker> invoker(new USCXMLInvoker());
	invoker->_interpreter = 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::DOMImplementation* implementation = xercesc::DOMImplementationRegistry::getDOMImplementation(X("core"));
		xercesc::DOMDocument* document = implementation->createDocument();

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

//        std::cout << *document << std::endl;

		// TODO: where do we get the namespace from?
		_invokedInterpreter = Interpreter::fromDocument(document, _interpreter->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;

		// copy monitors
//		std::set<InterpreterMonitor*>::const_iterator monIter = _interpreter->_monitors.begin();
//		while(monIter != _interpreter->_monitors.end()) {
//			if ((*monIter)->copyToInvokers()) {
//				_invokedInterpreter.getImpl()->_monitors.insert(*monIter);
//			}
//			monIter++;
//		}


		/// test240 assumes that invoke request params will carry over to the datamodel
//		_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
		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);
}

}