summaryrefslogtreecommitdiff
path: root/src/tests/eldbus/eldbus_fake_server.c
blob: 32f5b9bd170bbd2f4d815c28c90744eea58dffbb (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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <Ecore.h>

#include "eldbus_suite.h"
#include "eldbus_fake_server.h"

#define FAKE_SERVER_DATA_KEY "data"

static void _fake_server_name_request_cb(void *, const Eldbus_Message *, Eldbus_Pending *);
static Eina_Bool _fakse_server_property_get(const Eldbus_Service_Interface *, const char *, Eldbus_Message_Iter *, const Eldbus_Message *, Eldbus_Message **);
static Eldbus_Message *_fake_server_property_set(const Eldbus_Service_Interface *, const char *, Eldbus_Message_Iter *, const Eldbus_Message *);
static Eldbus_Message *_fake_server_sum(const Eldbus_Service_Interface *, const Eldbus_Message *);
static Eldbus_Message *_fake_server_ping(const Eldbus_Service_Interface *, const Eldbus_Message *);
static Eina_Bool _fake_server_send_pong_signal(void *);

static Eldbus_Connection *conn;

static const Eldbus_Method methods[] = {
   {
     FAKE_SERVER_SUM_METHOD_NAME, ELDBUS_ARGS({"i", "a"}, {"i", "b"}), ELDBUS_ARGS({"i", "result"}),
     _fake_server_sum,
      0
   },
   {
     FAKE_SERVER_PING_METHOD_NAME, ELDBUS_ARGS({"i", "a"}), NULL,
     _fake_server_ping,
      0
   },
   { NULL, NULL, NULL, NULL, 0 }
};

enum
{
   FAKE_SERVER_PONG_SIGNAL = 0
};

static const Eldbus_Signal signals[] = {
   [FAKE_SERVER_PONG_SIGNAL] = {FAKE_SERVER_PONG_SIGNAL_NAME, ELDBUS_ARGS({ "i", NULL }), 0},
   { NULL, NULL, 0 }
};

static const Eldbus_Property properties[] = {
   { FAKE_SERVER_READONLY_PROPERTY, "i", _fakse_server_property_get, NULL, 0 },
   { FAKE_SERVER_WRITEONLY_PROPERTY, "i", NULL, _fake_server_property_set, 0 },
   { FAKE_SERVER_READWRITE_PROPERTY, "i", _fakse_server_property_get, _fake_server_property_set, 0 },
   { NULL, NULL, NULL, NULL, 0 }
};

static const Eldbus_Service_Interface_Desc test_interface_desc = {
   FAKE_SERVER_INTERFACE, methods, signals, properties, NULL, NULL
};


Eldbus_Service_Interface *
fake_server_start(Fake_Server_Data *data, const char *name)
{
   ck_assert_ptr_ne(NULL, data);

   conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);

   Eldbus_Service_Interface *interface = eldbus_service_interface_register(conn, FAKE_SERVER_PATH, &test_interface_desc);
   eldbus_service_object_data_set(interface, FAKE_SERVER_DATA_KEY, data);

   eldbus_name_request(conn, name, ELDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
                       _fake_server_name_request_cb, interface);

   ecore_main_loop_begin();

   return interface;
}

void
fake_server_stop(Eldbus_Service_Interface *interface)
{
   eldbus_service_object_unregister(interface);
   eldbus_connection_unref(conn);
}

