Clean spec file for yocto compliance
[profile/ivi/ico-uxf-utilities.git] / test / tst_ico_uws_multi_client.c
1 /*
2  * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
3  *
4  * This program is licensed under the terms and conditions of the
5  * Apache License, version 2.0.  The full text of the Apache License is at
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  */
9 /**
10  * @brief   test client to connect to multi servers
11  *          (socket library for communicate)
12  *
13  * @date    June-27-2013
14  */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <pthread.h>
20 #include <unistd.h>
21
22 #include <glib.h>
23 #include "ico_uws.h"
24
25 #include "tst_ico_uws.h"
26
27 /* ----------------------------------------------- */
28 /* Variable                                        */
29 /* ----------------------------------------------- */
30 #define SLEEP_TIME  2
31 #define RETRY_NUM   10
32
33 struct tst_client_t{
34     struct tst_client_t *next;
35     struct ico_uws_context  *context;
36     char            uri[128];
37     unsigned char   data[256];
38     size_t          len;
39     void            *clt_id;        /* use to send data */
40     int             receive_flag;
41     int             open_flag;
42     int             set_cb_flag;
43     int             num_call_cb;
44     int             id;
45 };
46
47 struct tst_client_t *first_clt = NULL;
48 struct tst_client_t *second_clt = NULL;
49
50 /* pthread mutex initialize */
51 static pthread_mutex_t multi_mutex = PTHREAD_MUTEX_INITIALIZER;
52
53 /* ----------------------------------------------- */
54 /* Define of static function                       */
55 /* ----------------------------------------------- */
56 static void tst_uws_callback(const struct ico_uws_context *context,
57                              const ico_uws_evt_e event,
58                              const void *id,
59                              const ico_uws_detail *detail,
60                              void *user_data);
61 static void tst_create_context(struct tst_client_t *clt_t);
62 static void tst_get_ready_state(struct tst_client_t *clt_t,
63                                 int state, char *str_state);
64 static void tst_get_uri(struct tst_client_t *clt_t);
65 static void tst_send(struct tst_client_t *clt_t);
66 static void tst_service_open(struct tst_client_t *clt_t);
67 static void tst_service_receive(struct tst_client_t *clt_t);
68 static void tst_close(struct tst_client_t *clt_t);
69 static void tst_set_evt_callback(struct tst_client_t *clt_t);
70 static void tst_unset_evt_callback(struct tst_client_t *clt_t);
71 static struct tst_client_t *ico_uws_client_test_init(int id);
72 static void *tst_client_thread(void *args);
73 static void *tst_client_thread_sec(void *args);
74 static int ico_uws_client_test_multi(void);
75
76 /* ----------------------------------------------- */
77 /* Public API Test                                 */
78 /* ----------------------------------------------- */
79 /* event callback */
80 static void
81 tst_uws_callback(const struct ico_uws_context *context,
82                  const ico_uws_evt_e event,
83                  const void *id,
84                  const ico_uws_detail *detail,
85                  void *user_data)
86 {
87     char str[256];
88     char *ret_str;
89     struct tst_client_t *clt_t;
90
91     if (context == NULL) return;
92
93     if (first_clt != NULL && context == first_clt->context) {
94         clt_t = first_clt;
95     }
96     else if (second_clt != NULL && context == second_clt->context) {
97         clt_t = second_clt;
98     }
99     else {
100         return;
101     }
102
103     clt_t->num_call_cb++;
104     if (clt_t->set_cb_flag == SET_FLAG) {
105         ret_str = TEST_OK;
106     }
107     else {
108         ret_str = TEST_NG;
109     }
110
111     switch (event) {
112     case ICO_UWS_EVT_OPEN:
113         sprintf(str, "open");
114         clt_t->clt_id = (void *)id;
115         clt_t->open_flag = SET_FLAG;
116         break;
117     case ICO_UWS_EVT_ERROR:
118         sprintf(str, "error");
119         if (detail->_ico_uws_error.code == ICO_UWS_ERR_SEND) {
120             dbg_print("ico_uws_service (client %d) : %s\n",
121                       clt_t->id, TEST_NG);
122             dbg_print("ico_uws_send (client %d) : %s\n",
123                       clt_t->id, TEST_NG);
124             clt_t->receive_flag = SET_FLAG;
125         }
126         break;
127     case ICO_UWS_EVT_CLOSE:
128         sprintf(str, "close");
129         tst_get_ready_state(clt_t, ICO_UWS_STATE_CLOSED, "close");
130         break;
131     case ICO_UWS_EVT_RECEIVE:
132         sprintf(str, "receive");
133         char *data = (char *)detail->_ico_uws_message.recv_data;
134         char *send_data = (char *)clt_t->data;
135         if (strcmp(send_data, data) != 0) {
136             dbg_print("ico_uws_send (client %d) : %s\n",
137                       clt_t->id, TEST_NG);
138         } else {
139             dbg_print("ico_uws_send (client %d) : %s\n",
140                       clt_t->id, TEST_OK);
141         }
142         clt_t->receive_flag = SET_FLAG;
143         sprintf(str, "%s '%s'", str, data);
144         break;
145     case ICO_UWS_EVT_ADD_FD:
146         sprintf(str, "add fd(%d)", detail->_ico_uws_fd.fd);
147         break;
148     case ICO_UWS_EVT_DEL_FD:
149         sprintf(str, "delete fd(%d)", detail->_ico_uws_fd.fd);
150         break;
151     default:
152         /* other event is not test */
153         break;
154     }
155
156     printf("@@@ ico_uws_evt_cb [%d (%s)] (client %d) : %s\n",
157            event, str, clt_t->id, ret_str);
158
159     return;
160 }
161
162 /* create context */
163 static void
164 tst_create_context(struct tst_client_t *clt_t)
165 {
166     char *ret_str = TEST_OK;
167     int id;
168
169     /* mutex lock */
170     pthread_mutex_lock(&multi_mutex);
171     clt_t->context = ico_uws_create_context(clt_t->uri, PROTOCOL_NAME);
172     /* mutex unlock */
173     pthread_mutex_unlock(&multi_mutex);
174     if (clt_t->context == NULL) {
175         ret_str = TEST_NG;
176         for (id = 0; id < RETRY_NUM; id++) {
177             /* mutex lock */
178             pthread_mutex_lock(&multi_mutex);
179             clt_t->context = ico_uws_create_context(clt_t->uri, PROTOCOL_NAME);
180             /* mutex unlock */
181             pthread_mutex_unlock(&multi_mutex);
182             if (clt_t->context != NULL) {
183                 ret_str = TEST_OK;
184                 break;
185             }
186             sleep(0.01);
187         }
188     }
189     dbg_print("ico_uws_create_context (client %d) : %s\n",
190               clt_t->id, ret_str);
191
192     return;
193 }
194
195 /* get ready state */
196 static void
197 tst_get_ready_state(struct tst_client_t *clt_t,
198                     int state, char *str_state)
199 {
200     char *ret_str = TEST_OK;
201
202     /* mutex lock */
203     pthread_mutex_lock(&multi_mutex);
204     ico_uws_state_e cur_state = ico_uws_get_ready_state(clt_t->context);
205     /* mutex unlock */
206     pthread_mutex_unlock(&multi_mutex);
207     if (cur_state != state) {
208         ret_str = TEST_NG;
209     }
210     dbg_print("ico_uws_get_ready_state [%s] (client %d) : %s\n",
211               str_state, clt_t->id, ret_str);
212
213     return;
214 }
215
216 /* get uri */
217 static void
218 tst_get_uri(struct tst_client_t *clt_t)
219 {
220     char *ret_str = TEST_OK;
221     char *uri;
222
223     /* mutex lock */
224     pthread_mutex_lock(&multi_mutex);
225     uri = ico_uws_get_uri(clt_t->context);
226     /* mutex unlock */
227     pthread_mutex_unlock(&multi_mutex);
228     if (strcmp(uri, clt_t->uri) != 0) {
229         ret_str = TEST_NG;
230     }
231     dbg_print("ico_uws_get_uri [%s] (client %d) : %s\n",
232               uri, clt_t->id, ret_str);
233
234     return;
235 }
236
237 /* send data */
238 static void
239 tst_send(struct tst_client_t *clt_t)
240 {
241     int i;
242
243     for (i = 0; i < 10; i++) {
244         /* mutex lock */
245         pthread_mutex_lock(&multi_mutex);
246         ico_uws_service(clt_t->context);
247         /* mutex unlock */
248         pthread_mutex_unlock(&multi_mutex);
249         usleep(100);
250     }
251
252     /* mutex lock */
253     pthread_mutex_lock(&multi_mutex);
254     ico_uws_send(clt_t->context, clt_t->clt_id, clt_t->data, clt_t->len);
255     /* mutex unlock */
256     pthread_mutex_unlock(&multi_mutex);
257
258     return;
259 }
260
261 /* service loop (wait to open) */
262 static void
263 tst_service_open(struct tst_client_t *clt_t)
264 {
265     char *ret_str = TEST_OK;
266
267     while (clt_t->open_flag == UNSET_FLAG) {
268         /* mutex lock */
269         pthread_mutex_lock(&multi_mutex);
270         ico_uws_service(clt_t->context);
271         /* mutex unlock */
272         pthread_mutex_unlock(&multi_mutex);
273         usleep(50);
274     }
275     clt_t->open_flag = UNSET_FLAG;
276     dbg_print("ico_uws_service (client %d open) : %s\n", clt_t->id, ret_str);
277
278     return;
279 }
280
281 /* service loop (wait to receive data) */
282 static void
283 tst_service_receive(struct tst_client_t *clt_t)
284 {
285     char *ret_str = TEST_OK;
286
287     while (clt_t->receive_flag == UNSET_FLAG) {
288         /* mutex lock */
289         pthread_mutex_lock(&multi_mutex);
290         ico_uws_service(clt_t->context);
291         /* mutex unlock */
292         pthread_mutex_unlock(&multi_mutex);
293         usleep(50);
294     }
295     clt_t->receive_flag = UNSET_FLAG;
296     dbg_print("ico_uws_service (client %d receive) : %s\n", clt_t->id, ret_str);
297
298     return;
299 }
300
301 /* close */
302 static void
303 tst_close(struct tst_client_t *clt_t)
304 {
305     char *ret_str = TEST_OK;
306
307     /* mutex lock */
308     pthread_mutex_lock(&multi_mutex);
309     ico_uws_close(clt_t->context);
310     /* mutex unlock */
311     pthread_mutex_unlock(&multi_mutex);
312
313     dbg_print("ico_uws_close (client %d) : %s\n", clt_t->id, ret_str);
314
315     return;
316 }
317
318 /* set callback */
319 static void
320 tst_set_evt_callback(struct tst_client_t *clt_t)
321 {
322     int ret;
323     char *ret_str = TEST_OK;
324
325     clt_t->set_cb_flag = SET_FLAG;
326     /* mutex lock */
327     pthread_mutex_lock(&multi_mutex);
328     /* set callback */
329     ret = ico_uws_set_event_cb(clt_t->context, tst_uws_callback, NULL);
330     /* mutex unlock */
331     pthread_mutex_unlock(&multi_mutex);
332     if (ret != ICO_UWS_ERR_NONE) {
333         ret_str = TEST_NG;
334         dbg_print("ico_uws_set_event_cb (client %d) : %s (%d)\n",
335                   clt_t->id, ret_str, ret);
336         return;
337     }
338
339     dbg_print("ico_uws_set_event_cb (client %d) : %s\n",
340               clt_t->id, ret_str);
341
342     return;
343 }
344
345 /* unset callback */
346 static void
347 tst_unset_evt_callback(struct tst_client_t *clt_t)
348 {
349     char *ret_str = TEST_OK;
350     char *uri;
351
352     /* mutex lock */
353     pthread_mutex_lock(&multi_mutex);
354     /* unset callback */
355     ico_uws_unset_event_cb(clt_t->context);
356     /* mutex unlock */
357     pthread_mutex_unlock(&multi_mutex);
358
359     clt_t->set_cb_flag = UNSET_FLAG;
360     clt_t->num_call_cb = 0;
361
362     /* mutex lock */
363     pthread_mutex_lock(&multi_mutex);
364
365     /* occurs the error event */
366     printf("-- Occurs the error event to test unset_event_cb\n");
367     uri = ico_uws_get_uri(NULL);
368     if (uri == NULL) {
369         printf("-- Error event happened. (ico_uws_get_uri return Errror)\n");
370     }
371
372     /* mutex unlock */
373     pthread_mutex_unlock(&multi_mutex);
374     sleep(SLEEP_TIME);
375     if (clt_t->num_call_cb > 0) {
376         ret_str = TEST_NG;
377     }
378
379     dbg_print("ico_uws_unset_event_cb (client %d) : %s\n",
380               clt_t->id, ret_str);
381
382     return;
383 }
384
385 /* prepare for test */
386 static struct tst_client_t *
387 ico_uws_client_test_init(int id)
388 {
389     struct tst_client_t *clt_t;
390
391     clt_t = calloc(1, sizeof(struct tst_client_t));
392     if (clt_t == NULL) {
393         printf("calloc failed\n");
394         return NULL;
395     }
396
397     /* set uri to connect to */
398     sprintf(clt_t->uri, "%s:%s", SRV_URI, srv_ports[id]);
399     /* set data to send */
400     sprintf((char *)clt_t->data, "%s", clt_datas[id]);
401     clt_t->len = strlen(clt_datas[id]) + 1;
402     clt_t->data[clt_t->len] = '\0';
403
404     /* initialize */
405     clt_t->context = NULL;
406     clt_t->clt_id = NULL;
407     clt_t->receive_flag = UNSET_FLAG;
408     clt_t->open_flag = UNSET_FLAG;
409     clt_t->set_cb_flag = UNSET_FLAG;
410     clt_t->num_call_cb = 0;
411     clt_t->id = id;
412
413     return clt_t;
414 }
415
416 /* ----------------------------------------------- */
417 /* Test Main                                       */
418 /* ----------------------------------------------- */
419 static void *
420 tst_client_thread(void *args)
421 {
422     /* prepare for test */
423     first_clt = ico_uws_client_test_init(0);
424     if (first_clt == NULL) {
425         return NULL;
426     }
427
428     /* create context */
429     tst_create_context(first_clt);
430
431     if (first_clt->context != NULL) {
432         /* set callback */
433         tst_set_evt_callback(first_clt);
434
435         /* wait to open */
436         tst_service_open(first_clt);
437
438         /* check the state */
439         tst_get_ready_state(first_clt, ICO_UWS_STATE_OPEN, "open");
440
441         /* check the uri */
442         tst_get_uri(first_clt);
443
444         /* send data */
445         tst_send(first_clt);
446
447         /* wait to receive data */
448         tst_service_receive(first_clt);
449
450         /* interval */
451         sleep(SLEEP_TIME);
452
453         /* unset callback */
454         tst_unset_evt_callback(first_clt);
455
456         /* interval */
457         sleep(SLEEP_TIME);
458
459         /* session close */
460         tst_close(first_clt);
461     }
462     /* free memory */
463     free(first_clt);
464
465     return NULL;
466 }
467
468 static void *
469 tst_client_thread_sec(void *args)
470 {
471     /* prepare for test */
472     second_clt = ico_uws_client_test_init(1);
473     if (second_clt == NULL) {
474         return NULL;
475     }
476
477     /* create context */
478     tst_create_context(second_clt);
479
480     if (second_clt->context != NULL) {
481         /* set callback */
482         tst_set_evt_callback(second_clt);
483
484         /* wait to open */
485         tst_service_open(second_clt);
486
487         /* check the state */
488         tst_get_ready_state(second_clt, ICO_UWS_STATE_OPEN, "open");
489
490         /* check the uri */
491         tst_get_uri(second_clt);
492
493         /* send data */
494         tst_send(second_clt);
495
496         /* wait to receive data */
497         tst_service_receive(second_clt);
498
499         /* interval */
500         sleep(SLEEP_TIME);
501
502         /* unset callback */
503         tst_unset_evt_callback(second_clt);
504
505         /* interval */
506         sleep(SLEEP_TIME);
507
508         /* session close */
509         tst_close(second_clt);
510     }
511
512     /* free memory */
513     free(second_clt);
514
515     return NULL;
516 }
517
518 /* test main (to connect to multi servers) */
519 static int
520 ico_uws_client_test_multi()
521 {
522     pthread_t thread, thread_sec;
523
524     /* client to connect server (port: 8080) */
525     pthread_create( &thread, NULL, tst_client_thread, (void *)NULL );
526     /* client to connect server (port: 9090) */
527     pthread_create( &thread_sec, NULL, tst_client_thread_sec, (void *)NULL );
528
529     pthread_join( thread, NULL );
530     pthread_join( thread_sec, NULL );
531
532     /* interval */
533     sleep(SLEEP_TIME);
534
535     return 1;
536 }
537
538 /* ----------------------------------------------- */
539 /* Main                                            */
540 /* ----------------------------------------------- */
541 static GMainLoop *g_mainloop = NULL;
542
543 static gboolean
544 exit_program(gpointer data)
545 {
546     g_main_loop_quit(g_mainloop);
547
548     return FALSE;
549 }
550
551 /* main */
552 int
553 main(int argc, char **argv)
554 {
555     g_setenv("PKG_NAME", "org.tizen.ico.tst_ico_uws_mlt_client", 1);
556     g_mainloop = g_main_loop_new(NULL, 0);
557
558     printf("\n");
559     printf("##### ico_uws API (client to connect to multi servers)");
560     printf(" Test Start #####\n");
561     ico_uws_client_test_multi();
562     printf("##### ico_uws API (client to connect to multi servers)");
563     printf(" Test End #####\n");
564     printf("\n");
565
566     g_timeout_add_seconds(2, exit_program, NULL);
567     g_main_loop_run(g_mainloop);
568
569     return 0;
570 }