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>
28 #include <timestamp.h>
29 #include "uuidhelper.h"
31 #include <QVariantMap>
32 #include <QJsonDocument>
33 #include <QStringList>
37 #include "superptr.hpp"
39 #define __SMALLFILE__ std::string(__FILE__).substr(std::string(__FILE__).rfind("/")+1)
40 libwebsocket_context *context = NULL;
41 WebSocketSource *source;
42 AbstractRoutingEngine *m_re;
44 double oldTimestamp=0;
47 double averageLatency=0;
49 class UniquePropertyCache
52 bool hasProperty(std::string name, std::string source, Zone::Type zone)
54 for(auto i : mProperties)
57 i->sourceUuid == source &&
66 std::shared_ptr<AbstractPropertyType> append(std::string name, std::string source, Zone::Type zone, std::string type)
68 for(auto i : mProperties)
71 i->sourceUuid == source &&
78 auto t = VehicleProperty::getPropertyTypeForPropertyNameValue(name);
82 VehicleProperty::registerProperty(name, [name, type]() -> AbstractPropertyType* {
83 if(type == amb::BasicTypes::UInt16Str)
85 return new BasicPropertyType<uint16_t>(name, 0);
87 else if(type == amb::BasicTypes::Int16Str)
89 return new BasicPropertyType<int16_t>(name, 0);
91 else if(type == amb::BasicTypes::UInt32Str)
93 return new BasicPropertyType<uint32_t>(name, 0);
95 else if(type == amb::BasicTypes::Int32Str)
97 return new BasicPropertyType<int32_t>(name, 0);
99 else if(type == amb::BasicTypes::StringStr)
101 return new StringPropertyType(name);
103 else if(type == amb::BasicTypes::DoubleStr)
105 return new BasicPropertyType<double>(name, 0);
107 else if(type == amb::BasicTypes::BooleanStr)
109 return new BasicPropertyType<bool>(name, false);
111 DebugOut(DebugOut::Warning) << "Unknown or unsupported type: " << type << endl;
114 t = VehicleProperty::getPropertyTypeForPropertyNameValue(name);
117 if(t)/// check again to see if registration succeeded
119 t->sourceUuid = source;
122 mProperties.emplace_back(t);
125 return property(name, source, zone); /// will return nullptr if t didn't register
128 std::shared_ptr<AbstractPropertyType> property(std::string name, std::string source, Zone::Type zone)
130 for(auto i : mProperties)
132 if(i->name == name &&
133 i->sourceUuid == source &&
143 std::vector<std::shared_ptr<AbstractPropertyType>> properties() { return mProperties; }
146 std::vector<std::shared_ptr<AbstractPropertyType>> mProperties;
149 UniquePropertyCache properties;
151 static int callback_http_only(libwebsocket_context *context,struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason,void *user, void *in, size_t len);
152 static struct libwebsocket_protocols protocols[] = {
167 //Called when a client connects, subscribes, or unsubscribes.
168 void WebSocketSource::checkSubscriptions()
170 while (queuedRequests.size() > 0)
172 VehicleProperty::Property prop = queuedRequests.front();
173 removeOne(&queuedRequests,prop);
174 if (contains(activeRequests,prop))
178 activeRequests.push_back(prop);
182 reply["type"] = "method";
183 reply["name"] = "subscribe";
184 reply["property"] = prop.c_str();
185 reply["transactionid"] = "d293f670-f0b3-11e1-aff1-0800200c9a66";
187 lwsWriteVariant(clientsocket, reply);
190 void WebSocketSource::setConfiguration(map<string, string> config)
192 //printf("WebSocketSource::setConfiguration has been called\n");
195 configuration = config;
197 if(config.find("binaryProtocol") != config.end())
199 doBinary = config["binaryProtocol"] == "true";
202 for (map<string,string>::iterator i=configuration.begin();i!=configuration.end();i++)
204 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Incoming setting for WebSocketSource:" << (*i).first << ":" << (*i).second << "\n";
205 //printf("Incoming setting: %s:%s\n",(*i).first.c_str(),(*i).second.c_str());
206 if ((*i).first == "ip")
210 if ((*i).first == "port")
212 port = boost::lexical_cast<int>((*i).second);
214 if ((*i).first == "ssl")
216 if ((*i).second == "true")
222 m_sslEnabled = false;
226 //printf("Connecting to websocket server at %s port %i\n",ip.c_str(),port);
227 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Connecting to websocket server at" << ip << ":" << port << "\n";
231 DebugOut(5) << "SSL ENABLED" << endl;
235 clientsocket = libwebsocket_client_connect(context, ip.c_str(), port, sslval,"/", "localhost", "websocket", protocols[0].name, -1);
238 PropertyInfo WebSocketSource::getPropertyInfo(const VehicleProperty::Property &property)
240 Zone::ZoneList zones;
241 for(auto i : properties.properties())
243 if(i->name == property)
245 zones.push_back(i->zone);
249 return PropertyInfo(0, zones);
252 bool gioPollingFunc(GIOChannel *source, GIOCondition condition, gpointer data)
254 //This is the polling function. If it return false, glib will stop polling this FD.
256 oldTimestamp = amb::currentTime();
258 struct pollfd pollstruct;
259 int newfd = g_io_channel_unix_get_fd(source);
260 pollstruct.fd = newfd;
261 pollstruct.events = condition;
262 pollstruct.revents = condition;
263 libwebsocket_service_fd(context,&pollstruct);
264 if (condition & G_IO_HUP)
266 //Hang up. Returning false closes out the GIOChannel.
267 //printf("Callback on G_IO_HUP\n");
270 if (condition & G_IO_IN)
274 DebugOut() << "gioPollingFunc" << condition << endl;
279 static int checkTimeouts(gpointer data)
281 WebSocketSource *src = (WebSocketSource*)data;
282 for (auto i=src->uuidTimeoutMap.begin();i!= src->uuidTimeoutMap.end();i++)
284 if (src->uuidRangedReplyMap.find((*i).first) != src->uuidRangedReplyMap.end())
287 if (amb::currentTime() > (*i).second)
289 //We've reached timeout
290 DebugOut() << "Timeout reached for request ID:" << (*i).first << "\n";
291 src->uuidRangedReplyMap[(*i).first]->success = false;
292 src->uuidRangedReplyMap[(*i).first]->completed(src->uuidRangedReplyMap[(*i).first]);
293 src->uuidRangedReplyMap.erase((*i).first);
294 src->uuidTimeoutMap.erase((*i).first);
297 if (src->uuidTimeoutMap.size() == 0)
305 //No timeout yet, keep waiting.
310 //Reply has already come back, ignore and erase from list.
311 src->uuidTimeoutMap.erase((*i).first);
314 if (src->uuidTimeoutMap.size() == 0)
324 static int callback_http_only(libwebsocket_context *context, struct libwebsocket *wsi,enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len)
326 unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 4096 + LWS_SEND_BUFFER_POST_PADDING];
327 DebugOut() << __SMALLFILE__ << ":" << __LINE__ << reason << "callback_http_only" << endl;
330 case LWS_CALLBACK_CLOSED:
331 //fprintf(stderr, "mirror: LWS_CALLBACK_CLOSED\n");
333 //printf("Connection closed!\n");
336 //case LWS_CALLBACK_PROTOCOL_INIT:
337 case LWS_CALLBACK_CLIENT_ESTABLISHED:
339 //This happens when a client initally connects. We need to request the support event types.
340 source->clientConnected = true;
341 source->checkSubscriptions();
342 //printf("Incoming connection!\n");
343 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Incoming connection" << endl;
346 toSend["type"] = "method";
347 toSend["name"] = "getSupported";
348 toSend["transactionid"] = amb::createUuid().c_str();
350 lwsWriteVariant(wsi, toSend);
354 case LWS_CALLBACK_CLIENT_RECEIVE:
356 QByteArray d((char*)in, len);
358 WebSocketSource * manager = source;
360 if(manager->expectedMessageFrames && manager->partialMessageIndex < manager->expectedMessageFrames)
362 manager->incompleteMessage += d;
363 manager->partialMessageIndex++;
366 else if(manager->expectedMessageFrames && manager->partialMessageIndex == manager->expectedMessageFrames)
368 d = manager->incompleteMessage + d;
369 manager->expectedMessageFrames = 0;
375 doc = QJsonDocument::fromBinaryData(d);
378 doc = QJsonDocument::fromJson(d);
379 DebugOut(7)<<d.data()<<endl;
384 DebugOut(DebugOut::Warning)<<"Invalid message"<<endl;
388 QVariantMap call = doc.toVariant().toMap();
390 string type = call["type"].toString().toStdString();
391 string name = call["name"].toString().toStdString();
392 string id = call["transactionid"].toString().toStdString();
394 if(type == "multiframe")
396 manager->expectedMessageFrames = call["frames"].toInt();
397 manager->partialMessageIndex = 1;
398 manager->incompleteMessage = "";
401 else if (type == "valuechanged")
403 QVariantMap data = call["data"].toMap();
405 string value = data["value"].toString().toStdString();
406 double timestamp = data["timestamp"].toDouble();
407 int sequence = data["sequence"].toInt();
408 Zone::Type zone = data["zone"].toInt();
409 string type = data["type"].toString().toStdString();
411 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Value changed:" << name << value << endl;
415 auto property = properties.append(name, source->uuid(), zone, type);
419 DebugOut(DebugOut::Warning) << "We either don't have this or don't support it ("
420 << name << "," << zone << "," << type << ")" << endl;
423 property->timestamp = timestamp;
424 property->sequence = sequence;
425 property->fromString(value);
427 m_re->updateProperty(property.get(), source->uuid());
429 double currenttime = amb::currentTime();
431 /** This is now the latency between when something is available to read on the socket, until
432 * a property is about to be updated in AMB. This includes libwebsockets parsing and the
433 * JSON parsing in this section.
436 DebugOut()<<"websocket network + parse latency: "<<(currenttime - property->timestamp)*1000<<"ms"<<endl;
437 totalTime += (currenttime - oldTimestamp)*1000;
439 averageLatency = totalTime / numUpdates;
441 DebugOut()<<"Average parse latency: "<<averageLatency<<endl;
445 //printf("Exception %s\n",ex.what());
446 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Exception:" << ex.what() << "\n";
449 else if (type == "methodReply")
451 if (name == "getSupported" || name == "supportedChanged")
454 QVariant data = call["data"];
456 QVariantList supported = data.toList();
458 DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Got getSupported request"<<endl;
461 Q_FOREACH(QVariant p, supported)
463 QVariantMap d = p.toMap();
464 Zone::Type zone = d["zone"].toInt();
465 std::string name = d["property"].toString().toStdString();
466 std::string proptype = d["type"].toString().toStdString();
467 std::string source = d["source"].toString().toStdString();
469 properties.append(name, source, zone, proptype);
472 source->updateSupported();
475 else if (name == "getRanged")
477 QVariantList data = call["data"].toList();
479 std::list<AbstractPropertyType*> propertylist;
481 Q_FOREACH(QVariant d, data)
483 QVariantMap obj = d.toMap();
485 std::string name = obj["property"].toString().toStdString();
486 std::string value = obj["value"].toString().toStdString();
487 double timestamp = obj["timestamp"].toDouble();
488 int sequence = obj["sequence"].toInt();
490 AbstractPropertyType* type = VehicleProperty::getPropertyTypeForPropertyNameValue(name, value);
493 DebugOut() << "TODO: support custom types here: " << endl;
496 type->timestamp = timestamp;
497 type->sequence = sequence;
499 propertylist.push_back(type);
502 if (source->uuidRangedReplyMap.find(id) != source->uuidRangedReplyMap.end())
504 source->uuidRangedReplyMap[id]->values = propertylist;
505 source->uuidRangedReplyMap[id]->success = true;
506 source->uuidRangedReplyMap[id]->completed(source->uuidRangedReplyMap[id]);
507 source->uuidRangedReplyMap.erase(id);
511 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";
514 else if (name == "get")
517 DebugOut() << __SMALLFILE__ << ":" << __LINE__ << "Got \"GET\" reply" << endl;
518 if (source->uuidReplyMap.find(id) != source->uuidReplyMap.end())
520 QVariantMap obj = call["data"].toMap();
522 std::string property = obj["property"].toString().toStdString();
523 std::string value = obj["value"].toString().toStdString();
524 double timestamp = obj["timestamp"].toDouble();
525 int sequence = obj["sequence"].toInt();
526 Zone::Type zone = obj["zone"].toInt();
528 auto v = amb::make_unique(VehicleProperty::getPropertyTypeForPropertyNameValue(property, value));
530 v->timestamp = timestamp;
531 v->sequence = sequence;
534 if (source->uuidReplyMap.find(id) != source->uuidReplyMap.end() && source->uuidReplyMap[id]->error != AsyncPropertyReply::Timeout)
536 source->uuidReplyMap[id]->value = v.get();
537 source->uuidReplyMap[id]->success = true;
538 source->uuidReplyMap[id]->completed(source->uuidReplyMap[id]);
539 source->uuidReplyMap.erase(id);
544 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";
549 DebugOut() << __SMALLFILE__ << ":" << __LINE__ << "GET Method Reply INVALID! Multiple properties detected, only single are supported!!!" << "\n";
554 else if (name == "set")
556 DebugOut() << __SMALLFILE__ << ":" << __LINE__ << "Got \"SET\" event" << endl;
557 std::string id = call["transactionid"].toString().toStdString();
559 if(source->setReplyMap.find(id) != source->setReplyMap.end() && source->setReplyMap[id]->error != AsyncPropertyReply::Timeout)
561 AsyncPropertyReply* reply = source->setReplyMap[id];
563 reply->success = call["success"].toBool();
564 reply->error = AsyncPropertyReply::strToError(call["error"].toString().toStdString());
566 QVariantMap obj = call["data"].toMap();
568 std::string property = obj["property"].toString().toStdString();
569 std::string value = obj["value"].toString().toStdString();
571 double timestamp = obj["timestamp"].toDouble();
572 int sequence = obj["sequence"].toInt();
573 Zone::Type zone = obj["zone"].toInt();
575 auto v = amb::make_unique(VehicleProperty::getPropertyTypeForPropertyNameValue(property, value));
579 v->timestamp = timestamp;
580 v->sequence = sequence;
585 throw std::runtime_error("property may not be registered.");
588 reply->value = v.get();
589 reply->completed(reply);
590 source->setReplyMap.erase(id);
595 DebugOut(DebugOut::Warning) << "Unhandled methodReply: " << name << endl;
603 case LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED:
605 //printf("Requested extension: %s\n",(char*)in);
609 case LWS_CALLBACK_ADD_POLL_FD:
611 DebugOut(5) << __SMALLFILE__ << ":" << __LINE__ << "Adding poll for websocket IO channel" << endl;
612 //Add a FD to the poll list.
613 GIOChannel *chan = g_io_channel_unix_new(libwebsocket_get_socket_fd(wsi));
615 /// TODO: I changed this to be more consistent with the websocket sink end. it may not be correct. TEST
617 g_io_add_watch(chan,GIOCondition(G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP),(GIOFunc)gioPollingFunc,0);
618 g_io_channel_set_close_on_unref(chan,true);
619 g_io_channel_unref(chan); //Pass ownership of the GIOChannel to the watch.
626 void WebSocketSource::updateSupported()
629 for(auto i : properties.properties())
631 if(!contains(list, i->name))
632 list.push_back(i->name);
635 m_re->updateSupported(list, PropertyList(), this);
638 WebSocketSource::WebSocketSource(AbstractRoutingEngine *re, map<string, string> config) : AbstractSource(re, config), partialMessageIndex(0),expectedMessageFrames(0)
640 m_sslEnabled = false;
641 clientConnected = false;
644 struct lws_context_creation_info info;
645 memset(&info, 0, sizeof info);
646 info.protocols = protocols;
647 info.extensions = nullptr;
649 if(config.find("useExtensions") != config.end() && config["useExtensions"] == "true")
651 info.extensions = libwebsocket_get_internal_extensions();
656 info.port = CONTEXT_PORT_NO_LISTEN;
659 context = libwebsocket_create_context(&info);
661 setConfiguration(config);
663 //printf("websocketsource loaded!!!\n");
664 g_timeout_add(1000,checkTimeouts,this); //Do this once per second, check for functions that have timed out and reply with success = false;
667 PropertyList WebSocketSource::supported()
670 for(auto i : properties.properties())
672 list.push_back(i->name);
677 int WebSocketSource::supportedOperations()
679 /// TODO: need to do this correctly based on what the host supports.
680 return Get | Set | GetRanged;
683 const string WebSocketSource::uuid()
685 return "d293f670-f0b3-11e1-aff1-0800200c9a66";
688 void WebSocketSource::subscribeToPropertyChanges(VehicleProperty::Property property)
690 //printf("Subscribed to property: %s\n",property.c_str());
691 queuedRequests.push_back(property);
694 checkSubscriptions();
699 void WebSocketSource::unsubscribeToPropertyChanges(VehicleProperty::Property property)
701 removeRequests.push_back(property);
704 checkSubscriptions();
709 void WebSocketSource::getPropertyAsync(AsyncPropertyReply *reply)
711 std::string uuid = amb::createUuid();
712 uuidReplyMap[uuid] = reply;
713 uuidTimeoutMap[uuid] = amb::currentTime() + 10.0; ///TODO: 10 second timeout, make this configurable?
716 data["property"] = reply->property.c_str();
717 data["zone"] = reply->zoneFilter;
719 QVariantMap replyvar;
720 replyvar["type"] = "method";
721 replyvar["name"] = "get";
722 replyvar["data"] = data;
723 replyvar["transactionid"] = uuid.c_str();
725 lwsWriteVariant(clientsocket, replyvar);
728 void WebSocketSource::getRangePropertyAsync(AsyncRangePropertyReply *reply)
730 std::string uuid = amb::createUuid();
731 uuidRangedReplyMap[uuid] = reply;
732 uuidTimeoutMap[uuid] = amb::currentTime() + 60; ///TODO: 60 second timeout, make this configurable?
735 s << "{\"type\":\"method\",\"name\":\"getRanged\",\"data\": {";
737 QVariantMap replyvar;
738 replyvar["type"] = "method";
739 replyvar["name"] = "getRanged";
740 replyvar["transactionid"] = uuid.c_str();
741 replyvar["timeBegin"] = reply->timeBegin;
742 replyvar["timeEnd"] = reply->timeEnd;
743 replyvar["sequenceBegin"] = reply->sequenceBegin;
744 replyvar["sequenceEnd"] = reply->sequenceEnd;
746 QStringList properties;
748 for (auto itr = reply->properties.begin(); itr != reply->properties.end(); itr++)
750 VehicleProperty::Property p = *itr;
751 properties.append(p.c_str());
754 replyvar["data"] = properties;
756 lwsWriteVariant(clientsocket, replyvar);
759 AsyncPropertyReply * WebSocketSource::setProperty( AsyncSetPropertyRequest request )
761 AsyncPropertyReply* reply = new AsyncPropertyReply(request);
763 std::string uuid = amb::createUuid();
766 data["property"] = request.property.c_str();
767 data["value"] = request.value->toString().c_str();
768 data["zone"] = request.zoneFilter;
770 QVariantMap replyvar;
771 replyvar["type"] = "method";
772 replyvar["name"] = "set";
773 replyvar["data"] = data;
774 replyvar["transactionid"] = uuid.c_str();
776 lwsWriteVariant(clientsocket, replyvar);
778 setReplyMap[uuid] = reply;
783 extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map<string, string> config)
785 return new WebSocketSource(routingengine, config);