Tizen 2.0 Release
[framework/web/wrt-commons.git] / tests / dpl / 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='" + methodNameEcho + "'>"
40         "      <arg type='s' name='challenge' direction='in'/>"
41         "      <arg type='s' name='response' direction='out'/>"
42         "    </method>"
43         "    <method name='" + methodNameQuit + "'>"
44         "    </method>"
45         "  </interface>"
46         "</node>";
47 }
48
49 class TestServiceDispatcher : public DPL::DBus::Dispatcher
50 {
51 private:
52     void onMethodCall(GDBusConnection* /*connection*/,
53                       const gchar* /*sender*/,
54                       const gchar* /*objectPath*/,
55                       const gchar* /*interfaceName*/,
56                       const gchar* methodName,
57                       GVariant* parameters,
58                       GDBusMethodInvocation* invocation)
59     {
60         if (methodNameEcho == methodName)
61         {
62             LogDebug("Echo");
63             g_dbus_method_invocation_return_value(invocation,
64                                                   parameters);
65         }
66         else if (methodNameQuit == methodName)
67         {
68             LogDebug("Quit");
69             g_dbus_method_invocation_return_value(invocation, NULL);
70             LoopControl::wrt_end_loop();
71         }
72     }
73 };
74
75 int main(int argc, char* argv[])
76 {
77     LoopControl::init_loop(argc, argv);
78     TestServiceDispatcher dispatcher;
79
80     auto iface = DPL::DBus::Interface::fromXMLString(nodeInfo).at(0);
81     iface->setDispatcher(&dispatcher);
82     auto object = DPL::DBus::Object::create(objectPath, iface);
83     auto connection = DPL::DBus::Connection::sessionBus();
84     connection->registerObject(object);
85     connection->registerService(serviceName);
86     LoopControl::wrt_start_loop();
87
88     return 0;
89 }