[23] | 1 | /**
|
---|
[35] | 2 | * @file test.c
|
---|
| 3 | * @author myusgun <myusgun@gmail.com>
|
---|
| 4 | * @version 0.1
|
---|
[14] | 5 | */
|
---|
| 6 | #include "cf_file.h"
|
---|
| 7 | #include "cf_log.h"
|
---|
| 8 | #include "cf_socket.h"
|
---|
| 9 | #include "cf_thread.h"
|
---|
| 10 |
|
---|
[25] | 11 | /*
|
---|
| 12 | * before including "cf_debug.h",
|
---|
| 13 | * if _DEBUG is defined, the debugging code will be act.
|
---|
| 14 | */
|
---|
| 15 | #include "cf_debug.h"
|
---|
| 16 |
|
---|
[15] | 17 | #include <stdlib.h>
|
---|
[21] | 18 | #include <string.h>
|
---|
| 19 | #include <errno.h>
|
---|
[15] | 20 |
|
---|
[21] | 21 | #define THREAD_POOL 5
|
---|
| 22 | #define PORT 1234
|
---|
| 23 |
|
---|
[49] | 24 | CF_Mutex globalMutex;
|
---|
[14] | 25 |
|
---|
[40] | 26 | #define LOG_SOCKET 0
|
---|
| 27 | #define LOG_MT 1
|
---|
[21] | 28 |
|
---|
[15] | 29 | const char * file = "./log.txt";
|
---|
| 30 |
|
---|
[57] | 31 | int test_log_mt (void * arg)
|
---|
[38] | 32 | {
|
---|
[49] | 33 | static int cnt = 0;
|
---|
[38] | 34 | int i = 0;
|
---|
[49] | 35 | int th = 0;
|
---|
| 36 |
|
---|
| 37 | if (CF_Mutex_Lock (&globalMutex) < 0) { // for critical section
|
---|
| 38 | // error
|
---|
| 39 | }
|
---|
| 40 | th = cnt++;
|
---|
| 41 | if (CF_Mutex_Unlock (&globalMutex) < 0) { // for critical section
|
---|
| 42 | // error
|
---|
| 43 | }
|
---|
[38] | 44 |
|
---|
[49] | 45 | CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
|
---|
| 46 |
|
---|
[38] | 47 | for (i = 0 ; i < 100000 ; i++)
|
---|
| 48 | {
|
---|
[40] | 49 | CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
|
---|
[38] | 50 | }
|
---|
| 51 |
|
---|
[49] | 52 | CF_DEBUG_PRINT (stderr, "end %dth thread\n", th);
|
---|
| 53 |
|
---|
[57] | 54 | return 0;
|
---|
[38] | 55 | }
|
---|
| 56 |
|
---|
[24] | 57 | void test_log (const char * message)
|
---|
[14] | 58 | {
|
---|
[19] | 59 | int i, j;
|
---|
| 60 | char idname[16] = {0x00,};
|
---|
[38] | 61 | CF_Thread tid[10];
|
---|
[15] | 62 |
|
---|
[51] | 63 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[14] | 64 |
|
---|
[21] | 65 | /* initialize */
|
---|
[40] | 66 | if (CF_Log_Initialize (10) < 0)
|
---|
| 67 | {
|
---|
| 68 | fprintf (stderr, "failed to init. log\n");
|
---|
| 69 | return ;
|
---|
| 70 | }
|
---|
[19] | 71 |
|
---|
| 72 | for (i = 0 ; i < 10 ; i++)
|
---|
| 73 | {
|
---|
| 74 | sprintf (idname, "logid%d.txt", i);
|
---|
[40] | 75 | if (CF_Log_Open (i, idname, CF_LOG_BUFFER_NO) < 0)
|
---|
| 76 | fprintf (stderr, "failed to open log\n");
|
---|
[19] | 77 | }
|
---|
| 78 |
|
---|
| 79 | for (i = 0 ; i < 10 ; i++)
|
---|
| 80 | {
|
---|
| 81 | for (j = 0 ; j < 10000 ; j++)
|
---|
[40] | 82 | {
|
---|
| 83 | int result = CF_Log_Write (i, "LOG_ID_TEST", "turn %d\n", j);
|
---|
| 84 | if (result < 0)
|
---|
| 85 | fprintf (stderr, "failed to write log %d\n", result);
|
---|
| 86 | }
|
---|
[19] | 87 |
|
---|
[40] | 88 | if (CF_Log_Close (i) < 0)
|
---|
| 89 | fprintf (stderr, "failed to close log\n");
|
---|
[19] | 90 | }
|
---|
| 91 |
|
---|
[38] | 92 | /* mt {{{ */
|
---|
[40] | 93 | if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
[38] | 94 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
| 95 |
|
---|
[40] | 96 | if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0)
|
---|
[38] | 97 | CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
|
---|
| 98 |
|
---|
[49] | 99 | if (CF_Mutex_Create (&globalMutex) < 0) {
|
---|
| 100 | // error
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[38] | 103 | for (i = 0 ; i < 10 ; i++)
|
---|
| 104 | {
|
---|
| 105 | if (CF_Thread_Create (&tid[i], test_log_mt, &i) < 0)
|
---|
| 106 | {
|
---|
| 107 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
|
---|
| 108 | return ;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | for (i = 0 ; i < 10 ; i++)
|
---|
| 113 | {
|
---|
| 114 | if (CF_Thread_Join (&tid[i]) < 0)
|
---|
| 115 | CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
|
---|
| 116 | }
|
---|
[61] | 117 |
|
---|
| 118 | if (CF_Mutex_Destory (&globalMutex) < 0) {
|
---|
| 119 | // error
|
---|
| 120 | }
|
---|
[38] | 121 | /* }}} mt */
|
---|
| 122 |
|
---|
[21] | 123 | /* finalize */
|
---|
[19] | 124 | CF_Log_Finalize ();
|
---|
[51] | 125 |
|
---|
| 126 | CF_DEBUG_END_FUNCTION;
|
---|
[14] | 127 | }
|
---|
| 128 |
|
---|
[24] | 129 | void test_file (const char * message)
|
---|
[14] | 130 | {
|
---|
[15] | 131 | int fd = 0;
|
---|
[21] | 132 | char buffer[128] = {0x00,};
|
---|
[14] | 133 |
|
---|
[51] | 134 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[15] | 135 |
|
---|
[16] | 136 | fd = CF_File_Open (file, CF_FILE_RO);
|
---|
[15] | 137 | if (fd < 0)
|
---|
[51] | 138 | CF_DEBUG_PRINT (stderr, "what the ... file open ?\n");
|
---|
[15] | 139 |
|
---|
[38] | 140 | if (CF_File_Read (fd, buffer, sizeof (buffer)) < 0)
|
---|
[51] | 141 | CF_DEBUG_PRINT (stderr, "what the ... file read ?\n");
|
---|
[15] | 142 | else
|
---|
[51] | 143 | CF_DEBUG_PRINT_BIN (stdout, (unsigned char *)buffer,
|
---|
| 144 | sizeof (buffer),
|
---|
| 145 | "-- %d bytes of %d bytes\n",
|
---|
| 146 | sizeof (buffer),
|
---|
| 147 | CF_File_GetSize (fd));
|
---|
[15] | 148 |
|
---|
| 149 | CF_File_Close (fd);
|
---|
| 150 |
|
---|
[51] | 151 | CF_DEBUG_END_FUNCTION;
|
---|
[15] | 152 | }
|
---|
| 153 |
|
---|
[24] | 154 | void test_callstack3 (void)
|
---|
[15] | 155 | {
|
---|
[51] | 156 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
| 157 | CF_Debug_PrintCallStack (stdout);
|
---|
| 158 | CF_DEBUG_END_FUNCTION;
|
---|
[15] | 159 | }
|
---|
[24] | 160 | void test_callstack2 (void)
|
---|
[15] | 161 | {
|
---|
[51] | 162 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[24] | 163 | test_callstack3 ();
|
---|
[51] | 164 | CF_DEBUG_END_FUNCTION;
|
---|
[15] | 165 | }
|
---|
[24] | 166 | void test_callstack1 (void)
|
---|
[15] | 167 | {
|
---|
[51] | 168 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[24] | 169 | test_callstack2 ();
|
---|
[51] | 170 | CF_DEBUG_END_FUNCTION;
|
---|
[15] | 171 | }
|
---|
[14] | 172 |
|
---|
[57] | 173 | int socket_echo_server (void * arg)
|
---|
[21] | 174 | {
|
---|
| 175 | int srvsock = *((int *)arg);
|
---|
| 176 | int clntsock = 0;
|
---|
| 177 | int recvd = 0;
|
---|
| 178 | char buf[1024] = {0x00,};
|
---|
[38] | 179 | int i = 0;
|
---|
[21] | 180 |
|
---|
| 181 | /*------------------------------------------------------------*/
|
---|
[56] | 182 | clntsock = CF_Socket_Accept (srvsock);
|
---|
[21] | 183 | if (clntsock < 0)
|
---|
| 184 | {
|
---|
| 185 | CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
|
---|
[57] | 186 | return -1;
|
---|
[21] | 187 | }
|
---|
[40] | 188 | CF_Log_Write (LOG_SOCKET, "SERVER", "accepted\n");
|
---|
[21] | 189 |
|
---|
[38] | 190 | for (i = 0 ; i < 100 ; i++)
|
---|
[21] | 191 | {
|
---|
[38] | 192 | if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
|
---|
| 193 | {
|
---|
| 194 | CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
|
---|
[57] | 195 | return -2;
|
---|
[38] | 196 | }
|
---|
[40] | 197 | CF_Log_Write (LOG_SOCKET, "SERVER", "recv {%s}\n", buf);
|
---|
[21] | 198 |
|
---|
[38] | 199 | if (CF_Socket_Send (clntsock, buf, recvd) < 0)
|
---|
| 200 | {
|
---|
[57] | 201 | return -3;
|
---|
[38] | 202 | }
|
---|
[40] | 203 | CF_Log_Write (LOG_SOCKET, "SERVER", "resp {%s}\n", buf);
|
---|
[21] | 204 | }
|
---|
| 205 |
|
---|
[38] | 206 | CF_Socket_Close (clntsock);
|
---|
| 207 |
|
---|
[57] | 208 | return 0;
|
---|
[21] | 209 | /*------------------------------------------------------------*/
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[57] | 212 | int socket_echo_client (void * arg)
|
---|
[21] | 213 | {
|
---|
| 214 | int sock = 0;
|
---|
| 215 | int recvd = 0;
|
---|
| 216 | char buf[1024] = {0x00,};
|
---|
[38] | 217 | int i = 0;
|
---|
[21] | 218 |
|
---|
| 219 | sprintf (buf, "...wow ? is it succeed ?");
|
---|
| 220 |
|
---|
| 221 | /*------------------------------------------------------------*/
|
---|
| 222 | sock = CF_Socket_Connect ("localhost", PORT);
|
---|
| 223 | if (sock < 0)
|
---|
| 224 | {
|
---|
| 225 | CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
|
---|
[57] | 226 | return -1;
|
---|
[21] | 227 | }
|
---|
[40] | 228 | CF_Log_Write (LOG_SOCKET, "CLIENT", "connected\n");
|
---|
[21] | 229 |
|
---|
[38] | 230 | for (i = 0 ; i < 100 ; i++)
|
---|
[21] | 231 | {
|
---|
[38] | 232 | if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
|
---|
| 233 | {
|
---|
| 234 | CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
|
---|
[57] | 235 | return -2;
|
---|
[38] | 236 | }
|
---|
[40] | 237 | CF_Log_Write (LOG_SOCKET, "CLIENT", "sent {%s}\n", buf);
|
---|
[21] | 238 |
|
---|
[38] | 239 | memset (buf, 0x00, sizeof (buf));
|
---|
[21] | 240 |
|
---|
[38] | 241 | if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
|
---|
| 242 | {
|
---|
| 243 | CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
|
---|
[57] | 244 | return -3;
|
---|
[38] | 245 | }
|
---|
[40] | 246 | CF_Log_Write (LOG_SOCKET, "CLIENT", "recv {%s}\n", buf);
|
---|
[21] | 247 | }
|
---|
| 248 |
|
---|
| 249 | CF_Socket_Close (sock);
|
---|
| 250 |
|
---|
[57] | 251 | return 0;
|
---|
[21] | 252 | /*------------------------------------------------------------*/
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[24] | 255 | void test_socket (void)
|
---|
[21] | 256 | {
|
---|
| 257 | CF_Thread tid[THREAD_POOL];
|
---|
| 258 |
|
---|
| 259 | int sock = 0;
|
---|
| 260 | int iter = 0;
|
---|
| 261 |
|
---|
| 262 | /*------------------------------------------------------------*/
|
---|
[40] | 263 | if (CF_Log_Initialize (1) < 0)
|
---|
| 264 | {
|
---|
| 265 | fprintf (stderr, "failed to init. log\n");
|
---|
| 266 | return ;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | if (CF_Log_Open (LOG_SOCKET, "socket.txt", CF_LOG_BUFFER_DEFAULT) < 0)
|
---|
[21] | 270 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
| 271 |
|
---|
[40] | 272 | CF_Log_Write (LOG_SOCKET, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
[21] | 273 |
|
---|
| 274 | if (CF_Socket_Initialize () < 0)
|
---|
| 275 | {
|
---|
| 276 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
| 277 | return ;
|
---|
| 278 | }
|
---|
[40] | 279 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket initialized\n");
|
---|
[21] | 280 |
|
---|
| 281 | sock = CF_Socket_Server (PORT, 5);
|
---|
| 282 | if (sock < 0)
|
---|
| 283 | {
|
---|
| 284 | CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
|
---|
| 285 | return ;
|
---|
| 286 | }
|
---|
[40] | 287 | CF_Log_Write (LOG_SOCKET, "SOCKET", "socket ready\n");
|
---|
[21] | 288 |
|
---|
| 289 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 290 | {
|
---|
| 291 | if (CF_Thread_Create (&tid[iter], socket_echo_server, &sock) < 0)
|
---|
| 292 | {
|
---|
| 293 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
| 294 | return ;
|
---|
| 295 | }
|
---|
[40] | 296 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create server thread-%d\n", iter);
|
---|
[21] | 297 | }
|
---|
| 298 |
|
---|
| 299 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 300 | {
|
---|
| 301 | CF_Thread dummy;
|
---|
| 302 | if (CF_Thread_Create (&dummy, socket_echo_client, &sock) < 0)
|
---|
| 303 | {
|
---|
| 304 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
| 305 | return ;
|
---|
| 306 | }
|
---|
[40] | 307 | CF_Log_Write (LOG_SOCKET, "SOCKET", "create client thread-%d\n", iter);
|
---|
[21] | 308 | }
|
---|
| 309 |
|
---|
| 310 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 311 | {
|
---|
| 312 | CF_Thread_Join (&tid[iter]);
|
---|
| 313 | CF_Thread_Release (&tid[iter]);
|
---|
[40] | 314 | CF_Log_Write (LOG_SOCKET, "SOCKET", "join server thread-%d\n", iter);
|
---|
[21] | 315 | }
|
---|
| 316 |
|
---|
| 317 | CF_Socket_Close (sock);
|
---|
| 318 |
|
---|
| 319 | CF_Socket_Finalize ();
|
---|
| 320 |
|
---|
[40] | 321 | CF_Log_Close (LOG_SOCKET);
|
---|
| 322 |
|
---|
| 323 | CF_Log_Finalize ();
|
---|
[21] | 324 | /*------------------------------------------------------------*/
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[15] | 327 | int main (int argc, char ** argv)
|
---|
| 328 | {
|
---|
[51] | 329 | CF_DEBUG_BEGIN_FUNCTION;
|
---|
[15] | 330 |
|
---|
[21] | 331 | // 1
|
---|
[16] | 332 | CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
|
---|
[24] | 333 | test_log ("LOG_TEST");
|
---|
[14] | 334 |
|
---|
[21] | 335 | // 2
|
---|
[16] | 336 | CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
|
---|
[24] | 337 | test_file ("FILE_READ_TEST");
|
---|
[15] | 338 |
|
---|
[21] | 339 | // 3
|
---|
[16] | 340 | CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
|
---|
[24] | 341 | test_callstack1 ();
|
---|
[15] | 342 |
|
---|
[21] | 343 | // 4
|
---|
[38] | 344 | CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n");
|
---|
[24] | 345 | test_socket ();
|
---|
[21] | 346 |
|
---|
[16] | 347 | CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
|
---|
[14] | 348 |
|
---|
[51] | 349 | CF_DEBUG_END_FUNCTION;
|
---|
[14] | 350 |
|
---|
| 351 | return 0;
|
---|
| 352 | }
|
---|
[21] | 353 |
|
---|