summaryrefslogtreecommitdiff
path: root/src/object.cpp
blob: 96e20bab8a0b42464388a1e8ffbda741e4fcdd6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
 *
 *  D-Bus++ - C++ bindings for D-Bus
 *
 *  Copyright (C) 2005-2007  Paolo Durante <shackan@gmail.com>
 *
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <dbus-c++/debug.h>
#include <dbus-c++/object.h>
#include "internalerror.h"

#include <cstring>
#include <map>
#include <dbus/dbus.h>

#include "message_p.h"
#include "server_p.h"
#include "connection_p.h"

using namespace DBus;

Object::Object(Connection &conn, const Path &path, const char *service)
  : _conn(conn), _path(path), _service(service ? service : ""), _default_timeout(-1)
{
}

Object::~Object()
{
}

void Object::set_timeout(int new_timeout)
{
  debug_log("%s: %d millies", __PRETTY_FUNCTION__, new_timeout);
  if (new_timeout < 0 && new_timeout != -1)
    throw ErrorInvalidArgs("Bad timeout, cannot set it");
  _default_timeout = new_timeout;
}

struct ObjectAdaptor::Private
{
  static void unregister_function_stub(DBusConnection *, void *);
  static DBusHandlerResult message_function_stub(DBusConnection *, DBusMessage *, void *);
};

static DBusObjectPathVTable _vtable =
{
  ObjectAdaptor::Private::unregister_function_stub,
  ObjectAdaptor::Private::message_function_stub,
  NULL, NULL, NULL, NULL
};

void ObjectAdaptor::Private::unregister_function_stub(DBusConnection *conn, void *data)
{
  //TODO: what do we have to do here ?
}

DBusHandlerResult ObjectAdaptor::Private::message_function_stub(DBusConnection *, DBusMessage *dmsg, void *data)
{
  ObjectAdaptor *o = static_cast<ObjectAdaptor *>(data);

  if (o)
  {
    Message msg(new Message::Private(dmsg));

    debug_log("in object %s", o->path().c_str());
    debug_log(" got message #%d from %s to %s",
              msg.serial(),
              msg.sender(),
              msg.destination()
             );

    return o->handle_message(msg)
           ? DBUS_HANDLER_RESULT_HANDLED
           : DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  }
  else
  {
    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
  }
}

typedef std::map<Path, ObjectAdaptor *> ObjectAdaptorTable;
static ObjectAdaptorTable _adaptor_table;

ObjectAdaptor *ObjectAdaptor::from_path(const Path &path)
{
  ObjectAdaptorTable::iterator ati = _adaptor_table.find(path);

  if (ati != _adaptor_table.end())
    return ati->second;

  return NULL;
}

ObjectAdaptorPList ObjectAdaptor::from_path_prefix(const std::string &prefix)
{
  ObjectAdaptorPList ali;

  ObjectAdaptorTable::iterator ati = _adaptor_table.begin();

  size_t plen = prefix.length();

  while (ati != _adaptor_table.end())
  {
    if (!strncmp(ati->second->path().c_str(), prefix.c_str(), plen))
      ali.push_back(ati->second);

    ++ati;
  }

  return ali;
}

ObjectPathList ObjectAdaptor::child_nodes_from_prefix(const std::string &prefix)
{
  ObjectPathList ali;

  ObjectAdaptorTable::iterator ati = _adaptor_table.begin();

  size_t plen = prefix.length();

  while (ati != _adaptor_table.end())
  {
    if (!strncmp(ati->second->path().c_str(), prefix.c_str(), plen))
    {
      std::string p = ati->second->path().substr(plen);
      p = p.substr(0, p.find('/'));
      ali.push_back(p);
    }
    ++ati;
  }

  ali.sort();
  ali.unique();

  return ali;
}

ObjectAdaptor::ObjectAdaptor(Connection &conn, const Path &path)
  : Object(conn, path, conn.unique_name())
{
  register_obj();
}

ObjectAdaptor::~ObjectAdaptor()
{
  unregister_obj(false);
}

void ObjectAdaptor::register_obj()
{
  debug_log("registering local object %s", path().c_str());

  if (!dbus_connection_register_object_path(conn()._pvt->conn, path().c_str(), &_vtable, this))
  {
    throw ErrorNoMemory("unable to register object path");
  }

  _adaptor_table[path()] = this;
}

void ObjectAdaptor::unregister_obj(bool)
{
  _adaptor_table.erase(path());

  debug_log("unregistering local object %s", path().c_str());

  dbus_connection_unregister_object_path(conn()._pvt->conn, path().c_str());
}

void ObjectAdaptor::_emit_signal(SignalMessage &sig)
{
  sig.path(path().c_str());

  conn().send(sig);
}

