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 (socket library for communicate)
23 #include "tst_ico_uws.h"
25 /* ----------------------------------------------- */
27 /* ----------------------------------------------- */
32 static struct ico_uws_context *clt_context;
35 /* receive event check */
36 static int receive_flag = UNSET_FLAG;
38 /* callback function is setting or not setting */
39 static int set_cb_flag = UNSET_FLAG;
40 static int num_call_cb = 0;
42 /* ----------------------------------------------- */
43 /* Define of static function */
44 /* ----------------------------------------------- */
45 static void tst_uws_callback(const struct ico_uws_context *context,
46 const ico_uws_evt_e event,
48 const ico_uws_detail *detail,
50 static void tst_create_context(char *uri);
51 static void tst_get_ready_state(int state, char *str_state);
52 static void tst_get_uri(char *set_uri);
53 static void tst_send(unsigned char *data);
54 static void tst_service(void);
55 static void tst_close(void);
56 static void tst_set_evt_callback(unsigned char *send_data);
57 static void tst_unset_evt_callback(void);
58 static int ico_uws_client_test(char *uri, unsigned char *data);
60 /* ----------------------------------------------- */
62 /* ----------------------------------------------- */
65 tst_uws_callback(const struct ico_uws_context *context,
66 const ico_uws_evt_e event,
68 const ico_uws_detail *detail,
75 if (set_cb_flag == SET_FLAG) {
86 case ICO_UWS_EVT_OPEN:
88 if (clt_context != NULL) {
89 tst_get_ready_state(ICO_UWS_STATE_OPEN, "open");
92 case ICO_UWS_EVT_ERROR:
93 sprintf(str, "error");
94 if (detail->_ico_uws_error.code == ICO_UWS_ERR_SEND) {
95 dbg_print("ico_uws_service (client) : %s\n", TEST_NG);
96 dbg_print("ico_uws_send (client) : %s\n", TEST_NG);
97 receive_flag = SET_FLAG;
100 case ICO_UWS_EVT_CLOSE:
101 sprintf(str, "close");
102 if (clt_context != NULL) {
103 tst_get_ready_state(ICO_UWS_STATE_CLOSED, "close");
106 case ICO_UWS_EVT_RECEIVE:
107 sprintf(str, "receive");
108 char *data = (char *)detail->_ico_uws_message.recv_data;
109 if (strcmp((char *)user_data, data) != 0) {
110 dbg_print("ico_uws_send (client) : %s\n", TEST_NG);
112 dbg_print("ico_uws_send (client) : %s\n", TEST_OK);
114 sprintf(str, "%s '%s'", str, data);
115 receive_flag = SET_FLAG;
117 case ICO_UWS_EVT_ADD_FD:
118 sprintf(str, "add fd(%d)", detail->_ico_uws_fd.fd);
120 case ICO_UWS_EVT_DEL_FD:
121 sprintf(str, "delete fd(%d)", detail->_ico_uws_fd.fd);
124 /* other event is not test */
127 dbg_print("ico_uws_evt_cb [%d (%s)] (client) : %s\n",
128 event, str, ret_str);
135 tst_create_context(char *uri)
137 char *ret_str = TEST_OK;
140 clt_context = ico_uws_create_context(uri, PROTOCOL_NAME);
141 if (clt_context == NULL) {
143 for (id = 0; id < RETRY_NUM; id++) {
144 clt_context = ico_uws_create_context(uri, PROTOCOL_NAME);
145 if (clt_context != NULL) {
152 dbg_print("ico_uws_create_context (client) : %s\n", ret_str);
157 /* get ready state */
159 tst_get_ready_state(int state, char *str_state)
161 char *ret_str = TEST_OK;
163 ico_uws_state_e cur_state = ico_uws_get_ready_state(clt_context);
164 if (cur_state != state) {
167 dbg_print("ico_uws_get_ready_state [%s] (client) : %s\n",
175 tst_get_uri(char *set_uri)
177 char *ret_str = TEST_OK;
179 char *uri = ico_uws_get_uri(clt_context);
180 if (strcmp(uri, set_uri) != 0) {
183 dbg_print("ico_uws_get_uri [%s] (client) : %s\n",
191 tst_send(unsigned char *data)
194 size_t len = strlen((char *)data) + 1;
196 for (i = 0; i < 10; i++) {
197 ico_uws_service(clt_context);
200 ico_uws_send(clt_context, clt_id, data, len);
205 /* service loop (wait to receive data) */
209 char *ret_str = TEST_OK;
211 /* wait to close the connection */
212 while (receive_flag == UNSET_FLAG) {
213 ico_uws_service(clt_context);
216 receive_flag = UNSET_FLAG;
217 dbg_print("ico_uws_service (client) : %s\n", ret_str);
226 char *ret_str = TEST_OK;
228 ico_uws_close(clt_context);
229 dbg_print("ico_uws_close (client) : %s\n", ret_str);
236 tst_set_evt_callback(unsigned char *send_data)
239 char *ret_str = TEST_OK;
242 set_cb_flag = SET_FLAG;
243 ret = ico_uws_set_event_cb(clt_context, tst_uws_callback,
245 if (ret != ICO_UWS_ERR_NONE) {
247 dbg_print("ico_uws_set_event_cb (client) : %s (%d)\n",
252 dbg_print("ico_uws_set_event_cb (client) : %s\n", ret_str);
259 tst_unset_evt_callback()
261 char *ret_str = TEST_OK;
265 ico_uws_unset_event_cb(clt_context);
266 set_cb_flag = UNSET_FLAG;
269 /* occurs the error event */
270 printf("-- Occurs the error event to test unset_event_cb\n");
271 uri = ico_uws_get_uri(NULL);
273 printf("-- Error event happened. (ico_uws_get_uri return Errror)\n");
277 if (num_call_cb > 0) {
281 dbg_print("ico_uws_unset_event_cb (client) : %s\n", ret_str);
286 /* test main (to connect to single server) */
288 ico_uws_client_test(char *uri, unsigned char *data)
291 tst_create_context(uri);
294 tst_set_evt_callback(data);
306 /* wait to receive data */
313 tst_unset_evt_callback();
322 /* ----------------------------------------------- */
324 /* ----------------------------------------------- */
325 static GMainLoop *g_mainloop = NULL;
328 exit_program(gpointer data)
330 g_main_loop_quit(g_mainloop);
337 main(int argc, char **argv)
340 unsigned char data[256];
342 int set_uri_flag = UNSET_FLAG;
343 int set_data_flag = UNSET_FLAG;
345 for (id = 0; id < argc; id++) {
346 if (strcmp(argv[id], "-p") == 0) {
347 /* set uri to connect */
349 sprintf(uri, "%s:%s", SRV_URI, argv[id]);
350 set_uri_flag = SET_FLAG;
352 else if (strcmp(argv[id], "-d") == 0) {
353 /* set data to send */
355 sprintf((char *)data, "%s", argv[id]);
356 data[strlen(argv[id]) + 1] = '\0';
357 set_data_flag = SET_FLAG;
361 /* set default uri to connect */
362 if (set_uri_flag == UNSET_FLAG) {
363 sprintf(uri, "%s:%s", SRV_URI, SRV_PORT);
366 /* set default data to send */
367 if (set_data_flag == UNSET_FLAG) {
368 sprintf((char *)data, "%s", CLT_DATA);
371 g_setenv("PKG_NAME", "org.tizen.ico.tst_ico_uws_client", 1);
372 g_mainloop = g_main_loop_new(NULL, 0);
375 printf("##### ico_uws API (client) Test Start #####\n");
376 ico_uws_client_test(uri, data);
377 printf("##### ico_uws API (client) Test End #####\n");
380 g_timeout_add_seconds(2, exit_program, NULL);
381 g_main_loop_run(g_mainloop);