summaryrefslogtreecommitdiffstats
path: root/apps/w3c-mmi/im/MMISessionManager.cpp
blob: 2e68ff4ad151458d55aeeaed4ec289c7fa80dcfd (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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "MMISessionManager.h"
#include <uscxml/NameSpacingParser.h>
#include <uscxml/concurrency/tinythread.h>
#include <uscxml/UUID.h>

#include <io/uri.hpp>
#include <glog/logging.h>

namespace uscxml {

using namespace Arabica::DOM;

MMISessionManager::MMISessionManager(Interpreter interpreter) : _protoInterpreter(interpreter) {
	bool success = HTTPServer::registerServlet(interpreter.getName(), this);
	assert(success);
	_factory = new Factory(Factory::getInstance());
	_factory->registerIOProcessor(new MMIIOProcessor(this));
}

MMISessionManager::~MMISessionManager() {
	HTTPServer* httpServer = HTTPServer::getInstance();
	httpServer->unregisterServlet(this);
}

void MMISessionManager::setupHTMLClient(const HTTPServer::Request& req) {
	// open template file
	HTTPServer::Reply reply(req);
	URL templateURL(_protoInterpreter.getBaseURI().asString() + "/templates/mc-html.html");
	templateURL.download(true);
	std::string templateContent = templateURL.getInContent();
	boost::replace_all(templateContent, "${im.url}", _url);
	reply.content = templateContent;
	HTTPServer::reply(reply);
}

bool MMISessionManager::httpRecvRequest(const HTTPServer::Request& req) {
	// is this an initial request from an HTML MC?
	if (!req.data["query"]["token"] && // no token in query
	        !req.data["query"]["context"] && // no context in query
	        boost::iequals(req.data["type"].atom, "get") && // request type is GET
	        boost::icontains(req.data["header"]["Accept"].atom, "text/html") && // accepts html
	        req.content.length() == 0) { // no content
		setupHTMLClient(req);
		return true;
	}

	// is this a comet longpolling request?
	if (boost::iequals(req.data["type"].atom, "get") &&
	        (req.data["query"]["token"] || req.data["query"]["context"])) {
		std::string token = req.data["query"]["token"].atom;
		if (req.data["query"]["token"]) {
			std::string token = req.data["query"]["token"].atom;
			if (_tokens.find(token) != _tokens.end()) {
				MMISessionManager::CometMMISession* comet = static_cast<MMISessionManager::CometMMISession*>(_tokens[token]);
				comet->longPoll(req);
				return true;
			} else {
				LOG(ERROR) << "No session for given token";
			}
		} else if (req.data["query"]["context"]) {
			std::string context = req.data["query"]["context"].atom;
			if (_sessions.find(context) != _sessions.end()) {
				MMISessionManager::CometMMISession* comet = static_cast<MMISessionManager::CometMMISession*>(_sessions[context]);
				comet->longPoll(req);
				return true;
			} else {
				LOG(ERROR) << "No session for given context";
			}
		}
	}

	// assume that there is an mmi event inside
	Document<std::string> mmiDoc = NameSpacingParser::fromXML(req.data.compound.at("content").atom).getDocument();

	if (!mmiDoc) {
		evhttp_send_error(req.curlReq, 204, NULL);
		return true;
	}

	Node<std::string> mmiEvent = MMIEvent::getEventNode(mmiDoc);
	if (!mmiEvent) {
		evhttp_send_error(req.curlReq, 204, NULL);
		return true;
	}

	switch(MMIEvent::getType(mmiEvent)) {
	case MMIEvent::NEWCONTEXTREQUEST: {
		received(NewContextRequest::fromXML(mmiEvent), req.data["query"]["token"].atom);
		evhttp_send_error(req.curlReq, 204, NULL);
		break;
	}
	case MMIEvent::EXTENSIONNOTIFICATION: {
		received(ExtensionNotification::fromXML(mmiEvent));
		evhttp_send_error(req.curlReq, 204, NULL);
		break;
	}
	case MMIEvent::DONENOTIFICATION: {
		received(DoneNotification::fromXML(mmiEvent));
		evhttp_send_error(req.curlReq, 204, NULL);
		break;
	}
	case MMIEvent::STARTRESPONSE: {
		evhttp_send_error(req.curlReq, 204, NULL);
		break;
	}
	default: {
		LOG(ERROR) << "Unknown MMI Event: " << ATTR(mmiEvent, "localName");
		evhttp_send_error(req.curlReq, 204, NULL);
		break;
	}
	}
	return true;
}

void MMISessionManager::received(const ExtensionNotification& mmiEvent) {
	if(_sessions.find(mmiEvent.context) != _sessions.end()) {
		_sessions[mmiEvent.context]->_interpreter.receive(mmiEvent);
	} else {
		LOG(ERROR) << "No session for given context";
	}
}

void MMISessionManager::received(const DoneNotification& mmiEvent) {
	if(_sessions.find(mmiEvent.context) != _sessions.end()) {
		_sessions[mmiEvent.context]->_interpreter.receive(mmiEvent);
	} else {
		LOG(ERROR) << "No session for given context";
	}
}

void MMISessionManager::received(const NewContextRequest& mmiEvent, const std::string& token) {

	// copy DOM from prototype instance
	Arabica::DOM::DOMImplementation<std::string> domFactory = Arabica::SimpleDOM::DOMImplementation<std::string>::getDOMImplementation();
	Arabica::DOM::Document<std::string> newDOM = domFactory.createDocument("", "", 0);
	newDOM.appendChild(newDOM.importNode(_protoInterpreter.getDocument().getDocumentElement(), true));

	// instantiate new interpreter and name it after the context
	std::string contextId = UUID::getUUID();
	Interpreter interpreter = Interpreter::fromDOM(newDOM);
	interpreter.setFactory(_factory);
	interpreter.setName(contextId);
	interpreter.setNameSpaceInfo(_protoInterpreter.getNameSpaceInfo());
	interpreter.setBaseURI(_protoInterpreter.getBaseURI().asString());

	MMISession* session;

	if (token.length() > 0) {
		session = new MMISessionManager::CometMMISession();
		static_cast<MMISessionManager::CometMMISession*>(session)->_token = token;
		_tokens[token] = session;
	} else {
		// todo handle other cases
		session = new MMISessionManager::CometMMISession();
	}
	session->_interpreter = interpreter;

	// save interpreter
	_sessions[contextId] = session;

	interpreter.start();
	interpreter.receive(mmiEvent);

}

void MMISessionManager::received(const NewContextResponse& mmiEvent, const std::string& token) {
	if (_tokens.find(token) != _tokens.end()) {
		_tokens.erase(token);
	}
}

void MMISessionManager::send(const std::string& name, const SendRequest& req) {
	assert(_sessions.find(name) != _sessions.end());
	_sessions[name]->send(req);
}

void MMISessionManager::CometMMISession::send(const SendRequest& req) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	if (!_longPollingReq) {
		_outQueue.push_back(req);
		return;
	}

	if (req.dom) {
		std::stringstream ss;
		Node<std::string> mmiEvent = MMIEvent::getEventNode(req.dom);
		HTTPServer::Reply reply(_longPollingReq);

		switch (MMIEvent::getType(mmiEvent)) {
		case MMIEvent::NEWCONTEXTRESPONSE: {
			NewContextResponse response = NewContextResponse::fromXML(mmiEvent);
			ss << response.toXML();
			reply.content = ss.str();
			break;
		}
		case MMIEvent::STARTREQUEST: {
			StartRequest request = StartRequest::fromXML(mmiEvent);
			ss << request.toXML();
			reply.content = ss.str();
			break;
		}
		default:
			break;
		}
		reply.headers["Content-Type"] = "application/xml";
		HTTPServer::reply(reply);
		_longPollingReq = HTTPServer::Request();
	}
}

void MMISessionManager::CometMMISession::receive(const Arabica::DOM::Node<std::string>& msg) {

}

void MMISessionManager::CometMMISession::longPoll(const HTTPServer::Request& req) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	if (_longPollingReq)
		evhttp_send_error(_longPollingReq.curlReq, 204, NULL);
	_longPollingReq = req;
	if (!_outQueue.empty()) {
		send(_outQueue.front());
		_outQueue.pop_front();
	}
}

