efl

Форк
0
/
server.c 
247 строк · 6.9 Кб
1
//Compile with:
2
// gcc -o server server.c `pkg-config --cflags --libs eldbus ecore`
3

4
#include "Eldbus.h"
5
#include <Ecore.h>
6

7
#define BUS "org.Enlightenment"
8
#define PATH "/org/enlightenment"
9
#define PATH_TEST_SON "/org/enlightenment/son"
10
#define INTERFACE "org.enlightenment.Test"
11

12
static Eldbus_Connection *conn;
13

14
static Eldbus_Message *
15
_hello(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *message)
16
{
17
   Eldbus_Message *reply = eldbus_message_method_return_new(message);
18
   eldbus_message_arguments_append(reply, "s", "Hello World");
19
   printf("Hello\n");
20
   return reply;
21
}
22

23
static Eldbus_Message *
24
_quit(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *message)
25
{
26
   printf("Quit\n");
27
   ecore_main_loop_quit();
28
   return eldbus_message_method_return_new(message);
29
}
30

31
enum
32
{
33
   TEST_SIGNAL_ALIVE = 0,
34
   TEST_SIGNAL_HELLO
35
};
36

37
static Eina_Bool
38
send_signal_alive(void *data)
39
{
40
   Eldbus_Service_Interface *iface = data;
41
   eldbus_service_signal_emit(iface, TEST_SIGNAL_ALIVE);
42
   return ECORE_CALLBACK_RENEW;
43
}
44

45
static Eina_Bool
46
send_signal_hello(void *data)
47
{
48
   Eldbus_Service_Interface *iface = data;
49
   eldbus_service_signal_emit(iface, TEST_SIGNAL_HELLO, "Hello World");
50
   return ECORE_CALLBACK_RENEW;
51
}
52

53
static Eldbus_Message *
54
_send_bool(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
55
{
56
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);
57
   Eina_Bool bool;
58
   if (!eldbus_message_arguments_get(msg, "b", &bool))
59
     printf("eldbus_message_arguments_get() error\n");
60
   eldbus_message_arguments_append(reply, "b", bool);
61
   return reply;
62
}
63

64
static Eldbus_Message *
65
_send_byte(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
66
{
67
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);
68
   unsigned char byte;
69
   if (!eldbus_message_arguments_get(msg, "y", &byte))
70
     printf("eldbus_message_arguments_get() error\n");
71
   eldbus_message_arguments_append(reply, "y", byte);
72
   return reply;
73
}
74

75
static Eldbus_Message *
76
_send_uint32(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
77
{
78
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);
79
   unsigned int uint32;
80
   if (!eldbus_message_arguments_get(msg, "u", &uint32))
81
     printf("eldbus_message_arguments_get() error\n");
82
   eldbus_message_arguments_append(reply, "u", uint32);
83
   return reply;
84
}
85

86
static Eldbus_Message *
87
_send_int32(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
88
{
89
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);
90
   int int32;
91
   if (!eldbus_message_arguments_get(msg, "i", &int32))
92
     printf("eldbus_message_arguments_get() error\n");
93
   eldbus_message_arguments_append(reply, "i", int32);
94
   return reply;
95
}
96

97
static Eldbus_Message *
98
_send_int16(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
99
{
100
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);
101
   short int int16;
102
   if (!eldbus_message_arguments_get(msg, "n", &int16))
103
     printf("eldbus_message_arguments_get() error\n");
104
   eldbus_message_arguments_append(reply, "n", int16);
105
   return reply;
106
}
107

108
static Eldbus_Message *
109
_send_double(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
110
{
111
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);
112
   double d;
113
   if (!eldbus_message_arguments_get(msg, "d", &d))
114
     printf("eldbus_message_arguments_get() error\n");
115
   eldbus_message_arguments_append(reply, "d", d);
116
   return reply;
117
}
118

119
static Eldbus_Message *
120
_send_string(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
121
{
122
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);
123
   const char *txt;
124
   if (!eldbus_message_arguments_get(msg, "s", &txt))
125
     printf("eldbus_message_arguments_get() error\n");
126
   eldbus_message_arguments_append(reply, "s", txt);
127
   return reply;
128
}
129

130
static Eina_Bool
131
_resp_async(void *data)
132
{
133
   Eldbus_Message *msg = data;
134
   eldbus_message_arguments_append(msg, "s", "Async test ok");
135
   eldbus_connection_send(conn, msg, NULL, NULL, -1);
136
   return ECORE_CALLBACK_CANCEL;
137
}
138

