manager: claim busname after startup complete

Idiomatic DBus programming would have us defer claiming a well known bus
name until after all of our startup initialization is done.  This avoids
unnecessary PropertiesChanged or ObjectManager signals during our
startup phase with callbacks that generate dbus activity such as
createObjects or setProperty.

Change-Id: I5f91bbc2a9a5b343e8db214ebadb52bc71b2d459
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/app.cpp b/app.cpp
index 8d7fba3..482fe8a 100644
--- a/app.cpp
+++ b/app.cpp
@@ -28,8 +28,8 @@
     try
     {
         phosphor::inventory::manager::Manager manager(
-            sdbusplus::bus::new_system(), BUSNAME, INVENTORY_ROOT);
-        manager.run();
+            sdbusplus::bus::new_system(), INVENTORY_ROOT);
+        manager.run(BUSNAME);
         exit(EXIT_SUCCESS);
     }
     catch (const std::exception& e)
diff --git a/manager.cpp b/manager.cpp
index 0f5b77d..e78265c 100644
--- a/manager.cpp
+++ b/manager.cpp
@@ -57,10 +57,9 @@
     return 0;
 }
 
-Manager::Manager(sdbusplus::bus::bus&& bus, const char* busname,
-                 const char* root) :
-    ServerObject<ManagerIface>(bus, root),
-    _shutdown(false), _root(root), _bus(std::move(bus)), _manager(_bus, root)
+Manager::Manager(sdbusplus::bus::bus&& bus, const char* root) :
+    ServerObject<ManagerIface>(bus, root), _shutdown(false), _root(root),
+    _bus(std::move(bus)), _manager(_bus, root)
 #ifdef CREATE_ASSOCIATIONS
     ,
     _associations(_bus)
@@ -94,8 +93,6 @@
 
     // Restore any persistent inventory
     restore();
-
-    _bus.request_name(busname);
 }
 
 void Manager::shutdown() noexcept
@@ -103,7 +100,7 @@
     _shutdown = true;
 }
 
-void Manager::run() noexcept
+void Manager::run(const char* busname)
 {
     sdbusplus::message::message unusedMsg{nullptr};
 
@@ -119,6 +116,7 @@
         }
     }
 
+    _bus.request_name(busname);
     while (!_shutdown)
     {
         try
diff --git a/manager.hpp b/manager.hpp
index 82a3fd4..3046024 100644
--- a/manager.hpp
+++ b/manager.hpp
@@ -57,17 +57,19 @@
     /** @brief Construct an inventory manager.
      *
      *  @param[in] bus - An sdbusplus bus connection.
-     *  @param[in] busname - The DBus busname to own.
      *  @param[in] root - The DBus path on which to implement
      *      an inventory manager.
      */
-    Manager(sdbusplus::bus::bus&&, const char*, const char*);
+    Manager(sdbusplus::bus::bus&&, const char*);
 
     using EventInfo =
         std::tuple<std::vector<EventBasePtr>, std::vector<Action>>;
 
-    /** @brief Start processing DBus messages. */
-    void run() noexcept;
+    /** @brief Start processing DBus messages.
+     *
+     *  @param[in] busname - The DBus busname to own.
+     */
+    void run(const char*);
 
     /** @brief Provided for testing only. */
     void shutdown() noexcept;
diff --git a/test/test.cpp b/test/test.cpp
index 7be9c64..5d020e9 100644
--- a/test/test.cpp
+++ b/test/test.cpp
@@ -472,10 +472,10 @@
 int main()
 {
     phosphor::inventory::manager::Manager mgr(sdbusplus::bus::new_default(),
-                                              MGR_SERVICE, MGR_ROOT);
+                                              MGR_ROOT);
     ExampleService d;
 
-    auto f1 = [](auto mgr) { mgr->run(); };
+    auto f1 = [](auto mgr) { mgr->run(MGR_SERVICE); };
     auto f2 = [](auto d) { d->run(); };
 
     auto t1 = std::thread(f1, &mgr);