Config file for tests now is generated on each test (prevents the
[profile/ivi/common-api-dbus-runtime.git] / src / CommonAPI / DBus / DBusAddressTranslator.cpp
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/. */
7
8
9 #include "DBusAddressTranslator.h"
10 #include "DBusUtils.h"
11
12 #include <unordered_set>
13 #include <string.h>
14 #include <iostream>
15 #include <fstream>
16 #include <cassert>
17
18
19 namespace CommonAPI {
20 namespace DBus {
21
22
23 enum class TypeEnum {
24     DBUS_CONNECTION, DBUS_OBJECT, DBUS_INTERFACE
25 };
26
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}
31 };
32
33
34 inline void readValue(std::string& readLine, DBusServiceAddress dbusServiceAddress) {
35     std::stringstream readStream(readLine);
36
37     std::string paramName;
38     std::string paramValue;
39
40     getline(readStream, paramName, '=');
41
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;
48                 break;
49             case TypeEnum::DBUS_OBJECT:
50                 std::get<1>(dbusServiceAddress) = paramValue;
51                 break;
52             case TypeEnum::DBUS_INTERFACE:
53                 std::get<2>(dbusServiceAddress) = paramValue;
54                 break;
55         }
56     }
57 }
58
59
60 inline void reset(DBusServiceAddress& dbusServiceAddress) {
61     std::get<0>(dbusServiceAddress) = "";
62     std::get<1>(dbusServiceAddress) = "";
63     std::get<2>(dbusServiceAddress) = "";
64 }
65
66
67 void DBusAddressTranslator::fillUndefinedRequiredValues(DBusServiceAddress& dbusServiceAddress, const std::string& commonApiAddress) const {
68     std::string connectionName;
69     std::string objectPath;
70     std::string interfaceName;
71
72     findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
73
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);
76 }
77
78 //TODO: Fall, dass Datei nicht gefunden!
79 //TODO: Suche in etc/config (oder so)
80
81
82 DBusAddressTranslator::DBusAddressTranslator() {
83     std::string fqnOfConfigFile = getCurrentBinaryFileName();
84     fqnOfConfigFile += DBUS_CONFIG_SUFFIX;
85
86     std::ifstream addressConfigFile;
87
88     addressConfigFile.open(fqnOfConfigFile.c_str());
89
90     if(addressConfigFile) {
91         std::string currentlyParsedCommonApiAddress;
92         DBusServiceAddress dbusServiceAddress;
93         reset(dbusServiceAddress);
94
95         bool currentAddressNotYetContained = false;
96         bool atLeastOneAddressFound = false;
97
98         while (addressConfigFile.good()) {
99             std::string readLine;
100             getline(addressConfigFile, readLine);
101             const size_t readLineLength = readLine.length();
102
103             if (readLine[0] == '[' && readLine[readLineLength - 1] == ']') {
104                 if (atLeastOneAddressFound) {
105                     fillUndefinedRequiredValues(dbusServiceAddress, currentlyParsedCommonApiAddress);
106                     knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
107                     knownCommonAddresses.insert( {dbusServiceAddress, currentlyParsedCommonApiAddress});
108                 }
109                 reset(dbusServiceAddress);
110                 currentlyParsedCommonApiAddress = readLine.substr(1, readLineLength - 2);
111                 currentAddressNotYetContained =
112                                 knownDBusAddresses.find(currentlyParsedCommonApiAddress) == knownDBusAddresses.end()
113                                                 &&
114                                                 knownCommonAddresses.find(dbusServiceAddress)
115                                                                 == knownCommonAddresses.end();
116                 atLeastOneAddressFound = true;
117
118             } else if (currentAddressNotYetContained) {
119                 readValue(readLine, dbusServiceAddress);
120             }
121         }
122         knownDBusAddresses.insert( {currentlyParsedCommonApiAddress, dbusServiceAddress});
123
124         addressConfigFile.close();
125     }
126 }
127
128
129 DBusAddressTranslator& DBusAddressTranslator::getInstance() {
130     static DBusAddressTranslator* dbusNameService_;
131     if(!dbusNameService_) {
132         dbusNameService_ = new DBusAddressTranslator();
133     }
134     return *dbusNameService_;
135 }
136
137
138 void DBusAddressTranslator::searchForDBusAddress(const std::string& commonApiAddress,
139                                                  std::string& interfaceName,
140                                                  std::string& connectionName,
141                                                  std::string& objectPath) {
142
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);
148     } else {
149         findFallbackDBusAddress(commonApiAddress, interfaceName, connectionName, objectPath);
150         knownDBusAddresses.insert( {commonApiAddress, std::make_tuple(connectionName, objectPath, interfaceName) } );
151     }
152 }
153
154 void DBusAddressTranslator::searchForCommonAddress(const std::string& interfaceName,
155                                                    const std::string& connectionName,
156                                                    const std::string& objectPath,
157                                                    std::string& commonApiAddress) {
158
159     DBusServiceAddress dbusAddress(connectionName, objectPath, interfaceName);
160
161     const auto& foundAddressMapping = knownCommonAddresses.find(dbusAddress);
162     if (foundAddressMapping != knownCommonAddresses.end()) {
163         commonApiAddress = foundAddressMapping->second;
164     } else {
165         findFallbackCommonAddress(commonApiAddress, interfaceName, connectionName, objectPath);
166         knownCommonAddresses.insert( {std::move(dbusAddress), commonApiAddress} );
167     }
168 }
169
170
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(), '.', '/');
180 }
181
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;
187 }
188
189
190 }// namespace DBus
191 }// namespace CommonAPI