2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
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'/>"
34 " <interface name='org.tizen.QuitInterface'>"
35 " <method name='quit'>"
40 GMainLoop* g_loop = NULL;
42 auto g_interfaces = DPL::DBus::Interface::fromXMLString(xml);
44 class DBusDispatcher : public DPL::DBus::Dispatcher
47 void onMethodCall(GDBusConnection *connection,
49 const gchar *objectPath,
50 const gchar *interfaceName,
51 const gchar *methodName,
53 GDBusMethodInvocation *invocation)
55 LogInfo("On method call: " << methodName);
57 if (g_strcmp0(methodName, "echo") == 0)
59 const gchar* arg = NULL;
61 g_variant_get(parameters, "(&s)", &arg);
62 LogInfo("Client said: " << arg);
64 gchar* response = g_strdup_printf(arg);
65 g_dbus_method_invocation_return_value(invocation,
71 else if (g_strcmp0(methodName, "quit") == 0)
73 g_main_loop_quit(g_loop);
78 class NewConnectionListener :
79 public DPL::EventListener<DPL::DBus::ServerEvents::NewConnectionEvent>
82 void OnEventReceived(const DPL::DBus::ServerEvents::NewConnectionEvent& event)
84 m_connection = event.GetArg0();
86 auto quitInterface = g_interfaces.at(1);
87 quitInterface->setDispatcher(std::make_shared<DBusDispatcher>());
88 m_quitObject = DBus::Object::create("/object/quit", quitInterface);
90 m_connection->registerObject(m_quitObject);
94 DPL::DBus::ConnectionPtr m_connection;
95 DPL::DBus::ObjectPtr m_quitObject;
102 // --------------- echo
103 auto echoConnection = DPL::DBus::Connection::sessionBus();
105 auto echoInterface = g_interfaces.at(0);
106 echoInterface->setDispatcher(std::make_shared<DBusDispatcher>());
108 auto echoObject = DPL::DBus::Object::create("/object/echo", echoInterface);
110 echoConnection->registerObject(echoObject);
112 echoConnection->registerService("org.tizen.EchoService");
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());
120 g_loop = g_main_loop_new(NULL, FALSE);
121 g_main_loop_run(g_loop);
122 g_main_loop_unref(g_loop);