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