[dss-commits] r8789 - in dss/trunk: core core/scripting tests

dss-commits at forum.digitalstrom.org dss-commits at forum.digitalstrom.org
Tue Sep 22 17:09:32 CEST 2009


Author: pstaehlin
Date: 2009-09-22 17:09:31 +0200 (Tue, 22 Sep 2009)
New Revision: 8789

Modified:
   dss/trunk/core/event.cpp
   dss/trunk/core/event.h
   dss/trunk/core/scripting/modeljs.cpp
   dss/trunk/tests/eventtests.cpp
Log:
Always construct a subscriptions id.


Modified: dss/trunk/core/event.cpp
===================================================================
--- dss/trunk/core/event.cpp	2009-09-22 15:06:01 UTC (rev 8788)
+++ dss/trunk/core/event.cpp	2009-09-22 15:09:31 UTC (rev 8789)
@@ -68,6 +68,12 @@
     reset();
   }
 
+  Event::Event()
+  : m_Name("unnamed_event")
+  {
+    reset();
+  }
+
   Event::~Event() {
     delete m_RaisedAtDevice;
     m_RaisedAtDevice = NULL;
@@ -215,7 +221,7 @@
           {
             if((*ipSubscription)->matches(*toProcess)) {
               bool called = false;
-              Logger::getInstance()->log(string("EventInterpreter: Subscription'") + (*ipSubscription)->getID() + "' matches event");
+              Logger::getInstance()->log(string("EventInterpreter: Subscription '") + (*ipSubscription)->getID() + "' matches event");
 
               EventInterpreterPlugin* plugin = getPluginByName((*ipSubscription)->getHandlerName());
               if(plugin != NULL) {
@@ -229,7 +235,7 @@
               }
 
             } else {
-              Logger::getInstance()->log(string("EventInterpreter: No match on subscription'") + (*ipSubscription)->getID() + "'");
+              Logger::getInstance()->log(string("EventInterpreter: No match on subscription '") + (*ipSubscription)->getID() + "'");
             }
           }
 
@@ -252,6 +258,8 @@
   } // getPluginByName
 
   void EventInterpreter::subscribe(boost::shared_ptr<EventSubscription> _subscription) {
+    assert(_subscription != NULL);
+    assert(subscriptionByID(_subscription->getID()) == NULL);
     m_Subscriptions.push_back(_subscription);
   } // subscribe
 
@@ -266,6 +274,28 @@
     }
   } // unsubscribe
 
+  boost::shared_ptr<EventSubscription> EventInterpreter::subscriptionByID(const std::string& _subscriptionID) {
+    boost::shared_ptr<EventSubscription> result;
+    for(vector< boost::shared_ptr<EventSubscription> >::iterator ipSubscription = m_Subscriptions.begin(), e = m_Subscriptions.end();
+        ipSubscription != e; ++ipSubscription)
+    {
+      if((*ipSubscription)->getID() == _subscriptionID) {
+        result = *ipSubscription;
+        break;
+      }
+    }
+    return result;
+  } // subscriptionByID
+
+  std::string EventInterpreter::uniqueSubscriptionID(const std::string& _proposal) {
+    std::string result = _proposal;
+    int index = 1;
+    while(subscriptionByID(result) != NULL) {
+      result = _proposal + intToString(index++);
+    }
+    return result;
+  } // uniqueSubscriptionID
+
   void EventInterpreter::loadFromXML(const string& _fileName) {
     const int apartmentConfigVersion = 1;
     Logger::getInstance()->log(string("EventInterpreter: Loading subscriptions from '") + _fileName + "'");
@@ -365,7 +395,7 @@
       }
     }
 