struct ReturnLaterError
{
  const Tag *tag;
};

bool ObjectAdaptor::handle_message(const Message &msg)
{
  switch (msg.type())
  {
  case DBUS_MESSAGE_TYPE_METHOD_CALL:
  {
    const CallMessage &cmsg = reinterpret_cast<const CallMessage &>(msg);
    const char *member      = cmsg.member();
    const char *interface   = cmsg.interface();

    debug_log(" invoking method %s.%s", interface, member);

    InterfaceAdaptor *ii = find_interface(interface);
    if (ii)
    {
      try
      {
        Message ret = ii->dispatch_method(cmsg);
        conn().send(ret);
      }
      catch (Error &e)
      {
        ErrorMessage em(cmsg, e.name(), e.message());
        conn().send(em);
      }
      catch (ReturnLaterError &rle)
      {
        _continuations[rle.tag] = new Continuation(conn(), cmsg, rle.tag);
      }
      return true;
    }
    else
    {
      return false;
    }
  }
  default:
  {
    return false;
  }
  }
}

void ObjectAdaptor::return_later(const Tag *tag)
{
  ReturnLaterError rle = { tag };
  throw rle;
}

void ObjectAdaptor::return_now(Continuation *ret)
{
  ret->_conn.send(ret->_return);

  ContinuationMap::iterator di = _continuations.find(ret->_tag);

  delete di->second;

  _continuations.erase(di);
}

void ObjectAdaptor::return_error(Continuation *ret, const Error error)
{
  ret->_conn.send(ErrorMessage(ret->_call, error.name(), error.message()));

  ContinuationMap::iterator di = _continuations.find(ret->_tag);

  delete di->second;

  _continuations.erase(di);
}

ObjectAdaptor::Continuation *ObjectAdaptor::find_continuation(const Tag *tag)
{
  ContinuationMap::iterator di = _continuations.find(tag);

  return di != _continuations.end() ? di->second : NULL;
}

ObjectAdaptor::Continuation::Continuation(Connection &conn, const CallMessage &call, const Tag *tag)
  : _conn(conn), _call(call), _return(_call), _tag(tag)
{
  _writer = _return.writer(); //todo: verify
}

/*
*/

ObjectProxy::ObjectProxy(Connection &conn, const Path &path, const char *service)
  : Object(conn, path, service)
{
  register_obj();
}

ObjectProxy::~ObjectProxy()
{
  unregister_obj(false);
}

void ObjectProxy::register_obj()
{
  debug_log("registering remote object %s", path().c_str());

  _filtered = new Callback<ObjectProxy, bool, const Message &>(this, &ObjectProxy::handle_message);

  conn().add_filter(_filtered);

  InterfaceProxyTable::const_iterator ii = _interfaces.begin();
  while (ii != _interfaces.end())
  {
    std::string im = "type='signal',interface='" + ii->first + "',path='" + path() + "'";
    conn().add_match(im.c_str());
    ++ii;
  }
}

void ObjectProxy::unregister_obj(bool throw_on_error)
{
  debug_log("unregistering remote object %s", path().c_str());

  InterfaceProxyTable::const_iterator ii = _interfaces.begin();
  while (ii != _interfaces.end())
  {
    std::string im = "type='signal',interface='" + ii->first + "',path='" + path() + "'";
    conn().remove_match(im.c_str(), throw_on_error);
    ++ii;
  }
  conn().remove_filter(_filtered);
}

Message ObjectProxy::_invoke_method(CallMessage &call)
{
  if (call.path() == NULL)
    call.path(path().c_str());

  if (call.destination() == NULL)
    call.destination(service().c_str());

  return conn().send_blocking(call, get_timeout());
}

bool ObjectProxy::_invoke_method_noreply(CallMessage &call)
{
  if (call.path() == NULL)
    call.path(path().c_str());

  if (call.destination() == NULL)
    call.destination(service().c_str());

  return conn().send(call);
}

bool ObjectProxy::handle_message(const Message &msg)
{
  switch (msg.type())
  {
  case DBUS_MESSAGE_TYPE_SIGNAL:
  {
    const SignalMessage &smsg = reinterpret_cast<const SignalMessage &>(msg);
    const char *interface	= smsg.interface();
    const char *member	= smsg.member();
    const char *objpath	= smsg.path();

    if (objpath != path()) return false;

    debug_log("filtered signal %s(in %s) from %s to object %s",
              member, interface, msg.sender(), objpath);

    InterfaceProxy *ii = find_interface(interface);
    if (ii)
    {
      return ii->dispatch_signal(smsg);
    }
    else
    {
      return false;
    }
  }
  default:
  {
    return false;
  }
  }
}