aboutsummaryrefslogtreecommitdiff
path: root/src/lib/common/gnunet_dbus_lib_method.c
blob: ebf631d573b182b895fd1c3c5a97211f8b1bf391 (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
#include "config.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", __VA_ARGS__)

struct GNUNET_DBUS_Method
{
  struct GNUNET_DBUS_ArgIterator *args_front;
  struct GNUNET_DBUS_ArgIterator *args_back;

  struct GNUNET_DBUS_ArgIterator *return_args_front;
  struct GNUNET_DBUS_ArgIterator *return_args_back;

  char *name;

  void (*underlying_method)(struct GNUNET_DBUS_MethodContext *mc);

  unsigned ref_count;
};

struct GNUNET_DBUS_Method *
GNUNET_DBUS_method_create (
    const char *name,
    void (*underlying_method)(struct GNUNET_DBUS_MethodContext *mc))
{
  struct GNUNET_DBUS_Method *method = GNUNET_new (struct GNUNET_DBUS_Method);
  method->name = GNUNET_strdup (name);
  method->underlying_method = underlying_method;
  method->ref_count = 1;

  return method;
};

void
GNUNET_DBUS_method_ref (
    struct GNUNET_DBUS_Method *method)
{
  method->ref_count++;
};

void
GNUNET_DBUS_method_unref (
    struct GNUNET_DBUS_Method *method)
{
  if (method->ref_count == 0)
  {
    LOG (GNUNET_ERROR_TYPE_ERROR, "Tried to unreference method with ref count 0\n");
    GNUNET_abort_ ();
  };
  if (0 == --(method->ref_count))
  {
    struct GNUNET_DBUS_ArgIterator *arg_it = method->args_front;
    while (arg_it)
    {
      struct GNUNET_DBUS_ArgIterator *next = arg_it->next;
      GNUNET_DBUS_arg_unref (arg_it->arg);
      GNUNET_free (arg_it);
      arg_it = next;
    }

    arg_it = method->return_args_front;
    while (arg_it)
    {
      struct GNUNET_DBUS_ArgIterator *next = arg_it->next;
      GNUNET_DBUS_arg_unref (arg_it->arg);
      GNUNET_free (arg_it);
      arg_it = next;
    }

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

void
GNUNET_DBUS_method_add_arg (
    struct GNUNET_DBUS_Method *method,
    const char *name,
    const char *signature)
{
  struct GNUNET_DBUS_Arg *arg = GNUNET_DBUS_arg_create (name, signature);
  struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_new (struct GNUNET_DBUS_ArgIterator);
  arg_it->arg = arg;
  GNUNET_CONTAINER_DLL_insert_tail (method->args_front,
                                    method->args_back,
                                    arg_it);
};

void
GNUNET_DBUS_method_add_return_arg (
    struct GNUNET_DBUS_Method *method,
    const char *name,
    const char *signature)
{
  struct GNUNET_DBUS_Arg *arg = GNUNET_DBUS_arg_create (name, signature);
  struct GNUNET_DBUS_ArgIterator *arg_it = GNUNET_new (struct GNUNET_DBUS_ArgIterator);
  arg_it->arg = arg;
  GNUNET_CONTAINER_DLL_insert_tail (method->return_args_front,
                                    method->return_args_back,
                                    arg_it);
};

const char *
GNUNET_DBUS_method_get_name (
    const struct GNUNET_DBUS_Method *method)
{
  return method->name;
};

const struct GNUNET_DBUS_ArgIterator *
GNUNET_DBUS_method_iterate_args (
    const struct GNUNET_DBUS_Method *method)
{
  return method->args_front;
};

const struct GNUNET_DBUS_ArgIterator *
GNUNET_DBUS_method_iterate_return_args (
    const struct GNUNET_DBUS_Method *method)
{
  return method->return_args_front;
};

void
GNUNET_DBUS_method_call (
    struct GNUNET_DBUS_Method *method,
    struct GNUNET_DBUS_MethodContext *mc)
{
  method->underlying_method (mc);
};

const struct GNUNET_DBUS_MethodIterator *
GNUNET_DBUS_method_find (
    const struct GNUNET_DBUS_MethodIterator *meth_it,
    const char *name)
{
  for (; meth_it; meth_it = meth_it->next)
  {
    if (! strcmp (name, meth_it->method->name))
      return meth_it;
  }
  return NULL;
}