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

#include <inttypes.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-object", __VA_ARGS__)

struct GNUNET_DBUS_Object
{
  struct GNUNET_DBUS_InterfaceIterator *interfaces_front;
  struct GNUNET_DBUS_InterfaceIterator *interfaces_back;

  struct GNUNET_DBUS_ObjectIterator *subobjects_front;
  struct GNUNET_DBUS_ObjectIterator *subobjects_back;

  char *name;
  void *data;
  unsigned ref_count;
};

struct GNUNET_DBUS_Object *
GNUNET_DBUS_object_create (
    const char *name,
    void *data)
{
  struct GNUNET_DBUS_Object *object = GNUNET_new (struct GNUNET_DBUS_Object);
  object->name = GNUNET_strdup (name);
  object->data = data;
  object->interfaces_front = NULL;
  object->interfaces_back = NULL;
  object->subobjects_front = NULL;
  object->subobjects_back = NULL;
  object->ref_count = 1;
  
  return object;
};

void
GNUNET_DBUS_object_ref (
    struct GNUNET_DBUS_Object *object)
{
  object->ref_count++;
};

void
GNUNET_DBUS_object_unref (
    struct GNUNET_DBUS_Object *object)
{
  if (object->ref_count == 0)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference object with ref count 0\n");
    GNUNET_abort_ ();
  };
  if (0 == --(object->ref_count))
  {
    struct GNUNET_DBUS_InterfaceIterator *int_it = object->interfaces_front;
    while (int_it)
    {
      struct GNUNET_DBUS_InterfaceIterator *next = int_it->next;
      GNUNET_DBUS_interface_unref (int_it->interface);
      GNUNET_free (int_it);
      int_it = next;
    };

    struct GNUNET_DBUS_ObjectIterator *obj_it = object->subobjects_front;
    while (obj_it)
    {
      struct GNUNET_DBUS_ObjectIterator *next = obj_it->next;
      GNUNET_DBUS_object_unref (obj_it->object);
      GNUNET_free (obj_it);
      obj_it = next;
    };

    GNUNET_free (object->name);
    GNUNET_free (object);
  }
};

const char *
GNUNET_DBUS_object_get_name (
    const struct GNUNET_DBUS_Object *object)
{
  return object->name;
};

void
GNUNET_DBUS_object_add_interface (
    struct GNUNET_DBUS_Object *object,
    struct GNUNET_DBUS_Interface *interface)
{
  struct GNUNET_DBUS_InterfaceIterator *int_it = GNUNET_new (struct GNUNET_DBUS_InterfaceIterator);
  int_it->interface = interface;
  GNUNET_DBUS_interface_ref (interface);

  GNUNET_CONTAINER_DLL_insert (object->interfaces_front,
                               object->interfaces_back,
                               int_it);
};

const struct GNUNET_DBUS_InterfaceIterator *
GNUNET_DBUS_object_iterate_interfaces (
    struct GNUNET_DBUS_Object *object)
{
  return object->interfaces_front;
};

void *
GNUNET_DBUS_object_get_data (
    struct GNUNET_DBUS_Object *object)
{
  return object->data;
};

struct GNUNET_DBUS_Object *
GNUNET_DBUS_object_create_uniquely_named_subobject (
    struct GNUNET_DBUS_Object *object,
    void *data)
{
  while(true)
  {
    char name[9];
    uint32_t id = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 0xffffffff);
    GNUNET_snprintf (name, sizeof(name), "%08" PRIx32, id);
    const struct GNUNET_DBUS_ObjectIterator *obj_it = GNUNET_DBUS_object_iterate_subobjects (object);
    while (true)
    {
      if (! obj_it)
      {
        struct GNUNET_DBUS_Object *subobject = GNUNET_DBUS_object_create (name, data);
        GNUNET_DBUS_object_add_subobject (object, subobject);
        return subobject;
      }

      const struct GNUNET_DBUS_Object *test_object = obj_it->object;
      if (0 == strcmp (name, GNUNET_DBUS_object_get_name (test_object)))
        break;

      obj_it = obj_it->next;
    }
  }
}

void
GNUNET_DBUS_object_add_subobject (
    struct GNUNET_DBUS_Object *object,
    struct GNUNET_DBUS_Object *subobject)
{
  struct GNUNET_DBUS_ObjectIterator *obj_it = GNUNET_new (struct GNUNET_DBUS_ObjectIterator);
  obj_it->object = subobject;
  GNUNET_DBUS_object_ref (subobject);

  GNUNET_CONTAINER_DLL_insert (object->subobjects_front,
                               object->subobjects_back,
                               obj_it);
};

void
GNUNET_DBUS_object_remove_subobject (
    struct GNUNET_DBUS_Object *object,
    struct GNUNET_DBUS_ObjectIterator *subobject_it)
{
  GNUNET_DBUS_object_unref (subobject_it->object);
  GNUNET_CONTAINER_DLL_remove (object->subobjects_front,
                               object->subobjects_back,
                               subobject_it);
  GNUNET_free (subobject_it);
};

struct GNUNET_DBUS_ObjectIterator *
GNUNET_DBUS_object_iterate_subobjects (
    const struct GNUNET_DBUS_Object *object)
{
  return object->subobjects_front;
}

const struct GNUNET_DBUS_ObjectIterator *
GNUNET_DBUS_object_find (
    const struct GNUNET_DBUS_ObjectIterator *object_it,
    const char *name)
{
  while (object_it && strcmp (name, object_it->object->name))
    object_it = object_it->next;

  return object_it;
}