1 /* Copyright (C) 2013 BMW Group
2 * Author: Manfred Bathelt (manfred.bathelt@bmw.de)
3 * Author: Juergen Gehring (juergen.gehring@bmw.de)
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
9 #include "DBusAddressTranslator.h"
10 #include "DBusUtils.h"
12 #include <unordered_set>
24 DBUS_CONNECTION, DBUS_OBJECT, DBUS_INTERFACE
27 static const std::unordered_map<std::string, TypeEnum> allowedValueTypes = {
28 {"dbus_connection", TypeEnum::DBUS_CONNECTION},
29 {"dbus_object", TypeEnum::DBUS_OBJECT},
30 {"dbus_interface", TypeEnum::DBUS_INTERFACE}
34 inline void readValue(std::string& readLine, DBusServiceAddress dbusServiceAddress) {
35 std::stringstream readStream(readLine);
37 std::string paramName;
38 std::string paramValue;
40 getline(readStream, paramName, '=');
42 auto typeEntry = allowedValueTypes.find(paramName);
43 if(typeEntry != allowedValueTypes.end()) {
44 getline(readStream, paramValue);
45 switch(typeEntry->second) {
46 case TypeEnum::DBUS_CONNECTION:
47 std::get<0>(dbusServiceAddress) = paramValue;
49 case TypeEnum::DBUS_OBJECT:
50 std::get<1>(dbusServiceAddress) = paramValue;
52 case TypeEnum::DBUS_INTERFACE:
53 std::get<2>(dbusServiceAddress) = paramValue;
60 inline void reset(DBusServiceAddress& dbusServiceAddress) {
61 std::get<0>(dbusServiceAddress) = "";
62 std::get<1>(dbusServiceAddress) = "";
63 std::get<2>(dbusServiceAddress) = "";
67 void DBusAddressTranslator::fillUndefinedRequiredValues(DBusServiceAddress& dbusServiceAddress, const std::string& commonApiAddress) const {
68 std::string connectionName;
69 std::string objectPath;
70 std::string interfaceName;
72 findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
74 std::get<0>(dbusServiceAddress) = std::get<0>(dbusServiceAddress) == "" ? connectionName : std::get<0>(dbusServiceAddress);
75 std::get<1>(dbusServiceAddress) = std::get<1>(dbusServiceAddress) == "" ? objectPath : std::get<1>(dbusServiceAddress);
78 //TODO: Fall, dass Datei nicht gefunden!
79 //TODO: Suche in etc/config (oder so)
82 DBusAddressTranslator::DBusAddressTranslator() {
83 std::string fqnOfConfigFile = getCurrentBinaryFileName();
84 fqnOfConfigFile += DBUS_CONFIG_SUFFIX;
86 std::ifstream addressConfigFile;
88 addressConfigFile.open(fqnOfConfigFile.c_str());
90 if(addressConfigFile) {
91 std::string currentlyParsedCommonApiAddress;
92 DBusServiceAddress dbusServiceAddress;
93 reset(dbusServiceAddress);
95 bool currentAddressNotYetContained = false;
96 bool atLeastOneAddressFound = false;
98 while (addressConfigFile.good()) {
100 getline(addressConfigFile, readLine);
101 const size_t readLineLength = readLine.length();
103 if (readLine[0] == '[' && readLine[readLineLength - 1] == ']') {
104 if (atLeastOneAddressFound) {
105 fillUndefinedRequiredValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
106 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
107 knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
109 reset(dbusServiceAddress);
110 currentlyParsedCommonApiAddress = readLine.substr(1, readLineLength - 2);
111 currentAddressNotYetContained =
112 knownDBusAddresses.find(currentlyParsedCommonApiAddress) == knownDBusAddresses.end()
114 knownCommonAddresses.find(dbusServiceAddress)
115 == knownCommonAddresses.end();
116 atLeastOneAddressFound = true;
118 } else if (currentAddressNotYetContained) {
119 readValue(readLine, dbusServiceAddress);
122 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
124 addressConfigFile.close();
129 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
130 static DBusAddressTranslator* dbusNameService_;
131 if(!dbusNameService_) {
132 dbusNameService_ = new DBusAddressTranslator();
134 return *dbusNameService_;
138 void DBusAddressTranslator::searchForDBusAddress(const std::string& commonApiAddress,
139 std::string& interfaceName,
140 std::string& connectionName,
141 std::string& objectPath) {
143 const auto& foundAddressMapping = knownDBusAddresses.find(commonApiAddress);
144 if(foundAddressMapping != knownDBusAddresses.end()) {
145 connectionName = std::get<0>(foundAddressMapping->second);
146 objectPath = std::get<1>(foundAddressMapping->second);
147 interfaceName = std::get<2>(foundAddressMapping->second);
149 findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
150 knownDBusAddresses.insert( {commonApiAddress, std::make_tuple(connectionName, objectPath, interfaceName) } );
154 void DBusAddressTranslator::searchForCommonAddress(const std::string& interfaceName,
155 const std::string& connectionName,
156 const std::string& objectPath,
157 std::string& commonApiAddress) {
159 DBusServiceAddress dbusAddress(connectionName, objectPath, interfaceName);
161 const auto& foundAddressMapping = knownCommonAddresses.find(dbusAddress);
162 if (foundAddressMapping != knownCommonAddresses.end()) {
163 commonApiAddress = foundAddressMapping->second;
165 findFallbackCommonAddress(commonApiAddress, interfaceName, connectionName, objectPath);
166 knownCommonAddresses.insert( {std::move(dbusAddress), commonApiAddress} );
171 void DBusAddressTranslator::findFallbackDBusAddress(const std::string& commonApiAddress,
172 std::string& interfaceName,
173 std::string& connectionName,
174 std::string& objectPath) const {
175 std::vector<std::string> parts = split(commonApiAddress, ':');
176 interfaceName = parts[1];
177 connectionName = parts[2];
178 objectPath = '/' + parts[2];
179 std::replace(objectPath.begin(), objectPath.end(), '.', '/');
182 void DBusAddressTranslator::findFallbackCommonAddress(std::string& commonApiAddress,
183 const std::string& interfaceName,
184 const std::string& connectionName,
185 const std::string& objectPath) const {
186 commonApiAddress = "local:" + interfaceName + ":" + connectionName;
191 }// namespace CommonAPI