diff --git a/test/stresstests/celsius-thermometer-adaptor.h b/test/stresstests/celsius-thermometer-adaptor.h index 7b7100b..bbc5c7a 100644 --- a/test/stresstests/celsius-thermometer-adaptor.h +++ b/test/stresstests/celsius-thermometer-adaptor.h @@ -1,6 +1,6 @@ /* - * This file was automatically generated by sdbuscpp-xml2cpp; DO NOT EDIT! + * This file was automatically generated by sdbus-c++-xml2cpp; DO NOT EDIT! */ #ifndef __sdbuscpp__celsius_thermometer_adaptor_h__adaptor__H__ @@ -12,7 +12,7 @@ namespace org { namespace sdbuscpp { -namespace stresstest { +namespace stresstests { namespace celsius { class thermometer_adaptor diff --git a/test/stresstests/celsius-thermometer-proxy.h b/test/stresstests/celsius-thermometer-proxy.h index 15ccfbd..dcfb25c 100644 --- a/test/stresstests/celsius-thermometer-proxy.h +++ b/test/stresstests/celsius-thermometer-proxy.h @@ -1,6 +1,6 @@ /* - * This file was automatically generated by sdbuscpp-xml2cpp; DO NOT EDIT! + * This file was automatically generated by sdbus-c++-xml2cpp; DO NOT EDIT! */ #ifndef __sdbuscpp__celsius_thermometer_proxy_h__proxy__H__ @@ -12,7 +12,7 @@ namespace org { namespace sdbuscpp { -namespace stresstest { +namespace stresstests { namespace celsius { class thermometer_proxy diff --git a/test/stresstests/concatenator-adaptor.h b/test/stresstests/concatenator-adaptor.h index 25a072d..0621d45 100644 --- a/test/stresstests/concatenator-adaptor.h +++ b/test/stresstests/concatenator-adaptor.h @@ -1,6 +1,6 @@ /* - * This file was automatically generated by sdbuscpp-xml2cpp; DO NOT EDIT! + * This file was automatically generated by sdbus-c++-xml2cpp; DO NOT EDIT! */ #ifndef __sdbuscpp__concatenator_adaptor_h__adaptor__H__ @@ -12,7 +12,7 @@ namespace org { namespace sdbuscpp { -namespace stresstest { +namespace stresstests { class concatenator_adaptor { diff --git a/test/stresstests/concatenator-proxy.h b/test/stresstests/concatenator-proxy.h index 6751b51..bc76bef 100644 --- a/test/stresstests/concatenator-proxy.h +++ b/test/stresstests/concatenator-proxy.h @@ -1,6 +1,6 @@ /* - * This file was automatically generated by sdbuscpp-xml2cpp; DO NOT EDIT! + * This file was automatically generated by sdbus-c++-xml2cpp; DO NOT EDIT! */ #ifndef __sdbuscpp__concatenator_proxy_h__proxy__H__ @@ -12,7 +12,7 @@ namespace org { namespace sdbuscpp { -namespace stresstest { +namespace stresstests { class concatenator_proxy { diff --git a/test/stresstests/fahrenheit-thermometer-adaptor.h b/test/stresstests/fahrenheit-thermometer-adaptor.h index 998ddd1..71e57dc 100644 --- a/test/stresstests/fahrenheit-thermometer-adaptor.h +++ b/test/stresstests/fahrenheit-thermometer-adaptor.h @@ -1,6 +1,6 @@ /* - * This file was automatically generated by sdbuscpp-xml2cpp; DO NOT EDIT! + * This file was automatically generated by sdbus-c++-xml2cpp; DO NOT EDIT! */ #ifndef __sdbuscpp__fahrenheit_thermometer_adaptor_h__adaptor__H__ @@ -12,7 +12,7 @@ namespace org { namespace sdbuscpp { -namespace stresstest { +namespace stresstests { namespace fahrenheit { class thermometer_adaptor @@ -36,4 +36,33 @@ private: }}}} // namespaces +namespace org { +namespace sdbuscpp { +namespace stresstests { +namespace fahrenheit { +namespace thermometer { + +class factory_adaptor +{ +public: + static constexpr const char* interfaceName = "org.sdbuscpp.stresstests.fahrenheit.thermometer.factory"; + +protected: + factory_adaptor(sdbus::IObject& object) + : object_(object) + { + object_.registerMethod("createDelegateObject").onInterface(interfaceName).implementedAs([this](sdbus::Result&& result){ this->createDelegateObject(std::move(result)); }); + object_.registerMethod("destroyDelegateObject").onInterface(interfaceName).implementedAs([this](sdbus::Result<>&& result, sdbus::ObjectPath delegate){ this->destroyDelegateObject(std::move(result), std::move(delegate)); }).withNoReply(); + } + +private: + virtual void createDelegateObject(sdbus::Result&& result) = 0; + virtual void destroyDelegateObject(sdbus::Result<>&& result, sdbus::ObjectPath delegate) = 0; + +private: + sdbus::IObject& object_; +}; + +}}}}} // namespaces + #endif diff --git a/test/stresstests/fahrenheit-thermometer-proxy.h b/test/stresstests/fahrenheit-thermometer-proxy.h index 491a9d0..feb25d6 100644 --- a/test/stresstests/fahrenheit-thermometer-proxy.h +++ b/test/stresstests/fahrenheit-thermometer-proxy.h @@ -1,6 +1,6 @@ /* - * This file was automatically generated by sdbuscpp-xml2cpp; DO NOT EDIT! + * This file was automatically generated by sdbus-c++-xml2cpp; DO NOT EDIT! */ #ifndef __sdbuscpp__fahrenheit_thermometer_proxy_h__proxy__H__ @@ -12,7 +12,7 @@ namespace org { namespace sdbuscpp { -namespace stresstest { +namespace stresstests { namespace fahrenheit { class thermometer_proxy @@ -40,4 +40,40 @@ private: }}}} // namespaces +namespace org { +namespace sdbuscpp { +namespace stresstests { +namespace fahrenheit { +namespace thermometer { + +class factory_proxy +{ +public: + static constexpr const char* interfaceName = "org.sdbuscpp.stresstests.fahrenheit.thermometer.factory"; + +protected: + factory_proxy(sdbus::IObjectProxy& object) + : object_(object) + { + } + +public: + sdbus::ObjectPath createDelegateObject() + { + sdbus::ObjectPath result; + object_.callMethod("createDelegateObject").onInterface(interfaceName).storeResultsTo(result); + return result; + } + + void destroyDelegateObject(const sdbus::ObjectPath& delegate) + { + object_.callMethod("destroyDelegateObject").onInterface(interfaceName).withArguments(delegate).dontExpectReply(); + } + +private: + sdbus::IObjectProxy& object_; +}; + +}}}}} // namespaces + #endif diff --git a/test/stresstests/org.sdbuscpp.stresstest.celsius.thermometer.xml b/test/stresstests/org.sdbuscpp.stresstest.celsius.thermometer.xml index 59c3f9c..cabe248 100644 --- a/test/stresstests/org.sdbuscpp.stresstest.celsius.thermometer.xml +++ b/test/stresstests/org.sdbuscpp.stresstest.celsius.thermometer.xml @@ -1,7 +1,7 @@ - - + + diff --git a/test/stresstests/org.sdbuscpp.stresstest.concatenator.xml b/test/stresstests/org.sdbuscpp.stresstest.concatenator.xml index d77a817..770e374 100644 --- a/test/stresstests/org.sdbuscpp.stresstest.concatenator.xml +++ b/test/stresstests/org.sdbuscpp.stresstest.concatenator.xml @@ -1,7 +1,7 @@ - - + + diff --git a/test/stresstests/org.sdbuscpp.stresstest.fahrenheit.thermometer.xml b/test/stresstests/org.sdbuscpp.stresstest.fahrenheit.thermometer.xml index 17c6b0c..6544df3 100644 --- a/test/stresstests/org.sdbuscpp.stresstest.fahrenheit.thermometer.xml +++ b/test/stresstests/org.sdbuscpp.stresstest.fahrenheit.thermometer.xml @@ -1,9 +1,20 @@ - - + + + + + + + + + + + + + diff --git a/test/stresstests/sdbus-c++-stress-tests.cpp b/test/stresstests/sdbus-c++-stress-tests.cpp index 2aed9fd..933e288 100644 --- a/test/stresstests/sdbus-c++-stress-tests.cpp +++ b/test/stresstests/sdbus-c++-stress-tests.cpp @@ -46,16 +46,16 @@ using namespace std::chrono_literals; using namespace std::string_literals; -#define SERVICE_1_BUS_NAME "org.sdbuscpp.stresstests.service1" -#define SERVICE_2_BUS_NAME "org.sdbuscpp.stresstests.service2" -#define CELSIUS_THERMOMETER_OBJECT_PATH "/org/sdbuscpp/stresstests/celsius/thermometer" -#define FAHRENHEIT_THERMOMETER_OBJECT_PATH "/org/sdbuscpp/stresstests/fahrenheit/thermometer" -#define CONCATENATOR_OBJECT_PATH "/org/sdbuscpp/stresstests/concatenator" +#define SERVICE_1_BUS_NAME "org.sdbuscpp.stresstests.service1"s +#define SERVICE_2_BUS_NAME "org.sdbuscpp.stresstests.service2"s +#define CELSIUS_THERMOMETER_OBJECT_PATH "/org/sdbuscpp/stresstests/celsius/thermometer"s +#define FAHRENHEIT_THERMOMETER_OBJECT_PATH "/org/sdbuscpp/stresstests/fahrenheit/thermometer"s +#define CONCATENATOR_OBJECT_PATH "/org/sdbuscpp/stresstests/concatenator"s -class CelsiusThermometerAdaptor : public sdbus::Interfaces +class CelsiusThermometerAdaptor : public sdbus::Interfaces { public: - using sdbus::Interfaces::Interfaces; + using sdbus::Interfaces::Interfaces; protected: virtual uint32_t getCurrentTemperature() override @@ -67,17 +67,19 @@ private: uint32_t m_currentTemperature{}; }; -class CelsiusThermometerProxy : public sdbus::ProxyInterfaces +class CelsiusThermometerProxy : public sdbus::ProxyInterfaces { public: - using sdbus::ProxyInterfaces::ProxyInterfaces; + using sdbus::ProxyInterfaces::ProxyInterfaces; }; -class FahrenheitThermometerAdaptor : public sdbus::Interfaces +class FahrenheitThermometerAdaptor : public sdbus::Interfaces< org::sdbuscpp::stresstests::fahrenheit::thermometer_adaptor + , org::sdbuscpp::stresstests::fahrenheit::thermometer::factory_adaptor > { public: FahrenheitThermometerAdaptor(sdbus::IConnection& connection, std::string objectPath) - : sdbus::Interfaces(connection, std::move(objectPath)) + : sdbus::Interfaces< org::sdbuscpp::stresstests::fahrenheit::thermometer_adaptor + , org::sdbuscpp::stresstests::fahrenheit::thermometer::factory_adaptor >(connection, std::move(objectPath)) , celsiusProxy_(connection, SERVICE_2_BUS_NAME, CELSIUS_THERMOMETER_OBJECT_PATH) { } @@ -89,21 +91,55 @@ protected: return static_cast(celsiusProxy_.getCurrentTemperature() * 1.8 + 32.); } + virtual void createDelegateObject(sdbus::Result&& result) override + { + static size_t objectCounter{}; + objectCounter++; + + std::thread([this, result = std::move(result), objectCounter = objectCounter]() + { + auto& connection = getObject().getConnection(); + sdbus::ObjectPath newObjectPath = FAHRENHEIT_THERMOMETER_OBJECT_PATH + "/" + std::to_string(objectCounter); + + // Here we are testing dynamic creation of a D-Bus object in an async way + auto adaptor = std::make_unique(connection, newObjectPath); + + std::lock_guard lock{mutex_}; + children_.emplace(newObjectPath, std::move(adaptor)); + + result.returnResults(newObjectPath); + }).detach(); + } + + virtual void destroyDelegateObject(sdbus::Result<>&& /*result*/, sdbus::ObjectPath delegate) override + { + std::thread([this, delegate = std::move(delegate)]() + { + // Here we are testing dynamic removal of a D-Bus object in an async way + std::lock_guard lock{mutex_}; + children_.erase(delegate); + }).detach(); + } + private: CelsiusThermometerProxy celsiusProxy_; + std::map> children_; + std::mutex mutex_; }; -class FahrenheitThermometerProxy : public sdbus::ProxyInterfaces +class FahrenheitThermometerProxy : public sdbus::ProxyInterfaces< org::sdbuscpp::stresstests::fahrenheit::thermometer_proxy + , org::sdbuscpp::stresstests::fahrenheit::thermometer::factory_proxy > { public: - using sdbus::ProxyInterfaces::ProxyInterfaces; + using sdbus::ProxyInterfaces< org::sdbuscpp::stresstests::fahrenheit::thermometer_proxy + , org::sdbuscpp::stresstests::fahrenheit::thermometer::factory_proxy >::ProxyInterfaces; }; -class ConcatenatorAdaptor : public sdbus::Interfaces +class ConcatenatorAdaptor : public sdbus::Interfaces { public: ConcatenatorAdaptor(sdbus::IConnection& connection, std::string objectPath) - : sdbus::Interfaces(connection, std::move(objectPath)) + : sdbus::Interfaces(connection, std::move(objectPath)) { unsigned int workers = std::thread::hardware_concurrency(); if (workers < 4) @@ -167,10 +203,10 @@ private: std::atomic exit_{}; }; -class ConcatenatorProxy : public sdbus::ProxyInterfaces +class ConcatenatorProxy : public sdbus::ProxyInterfaces { public: - using sdbus::ProxyInterfaces::ProxyInterfaces; + using sdbus::ProxyInterfaces::ProxyInterfaces; private: virtual void onConcatenateReply(const std::string& result, const sdbus::Error* error) override @@ -204,8 +240,8 @@ private: } public: - std::atomic repliesReceived_; - std::atomic signalsReceived_; + std::atomic repliesReceived_{}; + std::atomic signalsReceived_{}; }; //----------------------------------------- @@ -258,8 +294,8 @@ int main(int /*argc*/, char */*argv*/[]) // Make sure the system is catching up with our async requests, // otherwise sleep a bit to slow down flooding the server. assert(localCounter >= concatenator.repliesReceived_); - while ((localCounter - concatenator.repliesReceived_) > 20 && !stopClients) - std::this_thread::sleep_for(2ms); + while ((localCounter - concatenator.repliesReceived_) > 40 && !stopClients) + std::this_thread::sleep_for(1ms); // Update statistics concatenationCallsMade = localCounter; @@ -271,6 +307,11 @@ int main(int /*argc*/, char */*argv*/[]) std::thread thermometerThread([&]() { + // Here we continuously remotely call getCurrentTemperature(). We have one proxy object, + // first we use it's factory interface to create another proxy object, call getCurrentTemperature() + // on that one, and then destroy that proxy object. All that continously in a loop. + // This tests dynamic creation and destruction of remote D-Bus objects and local object proxies. + FahrenheitThermometerProxy thermometer(con, SERVICE_1_BUS_NAME, FAHRENHEIT_THERMOMETER_OBJECT_PATH); uint32_t localCounter{}; uint32_t previousTemperature{}; @@ -278,13 +319,20 @@ int main(int /*argc*/, char */*argv*/[]) while (!stopClients) { localCounter++; - auto temperature = thermometer.getCurrentTemperature(); + + auto newObjectPath = thermometer.createDelegateObject(); + FahrenheitThermometerProxy proxy{con, SERVICE_1_BUS_NAME, newObjectPath}; + + auto temperature = proxy.getCurrentTemperature(); assert(temperature >= previousTemperature); // The temperature shall rise continually previousTemperature = temperature; - std::this_thread::sleep_for(5ms); + //std::this_thread::sleep_for(1ms); if ((localCounter % 10) == 0) thermometerCallsMade = localCounter; + + proxy.~FahrenheitThermometerProxy(); + thermometer.destroyDelegateObject(newObjectPath); } });