[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 |
|
---|
[14] | 24 | CF_Debug_Ctx gDebugCtx;
|
---|
| 25 | CF_Debug_CallStack gDebugCallstack;
|
---|
| 26 |
|
---|
[21] | 27 | CF_Log_Ctx gLogCtx;
|
---|
| 28 |
|
---|
[15] | 29 | const char * file = "./log.txt";
|
---|
| 30 |
|
---|
[24] | 31 | void test_log (const char * message)
|
---|
[14] | 32 | {
|
---|
[19] | 33 | int i, j;
|
---|
| 34 | char idname[16] = {0x00,};
|
---|
[15] | 35 |
|
---|
[14] | 36 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 37 |
|
---|
[21] | 38 | /* initialize */
|
---|
[27] | 39 | CF_Log_Initialize (10);
|
---|
[19] | 40 |
|
---|
[21] | 41 | /* context {{{ */
|
---|
[15] | 42 | gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
|
---|
[19] | 43 | if (gLogCtx == NULL)
|
---|
| 44 | CF_DEBUG_PRINT (stderr, "create log ctx error\n");
|
---|
[14] | 45 |
|
---|
[15] | 46 | for (i = 0 ; i < 10000 ; i++)
|
---|
[19] | 47 | CF_Log_Write (gLogCtx, "LOG_TEST", "turn %d\n", i);
|
---|
[15] | 48 |
|
---|
[14] | 49 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
[15] | 50 |
|
---|
| 51 | CF_Log_Write (gLogCtx, message,
|
---|
[14] | 52 | "here is the end of function [file:%s line:%d func:%s]\n",
|
---|
| 53 | gDebugCallstack.file,
|
---|
| 54 | gDebugCallstack.line,
|
---|
| 55 | gDebugCallstack.function);
|
---|
[15] | 56 |
|
---|
| 57 | CF_Log_DestroyCtx (gLogCtx);
|
---|
[21] | 58 | /* }}} context */
|
---|
[19] | 59 |
|
---|
[21] | 60 | /* id {{{ */
|
---|
[19] | 61 | for (i = 0 ; i < 10 ; i++)
|
---|
| 62 | {
|
---|
| 63 | sprintf (idname, "logid%d.txt", i);
|
---|
| 64 | CF_LOG_OPEN (i, idname, CF_LOG_BUFFER_NO);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | for (i = 0 ; i < 10 ; i++)
|
---|
| 68 | {
|
---|
| 69 | for (j = 0 ; j < 10000 ; j++)
|
---|
| 70 | CF_LOG_WRITE (i, "LOG_ID_TEST", "turn %d\n", j);
|
---|
| 71 |
|
---|
| 72 | CF_LOG_CLOSE (i);
|
---|
| 73 | }
|
---|
[21] | 74 | /* }}} id */
|
---|
[19] | 75 |
|
---|
[21] | 76 | /* finalize */
|
---|
[19] | 77 | CF_Log_Finalize ();
|
---|
[14] | 78 | }
|
---|
| 79 |
|
---|
[24] | 80 | void test_file (const char * message)
|
---|
[14] | 81 | {
|
---|
[15] | 82 | int fd = 0;
|
---|
[21] | 83 | char buffer[128] = {0x00,};
|
---|
[14] | 84 |
|
---|
[15] | 85 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 86 |
|
---|
[16] | 87 | fd = CF_File_Open (file, CF_FILE_RO);
|
---|
[15] | 88 | if (fd < 0)
|
---|
| 89 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
|
---|
| 90 |
|
---|
[21] | 91 | if (CF_File_Read (fd, buffer, sizeof (buffer) - 1) < 0)
|
---|
[15] | 92 | CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
|
---|
| 93 | else
|
---|
[21] | 94 | CF_DEBUG_TRACE_BIN (gDebugCtx, (unsigned char *)buffer,
|
---|
| 95 | sizeof (buffer) - 1,
|
---|
| 96 | "-- %d bytes of %d bytes\n",
|
---|
| 97 | sizeof (buffer) - 1,
|
---|
| 98 | CF_File_GetSize (fd));
|
---|
[15] | 99 |
|
---|
| 100 | CF_File_Close (fd);
|
---|
| 101 |
|
---|
| 102 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[24] | 105 | void test_callstack3 (void)
|
---|
[15] | 106 | {
|
---|
| 107 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 108 | while (CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack) == CF_OK)
|
---|
[14] | 109 | {
|
---|
[15] | 110 | CF_DEBUG_TRACE (gDebugCtx, "print callstack [file:%s line:%d func:%s]\n",
|
---|
| 111 | gDebugCallstack.file,
|
---|
| 112 | gDebugCallstack.line,
|
---|
| 113 | gDebugCallstack.function);
|
---|
[14] | 114 | }
|
---|
[15] | 115 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 116 | }
|
---|
[24] | 117 | void test_callstack2 (void)
|
---|
[15] | 118 | {
|
---|
| 119 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
[24] | 120 | test_callstack3 ();
|
---|
[15] | 121 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 122 | }
|
---|
[24] | 123 | void test_callstack1 (void)
|
---|
[15] | 124 | {
|
---|
| 125 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
[24] | 126 | test_callstack2 ();
|
---|
[15] | 127 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, NULL);
|
---|
| 128 | }
|
---|
[14] | 129 |
|
---|
[21] | 130 | CF_THREAD_RETURN CF_THREAD_CALL socket_echo_server (void * arg)
|
---|
| 131 | {
|
---|
| 132 | int srvsock = *((int *)arg);
|
---|
| 133 | int clntsock = 0;
|
---|
| 134 | int recvd = 0;
|
---|
| 135 | char buf[1024] = {0x00,};
|
---|
| 136 |
|
---|
| 137 | /*------------------------------------------------------------*/
|
---|
| 138 | clntsock = CF_Socket_Accept (srvsock, NULL);
|
---|
| 139 | if (clntsock < 0)
|
---|
| 140 | {
|
---|
| 141 | CF_DEBUG_PRINT (stderr, "failed to accept on server\n");
|
---|
| 142 | return (CF_THREAD_RETURN)-1;
|
---|
| 143 | }
|
---|
| 144 | CF_DEBUG_PRINT (stderr, "SERVER : accepted\n");
|
---|
| 145 | CF_Log_Write (gLogCtx, "SERVER", "accepted\n");
|
---|
| 146 |
|
---|
| 147 | if ((recvd = CF_Socket_Recv (clntsock, buf, sizeof (buf))) < 0)
|
---|
| 148 | {
|
---|
| 149 | CF_DEBUG_PRINT (stderr, "failed to recv on server\n");
|
---|
| 150 | return (CF_THREAD_RETURN)-2;
|
---|
| 151 | }
|
---|
| 152 | CF_DEBUG_PRINT (stderr, "SERVER : recv {%s}\n", buf);
|
---|
| 153 | CF_Log_Write (gLogCtx, "SERVER", "recv {%s}\n", buf);
|
---|
| 154 |
|
---|
| 155 | if (CF_Socket_Send (clntsock, buf, recvd) < 0)
|
---|
| 156 | {
|
---|
| 157 | CF_DEBUG_PRINT (stderr, "failed to send on server\n");
|
---|
| 158 | return (CF_THREAD_RETURN)-3;
|
---|
| 159 | }
|
---|
| 160 | CF_DEBUG_PRINT (stderr, "SERVER : resp {%s}\n", buf);
|
---|
| 161 | CF_Log_Write (gLogCtx, "SERVER", "resp {%s}\n", buf);
|
---|
| 162 |
|
---|
| 163 | return (CF_THREAD_RETURN)0;
|
---|
| 164 | /*------------------------------------------------------------*/
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | CF_THREAD_RETURN CF_THREAD_CALL socket_echo_client (void * arg)
|
---|
| 168 | {
|
---|
| 169 | int sock = 0;
|
---|
| 170 | int recvd = 0;
|
---|
| 171 | char buf[1024] = {0x00,};
|
---|
| 172 |
|
---|
| 173 | sprintf (buf, "...wow ? is it succeed ?");
|
---|
| 174 |
|
---|
| 175 | /*------------------------------------------------------------*/
|
---|
| 176 | sock = CF_Socket_Connect ("localhost", PORT);
|
---|
| 177 | if (sock < 0)
|
---|
| 178 | {
|
---|
| 179 | CF_DEBUG_PRINT (stderr, "failed to connect on client\n");
|
---|
| 180 | return (CF_THREAD_RETURN)-1;
|
---|
| 181 | }
|
---|
| 182 | CF_DEBUG_PRINT (stderr, "CLIENT : connected\n");
|
---|
| 183 | CF_Log_Write (gLogCtx, "CLIENT", "connected\n");
|
---|
| 184 |
|
---|
| 185 | if (CF_Socket_Send (sock, buf, sizeof (buf)) < 0)
|
---|
| 186 | {
|
---|
| 187 | CF_DEBUG_PRINT (stderr, "failed to send on client %d\n", errno);
|
---|
| 188 | return (CF_THREAD_RETURN)-2;
|
---|
| 189 | }
|
---|
| 190 | CF_DEBUG_PRINT (stderr, "CLIENT : sent {%s}\n", buf);
|
---|
| 191 | CF_Log_Write (gLogCtx, "CLIENT", "sent {%s}\n", buf);
|
---|
| 192 |
|
---|
| 193 | memset (buf, 0x00, sizeof (buf));
|
---|
| 194 |
|
---|
| 195 | if ((recvd = CF_Socket_Recv (sock, buf, sizeof (buf))) < 0)
|
---|
| 196 | {
|
---|
| 197 | CF_DEBUG_PRINT (stderr, "failed to recv on client\n");
|
---|
| 198 | return (CF_THREAD_RETURN)-3;
|
---|
| 199 | }
|
---|
| 200 | CF_DEBUG_PRINT (stderr, "CLIENT : recv {%s}\n", buf);
|
---|
| 201 | CF_Log_Write (gLogCtx, "CLIENT", "recv {%s}\n", buf);
|
---|
| 202 |
|
---|
| 203 | CF_Socket_Close (sock);
|
---|
| 204 |
|
---|
| 205 | return (CF_THREAD_RETURN)0;
|
---|
| 206 | /*------------------------------------------------------------*/
|
---|
| 207 | }
|
---|
| 208 |
|
---|
[24] | 209 | void test_socket (void)
|
---|
[21] | 210 | {
|
---|
| 211 | CF_Thread tid[THREAD_POOL];
|
---|
| 212 |
|
---|
| 213 | int sock = 0;
|
---|
| 214 | int iter = 0;
|
---|
| 215 | int reuse = 1;
|
---|
| 216 |
|
---|
| 217 | /*------------------------------------------------------------*/
|
---|
| 218 | gLogCtx = CF_Log_CreateCtx ("socket.txt", CF_LOG_BUFFER_DEFAULT);
|
---|
| 219 | if (gLogCtx == NULL)
|
---|
| 220 | CF_DEBUG_PRINT (stderr, "failed to open log\n");
|
---|
| 221 |
|
---|
| 222 | CF_Log_Write (gLogCtx, "LOG_MULTITHREAD", "multi-threaded logging test with socket\n");
|
---|
| 223 |
|
---|
| 224 | if (CF_Socket_Initialize () < 0)
|
---|
| 225 | {
|
---|
| 226 | CF_DEBUG_PRINT (stderr, "failed to initialize socket\n");
|
---|
| 227 | return ;
|
---|
| 228 | }
|
---|
| 229 | CF_Log_Write (gLogCtx, "SOCKET", "socket initialized\n");
|
---|
| 230 |
|
---|
| 231 | sock = CF_Socket_Server (PORT, 5);
|
---|
| 232 | if (sock < 0)
|
---|
| 233 | {
|
---|
| 234 | CF_DEBUG_PRINT (stderr, "failed to ready server %d\n", sock);
|
---|
| 235 | return ;
|
---|
| 236 | }
|
---|
| 237 | CF_Log_Write (gLogCtx, "SOCKET", "socket ready\n");
|
---|
| 238 |
|
---|
| 239 | if (CF_Socket_SetOption (sock, SO_REUSEADDR, &reuse, sizeof (reuse)) < 0)
|
---|
| 240 | {
|
---|
| 241 | CF_DEBUG_PRINT (stderr, "failed to set option\n");
|
---|
| 242 | return ;
|
---|
| 243 | }
|
---|
| 244 | CF_Log_Write (gLogCtx, "SOCKET", "set socket option\n");
|
---|
| 245 |
|
---|
| 246 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 247 | {
|
---|
| 248 | if (CF_Thread_Create (&tid[iter], socket_echo_server, &sock) < 0)
|
---|
| 249 | {
|
---|
| 250 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
| 251 | return ;
|
---|
| 252 | }
|
---|
| 253 | CF_Log_Write (gLogCtx, "SOCKET", "create server thread-%d\n", iter);
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 257 | {
|
---|
| 258 | CF_Thread dummy;
|
---|
| 259 | if (CF_Thread_Create (&dummy, socket_echo_client, &sock) < 0)
|
---|
| 260 | {
|
---|
| 261 | CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", iter);
|
---|
| 262 | return ;
|
---|
| 263 | }
|
---|
| 264 | CF_Log_Write (gLogCtx, "SOCKET", "create client thread-%d\n", iter);
|
---|
| 265 | }
|
---|
| 266 |
|
---|
| 267 | for (iter = 0 ; iter < THREAD_POOL ; iter++)
|
---|
| 268 | {
|
---|
| 269 | CF_DEBUG_PRINT (stderr, "tid[%d] 0x%08x\n", iter, tid[iter]);
|
---|
| 270 | CF_Thread_Join (&tid[iter]);
|
---|
| 271 | CF_Thread_Release (&tid[iter]);
|
---|
| 272 | CF_Log_Write (gLogCtx, "SOCKET", "join server thread-%d\n", iter);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | CF_Socket_Close (sock);
|
---|
| 276 |
|
---|
| 277 | CF_Socket_Finalize ();
|
---|
| 278 |
|
---|
| 279 | CF_Log_DestroyCtx (gLogCtx);
|
---|
| 280 | /*------------------------------------------------------------*/
|
---|
| 281 | }
|
---|
| 282 |
|
---|
[15] | 283 | int main (int argc, char ** argv)
|
---|
| 284 | {
|
---|
| 285 | gDebugCtx = CF_Debug_CreateCtx ();
|
---|
| 286 |
|
---|
[14] | 287 | CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
|
---|
| 288 |
|
---|
[21] | 289 | // 1
|
---|
[16] | 290 | CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
|
---|
[24] | 291 | test_log ("LOG_TEST");
|
---|
[14] | 292 |
|
---|
[21] | 293 | // 2
|
---|
[16] | 294 | CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
|
---|
[24] | 295 | test_file ("FILE_READ_TEST");
|
---|
[15] | 296 |
|
---|
[21] | 297 | // 3
|
---|
[16] | 298 | CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
|
---|
[24] | 299 | test_callstack1 ();
|
---|
[15] | 300 |
|
---|
[21] | 301 | // 4
|
---|
[24] | 302 | CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST with MULTI-THREADED LOGGING ==\n");
|
---|
| 303 | test_socket ();
|
---|
[21] | 304 |
|
---|
[14] | 305 | CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
|
---|
[15] | 306 |
|
---|
[16] | 307 | CF_DEBUG_PRINT (stderr, " == END OF TEST ==\n");
|
---|
[14] | 308 | CF_DEBUG_TRACE (gDebugCtx, "here is the end of function [file:%s line:%d func:%s]\n",
|
---|
| 309 | gDebugCallstack.file,
|
---|
| 310 | gDebugCallstack.line,
|
---|
| 311 | gDebugCallstack.function);
|
---|
| 312 |
|
---|
| 313 | CF_Debug_DestroyCtx (gDebugCtx);
|
---|
| 314 |
|
---|
| 315 | return 0;
|
---|
| 316 | }
|
---|
[21] | 317 |
|
---|