Move io context to singleton

The way we pass around io contexts is somewhat odd.  Boost maintainers
in slack recommended that we just have a method that returns an io
context, and from there we can control this (context link lost years
ago).

The new version of clang claims the singleton pattern of passing in an
io_context pattern is a potential nullptr dereference.  It's technically
correct, as calling the singleton without immediately initializing the
io context will lead to a crash.

This commit implements what the boost maintainers suggested, having a
single method that returns "the context" that should be used.  This also
helps to maintain isolation, as some pieces are no longer tied directly
to dbus to get their reactor.

Tested: WIP

Change-Id: Ifaa11335ae00a3d092ecfdfb26a38380227e8576
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/redfish-core/include/event_service_manager.hpp b/redfish-core/include/event_service_manager.hpp
index 61b837c..a5c22f4 100644
--- a/redfish-core/include/event_service_manager.hpp
+++ b/redfish-core/include/event_service_manager.hpp
@@ -10,6 +10,7 @@
 #include "event_matches_filter.hpp"
 #include "event_service_store.hpp"
 #include "filesystem_log_watcher.hpp"
+#include "io_context_singleton.hpp"
 #include "logging.hpp"
 #include "metric_report.hpp"
 #include "ossl_random.hpp"
@@ -18,7 +19,6 @@
 #include "subscription.hpp"
 #include "utils/time_utils.hpp"
 
-#include <boost/asio/io_context.hpp>
 #include <boost/circular_buffer.hpp>
 #include <boost/circular_buffer/base.hpp>
 #include <boost/container/flat_map.hpp>
@@ -77,8 +77,6 @@
     constexpr static size_t maxMessages = 200;
     boost::circular_buffer<Event> messages{maxMessages};
 
-    boost::asio::io_context& ioc;
-
   public:
     EventServiceManager(const EventServiceManager&) = delete;
     EventServiceManager& operator=(const EventServiceManager&) = delete;
@@ -86,16 +84,15 @@
     EventServiceManager& operator=(EventServiceManager&&) = delete;
     ~EventServiceManager() = default;
 
-    explicit EventServiceManager(boost::asio::io_context& iocIn) : ioc(iocIn)
+    explicit EventServiceManager()
     {
         // Load config from persist store.
         initConfig();
     }
 
-    static EventServiceManager&
-        getInstance(boost::asio::io_context* ioc = nullptr)
+    static EventServiceManager& getInstance()
     {
-        static EventServiceManager handler(*ioc);
+        static EventServiceManager handler;
         return handler;
     }
 
@@ -127,7 +124,7 @@
                 continue;
             }
             std::shared_ptr<Subscription> subValue =
-                std::make_shared<Subscription>(newSub, *url, ioc);
+                std::make_shared<Subscription>(newSub, *url, getIoContext());
             std::string id = subValue->userSub->id;
             subValue->deleter = [id]() {
                 EventServiceManager::getInstance().deleteSubscription(id);
@@ -283,7 +280,7 @@
                 {
                     if (!filesystemLogMonitor)
                     {
-                        filesystemLogMonitor.emplace(ioc);
+                        filesystemLogMonitor.emplace(getIoContext());
                     }
                 }
             }
@@ -380,7 +377,7 @@
             {
                 if (!filesystemLogMonitor)
                 {
-                    filesystemLogMonitor.emplace(ioc);
+                    filesystemLogMonitor.emplace(getIoContext());
                 }
             }
         }
diff --git a/redfish-core/include/redfish_aggregator.hpp b/redfish-core/include/redfish_aggregator.hpp
index 853f958..9535122 100644
--- a/redfish-core/include/redfish_aggregator.hpp
+++ b/redfish-core/include/redfish_aggregator.hpp
@@ -9,12 +9,12 @@
 #include "http_client.hpp"
 #include "http_request.hpp"
 #include "http_response.hpp"
+#include "io_context_singleton.hpp"
 #include "logging.hpp"
 #include "parsing.hpp"
 #include "ssl_key_handler.hpp"
 #include "utility.hpp"
 
-#include <boost/asio/io_context.hpp>
 #include <boost/beast/http/field.hpp>
 #include <boost/beast/http/status.hpp>
 #include <boost/beast/http/verb.hpp>
@@ -857,8 +857,8 @@
     }
 
   public:
-    explicit RedfishAggregator(boost::asio::io_context& ioc) :
-        client(ioc,
+    explicit RedfishAggregator() :
+        client(getIoContext(),
                std::make_shared<crow::ConnectionPolicy>(getAggregationPolicy()))
     {
         getSatelliteConfigs(constructorCallback);
@@ -869,9 +869,9 @@
     RedfishAggregator& operator=(RedfishAggregator&&) = delete;
     ~RedfishAggregator() = default;
 
-    static RedfishAggregator& getInstance(boost::asio::io_context* io = nullptr)
+    static RedfishAggregator& getInstance()
     {
-        static RedfishAggregator handler(*io);
+        static RedfishAggregator handler;
         return handler;
     }
 
diff --git a/redfish-core/lib/event_service.hpp b/redfish-core/lib/event_service.hpp
index 42eb638..fcde1a8 100644
--- a/redfish-core/lib/event_service.hpp
+++ b/redfish-core/lib/event_service.hpp
@@ -12,6 +12,7 @@
 #include "generated/enums/event_destination.hpp"
 #include "http/utility.hpp"
 #include "http_request.hpp"
+#include "io_context_singleton.hpp"
 #include "logging.hpp"
 #include "query.hpp"
 #include "registries.hpp"
@@ -491,7 +492,7 @@
             std::shared_ptr<Subscription> subValue =
                 std::make_shared<Subscription>(
                     std::make_shared<persistent_data::UserSubscription>(), *url,
-                    app.ioContext());
+                    getIoContext());
 
             if (subscriptionType)
             {
diff --git a/redfish-core/lib/eventservice_sse.hpp b/redfish-core/lib/eventservice_sse.hpp
index 86bf1d1..eb0552f 100644
--- a/redfish-core/lib/eventservice_sse.hpp
+++ b/redfish-core/lib/eventservice_sse.hpp
@@ -25,8 +25,7 @@
 inline void createSubscription(crow::sse_socket::Connection& conn,
                                const crow::Request& req)
 {
-    EventServiceManager& manager =
-        EventServiceManager::getInstance(&conn.getIoContext());
+    EventServiceManager& manager = EventServiceManager::getInstance();
     if ((manager.getNumberOfSubscriptions() >= maxNoOfSubscriptions) ||
         manager.getNumberOfSSESubscriptions() >= maxNoOfSSESubscriptions)
     {
@@ -79,8 +78,7 @@
 
 inline void deleteSubscription(crow::sse_socket::Connection& conn)
 {
-    redfish::EventServiceManager::getInstance(&conn.getIoContext())
-        .deleteSseSubscription(conn);
+    EventServiceManager::getInstance().deleteSseSubscription(conn);
 }
 
 inline void requestRoutesEventServiceSse(App& app)