boost::shared_ptr<IOProcessorImpl> MMISessionManager::MMIIOProcessor::create(InterpreterImpl* interpreter) {
	boost::shared_ptr<IOProcessorImpl> ioProc = boost::shared_ptr<IOProcessorImpl>(new MMISessionManager::MMIIOProcessor(_sessionManager));
	return ioProc;
}

Data MMISessionManager::MMIIOProcessor::getDataModelVariables() {
	Data data;
	return data;
}

void MMISessionManager::MMIIOProcessor::send(const SendRequest& req) {
	SendRequest reqCopy(req);

	if (req.dom) {
		Arabica::DOM::Node<std::string> mmiEvent = MMIEvent::getEventNode(req.dom);
		if (!mmiEvent || !mmiEvent.getNodeType() == Node_base::ELEMENT_NODE)
			return;

		Arabica::DOM::Element<std::string> mmiElem = Arabica::DOM::Element<std::string>(mmiEvent);
		switch (MMIEvent::getType(mmiEvent)) {
		case MMIEvent::STARTRESPONSE:
		case MMIEvent::PREPARERESPONSE:
		case MMIEvent::PAUSERESPONSE:
		case MMIEvent::RESUMERESPONSE:
		case MMIEvent::CANCELRESPONSE:
		case MMIEvent::DONENOTIFICATION:
		case MMIEvent::NEWCONTEXTRESPONSE:
		case MMIEvent::CLEARCONTEXTRESPONSE:
		case MMIEvent::STATUSRESPONSE: {
			// all of the above have a status
			if (!mmiElem.hasAttributeNS(MMIEvent::nameSpace, "Status")) {
				mmiElem.setAttributeNS(MMIEvent::nameSpace, "Status", "Success");
			}
		}
		case MMIEvent::PAUSEREQUEST:
		case MMIEvent::RESUMEREQUEST:
		case MMIEvent::CANCELREQUEST:
		case MMIEvent::CLEARCONTEXTREQUEST:
		case MMIEvent::STATUSREQUEST: {
			// all of the above have a context
			if (!mmiElem.hasAttributeNS(MMIEvent::nameSpace, "Context")) {
				mmiElem.setAttributeNS(MMIEvent::nameSpace, "Context", _interpreter->getName());
			}
		}
		default: {
			if (!mmiElem.hasAttributeNS(MMIEvent::nameSpace, "Source")) {
				mmiElem.setAttributeNS(MMIEvent::nameSpace, "Source", _sessionManager->getURL());
			}
			if (!mmiElem.hasAttributeNS(MMIEvent::nameSpace, "Target")) {
				if (boost::starts_with(_interpreter->getCurrentEvent().name, "mmi.")) {
					mmiElem.setAttributeNS(MMIEvent::nameSpace, "Target", _interpreter->getCurrentEvent().origin);
				}
			}
			if (!mmiElem.hasAttributeNS(MMIEvent::nameSpace, "RequestID")) {
				if (boost::starts_with(_interpreter->getCurrentEvent().name, "mmi.")) {
					mmiElem.setAttributeNS(MMIEvent::nameSpace, "RequestID", _interpreter->getCurrentEvent().sendid);
				}
			}
		}
		}

		if (MMIEvent::getType(mmiEvent) == MMIEvent::EXTENSIONNOTIFICATION && !mmiElem.hasAttribute("Name") && req.name.length() > 0) {
			mmiElem.setAttribute("Name", req.name);
		}
		// use session mgr to dispatch to session

		_sessionManager->send(_interpreter->getName(), reqCopy);
	}

}

}