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.conf";
86 std::ifstream addressConfigFile;
88 addressConfigFile.open(fqnOfConfigFile.c_str());
90 std::string currentlyParsedCommonApiAddress;
91 DBusServiceAddress dbusServiceAddress;
92 reset(dbusServiceAddress);
94 bool currentAddressNotYetContained = false;
95 bool atLeastOneAddressFound = false;
97 while (addressConfigFile.good()) {
99 getline(addressConfigFile, readLine);
100 const size_t readLineLength = readLine.length();
102 if (readLine[0] == '[' && readLine[readLineLength - 1] == ']') {
103 if(atLeastOneAddressFound) {
104 fillUndefinedRequiredValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
105 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress} );
106 knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress} );
108 reset(dbusServiceAddress);
109 currentlyParsedCommonApiAddress = readLine.substr(1, readLineLength - 2);
110 currentAddressNotYetContained =
111 knownDBusAddresses.find(currentlyParsedCommonApiAddress) == knownDBusAddresses.end() &&
112 knownCommonAddresses.find(dbusServiceAddress) == knownCommonAddresses.end();
113 atLeastOneAddressFound = true;
115 } else if(currentAddressNotYetContained) {
116 readValue(readLine, dbusServiceAddress);
119 knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress} );
121 addressConfigFile.close();
125 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
126 static DBusAddressTranslator* dbusNameService_;
127 if(!dbusNameService_) {
128 dbusNameService_ = new DBusAddressTranslator();
130 return *dbusNameService_;
134 void DBusAddressTranslator::searchForDBusAddress(const std::string& commonApiAddress,
135 std::string& interfaceName,
136 std::string& connectionName,
137 std::string& objectPath) {
139 const auto& foundAddressMapping = knownDBusAddresses.find(commonApiAddress);
140 if(foundAddressMapping != knownDBusAddresses.end()) {
141 connectionName = std::get<0>(foundAddressMapping->second);
142 objectPath = std::get<1>(foundAddressMapping->second);
143 interfaceName = std::get<2>(foundAddressMapping->second);
145 findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
146 knownDBusAddresses.insert( {commonApiAddress, std::make_tuple(connectionName, objectPath, interfaceName) } );
150 void DBusAddressTranslator::searchForCommonAddress(const std::string& interfaceName,
151 const std::string& connectionName,
152 const std::string& objectPath,
153 std::string& commonApiAddress) {
155 DBusServiceAddress dbusAddress(connectionName, objectPath, interfaceName);
157 const auto& foundAddressMapping = knownCommonAddresses.find(dbusAddress);
158 if (foundAddressMapping != knownCommonAddresses.end()) {
159 commonApiAddress = foundAddressMapping->second;
161 findFallbackCommonAddress(commonApiAddress, interfaceName, connectionName, objectPath);
162 knownCommonAddresses.insert( {std::move(dbusAddress), commonApiAddress} );
167 void DBusAddressTranslator::findFallbackDBusAddress(const std::string& commonApiAddress,
168 std::string& interfaceName,
169 std::string& connectionName,
170 std::string& objectPath) const {
171 std::vector<std::string> parts = split(commonApiAddress, ':');
172 interfaceName = parts[1];
173 connectionName = parts[2];
174 objectPath = '/' + parts[2];
175 std::replace(objectPath.begin(), objectPath.end(), '.', '/');
178 void DBusAddressTranslator::findFallbackCommonAddress(std::string& commonApiAddress,
179 const std::string& interfaceName,
180 const std::string& connectionName,
181 const std::string& objectPath) const {
182 commonApiAddress = "local:" + interfaceName + ":" + connectionName;
187 }// namespace CommonAPI