main: refactor service initialization
The service initialization tasks currently consist of:
- Instantiating freedesktop.ObjectManager
- Instantiating an updater instance and providing it with a dbus
connection
- Instantiating a watch instance and attaching it to the program event
loop
- Claiming a well known name on DBus
Move these service initialization tasks to a new function. This opens
the door to _not_ performing service initialization in an easy to read
way.
A couple of subtle ordering changes have occurred:
- The bus socket callbacks are attached to the event loop as soon as the
event loop is instantiated. This change doesn't have any functional
impact; any data waiting in the dbus socket isn't processed until
loop.loop() is called anyway. What the relocation does enable is access
to the event loop in the new method, via the of the bus.get_event()
method.
- watch is now instantiated prior to claiming a busname. That's ok
because that doesn't result in any observable difference to client
applications that may be watching for signals.
Change-Id: Ie0a42eb5764114110056cac2ad74da651c8dbd90
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/item_updater_main.cpp b/item_updater_main.cpp
index 0e4a35b..85dc2b3 100644
--- a/item_updater_main.cpp
+++ b/item_updater_main.cpp
@@ -16,6 +16,32 @@
#include <system_error>
+namespace openpower
+{
+namespace software
+{
+namespace updater
+{
+void initializeService(sdbusplus::bus::bus& bus)
+{
+ sdbusplus::server::manager::manager objManager(bus, SOFTWARE_OBJPATH);
+#ifdef UBIFS_LAYOUT
+ static ItemUpdaterUbi updater(bus, SOFTWARE_OBJPATH);
+ static Watch watch(
+ bus.get_event(),
+ std::bind(std::mem_fn(&ItemUpdater::updateFunctionalAssociation),
+ &updater, std::placeholders::_1));
+#elif defined MMC_LAYOUT
+ static ItemUpdaterMMC updater(bus, SOFTWARE_OBJPATH);
+#else
+ static ItemUpdaterStatic updater(bus, SOFTWARE_OBJPATH);
+#endif
+ bus.request_name(BUSNAME_UPDATER);
+}
+} // namespace updater
+} // namespace software
+} // namespace openpower
+
int main(int, char*[])
{
using namespace openpower::software::updater;
@@ -23,27 +49,12 @@
auto bus = sdbusplus::bus::new_default();
auto loop = sdeventplus::Event::get_default();
- // Add sdbusplus ObjectManager.
- sdbusplus::server::manager::manager objManager(bus, SOFTWARE_OBJPATH);
+ bus.attach_event(loop.get(), SD_EVENT_PRIORITY_NORMAL);
-#ifdef UBIFS_LAYOUT
- ItemUpdaterUbi updater(bus, SOFTWARE_OBJPATH);
-#elif defined MMC_LAYOUT
- ItemUpdaterMMC updater(bus, SOFTWARE_OBJPATH);
-#else
- ItemUpdaterStatic updater(bus, SOFTWARE_OBJPATH);
-#endif
+ initializeService(bus);
- bus.request_name(BUSNAME_UPDATER);
try
{
-#ifdef UBIFS_LAYOUT
- openpower::software::updater::Watch watch(
- loop.get(),
- std::bind(std::mem_fn(&ItemUpdater::updateFunctionalAssociation),
- &updater, std::placeholders::_1));
-#endif
- bus.attach_event(loop.get(), SD_EVENT_PRIORITY_NORMAL);
auto rc = loop.loop();
if (rc < 0)
{