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.
18 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
23 #ifndef DPL_DBUS_CONNECTION_H
24 #define DPL_DBUS_CONNECTION_H
30 #include <dpl/generic_event.h>
31 #include <dpl/event/event_support.h>
32 #include <dpl/dbus/object.h>
33 #include <dpl/dbus/object_proxy.h>
37 namespace ConnectionEvents {
39 * Emitted when service name is acquired.
43 DECLARE_GENERIC_EVENT_1(ServiceNameAcquiredEvent, std::string)
46 * Emitted when service name is lost.
50 DECLARE_GENERIC_EVENT_1(ServiceNameLostEvent, std::string)
53 * Emitted when remote host closes connection.
55 * Arg0 Low-level error message.
57 DECLARE_GENERIC_EVENT_1(ConnectionBrokenEvent, std::string)
60 * Emitted when invalid or malformed data appear on connection.
62 * Arg0 Low-level error message.
64 DECLARE_GENERIC_EVENT_1(ConnectionInvalidEvent, std::string)
70 typedef std::shared_ptr<Connection> ConnectionPtr;
72 typedef std::shared_ptr<ObjectProxy> ObjectProxyPtr;
75 public DPL::Event::EventSupport<ConnectionEvents::ServiceNameAcquiredEvent>,
76 public DPL::Event::EventSupport<ConnectionEvents::ServiceNameLostEvent>,
77 public DPL::Event::EventSupport<ConnectionEvents::ConnectionBrokenEvent>,
78 public DPL::Event::EventSupport<ConnectionEvents::ConnectionInvalidEvent>
82 * Acquires connection to session bus.
84 * @return Session bus connection.
85 * @throw DBus::Exception If unable to connect to session bus.
87 static ConnectionPtr sessionBus();
90 * Acquires connection to system bus.
92 * @return System bus connection.
93 * @throw DBus::Exception If unable to connect to system bus.
95 static ConnectionPtr systemBus();
98 * Acquires connection to specified bus.
100 * @return Bus connection.
101 * @throw DBus::Exception If unable to connect to a bus.
103 static ConnectionPtr connectTo(GBusType busType);
106 * Acquires connection to for specified address.
108 * @return Connection.
109 * @throw DBus::Exception If unable to connect.
111 * @remarks Address should be in DBus format (@see DBus documentation).
113 static ConnectionPtr connectTo(const std::string& address);
118 * Sets up a service on the connection.
120 * @param serviceName Service to register.
121 * @throw DBus::Exception If registration failed.
123 * @remarks Add objects before services to prevent notifications about new
124 * interfaces being added.
126 void registerService(const std::string& serviceName);
129 * Unregisters a service from the connection.
131 * @param serviceName Service to unregister.
132 * @throw DBus::Exception If service not registered.
134 void unregisterService(const std::string& serviceName);
137 * Adds object to the connection.
139 * @param object Object to register.
140 * @throw DBus::Exception If registration failed.
142 * @remarks Add objects before services to prevent notifications about new
143 * interfaces being added.
145 void registerObject(const ObjectPtr& object);
148 * Removed object from the connection.
150 * @param objectPath Path of the object to unregister.
151 * @throw DBus::Exception If object not registered.
153 void unregisterObject(const std::string& objectPath);
156 * Creates proxy to remote objects.
158 * @param serviceName Name of the DBus service.
159 * @param objectPath DBus path to the object.
160 * @return Object proxy.
161 * @throw DBus::ConnectionClosedException If connection is closed.
163 ObjectProxyPtr createObjectProxy(const std::string& serviceName,
164 const std::string& objectPath);
169 typedef std::map<std::string, guint> RegisteredServices;
171 struct ObjectRegistration
173 ObjectRegistration(guint _registrationId, const ObjectPtr& _object) :
174 registrationId(_registrationId),
178 guint registrationId;
181 typedef std::map<std::string, ObjectRegistration> RegisteredObjects;
183 static void onServiceNameAcquired(GDBusConnection* connection,
184 const gchar* serviceName,
187 static void onServiceNameLost(GDBusConnection* connection,
188 const gchar* serviceName,
191 static void onConnectionClosed(GDBusConnection* connection,
192 gboolean peerVanished,
196 explicit Connection(GDBusConnection* connection);
198 GDBusConnection* m_connection;
200 RegisteredServices m_registeredServices;
202 RegisteredObjects m_registeredObjects;
207 #endif // DPL_DBUS_CONNECTION_H