Add service object class in dbus tests 36/32936/17
authorMarcin Niesluchowski <m.niesluchow@samsung.com>
Mon, 29 Dec 2014 14:11:49 +0000 (15:11 +0100)
committerZbigniew Jasinski <z.jasinski@samsung.com>
Fri, 10 Apr 2015 08:07:51 +0000 (01:07 -0700)
ServiceObject class is handling incoming messages

Change-Id: I1bc92a32a8c134c06e43bf4fc42fb0427a92ab7e

tests/dbus-tests/CMakeLists.txt
tests/dbus-tests/common/dbus_test_service_object.cpp [new file with mode: 0644]
tests/dbus-tests/common/dbus_test_service_object.h [new file with mode: 0644]

index 3659053199f2480a4475dbac2dd0a30c77de4dfc..9dda30f8e081f0ac07c0cb5696eb5854da7e7ca3 100644 (file)
@@ -39,6 +39,7 @@ SET(TARGET_DBUS_TESTS "dbus-tests")
 
 SET(DBUS_TESTS_SOURCES
     ${PROJECT_SOURCE_DIR}/tests/cynara-tests/common/cynara_test_admin.cpp
+    ${PROJECT_SOURCE_DIR}/tests/dbus-tests/common/dbus_test_service_object.cpp
     ${PROJECT_SOURCE_DIR}/tests/dbus-tests/main.cpp
     ${PROJECT_SOURCE_DIR}/tests/dbus-tests/common/dbus_test_busconfig_writer.cpp
     ${PROJECT_SOURCE_DIR}/tests/dbus-tests/cynara_dbus_tests.cpp
diff --git a/tests/dbus-tests/common/dbus_test_service_object.cpp b/tests/dbus-tests/common/dbus_test_service_object.cpp
new file mode 100644 (file)
index 0000000..8cc1ca6
--- /dev/null
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/*
+ * @file        dbus_test_service_object.cpp
+ * @author      Marcin Niesluchowski <m.niesluchow@samsungcom>
+ * @version     1.0
+ * @brief       Definition of dbus test service object class for handling
+ *              incoming messages
+ */
+
+#include <dbus_test_service_object.h>
+
+namespace DBusTest
+{
+
+ServiceObject::ServiceObject(const std::string &interface)
+    : m_interface(interface)
+{
+}
+
+const std::string& ServiceObject::interface() const
+{
+    return m_interface;
+}
+
+void ServiceObject::insertMethodHandler(const std::string &method,
+                                        const MessageHandler &messageHandler)
+{
+    m_methodHandlers[method] = messageHandler;
+}
+
+void ServiceObject::handleMessage(DBus::Connection &connection,
+                                  DBus::MessageIn &messageIn,
+                                  std::string &errorStr)
+{
+    for (auto &methodHandler : m_methodHandlers) {
+        if (messageIn.isMethodCall(m_interface, methodHandler.first)) {
+            methodHandler.second(connection, messageIn, errorStr);
+            return;
+        }
+    }
+    errorStr = "Received unregistered message";
+}
+
+} // namespace DBusTest
diff --git a/tests/dbus-tests/common/dbus_test_service_object.h b/tests/dbus-tests/common/dbus_test_service_object.h
new file mode 100644 (file)
index 0000000..d2692c2
--- /dev/null
@@ -0,0 +1,64 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+/*
+ * @file        dbus_test_service_object.h
+ * @author      Marcin Niesluchowski <m.niesluchow@samsungcom>
+ * @version     1.0
+ * @brief       Declaration of dbus test service object class for handling
+ *              incoming messages
+ */
+
+#ifndef DBUS_TESTS_COMMON_DBUS_TEST_SERVICE_OBJECT_H
+#define DBUS_TESTS_COMMON_DBUS_TEST_SERVICE_OBJECT_H
+
+#include <dbus_connection.h>
+#include <dbus_message_in.h>
+
+#include <functional>
+#include <map>
+#include <string>
+
+namespace DBusTest
+{
+
+using MessageHandler = std::function<void(DBus::Connection &connection,
+                                          DBus::MessageIn &messageIn,
+                                          std::string &errorStr)>;
+
+class ServiceObject
+{
+public:
+    ServiceObject(const std::string &interface);
+    ServiceObject(const ServiceObject &other) = delete;
+    ~ServiceObject() = default;
+
+    ServiceObject& operator=(const ServiceObject &other) = delete;
+
+    const std::string& interface() const;
+    void insertMethodHandler(const std::string &method,
+                             const MessageHandler &messageHandler);
+    void handleMessage(DBus::Connection &connection,
+                       DBus::MessageIn &messageIn,
+                       std::string &errorStr);
+
+private:
+    std::map<std::string, MessageHandler> m_methodHandlers;
+    std::string m_interface;
+};
+
+} // namespace DBusTest
+
+#endif // DBUS_TESTS_COMMON_DBUS_TEST_SERVICE_OBJECT_H