summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/invoker/scxml/USCXMLInvoker.cpp
blob: 62f7a1ecb8998a5fa598944f6e11fef1f7781e6c (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
/**
 *  @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 <glog/logging.h>
#include "uscxml/dom/DOMUtils.h"
#include <DOM/SAX2DOM/SAX2DOM.hpp>

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

namespace uscxml {

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

USCXMLInvoker::USCXMLInvoker() : _cancelled(false) {
	_parentQueue._invoker = this;
}


USCXMLInvoker::~USCXMLInvoker() {
};

void USCXMLInvoker::uninvoke() {
	_cancelled = true;
	Event event;
	event.name = "unblock.and.die";
	if (_invokedInterpreter)
		_invokedInterpreter.receive(event);

}

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

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

void USCXMLInvoker::send(const SendRequest& req) {
	if (_invokedInterpreter)
		_invokedInterpreter.receive(req);
}

void USCXMLInvoker::cancel(const std::string sendId) {
	assert(false);
}

void USCXMLInvoker::invoke(const InvokeRequest& req) {
	_cancelled = false;
	if (req.src.length() > 0) {
		_invokedInterpreter = Interpreter::fromURL(req.src);
	} else if (req.dom) {
		Arabica::DOM::DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
		Arabica::DOM::Document<std::string> dom = domFactory.createDocument(req.dom.getNamespaceURI(), "", 0);
		// we need to import the parent - to support xpath test150
		Arabica::DOM::Node<std::string> newNode = dom.importNode(req.dom, true);
		dom.appendChild(newNode);
		// TODO: where do we get the namespace from?
		_invokedInterpreter = Interpreter::fromDOM(dom, _interpreter->getNameSpaceInfo(), _interpreter->getSourceURL());
	} else if (req.content.size() > 0) {
		_invokedInterpreter = Interpreter::fromXML(req.content, _interpreter->getSourceURL());
	} else {
		LOG(ERROR) << "Cannot invoke nested SCXML interpreter, neither src attribute nor content nor DOM is given";
	}
	if (_invokedInterpreter) {
		if (req.elem && HAS_ATTR(req.elem, "initial")) {
			_invokedInterpreter.setInitalConfiguration(tokenize(ATTR(req.elem, "initial")));
		}

		DataModel dataModel(_invokedInterpreter.getImpl()->getDataModel());
		_invokedInterpreter.getImpl()->setParentQueue(&_parentQueue);

		// 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++;
		}

		// transfer namespace prefixes
		_invokedInterpreter.setNameSpaceInfo(_parentInterpreter->getNameSpaceInfo());
		_invokedInterpreter.getImpl()->_sessionId = req.invokeid;

		/// test240 assumes that invoke request params will carry over to the datamodel
		_invokedInterpreter.getImpl()->setInvokeRequest(req);

		_invokedInterpreter.start();
	} else {
		/// test 530
		_parentInterpreter->receive(Event("done.invoke." + _invokeId, Event::PLATFORM));
	}
}

void USCXMLInvoker::ParentQueue::push(const SendRequest& event) {
	// test 252
	if (_invoker->_cancelled)
		return;
	SendRequest copyEvent(event);
	// this is somewhat hidden here!
	copyEvent.invokeid = _invoker->_invokeId;
	_invoker->_parentInterpreter->receive(copyEvent);
}

}