Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions | Variables
thrpool_client.c File Reference
#include "primpl.h"
#include "plgetopt.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "obsolete/prsem.h"

Go to the source code of this file.

Classes

struct  buffer
struct  Client_Param

Defines

#define DPRINTF(arg)   if (_debug_on) printf arg
#define BUF_DATA_SIZE   (2 * 1024)
#define TCP_MESG_SIZE   1024
#define NUM_TCP_CLIENTS   10 /* for a listen queue depth of 5 */
#define NUM_TCP_CONNECTIONS_PER_CLIENT   10
#define NUM_TCP_MESGS_PER_CONNECTION   10
#define TCP_SERVER_PORT   10000

Typedefs

typedef struct buffer buffer
typedef struct Client_Param Client_Param

Functions

static PRInt32 readn (PRFileDesc *sockfd, char *buf, int len)
static PRInt32 writen (PRFileDesc *sockfd, char *buf, int len)
static void PR_CALLBACK TCP_Client (void *arg)
static PRInt32 TCP_Socket_Client_Server_Test (void)
int main (int argc, char **argv)
 The Xalan testcases app.

Variables

static int _debug_on = 0
static int server_port = -1
static char * program_name = NULL
static PRInt32 num_tcp_clients = NUM_TCP_CLIENTS
static PRInt32 num_tcp_connections_per_client = NUM_TCP_CONNECTIONS_PER_CLIENT
static PRInt32 tcp_mesg_size = TCP_MESG_SIZE
static PRInt32 num_tcp_mesgs_per_connection = NUM_TCP_MESGS_PER_CONNECTION
int failed_already = 0
PRNetAddr tcp_server_addr
PRNetAddr udp_server_addr

Class Documentation

struct buffer

Definition at line 159 of file socket.c.

Class Members
char data
struct Client_Param

Definition at line 178 of file socket.c.

Collaboration diagram for Client_Param:
Class Members
PRInt32 datalen
PRInt32 * exit_counter
PRMonitor * exit_mon
PRNetAddr server_addr
PRInt32 udp_connect

Define Documentation

#define BUF_DATA_SIZE   (2 * 1024)

Definition at line 88 of file thrpool_client.c.

Definition at line 86 of file thrpool_client.c.

#define NUM_TCP_CLIENTS   10 /* for a listen queue depth of 5 */

Definition at line 90 of file thrpool_client.c.

Definition at line 92 of file thrpool_client.c.

Definition at line 93 of file thrpool_client.c.

#define TCP_MESG_SIZE   1024

Definition at line 89 of file thrpool_client.c.

#define TCP_SERVER_PORT   10000

Definition at line 94 of file thrpool_client.c.


Typedef Documentation

typedef struct buffer buffer
typedef struct Client_Param Client_Param

Function Documentation

int main ( int  argc,
char **  argv 
)

The Xalan testcases app.

Definition at line 350 of file thrpool_client.c.

{
    /*
     * -d           debug mode
     */
    PLOptStatus os;
    PLOptState *opt;
       program_name = argv[0];

    opt = PL_CreateOptState(argc, argv, "dp:");
    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    {
        if (PL_OPT_BAD == os) continue;
        switch (opt->option)
        {
        case 'd':  /* debug mode */
            _debug_on = 1;
            break;
        case 'p':
            server_port = atoi(opt->value);
            break;
        default:
            break;
        }
    }
    PL_DestroyOptState(opt);

    PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
    PR_STDIO_INIT();

#ifdef XP_MAC
    SetupMacPrintfLog("socket.log");
#endif
    PR_SetConcurrency(4);

       TCP_Socket_Client_Server_Test();

    PR_Cleanup();
    if (failed_already)
              return 1;
    else
              return 0;
}

Here is the call graph for this function:

static PRInt32 readn ( PRFileDesc sockfd,
char *  buf,
int  len 
) [static]

