2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #include "websocketsource.h"
22 #include <boost/assert.hpp>
23 #include <boost/lexical_cast.hpp>
26 #include <listplusplus.h>
27 #include <timestamp.h>
28 #include "uuidhelper.h"
30 #include <QVariantMap>
31 #include <QJsonDocument>
32 #include <QStringList>
37 #define __SMALLFILE__ std::string(__FILE__).substr(std::string(__FILE__).rfind("/")+1)
38 libwebsocket_context *context = NULL;
39 WebSocketSource *source;
40 AbstractRoutingEngine *m_re;
42 double oldTimestamp=0;
45 double averageLatency=0;
47 static int callback_http_only(libwebsocket_context *context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason,void *user, void *in, size_t len);
48 static struct libwebsocket_protocols protocols[] = {
63 //Called when a client connects, subscribes, or unsubscribes.
64 void WebSocketSource::checkSubscriptions()
66 while (queuedRequests.size() > 0)
68 VehicleProperty::Property prop = queuedRequests.front();
69 queuedRequests.pop_front();
70 if (contains(activeRequests,prop))
74 activeRequests.push_back(prop);
78 reply["type"] = "method";
79 reply["name"] = "subscribe";
80 reply["data"] = prop.c_str();
81 reply["transactionid"] = "d293f670-f0b3-11e1-aff1-0800200c9a66";
86 replystr = QJsonDocument::fromVariant(reply).toBinaryData();
89 replystr = QJsonDocument::fromVariant(reply).toJson();
93 lwsWrite(clientsocket, replystr, replystr.length());
96 void WebSocketSource::setConfiguration(map<string, string> config)
98 //printf("WebSocketSource::setConfiguration has been called\n");
101 configuration = config;
103 if(config.find("binaryProtocol") != config.end())
105 doBinary = config["binaryProtocol"] == "true";
108 for (map<string,string>::iterator i=configuration.begin();i!=configuration.end();i++)
110 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Incoming setting for WebSocketSource:" << (*i).first << ":" << (*i).second << "\n";
111 //printf("Incoming setting: %s:%s\n",(*i).first.c_str(),(*i).second.c_str());
112 if ((*i).first == "ip")
116 if ((*i).first == "port")
118 port = boost::lexical_cast<int>((*i).second);
120 if ((*i).first == "ssl")
122 if ((*i).second == "true")
128 m_sslEnabled = false;
132 //printf("Connecting to websocket server at %s port %i\n",ip.c_str(),port);
133 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Connecting to websocket server at" << ip << ":" << port << "\n";
137 DebugOut(5) << "SSL ENABLED" << endl;
141 clientsocket = libwebsocket_client_connect(context, ip.c_str(), port, sslval,"/", "localhost", "websocket",protocols[0].name, -1);
146 PropertyInfo WebSocketSource::getPropertyInfo(VehicleProperty::Property property)
148 return PropertyInfo::invalid();
151 bool gioPollingFunc(GIOChannel *source, GIOCondition condition, gpointer data)
153 //This is the polling function. If it return false, glib will stop polling this FD.
155 oldTimestamp = amb::currentTime();
157 struct pollfd pollstruct;
158 int newfd = g_io_channel_unix_get_fd(source);
159 pollstruct.fd = newfd;
160 pollstruct.events = condition;
161 pollstruct.revents = condition;
162 libwebsocket_service_fd(context,&pollstruct);
163 if (condition & G_IO_HUP)
165 //Hang up. Returning false closes out the GIOChannel.
166 //printf("Callback on G_IO_HUP\n");
169 if (condition & G_IO_IN)
173 DebugOut() << "gioPollingFunc" << condition << endl;
178 static int checkTimeouts(gpointer data)
180 WebSocketSource *src = (WebSocketSource*)data;
181 for (auto i=src->uuidTimeoutMap.begin();i!= src->uuidTimeoutMap.end();i++)
183 if (src->uuidRangedReplyMap.find((*i).first) != src->uuidRangedReplyMap.end())
186 if (amb::currentTime() > (*i).second)
188 //We've reached timeout
189 DebugOut() << "Timeout reached for request ID:" << (*i).first << "\n";
190 src->uuidRangedReplyMap[(*i).first]->success = false;
191 src->uuidRangedReplyMap[(*i).first]->completed(src->uuidRangedReplyMap[(*i).first]);
192 src->uuidRangedReplyMap.erase((*i).first);
193 src->uuidTimeoutMap.erase((*i).first);
196 if (src->uuidTimeoutMap.size() == 0)
204 //No timeout yet, keep waiting.
209 //Reply has already come back, ignore and erase from list.
210 src->uuidTimeoutMap.erase((*i).first);
213 if (src->uuidTimeoutMap.size() == 0)
223 static int callback_http_only(libwebsocket_context *context, struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len)
225 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 + LWS_SEND_BUFFER_POST_PADDING];
227 DebugOut() << __SMALLFILE__ << ":" << __LINE__ << reason << "callback_http_only" << endl;
230 case LWS_CALLBACK_CLOSED:
231 //fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED\n");
233 //printf("Connection closed!\n");
236 //case LWS_CALLBACK_PROTOCOL_INIT:
237 case LWS_CALLBACK_CLIENT_ESTABLISHED:
239 //This happens when a client initally connects. We need to request the support event types.
240 source->clientConnected = true;
241 source->checkSubscriptions();
242 //printf("Incoming connection!\n");
243 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Incoming connection" << endl;
246 toSend["type"] = "method";
247 toSend["name"] = "getSupportedEventTypes";
248 toSend["transactionid"] = amb::createUuid().c_str();
253 replystr = QJsonDocument::fromVariant(toSend).toBinaryData();
256 replystr = QJsonDocument::fromVariant(toSend).toJson();
260 lwsWrite(wsi, replystr, replystr.length());
264 case LWS_CALLBACK_CLIENT_RECEIVE:
266 QByteArray d((char*)in,len);
268 WebSocketSource * manager = source;
270 if(manager->expectedMessageFrames && manager->partialMessageIndex < manager->expectedMessageFrames)
272 manager->incompleteMessage += d;
273 manager->partialMessageIndex++;
276 else if(manager->expectedMessageFrames && manager->partialMessageIndex == manager->expectedMessageFrames)
278 d = manager->incompleteMessage + d;
279 manager->expectedMessageFrames = 0;
285 doc = QJsonDocument::fromBinaryData(d);
288 doc = QJsonDocument::fromJson(d);
289 DebugOut(7)<<d.data()<<endl;
294 DebugOut(DebugOut::Error)<<"Invalid message"<<endl;
298 QVariantMap call = doc.toVariant().toMap();
300 string type = call["type"].toString().toStdString();
301 string name = call["name"].toString().toStdString();
302 string id = call["transactionid"].toString().toStdString();
304 list<pair<string,string> > pairdata;
306 if(type == "multiframe")
308 manager->expectedMessageFrames = call["frames"].toInt();
309 manager->partialMessageIndex = 1;
310 manager->incompleteMessage = "";
313 else if (type == "valuechanged")
315 QVariantMap data = call["data"].toMap();
317 string value = data["value"].toString().toStdString();
318 double timestamp = data["timestamp"].toDouble();
319 int sequence = data["sequence"].toInt();
321 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Value changed:" << name << value << endl;
325 AbstractPropertyType* type = VehicleProperty::getPropertyTypeForPropertyNameValue(name,value);
326 type->timestamp = timestamp;
327 type->sequence = sequence;
328 m_re->updateProperty(type, source->uuid());
329 double currenttime = amb::currentTime();
331 /** This is now the latency between when something is available to read on the socket, until
332 * a property is about to be updated in AMB. This includes libwebsockets parsing and the
333 * JSON parsing in this section.
336 DebugOut(2)<<"websocket network + parse latency: "<<(currenttime - type->timestamp)*1000<<"ms"<<endl;
337 totalTime += (currenttime - oldTimestamp)*1000;
339 averageLatency = totalTime / numUpdates;
341 DebugOut(2)<<"Average parse latency: "<<averageLatency<<endl;
347 //printf("Exception %s\n",ex.what());
348 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Exception:" << ex.what() << "\n";
351 else if (type == "methodReply")
353 if (name == "getSupportedEventTypes")
356 QVariant data = call["data"];
358 QStringList supported = data.toStringList();
360 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Got getSupportedEventTypes request"<<endl;
363 Q_FOREACH(QString p, supported)
365 props.push_back(p.toStdString());
368 source->setSupported(props);
369 //m_re->updateSupported(m_supportedProperties,PropertyList());
371 else if (name == "getRanged")
373 QVariantList data = call["data"].toList();
375 std::list<AbstractPropertyType*> propertylist;
377 Q_FOREACH(QVariant d, data)
379 QVariantMap obj = d.toMap();
381 std::string name = obj["name"].toString().toStdString();
382 std::string value = obj["value"].toString().toStdString();
383 double timestamp = obj["timestamp"].toDouble();
384 int sequence = obj["sequence"].toInt();
386 AbstractPropertyType* type = VehicleProperty::getPropertyTypeForPropertyNameValue(name,value);
387 type->timestamp = timestamp;
388 type->sequence = sequence;
390 propertylist.push_back(type);
393 if (source->uuidRangedReplyMap.find(id) != source->uuidRangedReplyMap.end())
395 source->uuidRangedReplyMap[id]->values = propertylist;
396 source->uuidRangedReplyMap[id]->success = true;
397 source->uuidRangedReplyMap[id]->completed(source->uuidRangedReplyMap[id]);
398 source->uuidRangedReplyMap.erase(id);
402 DebugOut() << "getRanged methodReply has been recieved, without a request being in!. This is likely due to a request coming in after the timeout has elapsed.\n";
405 else if (name == "get")
408 DebugOut() << __SMALLFILE__ << ":" << __LINE__ << "Got \"GET\" event:" << pairdata.size()<<endl;
409 if (source->uuidReplyMap.find(id) != source->uuidReplyMap.end())
411 QVariantMap obj = call["data"].toMap();
413 std::string property = obj["property"].toString().toStdString();
414 std::string value = obj["value"].toString().toStdString();
415 double timestamp = obj["timestamp"].toDouble();
416 int sequence = obj["sequence"].toInt();
418 AbstractPropertyType* v = VehicleProperty::getPropertyTypeForPropertyNameValue(property,value);
419 v->timestamp = timestamp;
420 v->sequence = sequence;
422 if (source->uuidReplyMap.find(id) != source->uuidReplyMap.end() && source->uuidReplyMap[id]->error != AsyncPropertyReply::Timeout)
424 source->uuidReplyMap[id]->value = v;
425 source->uuidReplyMap[id]->success = true;
426 source->uuidReplyMap[id]->completed(source->uuidReplyMap[id]);
427 source->uuidReplyMap.erase(id);
432 DebugOut() << "get methodReply has been recieved, without a request being in!. This is likely due to a request coming in after the timeout has elapsed.\n";
439 DebugOut() << __SMALLFILE__ << ":" << __LINE__ << "GET Method Reply INVALID! Multiple properties detected, only single are supported!!!" << "\n";
442 //data will contain a property/value map.
450 case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
452 //printf("Requested extension: %s\n",(char*)in);
456 case LWS_CALLBACK_ADD_POLL_FD:
458 DebugOut(5) << __SMALLFILE__ << ":" << __LINE__ << "Adding poll for websocket IO channel" << endl;
459 //Add a FD to the poll list.
460 GIOChannel *chan = g_io_channel_unix_new(libwebsocket_get_socket_fd(wsi));
462 /// TODO: I changed this to be more consistent with the websocket sink end. it may not be correct. TEST
464 g_io_add_watch(chan,GIOCondition(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP),(GIOFunc)gioPollingFunc,0);
465 g_io_channel_set_close_on_unref(chan,true);
466 g_io_channel_unref(chan); //Pass ownership of the GIOChannel to the watch.
473 void WebSocketSource::setSupported(PropertyList list)
475 DebugOut() <<__SMALLFILE__ << ":" << __LINE__ <<" "<< __FUNCTION__ <<endl;
476 m_supportedProperties = list;
477 m_re->updateSupported(list,PropertyList(),this);
480 WebSocketSource::WebSocketSource(AbstractRoutingEngine *re, map<string, string> config) : AbstractSource(re, config), partialMessageIndex(0),expectedMessageFrames(0)
482 m_sslEnabled = false;
483 clientConnected = false;
486 struct lws_context_creation_info info;
487 memset(&info, 0, sizeof info);
488 info.protocols = protocols;
489 info.extensions = nullptr;
491 if(config.find("useExtensions") != config.end() && config["useExtensions"] == "true")
493 info.extensions = libwebsocket_get_internal_extensions();
498 info.port = CONTEXT_PORT_NO_LISTEN;
501 context = libwebsocket_create_context(&info);
503 setConfiguration(config);
505 //printf("websocketsource loaded!!!\n");
506 g_timeout_add(1000,checkTimeouts,this); //Do this once per second, check for functions that have timed out and reply with success = false;
509 PropertyList WebSocketSource::supported()
511 return m_supportedProperties;
514 int WebSocketSource::supportedOperations()
516 /// TODO: need to do this correctly based on what the host supports.
517 return Get | Set | GetRanged;
520 const string WebSocketSource::uuid()
522 return "d293f670-f0b3-11e1-aff1-0800200c9a66";
525 void WebSocketSource::subscribeToPropertyChanges(VehicleProperty::Property property)
527 //printf("Subscribed to property: %s\n",property.c_str());
528 queuedRequests.push_back(property);
531 checkSubscriptions();
536 void WebSocketSource::unsubscribeToPropertyChanges(VehicleProperty::Property property)
538 removeRequests.push_back(property);
541 checkSubscriptions();
546 void WebSocketSource::getPropertyAsync(AsyncPropertyReply *reply)
548 std::string uuid = amb::createUuid();
549 uuidReplyMap[uuid] = reply;
550 uuidTimeoutMap[uuid] = amb::currentTime() + 10.0; ///TODO: 10 second timeout, make this configurable?
553 data["property"] = reply->property.c_str();
554 data["zone"] = reply->zoneFilter;
556 QVariantMap replyvar;
557 replyvar["type"] = "method";
558 replyvar["name"] = "get";
559 replyvar["data"] = data;
560 replyvar["transactionid"] = uuid.c_str();
565 replystr = QJsonDocument::fromVariant(replyvar).toBinaryData();
568 replystr = QJsonDocument::fromVariant(replyvar).toJson();
572 lwsWrite(clientsocket, replystr, replystr.length());
575 void WebSocketSource::getRangePropertyAsync(AsyncRangePropertyReply *reply)
577 std::string uuid = amb::createUuid();
578 uuidRangedReplyMap[uuid] = reply;
579 uuidTimeoutMap[uuid] = amb::currentTime() + 60; ///TODO: 60 second timeout, make this configurable?
582 s << "{\"type\":\"method\",\"name\":\"getRanged\",\"data\": {";
584 QVariantMap replyvar;
585 replyvar["type"] = "method";
586 replyvar["name"] = "getRanged";
587 replyvar["transactionid"] = uuid.c_str();
588 replyvar["timeBegin"] = reply->timeBegin;
589 replyvar["timeEnd"] = reply->timeEnd;
590 replyvar["sequenceBegin"] = reply->sequenceBegin;
591 replyvar["sequenceEnd"] = reply->sequenceEnd;
594 QStringList properties;
596 for (auto itr = reply->properties.begin(); itr != reply->properties.end(); itr++)
598 VehicleProperty::Property p = *itr;
599 properties.append(p.c_str());
602 replyvar["data"] = properties;
607 replystr = QJsonDocument::fromVariant(replyvar).toBinaryData();
610 replystr = QJsonDocument::fromVariant(replyvar).toJson();
614 lwsWrite(clientsocket, replystr, replystr.length());
617 AsyncPropertyReply * WebSocketSource::setProperty( AsyncSetPropertyRequest request )
619 AsyncPropertyReply* reply = new AsyncPropertyReply(request);
622 data["property"] = request.property.c_str();
623 data["value"] = request.value->toString().c_str();
624 data["zone"] = request.zoneFilter;
627 QVariantMap replyvar;
628 replyvar["type"] = "method";
629 replyvar["name"] = "set";
630 replyvar["data"] = data;
631 replyvar["transactionid"] = amb::createUuid().c_str();
636 replystr = QJsonDocument::fromVariant(replyvar).toBinaryData();
639 replystr = QJsonDocument::fromVariant(replyvar).toJson();
643 lwsWrite(clientsocket, replystr, replystr.length());
645 ///TODO: we should actually wait for a response before we simply complete the call
646 reply->success = true;
647 reply->completed(reply);
651 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
653 return new WebSocketSource(routingengine, config);