Fawkes API  Fawkes Development Version
event_trigger_manager.h
1 /***************************************************************************
2  * event_trigger_manager.h - Manager to realize triggers on events in the robot memory
3  *
4  *
5  * Created: 3:53:45 PM 2016
6  * Copyright 2016 Frederik Zwilling
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #ifndef FAWKES_SRC_PLUGINS_ROBOT_MEMORY_EVENT_TRIGGER_MANAGER_H_
23 #define FAWKES_SRC_PLUGINS_ROBOT_MEMORY_EVENT_TRIGGER_MANAGER_H_
24 
25 #include "event_trigger.h"
26 
27 #include <aspect/configurable.h>
28 #include <aspect/logging.h>
29 #include <core/exception.h>
30 #include <core/threading/mutex_locker.h>
31 #include <plugin/loader.h>
32 #include <plugins/mongodb/aspect/mongodb_conncreator.h>
33 #include <plugins/mongodb/utils.h>
34 
35 #include <boost/bind.hpp>
36 #include <bsoncxx/builder/basic/document.hpp>
37 #include <list>
38 
39 namespace fawkes {
40 #ifdef USE_TIMETRACKER
41 class TimeTracker;
42 #endif
43 } // namespace fawkes
44 
46 {
47  /// Access for robot memory to use the check_events function in the loop
48  friend class RobotMemory;
49 
50 public:
52  fawkes::Configuration * config,
53  fawkes::MongoDBConnCreator *mongo_connection_manager);
54  virtual ~EventTriggerManager();
55 
56  /**
57  * Register a trigger to be notified when the robot memory is updated and the updated document matches the query
58  * @param query Query the updated document has to match
59  * @param dbcollection db.collection to use
60  * @param callback Callback function (e.g. &Class::callback)
61  * @param obj Pointer to class the callback is a function of (usaually this)
62  * @return Trigger object pointer, save it to remove the trigger later
63  */
64  template <typename T>
66  register_trigger(const bsoncxx::document::view &query,
67  std::string dbcollection,
68  void (T::*callback)(const bsoncxx::document::view &),
69  T *obj)
70  {
71  //logger_->log_warn("RM|EventTriggerManager", "Registering trigger on %s", dbcollection.c_str());
72  //lock to be thread safe (e.g. registration during checking)
73  fawkes::MutexLocker lock(mutex_);
74 
75  if (!query.empty()) {
76  throw fawkes::Exception("Non-empty queries are not implemented!");
77  }
78 
79  //check if dbcollection is local or replicated
80  mongocxx::client *con;
81  if (std::find(dbnames_distributed_.begin(),
82  dbnames_distributed_.end(),
83  get_db_name(dbcollection))
84  != dbnames_distributed_.end()) {
85  con = con_replica_;
86  } else {
87  con = con_local_;
88  }
89  auto db_coll_pair = split_db_collection_string(dbcollection);
90  auto collection = con->database(db_coll_pair.first)[db_coll_pair.second];
91  EventTrigger *trigger = new EventTrigger(create_change_stream(collection, query),
92  query,
93  dbcollection,
94  boost::bind(callback, obj, _1));
95  triggers.push_back(trigger);
96  return trigger;
97  }
98 
99  void remove_trigger(EventTrigger *trigger);
100 
101  static std::string get_db_name(const std::string &ns);
102 
103 private:
104  void check_events();
105  mongocxx::change_stream create_change_stream(mongocxx::collection & collection,
106  bsoncxx::document::view query);
107 
108  std::string name = "RobotMemory EventTriggerManager";
109  fawkes::Logger * logger_;
110  fawkes::Configuration *config_;
111  fawkes::Mutex * mutex_;
112 
113  fawkes::MongoDBConnCreator *mongo_connection_manager_;
114  //MongoDB connections (necessary because the mongos instance used by the robot memory has no access to the oplog)
115  mongocxx::client *con_local_;
116  mongocxx::client *con_replica_;
117 
118  std::vector<std::string> dbnames_distributed_;
119  std::vector<std::string> dbnames_local_;
120  bool cfg_debug_;
121 
122  std::list<EventTrigger *> triggers;
123 
124 #ifdef USE_TIMETRACKER
125  fawkes::TimeTracker *tt_;
126  unsigned int tt_loopcount_;
127  unsigned int ttc_trigger_loop_;
128  unsigned int ttc_callback_loop_;
129  unsigned int ttc_callback_;
130  unsigned int ttc_reinit_;
131 #endif
132 };
133 
134 #endif //FAWKES_SRC_PLUGINS_ROBOT_MEMORY_EVENT_TRIGGER_MANAGER_H_
EventTriggerManager::remove_trigger
void remove_trigger(EventTrigger *trigger)
Remove a previously registered trigger.
Definition: event_trigger_manager.cpp:163
fawkes::Mutex
Definition: mutex.h:36
EventTriggerManager::EventTriggerManager
EventTriggerManager(fawkes::Logger *logger, fawkes::Configuration *config, fawkes::MongoDBConnCreator *mongo_connection_manager)
Constructor for class managing EventTriggers.
Definition: event_trigger_manager.cpp:48
fawkes::MongoDBConnCreator
Definition: mongodb_conncreator.h:40
RobotMemory
Definition: robot_memory.h:45
fawkes::MutexLocker
Definition: mutex_locker.h:37
EventTrigger
Definition: event_trigger.h:30
fawkes::Configuration
Definition: config.h:68
fawkes::Logger
Definition: logger.h:40
EventTriggerManager::get_db_name
static std::string get_db_name(const std::string &ns)
Split database name from namespace.
Definition: event_trigger_manager.cpp:195
fawkes
EventTriggerManager
Definition: event_trigger_manager.h:44
EventTriggerManager::register_trigger
EventTrigger * register_trigger(const bsoncxx::document::view &query, std::string dbcollection, void(T::*callback)(const bsoncxx::document::view &), T *obj)
Register a trigger to be notified when the robot memory is updated and the updated document matches t...
Definition: event_trigger_manager.h:65
fawkes::TimeTracker
Definition: tracker.h:40
RobotMemory::query
mongocxx::cursor query(bsoncxx::document::view query, const std::string &collection_name="", mongocxx::options::find query_options=mongocxx::options::find())
Query information from the robot memory.
Definition: robot_memory.cpp:201
fawkes::Exception
Definition: exception.h:39