basic routing working
authorKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 16 Aug 2012 23:19:25 +0000 (16:19 -0700)
committerKevron Rees <kevron_m_rees@linux.intel.com>
Thu, 16 Aug 2012 23:19:25 +0000 (16:19 -0700)
13 files changed:
ambd/core.cpp
ambd/main.cpp
ambd/pluginloader.cpp
ambd/pluginloader.h
config
lib/abstractsink.cpp
lib/abstractsink.h
lib/abstractsource.cpp
lib/abstractsource.h
plugins/CMakeLists.txt
plugins/CMakeLists.txt~
plugins/exampleplugin.cpp
plugins/exampleplugin.h

index ada481d..224d4e9 100644 (file)
@@ -38,6 +38,15 @@ Core::~Core()
 void Core::setSupported(PropertyList supported, AbstractSource* source)
 {
        mSources.push_back(source);
+               
+       for(PropertyList::iterator itr = supported.begin(); itr != supported.end(); itr++)
+       {
+               if(!ListPlusPlus<VehicleProperty::Property>(&mMasterPropertyList).contains((*itr)))
+               {
+                       DebugOut()<<__FUNCTION__<<"() adding suport for property "<<VehicleProperty::name((*itr))<<endl;
+                       mMasterPropertyList.push_back((*itr));
+               }
+       }       
 }
 
 
@@ -88,35 +97,73 @@ void Core::updateSupported(PropertyList added, PropertyList removed)
 
 void Core::updateProperty(VehicleProperty::Property property, boost::any value)
 {
-
+       SinkList list = propertySinkMap[property];
+       
+       DebugOut()<<__FUNCTION__<<"() there are "<<list.size()<<" sinks connected to property: "<<VehicleProperty::name(property)<<endl;
+       
+       for(SinkList::iterator itr = list.begin(); itr != list.end(); itr++)
+       {
+               (*itr)->propertyChanged(property, value,(*itr)->uuid());
+       }
 }
 
-void Core::setProperty(VehicleProperty::Property , boost::any )
+void Core::setProperty(VehicleProperty::Property property, boost::any value)
 {
-
+       for(SourceList::iterator itr = mSources.begin(); itr != mSources.end(); itr++)
+       {
+               AbstractSource* src = (*itr);
+               PropertyList properties = src->supported();
+               if(ListPlusPlus<VehicleProperty::Property>(&properties).contains(property))
+               {
+                       src->setProperty(property, value);
+               }
+       }
 }
 
 void Core::subscribeToProperty(VehicleProperty::Property property, AbstractSink* self)
 {
-       if(propertySinkMap.find(property) == propertySinkMap.end())
+       if(!ListPlusPlus<VehicleProperty::Property>(&mMasterPropertyList).contains((property)))
        {
-               DebugOut()<<__FUNCTION__<<"property not supported: "<<VehicleProperty::name(property);
+               DebugOut()<<__FUNCTION__<<"(): property not supported: "<<VehicleProperty::name(property)<<endl;
                return; 
        }
        
+       if(propertySinkMap.find(property) == propertySinkMap.end())
+       {
+               propertySinkMap[property] = SinkList();
+       }
+       
        SinkList list = propertySinkMap[property];
        
        if(!ListPlusPlus<AbstractSink*>(&list).contains(self))
        {
-               list.push_back(self);
+               propertySinkMap[property].push_back(self);
        }
        
-       
 }
 
-void Core::unsubscribeToProperty(VehicleProperty::Property , AbstractSink* self)
+void Core::unsubscribeToProperty(VehicleProperty::Property property, AbstractSink* self)
 {
-
+       if(propertySinkMap.find(property) == propertySinkMap.end())
+       {
+               DebugOut()<<__FUNCTION__<<"property not supported: "<<VehicleProperty::name(property);
+               return; 
+       }
+       
+       SinkList list = propertySinkMap[property];
+       
+       ListPlusPlus<AbstractSink*>(&list).removeOne(self);
+       
+       for(SourceList::iterator itr = mSources.begin(); itr != mSources.end(); itr++)
+       {
+               AbstractSource* src = (*itr);
+               PropertyList properties = src->supported();
+               
+               if(ListPlusPlus<VehicleProperty::Property>(&properties).contains(property))
+               {
+                       src->unsubscribeToPropertyChanges(property);
+               }
+       }
 }
 
 
