~oif-team/geis/utopic

« back to all changes in this revision

Viewing changes to libutouch-geis/server/geis_dbus_announcer.c

  • Committer: Stephen M. Webb
  • Date: 2011-08-18 22:07:20 UTC
  • mto: (158.1.39 client-arch)
  • mto: This revision was merged to the branch mainline in revision 166.
  • Revision ID: stephen.webb@canonical.com-20110818220720-v443m85qsxf41e87
Introduced a skeletal geis-dbus server.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file geis_dbus_announcer.c
 
3
 * @brief Implementation of the GEIS DBus announcer.
 
4
 */
 
5
 
 
6
/*
 
7
 * Copyright 2011 Canonical Ltd.
 
8
 *
 
9
 * This library is free software; you can redistribute it and/or modify it under
 
10
 * the terms of the GNU Lesser General Public License as published by the Free
 
11
 * Software Foundation; either version 3 of the License, or (at your option) any
 
12
 * later version.
 
13
 *
 
14
 * This library is distributed in the hope that it will be useful, but WITHOUT
 
15
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
16
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 
17
 * details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 */
 
22
#include "geis_config.h"
 
23
#include "geis_dbus_announcer.h"
 
24
 
 
25
#include <dbus/dbus.h>
 
26
#include "geis_dbus.h"
 
27
#include <stdio.h>
 
28
#include <stdlib.h>
 
29
 
 
30
 
 
31
struct GeisDBusAnnouncer
 
32
{
 
33
  GeisDBusServer  server;
 
34
  DBusConnection *session_bus;
 
35
};
 
36
 
 
37
 
 
38
static void _unregister_messages(DBusConnection*, void*);
 
39
static DBusHandlerResult _dbus_messages(DBusConnection*, DBusMessage*, void*);
 
40
static DBusHandlerResult _geis_messages(DBusConnection*, DBusMessage*, void*);
 
41
 
 
42
static struct DBusObjectPathVTable _dbus_message_vtbl = 
 
43
{
 
44
  _unregister_messages,
 
45
  _dbus_messages,
 
46
  0, 0, 0, 0
 
47
};
 
48
 
 
49
static struct DBusObjectPathVTable _geis_message_vtbl = 
 
50
{
 
51
  _unregister_messages,
 
52
  _geis_messages,
 
53
  0, 0, 0, 0
 
54
};
 
55
 
 
56
 
 
57
static void
 
58
_unregister_messages(DBusConnection *connection GEIS_UNUSED,
 
59
                     void           *user_data GEIS_UNUSED)
 
60
{
 
61
  /* dummy */
 
62
}
 
63
 
 
64
 
 
65
/*
 
66
 * Processes DBus metamessages.
 
67
 */
 
68
static DBusHandlerResult
 
69
_dbus_messages(DBusConnection *connection GEIS_UNUSED,
 
70
               DBusMessage    *message,
 
71
               void           *user_data GEIS_UNUSED)
 
72
{
 
73
  DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
74
 
 
75
  fprintf(stderr, "%s() begins\n", __FUNCTION__);
 
76
  if (dbus_message_is_signal(message,
 
77
                             DBUS_SERVICE_DBUS,
 
78
                             "NameAcquired"))
 
79
  {
 
80
    fprintf(stderr, "%s() .. name acquired!\n", __FUNCTION__);
 
81
  }
 
82
 
 
83
  fprintf(stderr, "%s() ends\n", __FUNCTION__);
 
84
  return result;
 
85
}
 
86
 
 
87
 
 
88
/*
 
89
 * Processes GEIS messages.
 
90
 */
 
91
static DBusHandlerResult
 
92
_geis_messages(DBusConnection *connection,
 
93
               DBusMessage    *message,
 
94
               void           *user_data)
 
95
{
 
96
  DBusHandlerResult result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
97
  GeisDBusAnnouncer announcer = (GeisDBusAnnouncer)user_data;
 
98
 
 
99
  fprintf(stderr, "%s begins\n", __FUNCTION__);
 
100
  if (dbus_message_is_method_call(message,
 
101
                                  GEIS_DBUS_SERVICE_INTERFACE,
 
102
                                  GEIS_DBUS_GET_SERVER_ADDRESS))
 
103
  {
 
104
    char *server_addr = geis_dbus_server_address(announcer->server);
 
105
    DBusMessage *reply = dbus_message_new_method_return(message);
 
106
    dbus_message_append_args(reply,
 
107
                             DBUS_TYPE_STRING, &server_addr,
 
108
                             DBUS_TYPE_INVALID);
 
109
    dbus_connection_send(connection, reply, NULL);
 
110
    dbus_message_unref(reply);
 
111
    dbus_free(server_addr);
 
112
    result = DBUS_HANDLER_RESULT_HANDLED;
 
113
  }
 
114
 
 
115
  fprintf(stderr, "%s ends\n", __FUNCTION__);
 
116
  return result;
 
117
}
 
118
 
 
119
 
 
120
/*
 
121
 * Adds the announcer watches to the dispatcher watch list.
 
122
 */
 
123
static dbus_bool_t
 
124
_announcer_add_watch(DBusWatch *watch, void *data)
 
