added driving safety attributes
[profile/ivi/automotive-message-broker.git] / plugins / websocketsink / websocketsink.cpp
1 /*
2         Copyright (C) 2012  Intel Corporation
3
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.
8
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.
13
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
17 */
18
19
20 #include "websocketsink.h"
21 #include <glib.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <sys/types.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sstream>
31 #include "debugout.h"
32
33
34
35 WebSocketSink::WebSocketSink(AbstractRoutingEngine* re,libwebsocket *wsi,string uuid,VehicleProperty::Property property,std::string ambdproperty) : AbstractSink(re,map<string, string> ())
36 {
37         m_amdbproperty = ambdproperty;
38         m_uuid = uuid;
39         m_wsi = wsi;
40         m_property = property;
41         m_re = re;
42         re->subscribeToProperty(ambdproperty,this);
43 }
44 const string WebSocketSink::uuid()
45 {
46         return m_uuid;
47 }
48 void WebSocketSink::propertyChanged(AbstractPropertyType *value)
49 {
50         VehicleProperty::Property property = value->name;
51
52         stringstream s;
53         
54         //TODO: Dirty hack hardcoded stuff, jsut to make it work.
55         std::string tmpstr="";
56         if (m_property != property)
57         {
58                 tmpstr = m_property;
59         }
60         else
61         {
62                 tmpstr = property;
63         }
64         
65         s.precision(15);
66         
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 << "\"}";
71         
72         string replystr = s.str();
73         //printf("Reply: %s\n",replystr.c_str());
74         
75         DebugOut() << "Reply:" << replystr << "\n";
76
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);
82 }
83 WebSocketSink::~WebSocketSink()
84 {
85         m_re->unsubscribeToProperty(m_amdbproperty,this);
86 }
87 void WebSocketSink::supportedChanged(PropertyList supportedProperties)
88 {
89 }
90 PropertyList WebSocketSink::subscriptions()
91 {
92         return PropertyList();
93
94