index 1388a03..fc1a392 100644 (file)
@@ -44,7 +44,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 #endif
 
 #include "pluginloader.h"
-#include "abstractsource.h"
+#include "core.h"
 
 using namespace std;
 
@@ -129,7 +129,7 @@ int main(int argc, char **argv)
        
        g_type_init();
        
-       PluginLoader loader(config);
+       PluginLoader loader(config, new Core());
        
        if(!loader.sources().size())
        {
@@ -137,6 +137,7 @@ int main(int argc, char **argv)
        }
        
        
+       
 #ifdef USE_QT_CORE
        
        app.exec();
index a0643bb..8073ffa 100644 (file)
@@ -33,7 +33,7 @@ using namespace std;
  * 
 **********************************************/
 
-PluginLoader::PluginLoader(string configFile): f_create(NULL)
+PluginLoader::PluginLoader(string configFile, AbstractRoutingEngine* re): f_create(NULL), routingEngine(re)
 {
        DebugOut()<<"Loading config file: "<<configFile<<endl;
        
@@ -102,19 +102,23 @@ PluginLoader::PluginLoader(string configFile): f_create(NULL)
                json_reader_read_element(reader,i);
                
                string path = json_reader_get_string_value(reader);
-               AbstractSink* plugin = loadPlugin<AbstractSink*>(path);
+               AbstractSinkManager* plugin = loadPlugin<AbstractSinkManager*>(path);
                
-               if(plugin != nullptr)
-                       mSinks.push_back(plugin);
+               if(plugin == nullptr)
+               {
+                       throw std::runtime_error("plugin is not a SinkManager");
+               }
                
                json_reader_end_element(reader);
+               
        }
        
        json_reader_end_member(reader);
        
        ///TODO: this will probably explode:
        
-       g_error_free(error);
+       if(error) g_error_free(error);
+       
        g_object_unref(reader);
        g_object_unref(parser);
        
index cbb3971..43b6bfc 100644 (file)
@@ -26,20 +26,21 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include "abstractsource.h"
 #include "abstractsink.h"
+#include "abstractroutingengine.h"
 #include "debugout.h"
 
 
 
 using namespace std;
 
-typedef void* create_t();
+typedef void* create_t(AbstractRoutingEngine*);
 
 
 class PluginLoader
 {
 
 public:
-       PluginLoader(string configFile);
+       PluginLoader(string configFile, AbstractRoutingEngine* routingEngine);
 
        SourceList sources();
        SinkList sinks();
@@ -78,7 +79,7 @@ private: ///methods:
                
                if(f_create) 
                {
-                       void* obj = f_create();
+                       void* obj = f_create(routingEngine);
                        return static_cast<T>( obj );
                }
                
@@ -90,6 +91,8 @@ private:
        std::string mPluginPath;
        std::string mErrorString;
        
+       AbstractRoutingEngine* routingEngine;
+       
        SourceList mSources;
        SinkList mSinks;
        
diff --git a/config b/config
index 3fe011e..757c65c 100644 (file)
--- a/config
+++ b/config
@@ -1,4 +1,5 @@
 {
-       "sources" : [ "../plugins/exampleplugin.so" ]
+       "sources" : [ "../plugins/examplesourceplugin.so" ],
+       "sinks": [ "../plugins/examplesinkplugin.so" ]
 }
 
index b77e096..a52616b 100644 (file)
 #include "abstractsink.h"
 #include "abstractroutingengine.h"
 
-AbstractSink::AbstractSink()
+AbstractSink::AbstractSink(AbstractRoutingEngine* engine)
+:routingEngine(engine)
 {
 
 }
 
-void AbstractSink::setRoutingEngine(AbstractRoutingEngine* engine)
+AbstractSink::~AbstractSink()
 {
-       routingEngine = engine;
-}
 
+}
 