125
{
 
126
  dbus_bool_t status = TRUE;
 
127
  int fd = dbus_watch_get_unix_fd(watch);
 
128
  dbus_bool_t is_enabled = dbus_watch_get_enabled(watch);
 
129
  unsigned int flags = dbus_watch_get_flags(watch); 
 
130
  GeisDBusAnnouncer announcer = (GeisDBusAnnouncer)data;
 
131
 
 
132
  fprintf(stdout, "%s() watch=%p fd=%d is_enabled=%d flags=%d\n",
 
133
          __FUNCTION__, (void *)watch, fd, is_enabled, flags);
 
134
 
 
135
  geis_dbus_dispatcher_register(geis_dbus_server_dispatcher(announcer->server),
 
136
                                announcer->session_bus,
 
137
                                watch);
 
138
  return status;
 
139
}
 
140
 
 
141
 
 
142
/*
 
143
 * Toggles the enabled/disabled status of the announcer watches.
 
144
 */
 
145
static void
 
146
_announcer_toggle_watch(DBusWatch *watch, void *data)
 
147
{
 
148
  int fd = dbus_watch_get_unix_fd(watch);
 
149
  dbus_bool_t is_enabled = dbus_watch_get_enabled(watch);
 
150
  GeisDBusAnnouncer announcer = (GeisDBusAnnouncer)data;
 
151
 
 
152
  fprintf(stdout, "%s watch=%p fd=%d is_enabled=%d\n",
 
153
          __FUNCTION__, (void *)watch, fd, is_enabled);
 
154
  geis_dbus_dispatcher_toggle_watch(geis_dbus_server_dispatcher(announcer->server),
 
155
                                    watch);
 
156
}
 
157
 
 
158
 
 
159
/*
 
160
 * Removes the announcer watches from the dispatcher watch list.
 
161
 */
 
162
static void
 
163
_announcer_remove_watch(DBusWatch *watch, void *data)
 
164
{
 
165
  int fd = dbus_watch_get_unix_fd(watch);
 
166
  unsigned int flags = dbus_watch_get_flags(watch); 
 
167
  GeisDBusAnnouncer announcer = (GeisDBusAnnouncer)data;
 
168
 
 
169
  fprintf(stdout, "%s fd=%d flags=%d\n", __FUNCTION__, fd, flags);
 
170
  geis_dbus_dispatcher_unregister(geis_dbus_server_dispatcher(announcer->server),
 
171
                                  watch);
 
172
}
 
173
 
 
174
 
 
175
/*
 
176
 * Creates a new GeisDBusAnouncer object.
 
177
 */
 
178
GeisDBusAnnouncer
 
179
geis_dbus_announcer_new(GeisDBusServer server)
 
180
{
 
181
  DBusError error;
 
182
  GeisDBusAnnouncer announcer = NULL;
 
183
  
 
184
  fprintf(stderr, "%s begins\n", __FUNCTION__);
 
185
  announcer = calloc(1, sizeof(struct GeisDBusAnnouncer));
 
186
  if (!announcer)
 
187
    goto final_exit;
 
188
 
 
189
  announcer->server = server;
 
190
 
 
191
  dbus_error_init(&error);
 
192
  announcer->session_bus = dbus_bus_get(DBUS_BUS_SESSION, &error);
 
193
  if (!announcer->session_bus || dbus_error_is_set(&error))
 
194
  {
 
195
    char msg[128];
 
196
    sprintf(msg, "error %s connecting to session bus: %s",
 
197
            error.name, error.message);
 
198
    fprintf(stderr, "%s: %s\n", __FUNCTION__, msg);
 
199
    goto unwind_error;
 
200
  }
 
201
 
 
202
  dbus_connection_set_watch_functions(announcer->session_bus,
 
203
                                      _announcer_add_watch,
 
204
                                      _announcer_remove_watch,
 
205
                                      _announcer_toggle_watch,
 
206
                                      announcer, 0);
 
207
 
 
208
  dbus_connection_register_object_path(announcer->session_bus,
 
209
                                       DBUS_PATH_DBUS,
 
210
                                       &_dbus_message_vtbl,
 
211
                                       announcer);
 
212
 
 
213
  dbus_connection_register_object_path(announcer->session_bus,
 
214
                                       GEIS_DBUS_SERVICE_PATH,
 
215
                                       &_geis_message_vtbl,
 
216
                                       announcer);
 
217
 
 
218
  dbus_bus_request_name(announcer->session_bus,
 
219
                        GEIS_DBUS_SERVICE_INTERFACE,
 
220
                        DBUS_NAME_FLAG_REPLACE_EXISTING,
 
221
                        &error);
 
222
  if (dbus_error_is_set(&error))
 
223
  {
 
224
    fprintf(stderr, "error requesting Server name\n");
 
225
  }
 
226
 
 
227
 
 
228
unwind_error:
 
229
  dbus_error_free(&error);
 
230
final_exit:
 
231
  fprintf(stderr, "%s ends\n", __FUNCTION__);
 
232
  return announcer;
 
233
}
 
234
 
 
235
 
 
236
/*
 
237
 * Destroys a %GeisDBusAnnouncer object.
 
238
 */
 
239
void
 
240
geis_dbus_announcer_delete(GeisDBusAnnouncer announcer)
 
241
{
 
242
  fprintf(stderr, "%s() begins\n", __FUNCTION__);
 
243
  if (announcer)
 
244
  {
 
245
    fprintf(stderr, "%s() freeing announcer\n", __FUNCTION__);
 
246
    if (announcer->session_bus)
 
247
    {
 
248
      fprintf(stderr, "%s() unreffing session bus\n", __FUNCTION__);
 
249
      dbus_connection_unref(announcer->session_bus);
 
250
    }
 
251
    fprintf(stderr, "%s() freeing announcer\n", __FUNCTION__);
 
252
    free(announcer);
 
253
  }
 
254
  fprintf(stderr, "%s() ends\n", __FUNCTION__);
 
255
}
 
256