Initialize Tizen 2.3
[framework/web/wrt-commons.git] / modules_mobile / dbus / src / server.cpp
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    server.cpp
18  * @author  Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  * @version 1.0
20  * @brief
21  */
22 #include <stddef.h>
23 #include <dpl/assert.h>
24 #include <dpl/log/log.h>
25 #include <dpl/dbus/server.h>
26
27 namespace DPL {
28 namespace DBus {
29 ServerPtr Server::create(const std::string& address)
30 {
31     GError* error = NULL;
32
33     int flags = G_DBUS_SERVER_FLAGS_NONE |
34         G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
35
36     gchar* serverId = g_dbus_generate_guid();
37
38     GDBusServer* server = g_dbus_server_new_sync(
39             address.c_str(),
40             static_cast<GDBusServerFlags>(flags),
41             serverId,
42             NULL,
43             NULL,
44             &error);
45     g_free(serverId);
46
47     if (NULL == server) {
48         std::string message;
49         if (NULL != error) {
50             message = error->message;
51             g_error_free(error);
52         }
53
54         ThrowMsg(DPL::Exception, "Error on server creation: " << message);
55     }
56
57     return ServerPtr(new Server(server));
58 }
59
60 Server::Server(GDBusServer* server) :
61     m_server(server)
62 {}
63
64 Server::~Server()
65 {
66     if (g_dbus_server_is_active(m_server)) {
67         stop();
68     }
69     g_object_unref(m_server);
70 }
71
72 void Server::start()
73 {
74     Assert(!g_dbus_server_is_active(m_server) && "Server already started.");
75
76     g_dbus_server_start(m_server);
77
78     g_signal_connect(m_server,
79                      "new-connection",
80                      G_CALLBACK(onNewConnection),
81                      this);
82
83     LogDebug("Server started at: "
84             << g_dbus_server_get_client_address(m_server));
85 }
86
87 void Server::stop()
88 {
89     Assert(g_dbus_server_is_active(m_server) && "Server not started.");
90
91     g_dbus_server_stop(m_server);
92 }
93
94 gboolean Server::onNewConnection(GDBusServer* /*server*/,
95                                  GDBusConnection* connection,
96                                  gpointer data)
97 {
98     Assert(NULL != data && "User data cannot be NULL.");
99
100     Server* self = static_cast<Server*>(data);
101
102     ServerEvents::NewConnectionEvent event(
103         ConnectionPtr(new Connection(connection)));
104
105     LogDebug("Emitting new connection event");
106     // TODO Blocking to allow object registration before any DBus messages are
107     //      processed.
108     self->DPL::Event::EventSupport<ServerEvents::NewConnectionEvent>::
109         EmitEvent(event, DPL::Event::EmitMode::Blocking);
110
111     return TRUE;
112 }
113 }
114 }