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.
17 * @file connection.cpp
18 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
23 #include <dpl/log/log.h>
24 #include <dpl/dbus/connection.h>
25 #include <dpl/dbus/exception.h>
26 #include <dpl/dbus/object_proxy.h>
30 ConnectionPtr Connection::sessionBus()
32 return connectTo(G_BUS_TYPE_SESSION);
35 ConnectionPtr Connection::systemBus()
37 return connectTo(G_BUS_TYPE_SYSTEM);
40 ConnectionPtr Connection::connectTo(GBusType busType)
44 GDBusConnection* connection = g_bus_get_sync(busType,
47 if (NULL == connection) {
50 message = error->message;
53 ThrowMsg(DBus::Exception,
54 "Couldn't connect to bus: " << message);
57 g_dbus_connection_set_exit_on_close(connection, FALSE);
59 return ConnectionPtr(new Connection(connection));
62 ConnectionPtr Connection::connectTo(const std::string& address)
66 GDBusConnection* connection = g_dbus_connection_new_for_address_sync(
68 G_DBUS_CONNECTION_FLAGS_NONE,
72 if (NULL == connection) {
75 message = error->message;
78 ThrowMsg(DBus::Exception,
79 "Couldn't connect to " << address << ": " << message);
82 return ConnectionPtr(new Connection(connection));
85 Connection::Connection(GDBusConnection* connection) :
86 m_connection(connection)
88 g_signal_connect(m_connection,
90 G_CALLBACK(onConnectionClosed),
94 Connection::~Connection()
96 std::for_each(m_registeredServices.begin(),
97 m_registeredServices.end(),
98 [] (const RegisteredServices::value_type & value)
100 g_bus_unown_name(value.second);
103 std::for_each(m_registeredObjects.begin(),
104 m_registeredObjects.end(),
105 [this] (const RegisteredObjects::value_type & value)
107 g_dbus_connection_unregister_object(
109 value.second.registrationId);
112 if (!g_dbus_connection_is_closed(m_connection)) {
113 GError* error = NULL;
116 g_dbus_connection_flush_sync(m_connection, NULL, &error))
118 LogPedantic("Could not flush the connection"
119 << " <" << error->message << ">");
124 g_object_unref(m_connection);
127 void Connection::registerService(const std::string& serviceName)
129 guint regId = g_bus_own_name_on_connection(m_connection,
131 G_BUS_NAME_OWNER_FLAGS_NONE,
132 onServiceNameAcquired,
137 ThrowMsg(DBus::Exception, "Error while registering service.");
140 m_registeredServices.insert(RegisteredServices::value_type(serviceName,
144 void Connection::unregisterService(const std::string& serviceName)
146 auto it = m_registeredServices.find(serviceName);
147 if (m_registeredServices.end() == it) {
148 ThrowMsg(DBus::Exception, "Service not registered.");
151 g_bus_unown_name(it->second);
153 m_registeredServices.erase(it);
156 void Connection::registerObject(const ObjectPtr& object)
158 GError* error = NULL;
160 guint regId = g_dbus_connection_register_object(
162 object->getPath().c_str(),
163 object->getInterface()->getInfo(),
164 object->getInterface()->getVTable(),
165 // TODO This is ugly, fix this!
166 object->getInterface().get(),
172 message = error->message;
173 LogPedantic(error->message << " " << error->code);
176 ThrowMsg(DBus::Exception, "Error while registering an object: "
180 m_registeredObjects.insert(RegisteredObjects::value_type(
182 ObjectRegistration(regId, object)));
185 void Connection::unregisterObject(const std::string& objectPath)
187 auto it = m_registeredObjects.find(objectPath);
188 if (m_registeredObjects.end() == it) {
189 ThrowMsg(DBus::Exception, "Object not registered.");
192 gboolean result = g_dbus_connection_unregister_object(
194 it->second.registrationId);
195 if (FALSE == result) {
196 ThrowMsg(DBus::Exception, "Unregistering object failed.");
198 m_registeredObjects.erase(it);
201 ObjectProxyPtr Connection::createObjectProxy(const std::string& serviceName,
202 const std::string& objectPath)
204 if (g_dbus_connection_is_closed(m_connection)) {
205 ThrowMsg(DBus::ConnectionClosedException, "Connection closed.");
208 return ObjectProxyPtr(
209 new ObjectProxy(m_connection, serviceName, objectPath));
212 void Connection::onServiceNameAcquired(GDBusConnection* /*connection*/,
213 const gchar* serviceName,
216 Assert(data && "Connection should not be NULL");
218 Connection* self = static_cast<Connection*>(data);
220 LogPedantic("Emitting service name acquired event: " << serviceName);
222 ConnectionEvents::ServiceNameAcquiredEvent event(serviceName);
223 self->DPL::Event::EventSupport<ConnectionEvents::ServiceNameAcquiredEvent>
225 EmitEvent(event, DPL::Event::EmitMode::Queued);
228 void Connection::onServiceNameLost(GDBusConnection* /*connection*/,
229 const gchar* serviceName,
232 Assert(data && "Connection should not be NULL");
234 Connection* self = static_cast<Connection*>(data);
236 LogPedantic("Emitting service name lost event: " << serviceName);
238 ConnectionEvents::ServiceNameLostEvent event(serviceName);
239 self->DPL::Event::EventSupport<ConnectionEvents::ServiceNameLostEvent>::
240 EmitEvent(event, DPL::Event::EmitMode::Queued);
243 void Connection::onConnectionClosed(GDBusConnection* /*connection*/,
244 gboolean peerVanished,
248 Assert(NULL != data && "Connection cannot be NULL");
250 Connection* self = static_cast<Connection*>(data);
252 if ((NULL == error) && (FALSE == peerVanished)) {
253 // Connection closed by this.
254 } else if (NULL != error) {
255 std::string message = error->message;
259 if (TRUE == peerVanished) {
260 // Connection closed by remote host.
261 ConnectionEvents::ConnectionBrokenEvent event(message);
262 self->DPL::Event::EventSupport<ConnectionEvents::
263 ConnectionBrokenEvent>::
264 EmitEvent(event, DPL::Event::EmitMode::Queued);
266 // Invalid or malformed data on connection.
267 ConnectionEvents::ConnectionInvalidEvent event(message);
268 self->DPL::Event::EventSupport<ConnectionEvents::
269 ConnectionInvalidEvent>::
270 EmitEvent(event, DPL::Event::EmitMode::Queued);