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 #include <dpl/assert.h>
24 #include <dpl/log/log.h>
25 #include <dpl/dbus/server.h>
29 ServerPtr Server::create(const std::string& address)
33 int flags = G_DBUS_SERVER_FLAGS_NONE |
34 G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
36 gchar* serverId = g_dbus_generate_guid();
38 GDBusServer* server = g_dbus_server_new_sync(
40 static_cast<GDBusServerFlags>(flags),
50 message = error->message;
54 ThrowMsg(DPL::Exception, "Error on server creation: " << message);
57 return ServerPtr(new Server(server));
60 Server::Server(GDBusServer* server) :
66 if (g_dbus_server_is_active(m_server)) {
69 g_object_unref(m_server);
74 Assert(!g_dbus_server_is_active(m_server) && "Server already started.");
76 g_dbus_server_start(m_server);
78 g_signal_connect(m_server,
80 G_CALLBACK(onNewConnection),
83 LogInfo("Server started at: "
84 << g_dbus_server_get_client_address(m_server));
89 Assert(g_dbus_server_is_active(m_server) && "Server not started.");
91 g_dbus_server_stop(m_server);
94 gboolean Server::onNewConnection(GDBusServer* /*server*/,
95 GDBusConnection* connection,
98 Assert(NULL != data && "User data cannot be NULL.");
100 Server* self = static_cast<Server*>(data);
102 ServerEvents::NewConnectionEvent event(
103 ConnectionPtr(new Connection(connection)));
105 LogInfo("Emitting new connection event");
106 // TODO Blocking to allow object registration before any DBus messages are
108 self->DPL::Event::EventSupport<ServerEvents::NewConnectionEvent>::
109 EmitEvent(event, DPL::Event::EmitMode::Blocking);