aboutsummaryrefslogtreecommitdiff
path: root/src/lib/common/gnunet_dbus_lib_client.c
blob: 72b3aef1c71cbd41961db9effae54a0d296c8a64 (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
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
#include "config.h"

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

#include "gnunet_dbus_lib.h"

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

struct GNUNET_DBUS_Client
{
  char *unique_name;
  void *data;
  bool prefers_pretty_encodings;
  unsigned ref_count;
};

struct GNUNET_DBUS_Client *
GNUNET_DBUS_client_create (
    const char *unique_name)
{
  LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating client with unique name %s\n", unique_name);

  struct GNUNET_DBUS_Client *client = GNUNET_new (struct GNUNET_DBUS_Client);
  client->unique_name = GNUNET_strdup (unique_name);
  client->data = NULL;
  client->prefers_pretty_encodings = false;
  client->ref_count = 1;

  return client;
};

void
GNUNET_DBUS_client_ref (
    struct GNUNET_DBUS_Client *client)
{
  client->ref_count++;
}

void
GNUNET_DBUS_client_unref (
    struct GNUNET_DBUS_Client *client)
{
  if (client->ref_count == 0)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unref client with ref count 0\n");
    LOG (GNUNET_ERROR_TYPE_ERROR, "  unique_name == %s\n", client->unique_name);
    GNUNET_abort_ ();
  }

  if (0 == --(client->ref_count))
  {
    LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying client with unique name %s\n", client->unique_name);

    GNUNET_free (client->unique_name);
    GNUNET_free (client);
  }
};

const char *
GNUNET_DBUS_client_get_unique_name (
    const struct GNUNET_DBUS_Client *client)
{
  return client->unique_name;
};

void
GNUNET_DBUS_client_set_data (
    struct GNUNET_DBUS_Client *client,
    void *data)
{
  client->data = data;
};

void *
GNUNET_DBUS_client_get_data (
    const struct GNUNET_DBUS_Client *client)
{
  return client->data;
};

void
GNUNET_DBUS_client_set_prefers_pretty_encodings (
    struct GNUNET_DBUS_Client *client,
    bool prefers_pretty_encodings)
{
  client->prefers_pretty_encodings = prefers_pretty_encodings;
}

bool
GNUNET_DBUS_client_get_prefers_pretty_encodings (
    const struct GNUNET_DBUS_Client *client)
{
  return client->prefers_pretty_encodings;
};

DBusMessage *
GNUNET_DBUS_client_create_method_call (
    struct GNUNET_DBUS_Client *client,
    const char *object_path,
    const char *interface,
    const char *method,
    bool pretty,
    void (*return_callback)(DBusMessage *message))
{
  DBusMessage *ret = dbus_message_new_method_call (
      client->unique_name,
      object_path,
      interface,
      method);
  GNUNET_DBUS_message_set_pretty (ret, pretty);
  if (return_callback)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, "Method calls on external objects not implemented yet.\n");
    GNUNET_abort_ ();
  };
  return ret;
}