tizen 2.4 release
[framework/web/wrt-commons.git] / modules / dbus / include / dpl / dbus / connection.h
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    connection.h
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version 1.0
20  * @brief
21  */
22
23 #ifndef DPL_DBUS_CONNECTION_H
24 #define DPL_DBUS_CONNECTION_H
25
26 #include <memory>
27 #include <vector>
28 #include <string>
29 #include <dpl/generic_event.h>
30 #include <dpl/event/event_support.h>
31 #include <dpl/dbus/object.h>
32 #include <dpl/dbus/object_proxy.h>
33 #include <dpl/dbus/glib_util.h>
34
35 namespace DPL {
36 namespace DBus {
37 namespace ConnectionEvents {
38 /**
39  * Emitted when service name is acquired.
40  *
41  * Arg0 Acquired name.
42  */
43 DECLARE_GENERIC_EVENT_1(ServiceNameAcquiredEvent, std::string)
44
45 /**
46  * Emitted when service name is lost.
47  *
48  * Arg0 Lost name.
49  */
50 DECLARE_GENERIC_EVENT_1(ServiceNameLostEvent, std::string)
51
52 /**
53  * Emitted when remote host closes connection.
54  *
55  * Arg0 Low-level error message.
56  */
57 DECLARE_GENERIC_EVENT_1(ConnectionBrokenEvent, std::string)
58
59 /**
60  * Emitted when invalid or malformed data appear on connection.
61  *
62  * Arg0 Low-level error message.
63  */
64 DECLARE_GENERIC_EVENT_1(ConnectionInvalidEvent, std::string)
65 }
66
67 class Server;
68
69 class Connection;
70 typedef std::shared_ptr<Connection> ConnectionPtr;
71
72 typedef std::shared_ptr<ObjectProxy> ObjectProxyPtr;
73
74 class Connection :
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>
79 {
80   public:
81     /**
82      * Acquires connection to session bus.
83      *
84      * @return Session bus connection.
85      * @throw DBus::Exception If unable to connect to session bus.
86      */
87     static ConnectionPtr sessionBus();
88
89     /**
90      * Acquires connection to system bus.
91      *
92      * @return System bus connection.
93      * @throw DBus::Exception If unable to connect to system bus.
94      */
95     static ConnectionPtr systemBus();
96
97     /**
98      * Acquires connection to specified bus.
99      *
100      * @return Bus connection.
101      * @throw DBus::Exception If unable to connect to a bus.
102      */
103     static ConnectionPtr connectTo(GBusType busType);
104
105     /**
106      * Acquires connection to for specified address.
107      *
108      * @return Connection.
109      * @throw DBus::Exception If unable to connect.
110      *
111      * @remarks Address should be in DBus format (@see DBus documentation).
112      */
113     static ConnectionPtr connectTo(const std::string& address);
114
115     ~Connection();
116
117     /**
118      * Sets up a service on the connection.
119      *
120      * @param serviceName Service to register.
121      * @throw DBus::Exception If registration failed.
122      *
123      * @remarks Add objects before services to prevent notifications about new
124      *          interfaces being added.
125      */
126     void registerService(const std::string& serviceName);
127
128     /**
129      * Unregisters a service from the connection.
130      *
131      * @param serviceName Service to unregister.
132      * @throw DBus::Exception If service not registered.
133      */
134     void unregisterService(const std::string& serviceName);
135
136     /**
137      * Adds object to the connection.
138      *
139      * @param object Object to register.
140      * @throw DBus::Exception If registration failed.
141      *
142      * @remarks Add objects before services to prevent notifications about new
143      *          interfaces being added.
144      */
145     void registerObject(const ObjectPtr& object);
146
147     /**
148      * Removed object from the connection.
149      *
150      * @param objectPath Path of the object to unregister.
151      * @throw DBus::Exception If object not registered.
152      */
153     void unregisterObject(const std::string& objectPath);
154
155     /**
156      * Creates proxy to remote objects.
157      *
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.
162      */
163     ObjectProxyPtr createObjectProxy(const std::string& serviceName,
164                                      const std::string& objectPath);
165
166   private:
167     friend class Server;
168
169     typedef std::map<std::string, guint> RegisteredServices;
170
171     struct ObjectRegistration
172     {
173         ObjectRegistration(guint _registrationId, const ObjectPtr& _object) :
174             registrationId(_registrationId),
175             object(_object)
176         {}
177
178         guint registrationId;
179         ObjectPtr object;
180     };
181     typedef std::map<std::string, ObjectRegistration> RegisteredObjects;
182
183     static void onServiceNameAcquired(GDBusConnection* connection,
184                                       const gchar* serviceName,
185                                       gpointer data);
186
187     static void onServiceNameLost(GDBusConnection* connection,
188                                   const gchar* serviceName,
189                                   gpointer data);
190
191     static void onConnectionClosed(GDBusConnection* connection,
192                                    gboolean peerVanished,
193                                    GError* error,
194                                    gpointer data);
195
196     explicit Connection(GDBusConnection* connection);
197
198     GDBusConnection* m_connection;
199
200     RegisteredServices m_registeredServices;
201
202     RegisteredObjects m_registeredObjects;
203 };
204 }
205 }
206
207 #endif // DPL_DBUS_CONNECTION_H