Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend DBusObjectManager to use GetAll #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/CommonAPI/DBus/DBusStubAdapter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <CommonAPI/Stub.hpp>
#include <CommonAPI/DBus/DBusAddress.hpp>
#include <CommonAPI/DBus/DBusInterfaceHandler.hpp>
#include <CommonAPI/DBus/DBusOutputStream.hpp>

namespace CommonAPI {
namespace DBus {
Expand Down Expand Up @@ -47,6 +48,8 @@ class COMMONAPI_EXPORT_CLASS_EXPLICIT DBusStubAdapter
COMMONAPI_EXPORT virtual bool hasFreedesktopProperties();
COMMONAPI_EXPORT virtual bool onInterfaceDBusFreedesktopPropertiesMessage(const DBusMessage &_message) = 0;

COMMONAPI_EXPORT virtual bool appendGetAllReply(const DBusMessage& dbusMessage, DBusOutputStream& dbusOutputStream) = 0;

protected:
DBusAddress dbusAddress_;
const std::shared_ptr<DBusProxyConnection> connection_;
Expand Down
52 changes: 27 additions & 25 deletions include/CommonAPI/DBus/DBusStubAdapterHelper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ class DBusStubAdapterHelper {
(void) _isManaging;
(void) _stub;
}

bool appendGetAllReply(const DBusMessage& dbusMessage, DBusOutputStream& dbusOutputStream) {
(void) dbusMessage;
(void) dbusOutputStream;
return true;
}
protected:
bool findDispatcherAndHandle(const DBusMessage& dbusMessage, DBusInterfaceMemberPath& dbusInterfaceMemberPath) {
(void) dbusMessage;
Expand All @@ -94,11 +100,6 @@ class DBusStubAdapterHelper {
(void) _message;
return false;
}
bool appendGetAllReply(const DBusMessage& dbusMessage, DBusOutputStream& dbusOutputStream) {
(void) dbusMessage;
(void) dbusOutputStream;
return true;
}
public:
template <typename Stub_>
void addStubDispatcher(DBusInterfaceMemberPath _dbusInterfaceMemberPath,
Expand Down Expand Up @@ -161,6 +162,27 @@ class DBusStubAdapterHelper<StubClass_, Stubs_...>:
return remoteEventHandler_;
}


bool appendGetAllReply(const DBusMessage& dbusMessage, DBusOutputStream& dbusOutputStream)
{
for(auto attributeDispatcherIterator = stubAttributeTable_.begin(); attributeDispatcherIterator != stubAttributeTable_.end(); attributeDispatcherIterator++) {

//To prevent the destruction of the stub whilst still handling a message
if (stub_) {
StubDispatcher<StubClass_>* getterDispatcher = attributeDispatcherIterator->second.getter;
if (NULL == getterDispatcher) { // all attributes have at least a getter
COMMONAPI_ERROR(std::string(__FUNCTION__), "getterDispatcher == NULL");
return false;
} else {
dbusOutputStream.align(8);
dbusOutputStream << attributeDispatcherIterator->first;
getterDispatcher->appendGetAllReply(dbusMessage, stub_, dbusOutputStream);
}
}
}
return DBusStubAdapterHelper<Stubs_...>::appendGetAllReply(dbusMessage, dbusOutputStream);
}

protected:

virtual bool onInterfaceDBusMessage(const DBusMessage& dbusMessage) {
Expand Down Expand Up @@ -284,26 +306,6 @@ class DBusStubAdapterHelper<StubClass_, Stubs_...>:
return setterDispatcher->dispatchDBusMessage(dbusMessage, stub_, getRemoteEventHandler(), getDBusConnection());
}

bool appendGetAllReply(const DBusMessage& dbusMessage, DBusOutputStream& dbusOutputStream)
{
for(auto attributeDispatcherIterator = stubAttributeTable_.begin(); attributeDispatcherIterator != stubAttributeTable_.end(); attributeDispatcherIterator++) {

//To prevent the destruction of the stub whilst still handling a message
if (stub_) {
StubDispatcher<StubClass_>* getterDispatcher = attributeDispatcherIterator->second.getter;
if (NULL == getterDispatcher) { // all attributes have at least a getter
COMMONAPI_ERROR(std::string(__FUNCTION__), "getterDispatcher == NULL");
return false;
} else {
dbusOutputStream.align(8);
dbusOutputStream << attributeDispatcherIterator->first;
getterDispatcher->appendGetAllReply(dbusMessage, stub_, dbusOutputStream);
}
}
}
return DBusStubAdapterHelper<Stubs_...>::appendGetAllReply(dbusMessage, dbusOutputStream);
}

private:

template <typename Stub_>
Expand Down
40 changes: 28 additions & 12 deletions src/CommonAPI/DBus/DBusObjectManagerStub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,43 +307,59 @@ bool DBusObjectManagerStub::onInterfaceDBusMessage(const DBusMessage& dbusMessag
}

std::lock_guard<std::mutex> dbusObjectManagerStubLock(dbusObjectManagerStubLock_);
DBusObjectPathAndInterfacesDict dbusObjectPathAndInterfacesDict;

for (const auto& registeredDBusObjectPathIterator : registeredDBusObjectPathsMap_) {
DBusMessage dbusMessageReply = dbusMessage.createMethodReturn("a{oa{sa{sv}}}");
DBusOutputStream dbusOutputStream(dbusMessageReply);
dbusOutputStream.beginWriteMap();

for (const auto& registeredDBusObjectPathIterator : registeredDBusObjectPathsMap_)
{
const std::string& registeredDBusObjectPath = registeredDBusObjectPathIterator.first;
const auto& registeredDBusInterfacesMap = registeredDBusObjectPathIterator.second;
DBusInterfacesAndPropertiesDict dbusInterfacesAndPropertiesDict;

if (0 == registeredDBusObjectPath.length()) {
COMMONAPI_ERROR(std::string(__FUNCTION__), " empty object path");
} else {

if (0 == registeredDBusInterfacesMap.size()) {
COMMONAPI_ERROR(std::string(__FUNCTION__), " empty interfaces map for ", registeredDBusObjectPath);
}

for (const auto& registeredDBusInterfaceIterator : registeredDBusInterfacesMap) {
dbusOutputStream.align(8);
dbusOutputStream << registeredDBusObjectPath;
dbusOutputStream.beginWriteMap();

for (const auto& registeredDBusInterfaceIterator : registeredDBusInterfacesMap)
{
const std::string& registeredDBusInterfaceName = registeredDBusInterfaceIterator.first;
const auto& registeredDBusStubAdapter = registeredDBusInterfaceIterator.second.begin();

if (0 == registeredDBusInterfaceName.length()) {
COMMONAPI_ERROR(std::string(__FUNCTION__), " empty interface name for ", registeredDBusObjectPath);
} else {
dbusInterfacesAndPropertiesDict.insert({ registeredDBusInterfaceName, DBusPropertiesChangedDict() });
dbusOutputStream.align(8);
dbusOutputStream << registeredDBusInterfaceName;
dbusOutputStream.beginWriteMap();

(*registeredDBusStubAdapter)->appendGetAllReply(dbusMessage, dbusOutputStream);

dbusOutputStream.endWriteMap();
}

if ((*registeredDBusStubAdapter)->isManaging()) {
dbusInterfacesAndPropertiesDict.insert({ getInterfaceName(), DBusPropertiesChangedDict() });
if ((*registeredDBusStubAdapter)->isManaging())
{
// dbusOutputStream.align(8);
// dbusOutputStream << std::string(getInterfaceName());
// dbusOutputStream << DBusPropertiesChangedDict();

}
}

dbusObjectPathAndInterfacesDict.insert({ registeredDBusObjectPath, std::move(dbusInterfacesAndPropertiesDict) });
dbusOutputStream.endWriteMap();
}
}

DBusMessage dbusMessageReply = dbusMessage.createMethodReturn("a{oa{sa{sv}}}");
DBusOutputStream dbusOutputStream(dbusMessageReply);

dbusOutputStream << dbusObjectPathAndInterfacesDict;
dbusOutputStream.endWriteMap();
dbusOutputStream.flush();

const bool dbusMessageReplySent = dbusConnection->sendDBusMessage(dbusMessageReply);
Expand Down