-void AbstractSinkManager::setRoutingEngine(AbstractRoutingEngine* engine)
+AbstractSinkManager::AbstractSinkManager(AbstractRoutingEngine* engine)
+:routingEngine(engine)
 {
-       routingEngine = engine;
+
 }
+
+
index 3ce54d7..af71dd3 100644 (file)
@@ -38,15 +38,14 @@ class AbstractSink
 {
 
 public:
-       AbstractSink();
-       void setRoutingEngine(AbstractRoutingEngine* engine);
+       AbstractSink(AbstractRoutingEngine* engine);
+       virtual ~AbstractSink();
        
        ///Pure virtual methods:
        
        virtual string uuid() = 0;
        virtual void propertyChanged(VehicleProperty::Property property, boost::any value, string  uuid) = 0;
        virtual void supportedChanged(PropertyList supportedProperties) = 0;
-       virtual PropertyList subscriptions() = 0;
        
 protected:
        AbstractRoutingEngine* routingEngine;
@@ -56,8 +55,7 @@ class AbstractSinkManager
 {
 public:
        
-       virtual SinkList sinks() = 0;
-       void setRoutingEngine(AbstractRoutingEngine* engine);
+       AbstractSinkManager(AbstractRoutingEngine* engine);
        
 protected:
        AbstractRoutingEngine* routingEngine;
index ae2f25c..ada4b96 100644 (file)
 
 #include "abstractsource.h"
 
-AbstractSource::AbstractSource()
-: routingEngine(nullptr)
+AbstractSource::AbstractSource(AbstractRoutingEngine* engine)
+: AbstractSink(engine), routingEngine(engine)
 {
-
+       
 }
 
-void AbstractSource::setRoutingEngine(AbstractRoutingEngine* engine)
+AbstractSource::~AbstractSource()
 {
-       routingEngine = engine;
+
 }
 
 
index f4e37d2..d9ba3ce 100644 (file)
@@ -24,6 +24,7 @@
 #include <list>
 #include <boost/any.hpp>
 
+#include "abstractsink.h"
 #include "vehicleproperty.h"
 #include "abstractroutingengine.h"
 
@@ -33,16 +34,15 @@ class AbstractSource;
 
 typedef list<AbstractSource*> SourceList;
 
-class AbstractSource
+class AbstractSource: public AbstractSink
 {
 
 public:
-       AbstractSource();
-       void setRoutingEngine(AbstractRoutingEngine* engine);
+       AbstractSource(AbstractRoutingEngine* engine);
+       virtual ~AbstractSource();
        
        ///pure virtual methods:
 
-       virtual string uuid() = 0;
        virtual void setProperty(VehicleProperty::Property property, boost::any value) = 0;
        virtual void subscribeToPropertyChanges(VehicleProperty::Property property) = 0;
        virtual void unsubscribeToPropertyChanges(VehicleProperty::Property property) = 0;
@@ -51,7 +51,9 @@ public:
 
 protected:
        AbstractRoutingEngine* routingEngine;
-           
+       
+private:
+       AbstractSource():AbstractSink(nullptr) { }
 };
 
 #endif // ABSTRACTSOURCE_H
index ed50e32..f9a3476 100644 (file)
@@ -1,9 +1,17 @@
 
 include_directories(${CMAKE_SOURCE_DIR}/lib ${include_dirs})
 
-set(exampleplugin_headers exampleplugin.h)
-set(exampleplugin_sources exampleplugin.cpp)
-add_library(exampleplugin MODULE ${exampleplugin_sources})
-set_target_properties(exampleplugin PROPERTIES PREFIX "")
-target_link_libraries(exampleplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
+set(examplesourceplugin_headers exampleplugin.h)
+set(examplesourceplugin_sources exampleplugin.cpp)
 
+add_library(examplesourceplugin MODULE ${examplesourceplugin_sources})
+set_target_properties(examplesourceplugin PROPERTIES PREFIX "")
+target_link_libraries(examplesourceplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
+
+
+set(examplesinkplugin_headers examplesink.h)
+set(examplesinkplugin_sources examplesink.cpp)
+
+add_library(examplesinkplugin MODULE ${examplesinkplugin_sources})
+set_target_properties(examplesinkplugin PROPERTIES PREFIX "")
+target_link_libraries(examplesinkplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
\ No newline at end of file
index a9bcf6e..ed50e32 100644 (file)
@@ -7,15 +7,3 @@ add_library(exampleplugin MODULE ${exampleplugin_sources})
 set_target_properties(exampleplugin PROPERTIES PREFIX "")
 target_link_libraries(exampleplugin -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
 
-if(use_qtcore)
-       if(nobdy_compat)
-               pkg_check_modules(nobdy REQUIRED nobdy)
-               set(nobdyplugin_headers nobdycompatplugin.h)
-               set(nobdyplugin_sources nobdycompatplugin.cpp)
-               qt4_wrap_cpp(nobdy_moc_sources ${nobdyplugin_headers})
-               add_library(nobdyplugin MODULE ${nobdyplugin_sources} ${nobdy_moc_sources})
-               set_target_properties(nobdyplugin PROPERTIES PREFIX "")
-               include_directories(${nobdy_INCLUDE_DIRS} ${include_dirs})
-               target_link_libraries(nobdyplugin ${nobdy_LIBRARIES} -lamb -L${CMAKE_CURRENT_BINARY_DIR}/lib ${link_libraries})
-       endif(nobdy_compat)
-endif(use_qtcore)
\ No newline at end of file
index 5e02d67..253c587 100644 (file)
@@ -20,19 +20,37 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
 #include <iostream>
 #include <boost/assert.hpp>
+#include <glib.h>
 
 using namespace std;
 
 #include "debugout.h"
 
-ExampleSourcePlugin::ExampleSourcePlugin()
+static gboolean timeoutCallback(gpointer data)
 {
+       ExampleSourcePlugin* src = (ExampleSourcePlugin*)data;
        
+       src->randomizeProperties();
+       
+       return true;
 }
 
-extern "C" AbstractSource * create()
+ExampleSourcePlugin::ExampleSourcePlugin(AbstractRoutingEngine* re)
+:AbstractSource(re), velocity(0), engineSpeed(0)
 {
-       return new ExampleSourcePlugin();
+       re->setSupported(supported(), this);
+       
+       debugOut("setting timeout");
+       g_timeout_add(1000, timeoutCallback, this );
+       
+}
+
+
+
+extern "C" AbstractSource * create(AbstractRoutingEngine* routingengine)
+{
+       return new ExampleSourcePlugin(routingengine);
+       
 }
 
 string ExampleSourcePlugin::uuid()
@@ -64,3 +82,14 @@ void ExampleSourcePlugin::unsubscribeToPropertyChanges(VehicleProperty::Property
        mRequests.remove(property);
 }
 
+void ExampleSourcePlugin::randomizeProperties()
+{
+       velocity = 1 + (255.00 * (rand() / (RAND_MAX + 1.0)));
+       engineSpeed = 1 + (15000.00 * (rand() / (RAND_MAX + 1.0)));
+       
+       DebugOut()<<"setting velocity to: "<<velocity<<endl;
+       DebugOut()<<"setting enginespeed to: "<<engineSpeed<<endl;
+       
+       routingEngine->updateProperty(VehicleProperty::VehicleSpeed, velocity);
+       routingEngine->updateProperty(VehicleProperty::EngineSpeed, engineSpeed);
+}
index 3371c8f..bb622ae 100644 (file)
@@ -28,15 +28,23 @@ class ExampleSourcePlugin: public AbstractSource
 {
 
 public:
-       ExampleSourcePlugin();
+       ExampleSourcePlugin(AbstractRoutingEngine* re);
+       
        string uuid();
        void setProperty(VehicleProperty::Property, boost::any);
        void subscribeToPropertyChanges(VehicleProperty::Property property);
        void unsubscribeToPropertyChanges(VehicleProperty::Property property);
        PropertyList supported();
        
+       void propertyChanged(VehicleProperty::Property property, boost::any value, string uuid) {}
+       void supportedChanged(PropertyList) {}
+       
+       void randomizeProperties();
+       
 private:
        PropertyList mRequests;
+       uint16_t velocity;
+       uint16_t engineSpeed;
 };
 
 #endif // EXAMPLEPLUGIN_H