blob: 8fd11725d835144d77001645dea7a6c023c32ccd (
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
|
#include <uscxml/Common.h>
#include "uscxml/plugins/ioprocessor/comet/CometIOProcessor.h"
#include "uscxml/Message.h"
#include <iostream>
#include <string.h>
#ifdef BUILD_AS_PLUGINS
#include <Pluma/Connector.hpp>
#endif
namespace uscxml {
#ifdef BUILD_AS_PLUGINS
PLUMA_CONNECTOR
bool connect(pluma::Host& host) {
host.add( new CometIOProcessorProvider() );
return true;
}
#endif
CometIOProcessor::CometIOProcessor() {
}
CometIOProcessor::~CometIOProcessor() {
}
boost::shared_ptr<IOProcessorImpl> CometIOProcessor::create(InterpreterImpl* interpreter) {
boost::shared_ptr<CometIOProcessor> io = boost::shared_ptr<CometIOProcessor>(new CometIOProcessor());
io->_interpreter = interpreter;
// register at http server
std::string path = interpreter->getName();
int i = 2;
while (!HTTPServer::registerServlet(path + "/comet", io.get())) {
std::stringstream ss;
ss << interpreter->getName() << i++;
path = ss.str();
}
return io;
}
Data CometIOProcessor::getDataModelVariables() {
Data data;
return data;
}
void CometIOProcessor::send(const SendRequest& req) {
tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
if (!_longPollingReq) {
_outQueue.push_back(req);
return;
}
reply(req, _longPollingReq);
}
void CometIOProcessor::reply(const SendRequest& req, const HTTPServer::Request& longPoll) {
HTTPServer::Reply reply(longPoll);
if (req.dom) {
std::stringstream ss;
ss << req.dom;
reply.content = ss.str();
reply.headers["Content-Type"] = "application/xml";
} else if (req.data) {
reply.content = Data::toJSON(req.data);
reply.headers["Content-Type"] = "application/json";
} else if (req.content.length() > 0) {
reply.content = req.content;
reply.headers["Content-Type"] = "text/plain";
}
if (req.params.find("Content-Type") != req.params.end())
reply.headers["Content-Type"] = req.params.find("Content-Type")->first;
HTTPServer::reply(reply);
}
bool CometIOProcessor::httpRecvRequest(const HTTPServer::Request& request) {
tthread::lock_guard<tthread::recursive_mutex> lock(_mutex);
if (_longPollingReq)
// send 204 to last request and remember new one
evhttp_send_error(_longPollingReq.curlReq, 204, NULL);
_longPollingReq = request;
if (!_outQueue.empty()) {
send(_outQueue.front());
_outQueue.pop_front();
}
return true;
}
}
|