aboutsummaryrefslogtreecommitdiff
path: root/src/lib/common/gnunet_dbus_lib_method_context.c
blob: f7a4bac71b2f82e58976f77d2d706a1ff31b9020 (plain) (blame)
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
#include "config.h"

#include <dbus/dbus.h>

#include <gnunet/platform.h>
#include <gnunet/gnunet_common.h>
#include <gnunet/gnunet_container_lib.h>

#include "gnunet_dbus_lib.h"

#define LOG(kind, ...) GNUNET_log_from (kind, "dbus-method-context", __VA_ARGS__)

struct GNUNET_DBUS_MethodContext *
GNUNET_DBUS_method_context_create (
    struct GNUNET_DBUS_Client *client,
    struct GNUNET_DBUS_Service *service,
    struct GNUNET_DBUS_Object *object,
    struct GNUNET_DBUS_Interface *interface,
    struct GNUNET_DBUS_Method *method,
    DBusMessage *message)
{
  struct GNUNET_DBUS_MethodContext *ret = GNUNET_new (struct GNUNET_DBUS_MethodContext);
  ret->client         = client;
  ret->service        = service;
  ret->object         = object;
  ret->interface      = interface;
  ret->method         = method;
  ret->message        = message;
  ret->expects_reply  = ! dbus_message_get_no_reply (message);

  ret->replied = false;
  ret->ref_count = 1;
  dbus_message_ref (message);

  return ret;
};

void
GNUNET_DBUS_method_context_ref (
    struct GNUNET_DBUS_MethodContext *mc)
{
  mc->ref_count++;
};

void
GNUNET_DBUS_method_context_unref (
    struct GNUNET_DBUS_MethodContext *mc)
{
  if (0 == mc->ref_count)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference method context with ref count 0\n");
    GNUNET_abort_ ();
  };
  if (0 == --(mc->ref_count))
  {
    dbus_message_unref (mc->message);
    GNUNET_free (mc);
  }
};

void
GNUNET_DBUS_method_context_send_reply (
    struct GNUNET_DBUS_MethodContext *mc,
    DBusMessage *reply)
{
  if (! mc->expects_reply)
    return;

  GNUNET_DBUS_service_send (mc->service, reply);
  dbus_message_unref (reply);
};

DBusMessage *
GNUNET_DBUS_method_context_create_reply (
    struct GNUNET_DBUS_MethodContext *mc)
{
  DBusMessage *ret = dbus_message_new_method_return (mc->message);
  //GNUNET_DBUS_message_set_pretty (ret, GNUNET_DBUS_client_get_prefers_pretty_encodings (mc->client));
  GNUNET_DBUS_message_set_pretty (ret, GNUNET_DBUS_message_get_pretty (mc->message));
  return ret;
}