-    boost::shared_ptr<EventSubscription> subscription(new EventSubscription(evtName, handlerName, opts));
+    boost::shared_ptr<EventSubscription> subscription(new EventSubscription(evtName, handlerName, *this, opts));
     try {
       XMLNode& filterNode = _node.getChildByName("filter");
       loadFilter(filterNode, *subscription);
@@ -574,24 +604,15 @@
 
   //================================================== EventSubscription
 
-  EventSubscription::EventSubscription(const string& _eventName, const string& _handlerName, boost::shared_ptr<SubscriptionOptions> _options)
+  EventSubscription::EventSubscription(const string& _eventName, const string& _handlerName, EventInterpreter& _interpreter, boost::shared_ptr<SubscriptionOptions> _options)
   : m_EventName(_eventName),
     m_HandlerName(_handlerName),
-    m_ID(_eventName + _handlerName),
     m_SubscriptionOptions(_options)
   {
+    m_ID = _interpreter.uniqueSubscriptionID(_eventName + "_" +  _handlerName);
     initialize();
   } // ctor
 
-  EventSubscription::EventSubscription(const string& _eventName, const string& _handlerName, const string& _id, boost::shared_ptr<SubscriptionOptions> _options)
-  : m_EventName(_eventName),
-    m_HandlerName(_handlerName),
-    m_ID(_id),
-    m_SubscriptionOptions(_options)
-  {
-    initialize();
-  } // ctor(with id)
-
   EventSubscription::~EventSubscription() {
   } // dtor
 

Modified: dss/trunk/core/event.h
===================================================================
--- dss/trunk/core/event.h	2009-09-22 15:06:01 UTC (rev 8788)
+++ dss/trunk/core/event.h	2009-09-22 15:09:31 UTC (rev 8789)
@@ -76,6 +76,7 @@
     Event(const string& _name, Zone* _context);
     Event(const string& _name, DeviceReference* _ref);
     Event(const string& _name);
+    Event();
 
     ~Event();
 
@@ -186,8 +187,7 @@
   protected:
     void initialize();
   public:
-    EventSubscription(const string& _eventName, const string& _handlerName, boost::shared_ptr<SubscriptionOptions> _options);
-    EventSubscription(const string& _eventName, const string& _handlerName, const string& _id, boost::shared_ptr<SubscriptionOptions> _options);
+    EventSubscription(const string& _eventName, const string& _handlerName, EventInterpreter& _interpreter, boost::shared_ptr<SubscriptionOptions> _options);
 
     ~EventSubscription();
 
@@ -289,7 +289,7 @@
   private:
     void loadSubscription(XMLNode& _node);
     void loadFilter(XMLNode& _node, EventSubscription& _subscription);
-    EventInterpreterPlugin* getPluginByName(const string& _name);
+    boost::shared_ptr<EventSubscription> subscriptionByID(const std::string& _name);
   protected:
     virtual void doStart();
   public:
@@ -301,11 +301,13 @@
     virtual void execute();
 
     void addPlugin(EventInterpreterPlugin* _plugin);
+    EventInterpreterPlugin* getPluginByName(const std::string& _name);
 
     void subscribe(boost::shared_ptr<EventSubscription> _subscription);
     void unsubscribe(const string& _subscriptionID);
 
     void loadFromXML(const string& _fileName);
+    std::string uniqueSubscriptionID(const std::string& _proposal);
 
     int getEventsProcessed() const { return m_EventsProcessed; }
     EventQueue& getQueue() { return *m_Queue; }

Modified: dss/trunk/core/scripting/modeljs.cpp
===================================================================
--- dss/trunk/core/scripting/modeljs.cpp	2009-09-22 15:06:01 UTC (rev 8788)
+++ dss/trunk/core/scripting/modeljs.cpp	2009-09-22 15:09:31 UTC (rev 8789)
@@ -720,7 +720,7 @@
 
         }
 
-        boost::shared_ptr<EventSubscription> subscription(new EventSubscription(eventName, handlerName, opts));
+        boost::shared_ptr<EventSubscription> subscription(new EventSubscription(eventName, handlerName, ext->getEventInterpreter(), opts));
 
         JSObject* obj = ext->createJSSubscription(*ctx, subscription);
         *rval = OBJECT_TO_JSVAL(obj);

Modified: dss/trunk/tests/eventtests.cpp
===================================================================
--- dss/trunk/tests/eventtests.cpp	2009-09-22 15:06:01 UTC (rev 8788)
+++ dss/trunk/tests/eventtests.cpp	2009-09-22 15:09:31 UTC (rev 8789)
@@ -77,7 +77,7 @@
 
   boost::shared_ptr<SubscriptionOptions> opts(new SubscriptionOptions());
   opts->setParameter("event_name", "event1");
-  boost::shared_ptr<EventSubscription> subscription(new EventSubscription("my_event", "raise_event", opts));
+  boost::shared_ptr<EventSubscription> subscription(new EventSubscription("my_event", "raise_event", interpreter, opts));
   interpreter.subscribe(subscription);
 
   queue.pushEvent(pEvent);



More information about the dss-commits mailing list