tizen beta release
[framework/web/wrt-commons.git] / examples / dbus / server-example / server-example.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 #include <memory>
18 #include <iostream>
19 #include <gio/gio.h>
20 #include <dpl/event_listener.h>
21 #include <dpl/log/log.h>
22 #include <dpl/thread.h>
23 #include <dpl/dbus/server.h>
24 #include <dpl/dbus/connection.h>
25
26 std::string xml = 
27 "<node>"
28 "  <interface name='org.tizen.EchoInterface'>"
29 "    <method name='echo'>"
30 "      <arg type='s' name='input' direction='in'/>"
31 "      <arg type='s' name='output' direction='out'/>"
32 "    </method>"
33 "  </interface>"
34 "  <interface name='org.tizen.QuitInterface'>"
35 "    <method name='quit'>"
36 "    </method>"
37 "  </interface>"
38 "</node>";
39
40 GMainLoop* g_loop = NULL;
41
42 auto g_interfaces = DPL::DBus::Interface::fromXMLString(xml);
43
44 class DBusDispatcher : public DPL::DBus::Dispatcher
45 {
46 public:
47     void onMethodCall(GDBusConnection *connection,
48                       const gchar *sender,
49                       const gchar *objectPath,
50                       const gchar *interfaceName,
51                       const gchar *methodName,
52                       GVariant *parameters,
53                       GDBusMethodInvocation *invocation)
54     {
55           LogInfo("On method call: " << methodName);
56
57           if (g_strcmp0(methodName, "echo") == 0)
58           {
59               const gchar* arg = NULL;
60
61               g_variant_get(parameters, "(&s)", &arg);
62               LogInfo("Client said: " << arg);
63
64               gchar* response = g_strdup_printf(arg);
65               g_dbus_method_invocation_return_value(invocation,
66                                                     g_variant_new("(s)",
67                                                                   response));
68               g_free (response);
69               sleep(5);
70           }
71           else if (g_strcmp0(methodName, "quit") == 0)
72           {
73               g_main_loop_quit(g_loop);
74           }
75     }
76 };
77
78 class NewConnectionListener :
79         public DPL::EventListener<DPL::DBus::ServerEvents::NewConnectionEvent>
80 {
81 protected:
82     void OnEventReceived(const DPL::DBus::ServerEvents::NewConnectionEvent& event)
83     {
84         m_connection = event.GetArg0();
85
86         auto quitInterface = g_interfaces.at(1);
87         quitInterface->setDispatcher(std::make_shared<DBusDispatcher>());
88         m_quitObject = DBus::Object::create("/object/quit", quitInterface);
89
90         m_connection->registerObject(m_quitObject);
91     }
92
93 private:
94     DPL::DBus::ConnectionPtr m_connection;
95     DPL::DBus::ObjectPtr m_quitObject;
96 };
97
98 int main()
99 {
100   g_type_init();
101
102   // --------------- echo
103   auto echoConnection = DPL::DBus::Connection::sessionBus();
104
105   auto echoInterface = g_interfaces.at(0);
106   echoInterface->setDispatcher(std::make_shared<DBusDispatcher>());
107
108   auto echoObject = DPL::DBus::Object::create("/object/echo", echoInterface);
109
110   echoConnection->registerObject(echoObject);
111
112   echoConnection->registerService("org.tizen.EchoService");
113
114   // --------------- quit
115   std::unique_ptr<NewConnectionListener> listener(new NewConnectionListener);
116   auto server = DPL::DBus::Server::create("unix:abstract=someaddr");
117   server->AddListener(listener.get());
118   server->start();
119
120   g_loop = g_main_loop_new(NULL, FALSE);
121   g_main_loop_run(g_loop);
122   g_main_loop_unref(g_loop);
123
124   return 0;
125 }