Commits

Anonymous committed 96e092b

DionaeaHarvester: connects/disconnects to the DB whenever notify() is executed

  • Participants
  • Parent commits 47c1054

Comments (0)

Files changed (2)

File modules/channel/dionaeaHarvester/dionaeaHarvester.cc

 }
 
 bool DionaeaHarvester::prepare() {
+    return true;
+}
 
-    string_t connection_credentials = "dbname=" + credentials.database + 
+bool DionaeaHarvester::connect() {
+
+    static string_t connection_credentials = "dbname=" + credentials.database + 
                                       " user=" + credentials.user +
                                       " password=" + credentials.password + 
                                       " host=" + credentials.hostname;
+
+    // let's call it always, just to avoid memleaks and stuff
+    // BTW, should we delete dbconnection_t object?
+    disconnect();
     try {
         connection = new dbconnection_t(connection_credentials);
     } catch(alloc_ex& ex) {
 };
 
 bool DionaeaHarvester::notify() {
-    if(check_and_complain_connection(connection)) {
-        fetch_data();
-        return true;
-    }
-    return false;
+    connect();
+    fetch_data();
+    disconnect();
 }
 
 bool DionaeaHarvester::start() {
 }
 
 bool DionaeaHarvester::stop() {
+    if(check_connection(connection))
+        disconnect();
+    return true;
+}
+bool DionaeaHarvester::disconnect() {
     if(check_connection(connection)) {
         connection->disconnect();
         LOG(INFO) << "Disconnected from "<< credentials.user << "@"
                   << credentials.hostname << ":" << credentials.database;
+        delete connection;
         connection=NULL;
     }
 };
 };
 
 void DionaeaHarvester::update_select_query() {
-    if(fetch==INTERVAL) {
+    if(fetch == INTERVAL) {
         Python::calculate_ranges_from_interval(range.interval,range.from, range.to);
     }
     
-    if(fetch==ALL)
+    if(fetch == ALL)
         snprintf(formated_query,QUERY_LIMIT,select_query);
     else
         snprintf(formated_query,QUERY_LIMIT,select_query,range.from.c_str(),range.to.c_str());
      if(misc.exists("interval")) {
         setup_select_query_interval(misc);
     } else if(misc.exists("from") && misc.exists("to")  && from_to_zeroed(misc)) {
-        LOG(DEBUG) << "from to zeroed";
         setup_select_query_all();
     } else if(misc.exists("from") && misc.exists("to")  && !from_to_zeroed(misc)) {
-        LOG(DEBUG) << "from to not zeroed";
         setup_select_query_range(misc);
     } else {
         setup_default_query();

File modules/channel/dionaeaHarvester/dionaeaHarvester.h

         inline bool check_connection(const dbconnection_t* c);
         inline bool check_and_complain_connection(const dbconnection_t* c);
 
+        bool connect();
+        bool disconnect();
  private:
         int interval;
         times_s range;