Changeset 21 in libcf


Ignore:
Timestamp:
02/01/13 17:46:09 (11 years ago)
Author:
cheese
Message:

#1 add test code for multi-threading, socket and multi-threaded logging

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/cf_debug.h

    r16 r21  
    3333    do {                                                            \
    3434    CF_UPDATE_CTX (__ctx);                                          \
    35     CF_Debug_Trace (__ctx,__bin,__len,__fmt,##__VA_ARGS__);         \
     35    CF_Debug_TraceBin (__ctx,__bin,__len,__fmt,##__VA_ARGS__);      \
    3636    } while (0)
    3737# define CF_DEBUG_CALLSTACK_PUSH(__ctx)                             \
  • trunk/include/cf_socket.h

    r6 r21  
    77#include "cf_base.h"
    88#include <stddef.h>
     9
     10#ifdef _WIN32
     11# include <WinSock2.h>
     12#else
     13# include <netinet/in.h>
     14# include <sys/socket.h>
     15# include <arpa/inet.h>
     16# include <netdb.h>
     17# include <unistd.h>
     18#endif
    919
    1020#define CF_ERROR_SOCKET_INITIALIZE      CF_ERROR_SOCKET - 1
     
    7181
    7282CF_EXPORT int
    73 CF_Socket_Accept        (const int  sock,
    74                          void       * address /* struct sockaddr_in * */);
     83CF_Socket_Accept        (const int          sock,
     84                         struct sockaddr_in * address);
    7585
    7686CF_EXPORT int
  • trunk/src/cf_socket.c

    r11 r21  
    66
    77#ifdef _WIN32
    8 # include <WinSock2.h>
    98# pragma comment (lib, "ws2_32.lib")
    109# define close(__sock)  closesocket(__sock)
    1110# define sa_family_t    unsigned short
    1211#else
    13 # include <netinet/in.h>
    14 # include <sys/socket.h>
    15 # include <arpa/inet.h>
    16 # include <netdb.h>
    17 # include <unistd.h>
    1812#endif
    1913
     
    216210    }
    217211
    218     return CF_OK;
     212    return sock;
    219213}
    220214
     
    262256
    263257int
    264 CF_Socket_Accept (const int sock,
    265                   void      * address)
     258CF_Socket_Accept (const int             sock,
     259                  struct sockaddr_in    * address)
    266260{
    267261    int                 sockClient;
  • trunk/src/cf_thread.c

    r8 r21  
    2727
    2828int
    29 CF_CLOSE_CTX (void * ctx)
     29CF_Thread_Local_Close (void * ctx)
    3030{
    3131#ifdef _WIN32
     
    6767    CHECK_INVALID_THREAD (*threadID);
    6868
    69     CF_CLOSE_CTX (*threadID);
     69    CF_Thread_Local_Close (*threadID);
    7070
    7171    return CF_OK;
     
    8080    WaitForSingleObject ((THREAD_TYPE) *threadID, INFINITE);
    8181#else
    82     int status;
    83     pthread_join (*((THREAD_TYPE *) *threadID), (void *)&status);
     82    int status = 0;
     83    pthread_join (*((THREAD_TYPE *) *threadID), &status);
    8484#endif
    8585
     
    114114    CHECK_INVALID_MUTEX (*mutex);
    115115
    116     CF_CLOSE_CTX (*mutex);
     116    CF_Thread_Local_Close (*mutex);
    117117
    118118    return CF_OK;
  • trunk/test/test.c

    r19 r21  
    99
    1010#include <stdlib.h>
     11#include <string.h>
     12#include <errno.h>
     13
     14#define THREAD_POOL 5
     15#define PORT        1234
    1116
    1217CF_Debug_Ctx        gDebugCtx;
    1318CF_Debug_CallStack  gDebugCallstack;
    1419
     20CF_Log_Ctx          gLogCtx;
     21
    1522const char * file = "./log.txt";
    1623
     
    1926    int         i, j;
    2027    char        idname[16] = {0x00,};
    21     CF_Log_Ctx  gLogCtx;
    22 
    23     CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    24 
     28
     29    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     30
     31    /* initialize */
    2532    CF_Log_Initialize (10);
    2633
     34    /* context {{{ */
    2735    gLogCtx = CF_Log_CreateCtx (file, CF_LOG_BUFFER_DEFAULT);
    2836    if (gLogCtx == NULL)
     
    4149
    4250    CF_Log_DestroyCtx (gLogCtx);
    43 
    44     ///////////////////
    45 
     51    /* }}} context */
     52
     53    /* id {{{ */
    4654    for (i = 0 ; i < 10 ; i++)
    4755    {
     
    5765        CF_LOG_CLOSE (i);
    5866    }
    59 
     67    /* }}} id */
     68
     69    /* finalize */
    6070    CF_Log_Finalize ();
    6171}
     
    6373void file_test (const char * message)
    6474{
    65     int     size = 0;
    6675    int     fd = 0;
    67     char    * buffer = NULL;
     76    char    buffer[128] = {0x00,};
    6877
    6978    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
     
    7382        CF_DEBUG_TRACE (gDebugCtx, "what the ... file open ?\n");
    7483
    75     size = CF_File_GetSize (fd);
    76     if (size < 0)
    77         CF_DEBUG_TRACE (gDebugCtx, "what the ... file size ?\n");
    78 
    79     buffer = (char *) calloc (1, (size_t) (size + 1));
    80     if (buffer == NULL)
    81         CF_DEBUG_TRACE (gDebugCtx, "what the ... buffer ?\n");
    82 
    83     if (CF_File_Read (fd, buffer, (size_t)size) < 0)
     84    if (CF_File_Read (fd, buffer, sizeof (buffer) - 1) < 0)
    8485        CF_DEBUG_TRACE (gDebugCtx, "what the ... file read ?\n");
    8586    else
    86         CF_DEBUG_TRACE (gDebugCtx, "%s", buffer);
     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));
    8792
    8893    CF_File_Close (fd);
     
    116121}
    117122
     123CF_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
     160CF_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
     202void socket_test (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        int st;
     263        CF_DEBUG_PRINT (stderr, "tid[%d] 0x%08x\n", iter, tid[iter]);
     264        CF_Thread_Join (&tid[iter]);
     265//      pthread_join (*((pthread_t *)*((CF_Thread *)&tid[iter])), &st);
     266        CF_Thread_Release (&tid[iter]);
     267        CF_Log_Write (gLogCtx, "SOCKET", "join server thread-%d\n", iter);
     268    }
     269
     270    CF_Socket_Close (sock);
     271
     272    CF_Socket_Finalize ();
     273
     274    CF_Log_DestroyCtx (gLogCtx);
     275    /*------------------------------------------------------------*/
     276}
     277
    118278int main (int argc, char ** argv)
    119279{
     
    122282    CF_DEBUG_CALLSTACK_PUSH (gDebugCtx);
    123283
     284    // 1
    124285    CF_DEBUG_PRINT (stderr, " == LOG TEST ==\n");
    125286    log_test ("LOG_TEST");
    126287
     288    // 2
    127289    CF_DEBUG_PRINT (stderr, " == FILE READ TEST ==\n");
    128290    file_test ("FILE_READ_TEST");
    129291
     292    // 3
    130293    CF_DEBUG_PRINT (stderr, " == CALLSTACK TEST ==\n");
    131294    callee1 ();
     295
     296    // 4
     297    CF_DEBUG_PRINT (stderr, " == MULTI-THREADED SOCKET TEST ==\n");
     298    socket_test ();
    132299
    133300    CF_DEBUG_CALLSTACK_POP (gDebugCtx, &gDebugCallstack);
     
    143310    return 0;
    144311}
     312
Note: See TracChangeset for help on using the changeset viewer.