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 "websocketsink.h"
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <sys/types.h>
35 WebSocketSink::WebSocketSink(AbstractRoutingEngine* re,libwebsocket *wsi,string uuid,VehicleProperty::Property property,std::string ambdproperty) : AbstractSink(re,map<string, string> ())
37 m_amdbproperty = ambdproperty;
40 m_property = property;
42 re->subscribeToProperty(ambdproperty,this);
44 const string WebSocketSink::uuid()
48 void WebSocketSink::propertyChanged(AbstractPropertyType *value)
50 VehicleProperty::Property property = value->name;
54 //TODO: Dirty hack hardcoded stuff, jsut to make it work.
55 std::string tmpstr="";
56 if (m_property != property)
67 s << "{\"type\":\"valuechanged\",\"name\":\"" << tmpstr << "\",\"data\":";
68 s << "{ \"value\":\"" << value->toString() << "\",\"zone\":\""<<value->zone;
69 s << "\",\"timestamp\":\""<<value->timestamp<<"\",\"sequence\":\""<<value->sequence<<"\"},";
70 s << "\"transactionid\":\"" << m_uuid << "\"}";
72 string replystr = s.str();
73 //printf("Reply: %s\n",replystr.c_str());
75 DebugOut() << "Reply:" << replystr << "\n";
77 char *new_response = new char[LWS_SEND_BUFFER_PRE_PADDING + strlen(replystr.c_str()) + LWS_SEND_BUFFER_POST_PADDING];
78 new_response+=LWS_SEND_BUFFER_PRE_PADDING;
79 strcpy(new_response,replystr.c_str());
80 libwebsocket_write(m_wsi, (unsigned char*)new_response, strlen(new_response), LWS_WRITE_TEXT);
81 delete [] (char*)(new_response-LWS_SEND_BUFFER_PRE_PADDING);
83 WebSocketSink::~WebSocketSink()
85 m_re->unsubscribeToProperty(m_amdbproperty,this);
87 void WebSocketSink::supportedChanged(PropertyList supportedProperties)
90 PropertyList WebSocketSink::subscriptions()
92 return PropertyList();