tizen 2.4 release
[framework/web/wrt-commons.git] / tests / dbus / test_service.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 /**
17  * @file    test_service.cpp
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @brief   Implementation file for wrt-dbus-test-service.
20  */
21
22 #include <string>
23 #include <dpl/dbus/connection.h>
24 #include <dpl/dbus/object.h>
25 #include <dpl/dbus/interface.h>
26 #include <dpl/dbus/dispatcher.h>
27 #include <loop_control.h>
28
29 namespace {
30 const std::string serviceName = "org.tizen.DBusTestService";
31 const std::string objectPath = "/org/tizen/DBusTestService";
32 const std::string interfaceName = "org.tizen.DBusTestService";
33 const std::string methodNameEcho = "echo";
34 const std::string methodNameQuit = "quit";
35 const std::string nodeInfo =
36     "<?xml version='1.0'?>"
37     "<node>"
38     "  <interface name='" + interfaceName + "'>"
39                                             "    <method name='" +
40     methodNameEcho + "'>"
41                      "      <arg type='s' name='challenge' direction='in'/>"
42                      "      <arg type='s' name='response' direction='out'/>"
43                      "    </method>"
44                      "    <method name='"
45     + methodNameQuit + "'>"
46                        "    </method>"
47                        "  </interface>"
48                        "</node>";
49 }
50
51 class TestServiceDispatcher : public DPL::DBus::Dispatcher
52 {
53   private:
54     void onMethodCall(GDBusConnection* /*connection*/,
55                       const gchar* /*sender*/,
56                       const gchar* /*objectPath*/,
57                       const gchar* /*interfaceName*/,
58                       const gchar* methodName,
59                       GVariant* parameters,
60                       GDBusMethodInvocation* invocation)
61     {
62         if (methodNameEcho == methodName) {
63             WrtLogD("Echo");
64             g_dbus_method_invocation_return_value(invocation,
65                                                   parameters);
66         } else if (methodNameQuit == methodName) {
67             WrtLogD("Quit");
68             g_dbus_method_invocation_return_value(invocation, NULL);
69             LoopControl::wrt_end_loop();
70         }
71     }
72 };
73
74 int main(int argc, char* argv[])
75 {
76     LoopControl::init_loop(argc, argv);
77     TestServiceDispatcher dispatcher;
78
79     auto iface = DPL::DBus::Interface::fromXMLString(nodeInfo).at(0);
80     iface->setDispatcher(&dispatcher);
81     auto object = DPL::DBus::Object::create(objectPath, iface);
82     auto connection = DPL::DBus::Connection::sessionBus();
83     connection->registerObject(object);
84     connection->registerService(serviceName);
85     LoopControl::wrt_start_loop();
86
87     return 0;
88 }