Recreate the network objects once the timer expires

As we get multiple network change events once systemd-
networkd starts,This commit starts the timer once it
gets the network change event and refresh the network
objects once the timer expires.

Change-Id: I86d1547763d03f527a8bdb04e2b2780f6cd6fdc2
Signed-off-by: Ratan Gupta <ratagupt@in.ibm.com>
diff --git a/network_manager_main.cpp b/network_manager_main.cpp
index 993efc7..7fa3b05 100644
--- a/network_manager_main.cpp
+++ b/network_manager_main.cpp
@@ -1,5 +1,7 @@
+#include "config.h"
 #include "network_manager.hpp"
 #include "rtnetlink_server.hpp"
+#include "timer.hpp"
 
 #include <memory>
 
@@ -13,6 +15,12 @@
 {
 
 std::unique_ptr<phosphor::network::Manager> manager = nullptr;
+std::unique_ptr<phosphor::network::Timer> refreshTimer = nullptr;
+
+void refreshObjects()
+{
+    manager->createChildObjects();
+}
 
 } //namespace network
 } //namespace phosphor
@@ -21,6 +29,12 @@
 {
     using namespace phosphor::logging;
 
+    std::function<void()> func(
+            std::bind(&phosphor::network::refreshObjects));
+
+    phosphor::network::refreshTimer =
+        std::make_unique<phosphor::network::Timer>(func);
+
     auto bus = sdbusplus::bus::new_default();
 
     // Need sd_event to watch for OCC device errors
@@ -51,6 +65,6 @@
 
     phosphor::network::manager->createChildObjects();
 
-
     return svr.run();
 }
+
diff --git a/rtnetlink_server.cpp b/rtnetlink_server.cpp
index d3cac19..8e636a1 100644
--- a/rtnetlink_server.cpp
+++ b/rtnetlink_server.cpp
@@ -1,5 +1,5 @@
-#include "xyz/openbmc_project/Common/error.hpp"
 #include "rtnetlink_server.hpp"
+#include "timer.hpp"
 #include "types.hpp"
 #include "util.hpp"
 
@@ -13,18 +13,22 @@
 #include <unistd.h>
 
 #include <phosphor-logging/log.hpp>
-#include <phosphor-logging/elog-errors.hpp>
 
 #include <memory>
-#include <iostream>
 
 namespace phosphor
 {
 namespace network
 {
+
+extern std::unique_ptr<phosphor::network::Timer> refreshTimer;
+
 namespace rtnetlink
 {
 
+using namespace std::chrono_literals;
+constexpr auto networkChangeTimeout = 1s; //seconds
+
 /* Call Back for the sd event loop */
 static int eventHandler(sd_event_source* es, int fd, uint32_t revents,
                         void* userdata)
@@ -43,9 +47,16 @@
             if (netLinkHeader->nlmsg_type == RTM_NEWADDR ||
                 netLinkHeader->nlmsg_type == RTM_DELADDR)
             {
-                // TODO delete the below trace in later commit.
-                std::cout << "Address Changed\n";
-
+                // starting the timer here to make sure that we don't want
+                // create the child objects multiple times.
+                if (refreshTimer->isExpired())
+                {
+                    using namespace std::chrono;
+                    auto time = duration_cast<microseconds>(networkChangeTimeout);
+                    // if start timer throws exception then let the application
+                    // crash
+                    refreshTimer->startTimer(time);
+                } // end if
             } // end if
 
         } // end for