From: Michael Carpenter Date: Wed, 21 Nov 2012 18:34:42 +0000 (-0500) Subject: Addition of getPropertyAsync and setProperty support through websocketsource X-Git-Tag: 0.6.0~8^2~21 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F09%2F2509%2F1;p=profile%2Fivi%2Fautomotive-message-broker.git Addition of getPropertyAsync and setProperty support through websocketsource --- diff --git a/plugins/websocketsourceplugin/websocketsource.cpp b/plugins/websocketsourceplugin/websocketsource.cpp index 35a3078..ccb7fbd 100644 --- a/plugins/websocketsourceplugin/websocketsource.cpp +++ b/plugins/websocketsourceplugin/websocketsource.cpp @@ -197,27 +197,70 @@ static int callback_http_only(libwebsocket_context *context,struct libwebsocket json_reader_end_member(reader); list data; - json_reader_read_member(reader,"data"); - if (json_reader_is_array(reader)) + list > pairdata; + if (name == "get") { - for(int i=0; i < json_reader_count_elements(reader); i++) + json_reader_read_member(reader,"data"); + if (json_reader_is_array(reader)) { - json_reader_read_element(reader,i); - string path = json_reader_get_string_value(reader); - data.push_back(path); - json_reader_end_element(reader); + for(int i=0; i < json_reader_count_elements(reader); i++) + { + + pair pair; + json_reader_read_element(reader,i); + + json_reader_read_member(reader,"property"); + pair.first = json_reader_get_string_value(reader); + json_reader_end_member(reader); + + json_reader_read_member(reader,"value"); + pair.second = json_reader_get_string_value(reader); + json_reader_end_member(reader); + + json_reader_end_element(reader); + + pairdata.push_back(pair); + } + } + else + { + pair pair; + + json_reader_read_member(reader,"property"); + pair.first = json_reader_get_string_value(reader); + json_reader_end_member(reader); + + json_reader_read_member(reader,"value"); + pair.second = json_reader_get_string_value(reader); + json_reader_end_member(reader); + + pairdata.push_back(pair); } + json_reader_end_member(reader); } else { - string path = json_reader_get_string_value(reader); - if (path != "") + json_reader_read_member(reader,"data"); + if (json_reader_is_array(reader)) { - data.push_back(path); + for(int i=0; i < json_reader_count_elements(reader); i++) + { + json_reader_read_element(reader,i); + string path = json_reader_get_string_value(reader); + data.push_back(path); + json_reader_end_element(reader); + } } + else + { + string path = json_reader_get_string_value(reader); + if (path != "") + { + data.push_back(path); + } + } + json_reader_end_member(reader); } - json_reader_end_member(reader); - string id; json_reader_read_member(reader,"transactionid"); if (strcmp("gchararray",g_type_name(json_node_get_value_type(json_reader_get_value(reader)))) == 0) @@ -285,6 +328,24 @@ static int callback_http_only(libwebsocket_context *context,struct libwebsocket source->setSupported(props); //m_re->updateSupported(m_supportedProperties,PropertyList()); } + else if (name == "get") + { + + DebugOut() << __SMALLFILE__ << ":" << __LINE__ << "Got \"GET\" event:" << pairdata.size(); + while (pairdata.size() > 0) + { + pair pair = pairdata.front(); + pairdata.pop_front(); + if (source->propertyReplyMap.find(pair.first) != source->propertyReplyMap.end()) + { + source->propertyReplyMap[pair.first]->value = VehicleProperty::getPropertyTypeForPropertyNameValue(source->propertyReplyMap[pair.first]->property,pair.second); + source->propertyReplyMap[pair.first]->completed(source->propertyReplyMap[pair.first]); + source->propertyReplyMap.erase(pair.first); + } + } + //data will contain a property/value map. + } + } break; } @@ -358,6 +419,25 @@ void WebSocketSource::unsubscribeToPropertyChanges(VehicleProperty::Property pro void WebSocketSource::getPropertyAsync(AsyncPropertyReply *reply) { ///TODO: fill in + //s << "{\"type\":\"method\",\"name\":\"getSupportedEventTypes\",\"data\":[],\"transactionid\":\"" << "d293f670-f0b3-11e1-aff1-0800200c9a66" << "\"}"; + //m_re->getPropertyAsync(); + /*reply.value = 1; + reply->completed(reply); + reply->completed = [](AsyncPropertyReply* reply) { + DebugOut()<<"Velocity Async request completed: "<value->toString()<property] = reply; + stringstream s; + s << "{\"type\":\"method\",\"name\":\"get\",\"data\":[\"" << reply->property << "\"],\"transactionid\":\"" << "d293f670-f0b3-11e1-aff1-0800200c9a66" << "\"}"; + string replystr = s.str(); + DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Reply:" << replystr << "\n"; + //printf("Reply: %s\n",replystr.c_str()); + char *new_response = new char[LWS_SEND_BUFFER_PRE_PADDING + strlen(replystr.c_str()) + LWS_SEND_BUFFER_POST_PADDING]; + new_response+=LWS_SEND_BUFFER_PRE_PADDING; + strcpy(new_response,replystr.c_str()); + libwebsocket_write(clientsocket, (unsigned char*)new_response, strlen(new_response), LWS_WRITE_TEXT); + delete (char*)(new_response-LWS_SEND_BUFFER_PRE_PADDING); } void WebSocketSource::getRangePropertyAsync(AsyncRangePropertyReply *reply) @@ -368,7 +448,20 @@ void WebSocketSource::getRangePropertyAsync(AsyncRangePropertyReply *reply) AsyncPropertyReply * WebSocketSource::setProperty( AsyncSetPropertyRequest request ) { ///TODO: fill in - return NULL; + AsyncPropertyReply* reply = new AsyncPropertyReply(request); + reply->success = true; + stringstream s; + s << "{\"type\":\"method\",\"name\":\"set\",\"data\":[\"property\" : \"" << request.property << "\",\"value\" : \"" << request.value << "\"],\"transactionid\":\"" << "d293f670-f0b3-11e1-aff1-0800200c9a66" << "\"}"; + string replystr = s.str(); + DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Reply:" << replystr << "\n"; + //printf("Reply: %s\n",replystr.c_str()); + char *new_response = new char[LWS_SEND_BUFFER_PRE_PADDING + strlen(replystr.c_str()) + LWS_SEND_BUFFER_POST_PADDING]; + new_response+=LWS_SEND_BUFFER_PRE_PADDING; + strcpy(new_response,replystr.c_str()); + libwebsocket_write(clientsocket, (unsigned char*)new_response, strlen(new_response), LWS_WRITE_TEXT); + delete (char*)(new_response-LWS_SEND_BUFFER_PRE_PADDING); + reply->completed(reply); + return reply; } extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine, map config) diff --git a/plugins/websocketsourceplugin/websocketsource.h b/plugins/websocketsourceplugin/websocketsource.h index bf05cd7..eb14f1e 100644 --- a/plugins/websocketsourceplugin/websocketsource.h +++ b/plugins/websocketsourceplugin/websocketsource.h @@ -50,6 +50,7 @@ public: void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, string uuid,double, uint32_t) {} void supportedChanged(PropertyList) {} void setConfiguration(map config); + map propertyReplyMap; private: PropertyList m_supportedProperties;