static Eldbus_Message *
_fake_server_sum(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
{
   int a, b;
   if (!eldbus_message_arguments_get(msg, "ii", &a, &b))
     return eldbus_message_error_new(msg, "Invalid arguments", "Error getting arguments..");

   int sum = a + b;

   Eldbus_Message *reply = eldbus_message_method_return_new(msg);
   eldbus_message_arguments_append(reply, "i", sum);
   return reply;
}

static Eldbus_Message *
_fake_server_ping(const Eldbus_Service_Interface *iface , const Eldbus_Message *msg)
{
   int a;
   if (!eldbus_message_arguments_get(msg, "i", &a))
     return eldbus_message_error_new(msg, "Invalid arguments", "Error getting arguments..");

   Fake_Server_Data *pd = eldbus_service_object_data_get(iface, FAKE_SERVER_DATA_KEY);
   ck_assert_ptr_ne(NULL, pd);

   pd->pong_response = a + 1;
   Ecore_Timer *timer = ecore_timer_add(0.1, _fake_server_send_pong_signal, iface);
   ck_assert_ptr_ne(NULL, timer);

   return NULL;
}

static Eina_Bool
_fake_server_send_pong_signal(void *data)
{
   Eldbus_Service_Interface *iface = (Eldbus_Service_Interface*)data;

   Fake_Server_Data *pd = eldbus_service_object_data_get(iface, FAKE_SERVER_DATA_KEY);
   ck_assert_ptr_ne(NULL, pd);

   eldbus_service_signal_emit(iface, FAKE_SERVER_PONG_SIGNAL, pd->pong_response);
   return ECORE_CALLBACK_CANCEL;
}

Eina_Bool
_fakse_server_property_get(const Eldbus_Service_Interface *iface,
                           const char *propname,
                           Eldbus_Message_Iter *iter,
                           const Eldbus_Message *request_msg EINA_UNUSED,
                           Eldbus_Message **error EINA_UNUSED)
{
   Fake_Server_Data *data = eldbus_service_object_data_get(iface, FAKE_SERVER_DATA_KEY);
   ck_assert_ptr_ne(NULL, data);

   if (strcmp(propname, FAKE_SERVER_READONLY_PROPERTY) == 0)
     eldbus_message_iter_arguments_append(iter, "i", data->readonly_property);
   else
   if (strcmp(propname, FAKE_SERVER_READWRITE_PROPERTY) == 0)
     eldbus_message_iter_arguments_append(iter, "i", data->readwrite_property);
   else
     return EINA_FALSE;

   return EINA_TRUE;
}

Eldbus_Message *
_fake_server_property_set(const Eldbus_Service_Interface *iface,
                          const char *propname,
                          Eldbus_Message_Iter *iter,
                          const Eldbus_Message *msg)
{
   Fake_Server_Data *data = eldbus_service_object_data_get(iface, FAKE_SERVER_DATA_KEY);
   ck_assert_ptr_ne(NULL, data);

   if (strcmp(propname, FAKE_SERVER_WRITEONLY_PROPERTY) == 0)
     {
        if (!eldbus_message_iter_arguments_get(iter, "i", &data->writeonly_property))
          ck_abort_msg("error getting iter arguments on _fake_server_property_set");
     }
   else
     {
        if (strcmp(propname, FAKE_SERVER_READWRITE_PROPERTY) == 0)
          {
             if (!eldbus_message_iter_arguments_get(iter, "i", &data->readwrite_property))
               ck_abort_msg("error getting iter arguments on _fake_server_property_set");
          }
        else
          return eldbus_message_error_new(msg, "Invalid property", "Invalid property.");
     }

   return eldbus_message_method_return_new(msg);
}

static void
_fake_server_name_request_cb(void *data EINA_UNUSED,
                             const Eldbus_Message *msg,
                             Eldbus_Pending *pending EINA_UNUSED)
{
   const char *name, *text;
   if (eldbus_message_error_get(msg, &name, &text))
     ck_abort_msg("error on _fake_server_name_request_cb: %s %s", name, text);

   unsigned int reply;
   if (!eldbus_message_arguments_get(msg, "u", &reply))
     ck_abort_msg("error geting arguments on _fake_server_name_request_cb");

   if (ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER != reply)
     {
        const char *errcode = "Unknown reply";
        switch (reply)
          {
           case ELDBUS_NAME_REQUEST_REPLY_IN_QUEUE:
             errcode = "Service could not become the primary owner and has "
                       "been placed in the queue";
             break;
           case ELDBUS_NAME_REQUEST_REPLY_EXISTS:
             errcode = "Service is already in the queue";
             break;
           case ELDBUS_NAME_REQUEST_REPLY_ALREADY_OWNER:
             errcode = "Service is already the primary owner";
             break;
           default: break;
          }
        ck_abort_msg( "Failed to start fake server: %s (%u)",
                 errcode, reply);
     }

   ecore_main_loop_quit();
}