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>
38 namespace ConnectionEvents
41 * Emitted when service name is acquired.
45 DECLARE_GENERIC_EVENT_1(ServiceNameAcquiredEvent, std::string)
48 * Emitted when service name is lost.
52 DECLARE_GENERIC_EVENT_1(ServiceNameLostEvent, std::string)
55 * Emitted when remote host closes connection.
57 * Arg0 Low-level error message.
59 DECLARE_GENERIC_EVENT_1(ConnectionBrokenEvent, std::string)
62 * Emitted when invalid or malformed data appear on connection.
64 * Arg0 Low-level error message.
66 DECLARE_GENERIC_EVENT_1(ConnectionInvalidEvent, std::string)
72 typedef std::shared_ptr<Connection> ConnectionPtr;
74 typedef std::shared_ptr<ObjectProxy> ObjectProxyPtr;
77 public DPL::Event::EventSupport<ConnectionEvents::ServiceNameAcquiredEvent>,
78 public DPL::Event::EventSupport<ConnectionEvents::ServiceNameLostEvent>,
79 public DPL::Event::EventSupport<ConnectionEvents::ConnectionBrokenEvent>,
80 public DPL::Event::EventSupport<ConnectionEvents::ConnectionInvalidEvent>
84 * Acquires connection to session bus.
86 * @return Session bus connection.
87 * @throw DBus::Exception If unable to connect to session bus.
89 static ConnectionPtr sessionBus();
92 * Acquires connection to system bus.
94 * @return System bus connection.
95 * @throw DBus::Exception If unable to connect to system bus.
97 static ConnectionPtr systemBus();
100 * Acquires connection to specified bus.
102 * @return Bus connection.
103 * @throw DBus::Exception If unable to connect to a bus.
105 static ConnectionPtr connectTo(GBusType busType);
108 * Acquires connection to for specified address.
110 * @return Connection.
111 * @throw DBus::Exception If unable to connect.
113 * @remarks Address should be in DBus format (@see DBus documentation).
115 static ConnectionPtr connectTo(const std::string& address);
120 * Sets up a service on the connection.
122 * @param serviceName Service to register.
123 * @throw DBus::Exception If registration failed.
125 * @remarks Add objects before services to prevent notifications about new
126 * interfaces being added.
128 void registerService(const std::string& serviceName);
131 * Unregisters a service from the connection.
133 * @param serviceName Service to unregister.
134 * @throw DBus::Exception If service not registered.
136 void unregisterService(const std::string& serviceName);
139 * Adds object to the connection.
141 * @param object Object to register.
142 * @throw DBus::Exception If registration failed.
144 * @remarks Add objects before services to prevent notifications about new
145 * interfaces being added.
147 void registerObject(const ObjectPtr& object);
150 * Removed object from the connection.
152 * @param objectPath Path of the object to unregister.
153 * @throw DBus::Exception If object not registered.
155 void unregisterObject(const std::string& objectPath);
158 * Creates proxy to remote objects.
160 * @param serviceName Name of the DBus service.
161 * @param objectPath DBus path to the object.
162 * @return Object proxy.
163 * @throw DBus::ConnectionClosedException If connection is closed.
165 ObjectProxyPtr createObjectProxy(const std::string& serviceName,
166 const std::string& objectPath);
171 typedef std::map<std::string, guint> RegisteredServices;
173 struct ObjectRegistration
175 ObjectRegistration(guint registrationId, const ObjectPtr& object)
176 : registrationId(registrationId),
181 guint registrationId;
184 typedef std::map<std::string, ObjectRegistration> RegisteredObjects;
186 static void onServiceNameAcquired(GDBusConnection* connection,
187 const gchar* serviceName,
190 static void onServiceNameLost(GDBusConnection* connection,
191 const gchar* serviceName,
194 static void onConnectionClosed(GDBusConnection* connection,
195 gboolean peerVanished,
199 explicit Connection(GDBusConnection* connection);
201 GDBusConnection* m_connection;
203 RegisteredServices m_registeredServices;
205 RegisteredObjects m_registeredObjects;
211 #endif // DPL_DBUS_CONNECTION_H