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 /* ----------------------------------------------- */
31 static struct ico_uws_context *clt_context;
34 /* receive event check */
35 static int receive_flag = UNSET_FLAG;
37 /* callback function is setting or not setting */
38 static int set_cb_flag = UNSET_FLAG;
39 static int num_call_cb = 0;
41 /* ----------------------------------------------- */
42 /* Define of static function */
43 /* ----------------------------------------------- */
44 static void tst_uws_callback(const struct ico_uws_context *context,
45 const ico_uws_evt_e event,
47 const ico_uws_detail *detail,
49 static void tst_create_context(char *uri);
50 static void tst_get_ready_state(int state, char *str_state);
51 static void tst_get_uri(char *set_uri);
52 static void tst_send(unsigned char *data);
53 static void tst_service(void);
54 static void tst_close(void);
55 static void tst_set_evt_callback(unsigned char *send_data);
56 static void tst_unset_evt_callback(void);
57 static int ico_uws_client_test(char *uri, unsigned char *data);
59 /* ----------------------------------------------- */
61 /* ----------------------------------------------- */
64 tst_uws_callback(const struct ico_uws_context *context,
65 const ico_uws_evt_e event,
67 const ico_uws_detail *detail,
74 if (set_cb_flag == SET_FLAG) {
85 case ICO_UWS_EVT_OPEN:
87 if (clt_context != NULL) {
88 tst_get_ready_state(ICO_UWS_STATE_OPEN, "open");
91 case ICO_UWS_EVT_ERROR:
92 sprintf(str, "error");
93 if (detail->_ico_uws_error.code == ICO_UWS_ERR_SEND) {
94 dbg_print("ico_uws_service (client) : %s\n", TEST_NG);
95 dbg_print("ico_uws_send (client) : %s\n", TEST_NG);
96 receive_flag = SET_FLAG;
99 case ICO_UWS_EVT_CLOSE:
100 sprintf(str, "close");
101 if (clt_context != NULL) {
102 tst_get_ready_state(ICO_UWS_STATE_CLOSED, "close");
105 case ICO_UWS_EVT_RECEIVE:
106 sprintf(str, "receive");
107 char *data = (char *)detail->_ico_uws_message.recv_data;
108 if (strcmp((char *)user_data, data) != 0) {
109 dbg_print("ico_uws_send (client) : %s\n", TEST_NG);
111 dbg_print("ico_uws_send (client) : %s\n", TEST_OK);
113 sprintf(str, "%s '%s'", str, data);
114 receive_flag = SET_FLAG;
116 case ICO_UWS_EVT_ADD_FD:
117 sprintf(str, "add fd(%d)", detail->_ico_uws_fd.fd);
119 case ICO_UWS_EVT_DEL_FD:
120 sprintf(str, "delete fd(%d)", detail->_ico_uws_fd.fd);
123 /* other event is not test */
126 dbg_print("ico_uws_evt_cb [%d (%s)] (client) : %s\n",
127 event, str, ret_str);
134 tst_create_context(char *uri)
136 char *ret_str = TEST_OK;
138 clt_context = ico_uws_create_context(uri, PROTOCOL_NAME);
139 if (clt_context == NULL) {
142 dbg_print("ico_uws_create_context (client) : %s\n", ret_str);
147 /* get ready state */
149 tst_get_ready_state(int state, char *str_state)
151 char *ret_str = TEST_OK;
153 ico_uws_state_e cur_state = ico_uws_get_ready_state(clt_context);
154 if (cur_state != state) {
157 dbg_print("ico_uws_get_ready_state [%s] (client) : %s\n",
165 tst_get_uri(char *set_uri)
167 char *ret_str = TEST_OK;
169 char *uri = ico_uws_get_uri(clt_context);
170 if (strcmp(uri, set_uri) != 0) {
173 dbg_print("ico_uws_get_uri [%s] (client) : %s\n",
181 tst_send(unsigned char *data)
184 size_t len = strlen((char *)data) + 1;
186 for (i = 0; i < 10; i++) {
187 ico_uws_service(clt_context);
190 ico_uws_send(clt_context, clt_id, data, len);
195 /* service loop (wait to receive data) */
199 char *ret_str = TEST_OK;
201 /* wait to close the connection */
202 while (receive_flag == UNSET_FLAG) {
203 ico_uws_service(clt_context);
206 receive_flag = UNSET_FLAG;
207 dbg_print("ico_uws_service (client) : %s\n", ret_str);
216 char *ret_str = TEST_OK;
218 ico_uws_close(clt_context);
219 dbg_print("ico_uws_close (client) : %s\n", ret_str);
226 tst_set_evt_callback(unsigned char *send_data)
229 char *ret_str = TEST_OK;
232 set_cb_flag = SET_FLAG;
233 ret = ico_uws_set_event_cb(clt_context, tst_uws_callback,
235 if (ret != ICO_UWS_ERR_NONE) {
237 dbg_print("ico_uws_set_event_cb (client) : %s (%d)\n",
242 dbg_print("ico_uws_set_event_cb (client) : %s\n", ret_str);
249 tst_unset_evt_callback()
251 char *ret_str = TEST_OK;
254 ico_uws_unset_event_cb(clt_context);
255 set_cb_flag = UNSET_FLAG;
258 /* occurs the error event */
259 (void)ico_uws_get_uri(NULL);
261 if (num_call_cb > 0) {
265 dbg_print("ico_uws_unset_event_cb (client) : %s\n", ret_str);
270 /* test main (to connect to single server) */
272 ico_uws_client_test(char *uri, unsigned char *data)
275 tst_create_context(uri);
278 tst_set_evt_callback(data);
290 /* wait to receive data */
297 tst_unset_evt_callback();
306 /* ----------------------------------------------- */
308 /* ----------------------------------------------- */
309 static GMainLoop *g_mainloop = NULL;
312 exit_program(gpointer data)
314 g_main_loop_quit(g_mainloop);
321 main(int argc, char **argv)
324 unsigned char data[256];
326 int set_uri_flag = UNSET_FLAG;
327 int set_data_flag = UNSET_FLAG;
329 for (id = 0; id < argc; id++) {
330 if (strcmp(argv[id], "-p") == 0) {
331 /* set uri to connect */
333 sprintf(uri, "%s:%s", SRV_URI, argv[id]);
334 set_uri_flag = SET_FLAG;
336 else if (strcmp(argv[id], "-d") == 0) {
337 /* set data to send */
339 sprintf((char *)data, "%s", argv[id]);
340 data[strlen(argv[id]) + 1] = '\0';
341 set_data_flag = SET_FLAG;
345 /* set default uri to connect */
346 if (set_uri_flag == UNSET_FLAG) {
347 sprintf(uri, "%s:%s", SRV_URI, SRV_PORT);
350 /* set default data to send */
351 if (set_data_flag == UNSET_FLAG) {
352 sprintf((char *)data, "%s", CLT_DATA);
355 g_setenv("PKG_NAME", "org.tizen.ico.tst_ico_uws_client", 1);
356 g_mainloop = g_main_loop_new(NULL, 0);
359 printf("##### ico_uws API (client) Test Start #####\n");
360 ico_uws_client_test(uri, data);
361 printf("##### ico_uws API (client) Test End #####\n");
364 g_timeout_add_seconds(2, exit_program, NULL);
365 g_main_loop_run(g_mainloop);