139
static Eldbus_Message *
140
_async_test(const Eldbus_Service_Interface *iface EINA_UNUSED, const Eldbus_Message *msg)
141
{
142
   Eldbus_Message *reply = eldbus_message_method_return_new(msg);
143
   printf("Received a call to AsyncTest.\n");
144
   printf("Response will be send in 5 seconds.\n");
145
   ecore_timer_add(5, _resp_async, reply);
146
   return NULL;
147
}
148

149
static const Eldbus_Signal signals[] = {
150
   [TEST_SIGNAL_ALIVE] = {"Alive", NULL, 0},
151
   [TEST_SIGNAL_HELLO] = {"Hello", ELDBUS_ARGS({ "s", "message" }), 0},
152
   { }
153
};
154

155
static const Eldbus_Method methods[] = {
156
      {
157
        "Hello", NULL, ELDBUS_ARGS({"s", "message"}),
158
        _hello
159
      },
160
      {
161
        "Quit", NULL, NULL,
162
        _quit, ELDBUS_METHOD_FLAG_DEPRECATED
163
      },
164
      { "SendBool", ELDBUS_ARGS({"b", "bool"}), ELDBUS_ARGS({"b", "bool"}),
165
        _send_bool
166
      },
167
      { "SendByte", ELDBUS_ARGS({"y", "byte"}), ELDBUS_ARGS({"y", "byte"}),
168
        _send_byte
169
      },
170
      { "SendUint32", ELDBUS_ARGS({"u", "uint32"}), ELDBUS_ARGS({"u", "uint32"}),
171
        _send_uint32
172
      },
173
      { "SendInt32", ELDBUS_ARGS({"i", "int32"}), ELDBUS_ARGS({"i", "int32"}),
174
        _send_int32
175
      },
176
      { "SendInt16", ELDBUS_ARGS({"n", "int16"}), ELDBUS_ARGS({"n", "int16"}),
177
        _send_int16
178
      },
179
      { "SendDouble", ELDBUS_ARGS({"d", "double"}), ELDBUS_ARGS({"d", "double"}),
180
        _send_double
181
      },
182
      { "SendString", ELDBUS_ARGS({"s", "string"}), ELDBUS_ARGS({"s", "string"}),
183
        _send_string
184
      },
185
      { "AsyncTest", NULL, ELDBUS_ARGS({"s", "text"}),
186
        _async_test
187
      },
188
      { }
189
};
190

191
static const Eldbus_Service_Interface_Desc iface_desc = {
192
   INTERFACE, methods, signals
193
};
194

195
static void
196
on_name_request(void *data, const Eldbus_Message *msg, Eldbus_Pending *pending EINA_UNUSED)
197
{
198
   Eldbus_Service_Interface *iface;
199
   unsigned int reply;
200

201
   iface = data;
202
   if (eldbus_message_error_get(msg, NULL, NULL))
203
     {
204
        printf("error on on_name_request\n");
205
        return;
206
     }
207

208
   if (!eldbus_message_arguments_get(msg, "u", &reply))
209
    {
210
       printf("error geting arguments on on_name_request\n");
211
       return;
212
    }
213

214
   if (reply != ELDBUS_NAME_REQUEST_REPLY_PRIMARY_OWNER)
215
     {
216
        printf("error name already in use\n");
217
        return;
218
     }
219

220
   ecore_timer_add(5, send_signal_alive, iface);
221
   ecore_timer_add(6, send_signal_hello, iface);
222
}
223

224
int
225
main(void)
226
{
227
   Eldbus_Service_Interface *iface;
228

229
   ecore_init();
230
   eldbus_init();
231

232
   conn = eldbus_connection_get(ELDBUS_CONNECTION_TYPE_SESSION);
233

234
   iface = eldbus_service_interface_register(conn, PATH, &iface_desc);
235
   eldbus_name_request(conn, BUS, ELDBUS_NAME_REQUEST_FLAG_DO_NOT_QUEUE,
236
                      on_name_request, iface);
237

238
   eldbus_service_interface_register(conn, PATH_TEST_SON, &iface_desc);
239

240
   ecore_main_loop_begin();
241

242
   eldbus_connection_unref(conn);
243

244
   eldbus_shutdown();
245
   ecore_shutdown();
246
   return 0;
247
}
248

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.