From 7a6851551da9e232d3fd9f3780a051d0fff394a6 Mon Sep 17 00:00:00 2001 From: Kevron Rees Date: Wed, 17 Oct 2012 16:17:47 -0700 Subject: [PATCH] fixed websocketsource. possibly removed mem leak --- TODO | 1 + ambd/core.cpp | 17 ++++++ ambd/core.h | 1 + lib/abstractroutingengine.h | 67 ++++++++++++++++++++++- lib/abstractsource.h | 1 + lib/vehicleproperty.h | 11 ++++ plugins/exampleplugin.cpp | 10 +++- plugins/exampleplugin.h | 1 + plugins/obd2plugin/obd2source.h | 1 + plugins/websocketsink/websocketsinkmanager.cpp | 19 +++++-- plugins/websocketsink/websocketsinkmanager.h | 2 +- plugins/websocketsourceplugin/websocketsource.cpp | 48 ++++------------ plugins/websocketsourceplugin/websocketsource.h | 4 +- plugins/wheel/wheelplugin.h | 1 + 14 files changed, 135 insertions(+), 49 deletions(-) diff --git a/TODO b/TODO index c6db1a1..53c7aaa 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,6 @@ - bluetooth adapter setting in obd2source - test for memory leaks in websocket sink plugin +- implement websocket source getProperty methods and setProperty - per-source property filtering in routing engine - historic data "get" interface in routing engine - sqlite database storage plugin diff --git a/ambd/core.cpp b/ambd/core.cpp index 1059498..ff014b3 100644 --- a/ambd/core.cpp +++ b/ambd/core.cpp @@ -188,6 +188,23 @@ AsyncPropertyReply *Core::getPropertyAsync(AsyncPropertyRequest request) return reply; } +AsyncRangePropertyReply *Core::getRangePropertyAsync(AsyncRangePropertyRequest request) +{ + AsyncRangePropertyReply * reply = new AsyncRangePropertyReply(request); + + for(SourceList::iterator itr = mSources.begin(); itr != mSources.end(); itr++) + { + AbstractSource* src = (*itr); + PropertyList properties = src->supported(); + if(ListPlusPlus(&properties).contains(request.property)) + { + src->getRangePropertyAsync(reply); + } + } + + return reply; +} + void Core::setProperty(VehicleProperty::Property property, AbstractPropertyType *value) { for(SourceList::iterator itr = mSources.begin(); itr != mSources.end(); itr++) diff --git a/ambd/core.h b/ambd/core.h index b03d84d..4f4b14a 100644 --- a/ambd/core.h +++ b/ambd/core.h @@ -43,6 +43,7 @@ public: void registerSink(AbstractSink *self); void unregisterSink(AbstractSink *self); AsyncPropertyReply* getPropertyAsync(AsyncPropertyRequest request); + AsyncRangePropertyReply* getRangePropertyAsync(AsyncRangePropertyRequest request); void setProperty(VehicleProperty::Property,AbstractPropertyType*); void subscribeToProperty(VehicleProperty::Property, AbstractSink* self); void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self); diff --git a/lib/abstractroutingengine.h b/lib/abstractroutingengine.h index 1d90a96..b01df5a 100644 --- a/lib/abstractroutingengine.h +++ b/lib/abstractroutingengine.h @@ -22,6 +22,7 @@ #include #include +#include #include "vehicleproperty.h" #include "abstractpropertytype.h" @@ -35,6 +36,26 @@ typedef std::function CompletedSignal; class AsyncPropertyRequest { public: + AsyncPropertyRequest() + :property(VehicleProperty::NoValue) + { + + } + + AsyncPropertyRequest(const AsyncPropertyRequest &request) + { + this->property = request.property; + this->completed = request.completed; + } + + AsyncPropertyRequest & operator = (const AsyncPropertyRequest & other) + { + this->property = other.property; + this->completed = other.completed; + + return *this; + } + VehicleProperty::Property property; CompletedSignal completed; }; @@ -42,13 +63,54 @@ public: class AsyncPropertyReply: public AsyncPropertyRequest { public: - AsyncPropertyReply(AsyncPropertyRequest request) + AsyncPropertyReply(const AsyncPropertyRequest &request) + :AsyncPropertyRequest(request), value(NULL) + { + + } + + AbstractPropertyType* value; +}; + +class AsyncRangePropertyRequest: public AsyncPropertyRequest +{ +public: + AsyncRangePropertyRequest() + :begin(0), end(0) + { + + } + + AsyncRangePropertyRequest(const AsyncPropertyRequest &request) + :AsyncPropertyRequest(request), begin(0), end(0) { this->property = request.property; this->completed = request.completed; } - AbstractPropertyType* value; + AsyncRangePropertyRequest(const AsyncRangePropertyRequest &request) + :AsyncPropertyRequest(request) + { + this->property = request.property; + this->completed = request.completed; + this->begin = request.begin; + this->end = request.end; + } + + time_t begin; + time_t end; +}; + +class AsyncRangePropertyReply: public AsyncRangePropertyRequest +{ +public: + AsyncRangePropertyReply(AsyncRangePropertyRequest request) + :AsyncRangePropertyRequest(request) + { + + } + + std::list values; }; class AbstractRoutingEngine @@ -62,6 +124,7 @@ public: virtual void registerSink(AbstractSink* self) = 0; virtual void unregisterSink(AbstractSink* self) = 0; virtual AsyncPropertyReply *getPropertyAsync(AsyncPropertyRequest request) = 0; + virtual AsyncRangePropertyReply * getRangePropertyAsync(AsyncRangePropertyRequest request) = 0; virtual void setProperty(VehicleProperty::Property, AbstractPropertyType*) = 0; virtual void subscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0; virtual void unsubscribeToProperty(VehicleProperty::Property, AbstractSink* self) = 0; diff --git a/lib/abstractsource.h b/lib/abstractsource.h index 70d55f5..d9a0ee3 100644 --- a/lib/abstractsource.h +++ b/lib/abstractsource.h @@ -45,6 +45,7 @@ public: ///pure virtual methods: virtual void getPropertyAsync(AsyncPropertyReply *reply) = 0; + virtual void getRangePropertyAsync(AsyncRangePropertyReply *reply) = 0; virtual void setProperty(VehicleProperty::Property property, AbstractPropertyType* value) = 0; virtual void subscribeToPropertyChanges(VehicleProperty::Property property) = 0; virtual void unsubscribeToPropertyChanges(VehicleProperty::Property property) = 0; diff --git a/lib/vehicleproperty.h b/lib/vehicleproperty.h index 57d3fd8..23813f9 100644 --- a/lib/vehicleproperty.h +++ b/lib/vehicleproperty.h @@ -88,6 +88,12 @@ enum Mode { } namespace Power { +/**< Vehicle Power Modes + * Off = Vehicle is off and key is in the "off" position. + * Accessory1 = Vehicle is off and key is in Accessory1 position. + * Accessory2 = Vehicle is off and key is in Accessory2 position. + * Run = Vehichle is running. Key is in the running position. + */ enum PowerModes { Off = 0, @@ -227,6 +233,9 @@ public: static const Property TirePressureRightRear; typedef BasicPropertyType TirePressureType; + /**< Vehicle Power Mode. + *@see Power::PowerModes + */ static const Property VehiclePowerMode; typedef BasicPropertyType VehiclePowerModeType; @@ -255,6 +264,8 @@ public: static const Property InteriorLightCenter; static const Property InteriorLightPassenger; + + static std::list capabilities(); /*! getPropertyTypeForPropertyNameValue returns an AbstractPropertyType* for the property name diff --git a/plugins/exampleplugin.cpp b/plugins/exampleplugin.cpp index ab6a718..923ccd8 100644 --- a/plugins/exampleplugin.cpp +++ b/plugins/exampleplugin.cpp @@ -68,6 +68,9 @@ string ExampleSourcePlugin::uuid() void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply) { DebugOut()<<"ExampleSource: getPropertyAsync called for property: "<property<property == VehicleProperty::VehicleSpeed) { VehicleProperty::VehicleSpeedType temp(velocity); @@ -118,6 +121,11 @@ void ExampleSourcePlugin::getPropertyAsync(AsyncPropertyReply *reply) } } +void ExampleSourcePlugin::getRangePropertyAsync(AsyncRangePropertyReply *reply) +{ + +} + void ExampleSourcePlugin::setProperty(VehicleProperty::Property , AbstractPropertyType *) { @@ -182,5 +190,5 @@ void ExampleSourcePlugin::randomizeProperties() routingEngine->updateProperty(VehicleProperty::TransmissionShiftPosition,&tsp); routingEngine->updateProperty(VehicleProperty::ThrottlePosition, &tp); routingEngine->updateProperty(VehicleProperty::EngineCoolantTemperature, &ec); - routingEngine->updateProperty(VehicleProperty::MachineGunTurretStatus, &mgt); + //routingEngine->updateProperty(VehicleProperty::MachineGunTurretStatus, &mgt); } diff --git a/plugins/exampleplugin.h b/plugins/exampleplugin.h index 5187c5c..5bd2776 100644 --- a/plugins/exampleplugin.h +++ b/plugins/exampleplugin.h @@ -32,6 +32,7 @@ public: string uuid(); void getPropertyAsync(AsyncPropertyReply *reply); + void getRangePropertyAsync(AsyncRangePropertyReply *reply); void setProperty(VehicleProperty::Property, AbstractPropertyType*); void subscribeToPropertyChanges(VehicleProperty::Property property); void unsubscribeToPropertyChanges(VehicleProperty::Property property); diff --git a/plugins/obd2plugin/obd2source.h b/plugins/obd2plugin/obd2source.h index 9c97d35..ae9b1bb 100644 --- a/plugins/obd2plugin/obd2source.h +++ b/plugins/obd2plugin/obd2source.h @@ -57,6 +57,7 @@ public: string uuid(); int portHandle; void getPropertyAsync(AsyncPropertyReply *reply); + void getRangePropertyAsync(AsyncRangePropertyReply *reply){} void setProperty(VehicleProperty::Property, AbstractPropertyType*); void subscribeToPropertyChanges(VehicleProperty::Property property); void unsubscribeToPropertyChanges(VehicleProperty::Property property); diff --git a/plugins/websocketsink/websocketsinkmanager.cpp b/plugins/websocketsink/websocketsinkmanager.cpp index 50f9125..9373b15 100644 --- a/plugins/websocketsink/websocketsinkmanager.cpp +++ b/plugins/websocketsink/websocketsinkmanager.cpp @@ -228,19 +228,28 @@ extern "C" AbstractSinkManager * create(AbstractRoutingEngine* routingengine, ma } void WebSocketSinkManager::disconnectAll(libwebsocket* socket) { + std::list toDeleteList; + for (auto i=m_sinkMap.begin(); i != m_sinkMap.end();i++) { if ((*i).second->socket() == socket) { //This is the sink in question. WebSocketSink* sink = (*i).second; - delete sink; - m_sinkMap.erase((*i).first); - i--; - //printf("Sink removed\n"); - DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Sink removed\n"; + if(!ListPlusPlus(&toDeleteList).contains(sink)) + { + toDeleteList.push_back(sink); + } + m_sinkMap.erase(i); + i=m_sinkMap.begin(); + DebugOut() << __SMALLFILE__ <<":"<< __LINE__ << "Sink removed"< m_sinkMap; + map m_sinkMap; void setConfiguration(map config); void setValue(string property,string value); private: diff --git a/plugins/websocketsourceplugin/websocketsource.cpp b/plugins/websocketsourceplugin/websocketsource.cpp index b172621..16a4a45 100644 --- a/plugins/websocketsourceplugin/websocketsource.cpp +++ b/plugins/websocketsourceplugin/websocketsource.cpp @@ -315,8 +315,9 @@ WebSocketSource::WebSocketSource(AbstractRoutingEngine *re, map m_re = re; context = libwebsocket_create_context(CONTEXT_PORT_NO_LISTEN, NULL,protocols, libwebsocket_internal_extensions,NULL, NULL, -1, -1, 0); - + setConfiguration(config); re->setSupported(supported(), this); + //printf("websocketsource loaded!!!\n"); } @@ -333,17 +334,7 @@ string WebSocketSource::uuid() { return "d293f670-f0b3-11e1-aff1-0800200c9a66"; } -boost::any WebSocketSource::getProperty(VehicleProperty::Property property) -{ - if(property == VehicleProperty::VehicleSpeed) - { - //return velocity; - } - else if(property == VehicleProperty::EngineSpeed) - { - //return engineSpeed; - } -} + void WebSocketSource::subscribeToPropertyChanges(VehicleProperty::Property property) { //printf("Subscribed to property: %s\n",property.c_str()); @@ -367,34 +358,15 @@ void WebSocketSource::unsubscribeToPropertyChanges(VehicleProperty::Property pro void WebSocketSource::getPropertyAsync(AsyncPropertyReply *reply) { - /*if(reply->property == VehicleProperty::VehicleSpeed) - { - reply->value = velocity; - reply->completed(reply); - } - else if(reply->property == VehicleProperty::EngineSpeed) - { - reply->value = engineSpeed; - reply->completed(reply); - } - else if(reply->property == VehicleProperty::AccelerationX) - { - reply->value = accelerationX; - reply->completed(reply); - } - else if(reply->property == VehicleProperty::TransmissionShiftPosition) - { - reply->value = transmissionShiftPostion; - reply->completed(reply); - } - else if(reply->property == VehicleProperty::SteeringWheelAngle) - { - reply->value = steeringWheelAngle; - reply->completed(reply); - }*/ + ///TODO: fill in } -void WebSocketSource::setProperty(VehicleProperty::Property , AbstractPropertyType * ) +void WebSocketSource::getRangePropertyAsync(AsyncRangePropertyReply *reply) { + ///TODO: fill in +} +void WebSocketSource::setProperty(VehicleProperty::Property , AbstractPropertyType * ) +{ + ///TODO: fill in } diff --git a/plugins/websocketsourceplugin/websocketsource.h b/plugins/websocketsourceplugin/websocketsource.h index 3eacd70..b5eeb5c 100644 --- a/plugins/websocketsourceplugin/websocketsource.h +++ b/plugins/websocketsourceplugin/websocketsource.h @@ -34,8 +34,8 @@ class WebSocketSource : public AbstractSource public: WebSocketSource(AbstractRoutingEngine* re, map config); string uuid(); - boost::any getProperty(VehicleProperty::Property property); void getPropertyAsync(AsyncPropertyReply *reply); + void getRangePropertyAsync(AsyncRangePropertyReply *reply); void setProperty(VehicleProperty::Property, AbstractPropertyType*); void subscribeToPropertyChanges(VehicleProperty::Property property); void unsubscribeToPropertyChanges(VehicleProperty::Property property); @@ -50,7 +50,7 @@ public: void propertyChanged(VehicleProperty::Property property, AbstractPropertyType* value, string uuid) {} void supportedChanged(PropertyList) {} void setConfiguration(map config); - //void randomizeProperties(); + private: PropertyList m_supportedProperties; diff --git a/plugins/wheel/wheelplugin.h b/plugins/wheel/wheelplugin.h index 7b94c5f..5ac1528 100644 --- a/plugins/wheel/wheelplugin.h +++ b/plugins/wheel/wheelplugin.h @@ -35,6 +35,7 @@ public: string uuid(); void getPropertyAsync(AsyncPropertyReply *reply); + void getRangePropertyAsync(AsyncRangePropertyReply *reply){} void setProperty(VehicleProperty::Property, AbstractPropertyType*); void subscribeToPropertyChanges(VehicleProperty::Property property); void unsubscribeToPropertyChanges(VehicleProperty::Property property); -- 2.7.4