2 * Copyright (c) 2013, TOYOTA MOTOR CORPORATION.
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
10 * @brief test client to connect to multi servers
11 * (socket library for communicate)
25 #include "tst_ico_uws.h"
27 /* ----------------------------------------------- */
29 /* ----------------------------------------------- */
34 struct tst_client_t *next;
35 struct ico_uws_context *context;
37 unsigned char data[256];
39 void *clt_id; /* use to send data */
47 struct tst_client_t *first_clt = NULL;
48 struct tst_client_t *second_clt = NULL;
50 /* pthread mutex initialize */
51 static pthread_mutex_t multi_mutex = PTHREAD_MUTEX_INITIALIZER;
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,
59 const ico_uws_detail *detail,
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);
76 /* ----------------------------------------------- */
78 /* ----------------------------------------------- */
81 tst_uws_callback(const struct ico_uws_context *context,
82 const ico_uws_evt_e event,
84 const ico_uws_detail *detail,
89 struct tst_client_t *clt_t;
91 if (context == NULL) return;
93 if (first_clt != NULL && context == first_clt->context) {
96 else if (second_clt != NULL && context == second_clt->context) {
103 clt_t->num_call_cb++;
104 if (clt_t->set_cb_flag == SET_FLAG) {
112 case ICO_UWS_EVT_OPEN:
113 sprintf(str, "open");
114 clt_t->clt_id = (void *)id;
115 clt_t->open_flag = SET_FLAG;
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",
122 dbg_print("ico_uws_send (client %d) : %s\n",
124 clt_t->receive_flag = SET_FLAG;
127 case ICO_UWS_EVT_CLOSE:
128 sprintf(str, "close");
129 tst_get_ready_state(clt_t, ICO_UWS_STATE_CLOSED, "close");
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",
139 dbg_print("ico_uws_send (client %d) : %s\n",
142 clt_t->receive_flag = SET_FLAG;
143 sprintf(str, "%s '%s'", str, data);
145 case ICO_UWS_EVT_ADD_FD:
146 sprintf(str, "add fd(%d)", detail->_ico_uws_fd.fd);
148 case ICO_UWS_EVT_DEL_FD:
149 sprintf(str, "delete fd(%d)", detail->_ico_uws_fd.fd);
152 /* other event is not test */
156 printf("@@@ ico_uws_evt_cb [%d (%s)] (client %d) : %s\n",
157 event, str, clt_t->id, ret_str);
164 tst_create_context(struct tst_client_t *clt_t)
166 char *ret_str = TEST_OK;
170 pthread_mutex_lock(&multi_mutex);
171 clt_t->context = ico_uws_create_context(clt_t->uri, PROTOCOL_NAME);
173 pthread_mutex_unlock(&multi_mutex);
174 if (clt_t->context == NULL) {
176 for (id = 0; id < RETRY_NUM; id++) {
178 pthread_mutex_lock(&multi_mutex);
179 clt_t->context = ico_uws_create_context(clt_t->uri, PROTOCOL_NAME);
181 pthread_mutex_unlock(&multi_mutex);
182 if (clt_t->context != NULL) {
189 dbg_print("ico_uws_create_context (client %d) : %s\n",
195 /* get ready state */
197 tst_get_ready_state(struct tst_client_t *clt_t,
198 int state, char *str_state)
200 char *ret_str = TEST_OK;
203 pthread_mutex_lock(&multi_mutex);
204 ico_uws_state_e cur_state = ico_uws_get_ready_state(clt_t->context);
206 pthread_mutex_unlock(&multi_mutex);
207 if (cur_state != state) {
210 dbg_print("ico_uws_get_ready_state [%s] (client %d) : %s\n",
211 str_state, clt_t->id, ret_str);
218 tst_get_uri(struct tst_client_t *clt_t)
220 char *ret_str = TEST_OK;
224 pthread_mutex_lock(&multi_mutex);
225 uri = ico_uws_get_uri(clt_t->context);
227 pthread_mutex_unlock(&multi_mutex);
228 if (strcmp(uri, clt_t->uri) != 0) {
231 dbg_print("ico_uws_get_uri [%s] (client %d) : %s\n",
232 uri, clt_t->id, ret_str);
239 tst_send(struct tst_client_t *clt_t)
243 for (i = 0; i < 10; i++) {
245 pthread_mutex_lock(&multi_mutex);
246 ico_uws_service(clt_t->context);
248 pthread_mutex_unlock(&multi_mutex);
253 pthread_mutex_lock(&multi_mutex);
254 ico_uws_send(clt_t->context, clt_t->clt_id, clt_t->data, clt_t->len);
256 pthread_mutex_unlock(&multi_mutex);
261 /* service loop (wait to open) */
263 tst_service_open(struct tst_client_t *clt_t)
265 char *ret_str = TEST_OK;
267 while (clt_t->open_flag == UNSET_FLAG) {
269 pthread_mutex_lock(&multi_mutex);
270 ico_uws_service(clt_t->context);
272 pthread_mutex_unlock(&multi_mutex);
275 clt_t->open_flag = UNSET_FLAG;
276 dbg_print("ico_uws_service (client %d open) : %s\n", clt_t->id, ret_str);
281 /* service loop (wait to receive data) */
283 tst_service_receive(struct tst_client_t *clt_t)
285 char *ret_str = TEST_OK;
287 while (clt_t->receive_flag == UNSET_FLAG) {
289 pthread_mutex_lock(&multi_mutex);
290 ico_uws_service(clt_t->context);
292 pthread_mutex_unlock(&multi_mutex);
295 clt_t->receive_flag = UNSET_FLAG;
296 dbg_print("ico_uws_service (client %d receive) : %s\n", clt_t->id, ret_str);
303 tst_close(struct tst_client_t *clt_t)
305 char *ret_str = TEST_OK;
308 pthread_mutex_lock(&multi_mutex);
309 ico_uws_close(clt_t->context);
311 pthread_mutex_unlock(&multi_mutex);
313 dbg_print("ico_uws_close (client %d) : %s\n", clt_t->id, ret_str);
320 tst_set_evt_callback(struct tst_client_t *clt_t)
323 char *ret_str = TEST_OK;
325 clt_t->set_cb_flag = SET_FLAG;
327 pthread_mutex_lock(&multi_mutex);
329 ret = ico_uws_set_event_cb(clt_t->context, tst_uws_callback, NULL);
331 pthread_mutex_unlock(&multi_mutex);
332 if (ret != ICO_UWS_ERR_NONE) {
334 dbg_print("ico_uws_set_event_cb (client %d) : %s (%d)\n",
335 clt_t->id, ret_str, ret);
339 dbg_print("ico_uws_set_event_cb (client %d) : %s\n",
347 tst_unset_evt_callback(struct tst_client_t *clt_t)
349 char *ret_str = TEST_OK;
353 pthread_mutex_lock(&multi_mutex);
355 ico_uws_unset_event_cb(clt_t->context);
357 pthread_mutex_unlock(&multi_mutex);
359 clt_t->set_cb_flag = UNSET_FLAG;
360 clt_t->num_call_cb = 0;
363 pthread_mutex_lock(&multi_mutex);
365 /* occurs the error event */
366 printf("-- Occurs the error event to test unset_event_cb\n");
367 uri = ico_uws_get_uri(NULL);
369 printf("-- Error event happened. (ico_uws_get_uri return Errror)\n");
373 pthread_mutex_unlock(&multi_mutex);
375 if (clt_t->num_call_cb > 0) {
379 dbg_print("ico_uws_unset_event_cb (client %d) : %s\n",
385 /* prepare for test */
386 static struct tst_client_t *
387 ico_uws_client_test_init(int id)
389 struct tst_client_t *clt_t;
391 clt_t = calloc(1, sizeof(struct tst_client_t));
393 printf("calloc failed\n");
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';
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;
416 /* ----------------------------------------------- */
418 /* ----------------------------------------------- */
420 tst_client_thread(void *args)
422 /* prepare for test */
423 first_clt = ico_uws_client_test_init(0);
424 if (first_clt == NULL) {
429 tst_create_context(first_clt);
431 if (first_clt->context != NULL) {
433 tst_set_evt_callback(first_clt);
436 tst_service_open(first_clt);
438 /* check the state */
439 tst_get_ready_state(first_clt, ICO_UWS_STATE_OPEN, "open");
442 tst_get_uri(first_clt);
447 /* wait to receive data */
448 tst_service_receive(first_clt);
454 tst_unset_evt_callback(first_clt);
460 tst_close(first_clt);
469 tst_client_thread_sec(void *args)
471 /* prepare for test */
472 second_clt = ico_uws_client_test_init(1);
473 if (second_clt == NULL) {
478 tst_create_context(second_clt);
480 if (second_clt->context != NULL) {
482 tst_set_evt_callback(second_clt);
485 tst_service_open(second_clt);
487 /* check the state */
488 tst_get_ready_state(second_clt, ICO_UWS_STATE_OPEN, "open");
491 tst_get_uri(second_clt);
494 tst_send(second_clt);
496 /* wait to receive data */
497 tst_service_receive(second_clt);
503 tst_unset_evt_callback(second_clt);
509 tst_close(second_clt);
518 /* test main (to connect to multi servers) */
520 ico_uws_client_test_multi()
522 pthread_t thread, thread_sec;
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 );
529 pthread_join( thread, NULL );
530 pthread_join( thread_sec, NULL );
538 /* ----------------------------------------------- */
540 /* ----------------------------------------------- */
541 static GMainLoop *g_mainloop = NULL;
544 exit_program(gpointer data)
546 g_main_loop_quit(g_mainloop);
553 main(int argc, char **argv)
555 g_setenv("PKG_NAME", "org.tizen.ico.tst_ico_uws_mlt_client", 1);
556 g_mainloop = g_main_loop_new(NULL, 0);
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");
566 g_timeout_add_seconds(2, exit_program, NULL);
567 g_main_loop_run(g_mainloop);