diff options
Diffstat (limited to 'control/zone.cpp')
-rw-r--r-- | control/zone.cpp | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/control/zone.cpp b/control/zone.cpp index 209d773..41ee0cc 100644 --- a/control/zone.cpp +++ b/control/zone.cpp @@ -479,6 +479,93 @@ void Zone::handleEvent(sdbusplus::message::message& msg, }); } +const std::string& Zone::getService(const std::string& path, + const std::string& intf) +{ + // Retrieve service from cache + auto srvIter = _servTree.find(path); + if (srvIter != _servTree.end()) + { + for (auto& serv : srvIter->second) + { + auto it = std::find_if( + serv.second.begin(), + serv.second.end(), + [&intf](auto const& interface) + { + return intf == interface; + }); + if (it != std::end(serv.second)) + { + // Service found + return serv.first; + } + } + // Interface not found in cache, add and return + return addServices(path, intf, 0); + } + else + { + // Path not found in cache, add and return + return addServices(path, intf, 0); + } +} + +const std::string& Zone::addServices(const std::string& path, + const std::string& intf, + int32_t depth) +{ + static const std::string empty = ""; + auto it = _servTree.end(); + + // Get all subtree objects for the given interface + auto objects = util::SDBusPlus::getSubTree(_bus, "/", intf, depth); + // Add what's returned to the cache of path->services + for (auto& pIter : objects) + { + auto pathIter = _servTree.find(pIter.first); + if (pathIter != _servTree.end()) + { + // Path found in cache + for (auto& sIter : pIter.second) + { + auto servIter = pathIter->second.find(sIter.first); + if (servIter != pathIter->second.end()) + { + // Service found in cache + for (auto& iIter : sIter.second) + { + // Add interface to cache + servIter->second.emplace_back(iIter); + } + } + else + { + // Service not found in cache + pathIter->second.insert(sIter); + } + } + } + else + { + _servTree.insert(pIter); + } + // When the paths match, since a single interface constraint is given, + // that is the service to return + if (path == pIter.first) + { + it = _servTree.find(pIter.first); + } + } + + if (it != _servTree.end()) + { + return it->second.begin()->first; + } + + return empty; +} + } } } |