Definition at line 121 of file thrpool_client.c.

{
    int rem;
    int bytes;
    int offset = 0;
       PRIntervalTime timeout = PR_INTERVAL_NO_TIMEOUT;

    for (rem=len; rem; offset += bytes, rem -= bytes) {
        DPRINTF(("thread = 0x%lx: calling PR_Recv, bytes = %d\n",
            PR_GetCurrentThread(), rem));
        bytes = PR_Recv(sockfd, buf + offset, rem, 0,
              timeout);
        DPRINTF(("thread = 0x%lx: returning from PR_Recv, bytes = %d\n",
            PR_GetCurrentThread(), bytes));
        if (bytes < 0) {
                     return -1;
              }      
    }
    return len;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void PR_CALLBACK TCP_Client ( void arg) [static]

Definition at line 175 of file thrpool_client.c.

{
    Client_Param *cp = (Client_Param *) arg;
    PRFileDesc *sockfd;
    buffer *in_buf, *out_buf;
    union PRNetAddr netaddr;
    PRInt32 bytes, i, j;


    DPRINTF(("TCP client started\n"));
    bytes = cp->datalen;
    out_buf = PR_NEW(buffer);
    if (out_buf == NULL) {
        fprintf(stderr,"%s: failed to alloc buffer struct\n", program_name);
        failed_already=1;
        return;
    }
    in_buf = PR_NEW(buffer);
    if (in_buf == NULL) {
        fprintf(stderr,"%s: failed to alloc buffer struct\n", program_name);
        failed_already=1;
        return;
    }
    netaddr.inet.family = cp->server_addr.inet.family;
    netaddr.inet.port = cp->server_addr.inet.port;
    netaddr.inet.ip = cp->server_addr.inet.ip;

    for (i = 0; i < num_tcp_connections_per_client; i++) {
        if ((sockfd = PR_OpenTCPSocket(PR_AF_INET)) == NULL) {
            fprintf(stderr,"%s: PR_OpenTCPSocket failed\n", program_name);
            failed_already=1;
            return;
        }

        DPRINTF(("TCP client connecting to server:%d\n", server_port));
        if (PR_Connect(sockfd, &netaddr,PR_INTERVAL_NO_TIMEOUT) < 0){
              fprintf(stderr, "PR_Connect failed: (%ld, %ld)\n",
                     PR_GetError(), PR_GetOSError());
            failed_already=1;
            return;
        }
        for (j = 0; j < num_tcp_mesgs_per_connection; j++) {
            /*
             * fill in random data
             */
            memset(out_buf->data, ((PRInt32) (&netaddr)) + i + j, bytes);
            /*
             * write to server
             */
            if (writen(sockfd, out_buf->data, bytes) < bytes) {
                fprintf(stderr,"%s: ERROR - TCP_Client:writen\n", program_name);
                failed_already=1;
                return;
            }
                     /*
            DPRINTF(("TCP Client [0x%lx]: out_buf = 0x%lx out_buf[0] = 0x%lx\n",
                PR_GetCurrentThread(), out_buf, (*((int *) out_buf->data))));
                     */
            if (readn(sockfd, in_buf->data, bytes) < bytes) {
                fprintf(stderr,"%s: ERROR - TCP_Client:readn\n", program_name);
                failed_already=1;
                return;
            }
            /*
             * verify the data read
             */
            if (memcmp(in_buf->data, out_buf->data, bytes) != 0) {
                fprintf(stderr,"%s: ERROR - data corruption\n", program_name);
                failed_already=1;
                return;
            }
        }
        /*
         * shutdown reads and writes
         */
        if (PR_Shutdown(sockfd, PR_SHUTDOWN_BOTH) < 0) {
            fprintf(stderr,"%s: ERROR - PR_Shutdown\n", program_name);
            failed_already=1;
        }
        PR_Close(sockfd);
    }

    PR_DELETE(out_buf);
    PR_DELETE(in_buf);

    /*
     * Decrement exit_counter and notify parent thread
     */

    PR_EnterMonitor(cp->exit_mon);
    --(*cp->exit_counter);
    PR_Notify(cp->exit_mon);
    PR_ExitMonitor(cp->exit_mon);
    DPRINTF(("TCP_Client exiting\n"));
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 283 of file thrpool_client.c.

{
    int i;
    Client_Param *cparamp;
    PRMonitor *mon2;
    PRInt32    datalen;
    PRInt32    connections = 0;
       PRThread *thr;

    datalen = tcp_mesg_size;
    connections = 0;

    mon2 = PR_NewMonitor();
    if (mon2 == NULL) {
        fprintf(stderr,"%s: PR_NewMonitor failed\n", program_name);
        failed_already=1;
        return -1;
    }

    /*
     * Start client jobs
     */
    cparamp = PR_NEW(Client_Param);
    if (cparamp == NULL) {
        fprintf(stderr,"%s: PR_NEW failed\n", program_name);
        failed_already=1;
        return -1;
    }
    cparamp->server_addr.inet.family = PR_AF_INET;
    cparamp->server_addr.inet.port = PR_htons(server_port);
    cparamp->server_addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
    cparamp->exit_mon = mon2;
    cparamp->exit_counter = &connections;
    cparamp->datalen = datalen;
    for (i = 0; i < num_tcp_clients; i++) {
              thr = PR_CreateThread(PR_USER_THREAD, TCP_Client, (void *)cparamp,
                     PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0);
        if (NULL == thr) {
            fprintf(stderr,"%s: PR_CreateThread failed\n", program_name);
            failed_already=1;
            return -1;
        }
       PR_EnterMonitor(mon2);
        connections++;
       PR_ExitMonitor(mon2);
        DPRINTF(("Created TCP client = 0x%lx\n", thr));
    }
    /* Wait for client jobs to exit */
    PR_EnterMonitor(mon2);
    while (0 != connections) {
        PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT);
        DPRINTF(("Client job count = %d\n", connections));
    }
    PR_ExitMonitor(mon2);
    printf("%30s","TCP_Socket_Client_Server_Test:");
    printf("%2ld Server %2ld Clients %2ld connections_per_client\n",1l,
        num_tcp_clients, num_tcp_connections_per_client);
    printf("%30s %2ld messages_per_connection %4ld bytes_per_message\n",":",
        num_tcp_mesgs_per_connection, tcp_mesg_size);

    PR_DELETE(cparamp);
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PRInt32 writen ( PRFileDesc sockfd,
char *  buf,
int  len 
) [static]

Definition at line 147 of file thrpool_client.c.

{
    int rem;
    int bytes;
    int offset = 0;

    for (rem=len; rem; offset += bytes, rem -= bytes) {
        DPRINTF(("thread = 0x%lx: calling PR_Send, bytes = %d\n",
            PR_GetCurrentThread(), rem));
        bytes = PR_Send(sockfd, buf + offset, rem, 0,
            PR_INTERVAL_NO_TIMEOUT);
        DPRINTF(("thread = 0x%lx: returning from PR_Send, bytes = %d\n",
            PR_GetCurrentThread(), bytes));
        if (bytes <= 0)
            return -1;
    }
    return len;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

int _debug_on = 0 [static]

Definition at line 64 of file thrpool_client.c.

Definition at line 101 of file thrpool_client.c.

Definition at line 96 of file thrpool_client.c.

Definition at line 97 of file thrpool_client.c.

Definition at line 99 of file thrpool_client.c.

char* program_name = NULL [static]

Definition at line 66 of file thrpool_client.c.

int server_port = -1 [static]

Definition at line 65 of file thrpool_client.c.

Definition at line 98 of file thrpool_client.c.

Definition at line 107 of file thrpool_client.c.

Definition at line 107 of file thrpool_client.c.