summaryrefslogtreecommitdiffstats
path: root/src/uscxml/debug/DebuggerServlet.cpp
blob: e179b8c2e12984e5fe3f22ca49ce79d72fa2b2f0 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/**
 *  @file
 *  @author     2012-2014 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 "uscxml/debug/DebuggerServlet.h"
#include "uscxml/UUID.h"
#include <boost/algorithm/string.hpp>

namespace uscxml {

void DebuggerServlet::pushData(Data pushData) {
	std::cout << "trying to push " << pushData["replyType"].atom << std::endl;
	_sendQueue.push(pushData);
	serverPushData();
}

void DebuggerServlet::serverPushData() {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	
	if (_sendQueue.isEmpty())
		return;
	
	if (!_clientConn)
		return;
	
	Data reply = _sendQueue.pop();
	std::cout << "pushing " << reply["replyType"].atom << std::endl;
	returnData(_clientConn, reply);
	_clientConn = HTTPServer::Request();
}

void DebuggerServlet::returnData(const HTTPServer::Request& request, Data replyData) {
	HTTPServer::Reply reply(request);
	
	if (!replyData.hasKey("status")) {
		replyData.compound["status"] = Data("success", Data::VERBATIM);
	}
	
	reply.content = Data::toJSON(replyData);
	reply.headers["Access-Control-Allow-Origin"] = "*";
	reply.headers["Content-Type"] = "application/json";
	HTTPServer::reply(reply);
}
	
void DebuggerServlet::hitBreakpoint(const Interpreter& interpreter,
																		Data data) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);

	data.compound["replyType"] = Data("breakpoint", Data::VERBATIM);
	pushData(data);
	
	_resumeCond.wait(_mutex);
	tthread::this_thread::sleep_for(tthread::chrono::milliseconds(200));
}

bool DebuggerServlet::isCORS(const HTTPServer::Request& request) {
	return (request.data["type"].atom == "options" &&
					request.data["header"].hasKey("Origin") &&
					request.data["header"].hasKey("Access-Control-Request-Method"));
}

void DebuggerServlet::handleCORS(const HTTPServer::Request& request) {
	HTTPServer::Reply corsReply(request);
	if (request.data["header"].hasKey("Origin")) {
		corsReply.headers["Access-Control-Allow-Origin"] = request.data["header"]["Origin"].atom;
	} else {
		corsReply.headers["Access-Control-Allow-Origin"] = "*";
	}
	if (request.data["header"].hasKey("Access-Control-Request-Method"))
		corsReply.headers["Access-Control-Allow-Methods"] = request.data["header"]["Access-Control-Request-Method"].atom;
	if (request.data["header"].hasKey("Access-Control-Request-Headers"))
		corsReply.headers["Access-Control-Allow-Headers"] = request.data["header"]["Access-Control-Request-Headers"].atom;
	
	//		std::cout << "CORS!" << std::endl << request << std::endl;
	HTTPServer::reply(corsReply);
}

bool DebuggerServlet::httpRecvRequest(const HTTPServer::Request& request) {
	if (!request.data.hasKey("path"))
		return false; //		returnError(request);
	
	if (isCORS(request)) {
		handleCORS(request);
		return true;
	}
	
	std::cout << request.data["path"] << ": " << request.data["content"] << std::endl;
	
	if (false) {
	} else if (boost::starts_with(request.data["path"].atom, "/poll")) {
		processPoll(request);
	} else if (boost::starts_with(request.data["path"].atom, "/connect")) {
		processConnect(request);
	} else if (boost::starts_with(request.data["path"].atom, "/disconnect")) {
		processDisconnect(request);
	} else if (boost::starts_with(request.data["path"].atom, "/sessions")) {
		processListSessions(request);
	} else if (boost::starts_with(request.data["path"].atom, "/breakpoint/add")) {
		processAddBreakPoint(request);
	} else if (boost::starts_with(request.data["path"].atom, "/breakpoint/remove")) {
		processRemoveBreakPoint(request);
	} else if (boost::starts_with(request.data["path"].atom, "/debug/prepare")) {
		processDebugPrepare(request);
	} else if (boost::starts_with(request.data["path"].atom, "/debug/start")) {
		processDebugStart(request);
	} else if (boost::starts_with(request.data["path"].atom, "/debug/stop")) {
		processDebugStop(request);
	} else if (boost::starts_with(request.data["path"].atom, "/debug/step")) {
		processDebugStep(request);
	} else if (boost::starts_with(request.data["path"].atom, "/debug/pause")) {
		processDebugPause(request);
	} else if (boost::starts_with(request.data["path"].atom, "/debug/resume")) {
		processDebugResume(request);
	} else if (boost::starts_with(request.data["path"].atom, "/debug/eval")) {
		processDebugEval(request);
	}
		
	return true;
}
	
void DebuggerServlet::processPoll(const HTTPServer::Request& request) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	_clientConn = request;
	serverPushData();
}

void DebuggerServlet::processDebugPrepare(const HTTPServer::Request& request) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);

//	std::cout << "clearing all pushes" << std::endl;
//	_sendQueue.clear();

	// this will call the destructor if _interpreter already is set
	_resumeCond.notify_all();
	_interpreter = Interpreter::fromXML(request.data["content"].atom);

	Data replyData;
	if (_interpreter) {
		// register ourself as a monitor
		_interpreter.addMonitor(this);
		replyData.compound["status"] = Data("success", Data::VERBATIM);
	} else {
		replyData.compound["status"] = Data("failure", Data::VERBATIM);
	}
	returnData(request, replyData);
}

void DebuggerServlet::processDebugStart(const HTTPServer::Request& request) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	
	Data replyData;
	if (_interpreter) {
		// register ourself as a monitor
		_interpreter.start();
		replyData.compound["status"] = Data("success", Data::VERBATIM);
	} else {
		replyData.compound["status"] = Data("failure", Data::VERBATIM);
	}
	returnData(request, replyData);
}

void DebuggerServlet::processDebugStop(const HTTPServer::Request& request) {
//		tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);

	stepping(false);

	Data replyData;
	if (_interpreter) {
		_interpreter.stop();
		_resumeCond.notify_all(); // unblock breakpoints
		_interpreter.join();
		_interpreter = Interpreter(); // empty interpreter, calls destructor
		replyData.compound["status"] = Data("success", Data::VERBATIM);
	} else {
		replyData.compound["status"] = Data("failure", Data::VERBATIM);
		replyData.compound["reason"] = Data("Interpreter already stopped", Data::VERBATIM);
	}
	returnData(request, replyData);
}

void DebuggerServlet::processDebugEval(const HTTPServer::Request& request) {
	Data replyData;
	if (!_interpreter) {
		replyData.compound["status"] = Data("failure", Data::VERBATIM);
		replyData.compound["reason"] = Data("No interpreter running", Data::VERBATIM);
	} else if (!_interpreter.getDataModel()) {
		replyData.compound["status"] = Data("failure", Data::VERBATIM);
		replyData.compound["reason"] = Data("No datamodel available", Data::VERBATIM);
	} else if (!request.data["content"].hasKey("expression")) {
		replyData.compound["status"] = Data("failure", Data::VERBATIM);
		replyData.compound["reason"] = Data("No expression given", Data::VERBATIM);
	} else {
		std::string expr = request.data["content"]["expression"].atom;
		try {
			replyData.compound["eval"] = _interpreter.getDataModel().getStringAsData(expr);
		} catch (Event e) {
			replyData.compound["eval"] = e.data;
			replyData.compound["eval"].compound["error"] = Data(e.name, Data::VERBATIM);
		}
		replyData.compound["status"] = Data("success", Data::VERBATIM);
	}
	returnData(request, replyData);
}

void DebuggerServlet::processDebugStep(const HTTPServer::Request& request) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);

	stepping(true);
	_resumeCond.notify_one();
	
	Data replyData;
	if (_interpreter && !_interpreter.isRunning()) {
		// register ourself as a monitor
		_interpreter.start();
		replyData.compound["status"] = Data("success", Data::VERBATIM);
	} else {
		replyData.compound["status"] = Data("failure", Data::VERBATIM);
	}
	returnData(request, replyData);
	
}

void DebuggerServlet::processDebugResume(const HTTPServer::Request& request) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	
	stepping(false);

	Data replyData;
	replyData.compound["status"] = Data("success", Data::VERBATIM);
	returnData(request, replyData);

	_resumeCond.notify_one();
}

void DebuggerServlet::processDebugPause(const HTTPServer::Request& request) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	
	Data replyData;
	replyData.compound["status"] = Data("success", Data::VERBATIM);
	returnData(request, replyData);
}

void DebuggerServlet::processConnect(const HTTPServer::Request& request) {
	tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
	_sessionId = UUID::getUUID();
	_breakPoints.clear();
//	_sendQueue.clear();
	
	Data replyData;
	replyData.compound["session"] = Data(_sessionId, Data::VERBATIM);
	replyData.compound["status"] = Data("success", Data::VERBATIM);
	returnData(request, replyData);
}

void DebuggerServlet::processListSessions(const HTTPServer::Request& request) {
	Data replyData;
	
	// TODO: return actual data
	Data sessionData;
	sessionData.compound["name"] = Data("Not actually a Session", Data::VERBATIM);
	sessionData.compound["id"] = Data("23452523-wg23g2g2-234t2g-23g2g", Data::VERBATIM);
	replyData.compound["sessions"].array.push_back(sessionData);
	
	sessionData.compound["name"] = Data("But returned from the server!", Data::VERBATIM);
	sessionData.compound["id"] = Data("swfgsgfw-g232vqvq-234t2g-23g2g", Data::VERBATIM);
	replyData.compound["sessions"].array.push_back(sessionData);
	
	replyData.compound["status"] = Data("success", Data::VERBATIM);
	returnData(request, replyData);
}

void DebuggerServlet::processDisconnect(const HTTPServer::Request& request) {
	Data replyData;
	replyData.compound["status"] = Data("success", Data::VERBATIM);
	returnData(request, replyData);
}

void DebuggerServlet::processAddBreakPoint(const HTTPServer::Request& request) {
	Breakpoint breakPoint(request.data["content"]);
	Data replyData;
	if (breakPoint.isValid()) {
		replyData.compound["status"] = Data("success", Data::VERBATIM);

		if (_breakPoints.find(breakPoint) == _breakPoints.end()) {
			_breakPoints.insert(breakPoint);
		}
	} else {
		replyData.compound["status"] = Data("failure", Data::VERBATIM);
	}
	returnData(request, replyData);
}

void DebuggerServlet::processRemoveBreakPoint(const HTTPServer::Request& request) {
	Breakpoint breakPoint(request.data["content"]);
	Data replyData;
	if (_breakPoints.erase(breakPoint) > 0) {
		replyData.compound["status"] = Data("success", Data::VERBATIM);
	} else {
		replyData.compound["message"] = Data("No such breakpoint", Data::VERBATIM);
		replyData.compound["status"] = Data("failure", Data::VERBATIM);
	}
	returnData(request, replyData);
}


}