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