michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /*********************************************************************** michael@0: ** michael@0: ** Name: thrpool_client.c michael@0: ** michael@0: ** Description: Test threadpool functionality. michael@0: ** michael@0: ** Modification History: michael@0: */ michael@0: #include "primpl.h" michael@0: michael@0: #include "plgetopt.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef XP_UNIX michael@0: #include michael@0: #endif michael@0: #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS) michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #endif michael@0: michael@0: static int _debug_on = 0; michael@0: static int server_port = -1; michael@0: static char *program_name = NULL; michael@0: michael@0: #include "obsolete/prsem.h" michael@0: michael@0: #ifdef XP_PC michael@0: #define mode_t int michael@0: #endif michael@0: michael@0: #define DPRINTF(arg) if (_debug_on) printf arg michael@0: michael@0: #define BUF_DATA_SIZE (2 * 1024) michael@0: #define TCP_MESG_SIZE 1024 michael@0: #define NUM_TCP_CLIENTS 10 /* for a listen queue depth of 5 */ michael@0: michael@0: #define NUM_TCP_CONNECTIONS_PER_CLIENT 10 michael@0: #define NUM_TCP_MESGS_PER_CONNECTION 10 michael@0: #define TCP_SERVER_PORT 10000 michael@0: michael@0: static PRInt32 num_tcp_clients = NUM_TCP_CLIENTS; michael@0: static PRInt32 num_tcp_connections_per_client = NUM_TCP_CONNECTIONS_PER_CLIENT; michael@0: static PRInt32 tcp_mesg_size = TCP_MESG_SIZE; michael@0: static PRInt32 num_tcp_mesgs_per_connection = NUM_TCP_MESGS_PER_CONNECTION; michael@0: michael@0: int failed_already=0; michael@0: michael@0: typedef struct buffer { michael@0: char data[BUF_DATA_SIZE]; michael@0: } buffer; michael@0: michael@0: PRNetAddr tcp_server_addr, udp_server_addr; michael@0: michael@0: typedef struct Client_Param { michael@0: PRNetAddr server_addr; michael@0: PRMonitor *exit_mon; /* monitor to signal on exit */ michael@0: PRInt32 *exit_counter; /* counter to decrement, before exit */ michael@0: PRInt32 datalen; michael@0: } Client_Param; michael@0: michael@0: /* michael@0: * readn michael@0: * read data from sockfd into buf michael@0: */ michael@0: static PRInt32 michael@0: readn(PRFileDesc *sockfd, char *buf, int len) michael@0: { michael@0: int rem; michael@0: int bytes; michael@0: int offset = 0; michael@0: PRIntervalTime timeout = PR_INTERVAL_NO_TIMEOUT; michael@0: michael@0: for (rem=len; rem; offset += bytes, rem -= bytes) { michael@0: DPRINTF(("thread = 0x%lx: calling PR_Recv, bytes = %d\n", michael@0: PR_GetCurrentThread(), rem)); michael@0: bytes = PR_Recv(sockfd, buf + offset, rem, 0, michael@0: timeout); michael@0: DPRINTF(("thread = 0x%lx: returning from PR_Recv, bytes = %d\n", michael@0: PR_GetCurrentThread(), bytes)); michael@0: if (bytes < 0) { michael@0: return -1; michael@0: } michael@0: } michael@0: return len; michael@0: } michael@0: michael@0: /* michael@0: * writen michael@0: * write data from buf to sockfd michael@0: */ michael@0: static PRInt32 michael@0: writen(PRFileDesc *sockfd, char *buf, int len) michael@0: { michael@0: int rem; michael@0: int bytes; michael@0: int offset = 0; michael@0: michael@0: for (rem=len; rem; offset += bytes, rem -= bytes) { michael@0: DPRINTF(("thread = 0x%lx: calling PR_Send, bytes = %d\n", michael@0: PR_GetCurrentThread(), rem)); michael@0: bytes = PR_Send(sockfd, buf + offset, rem, 0, michael@0: PR_INTERVAL_NO_TIMEOUT); michael@0: DPRINTF(("thread = 0x%lx: returning from PR_Send, bytes = %d\n", michael@0: PR_GetCurrentThread(), bytes)); michael@0: if (bytes <= 0) michael@0: return -1; michael@0: } michael@0: return len; michael@0: } michael@0: michael@0: /* michael@0: * TCP_Client michael@0: * Client job michael@0: * Connect to the server at the address specified in the argument. michael@0: * Fill in a buffer, write data to server, read it back and check michael@0: * for data corruption. michael@0: * Close the socket for server connection michael@0: */ michael@0: static void PR_CALLBACK michael@0: TCP_Client(void *arg) michael@0: { michael@0: Client_Param *cp = (Client_Param *) arg; michael@0: PRFileDesc *sockfd; michael@0: buffer *in_buf, *out_buf; michael@0: union PRNetAddr netaddr; michael@0: PRInt32 bytes, i, j; michael@0: michael@0: michael@0: DPRINTF(("TCP client started\n")); michael@0: bytes = cp->datalen; michael@0: out_buf = PR_NEW(buffer); michael@0: if (out_buf == NULL) { michael@0: fprintf(stderr,"%s: failed to alloc buffer struct\n", program_name); michael@0: failed_already=1; michael@0: return; michael@0: } michael@0: in_buf = PR_NEW(buffer); michael@0: if (in_buf == NULL) { michael@0: fprintf(stderr,"%s: failed to alloc buffer struct\n", program_name); michael@0: failed_already=1; michael@0: return; michael@0: } michael@0: netaddr.inet.family = cp->server_addr.inet.family; michael@0: netaddr.inet.port = cp->server_addr.inet.port; michael@0: netaddr.inet.ip = cp->server_addr.inet.ip; michael@0: michael@0: for (i = 0; i < num_tcp_connections_per_client; i++) { michael@0: if ((sockfd = PR_OpenTCPSocket(PR_AF_INET)) == NULL) { michael@0: fprintf(stderr,"%s: PR_OpenTCPSocket failed\n", program_name); michael@0: failed_already=1; michael@0: return; michael@0: } michael@0: michael@0: DPRINTF(("TCP client connecting to server:%d\n", server_port)); michael@0: if (PR_Connect(sockfd, &netaddr,PR_INTERVAL_NO_TIMEOUT) < 0){ michael@0: fprintf(stderr, "PR_Connect failed: (%ld, %ld)\n", michael@0: PR_GetError(), PR_GetOSError()); michael@0: failed_already=1; michael@0: return; michael@0: } michael@0: for (j = 0; j < num_tcp_mesgs_per_connection; j++) { michael@0: /* michael@0: * fill in random data michael@0: */ michael@0: memset(out_buf->data, ((PRInt32) (&netaddr)) + i + j, bytes); michael@0: /* michael@0: * write to server michael@0: */ michael@0: if (writen(sockfd, out_buf->data, bytes) < bytes) { michael@0: fprintf(stderr,"%s: ERROR - TCP_Client:writen\n", program_name); michael@0: failed_already=1; michael@0: return; michael@0: } michael@0: /* michael@0: DPRINTF(("TCP Client [0x%lx]: out_buf = 0x%lx out_buf[0] = 0x%lx\n", michael@0: PR_GetCurrentThread(), out_buf, (*((int *) out_buf->data)))); michael@0: */ michael@0: if (readn(sockfd, in_buf->data, bytes) < bytes) { michael@0: fprintf(stderr,"%s: ERROR - TCP_Client:readn\n", program_name); michael@0: failed_already=1; michael@0: return; michael@0: } michael@0: /* michael@0: * verify the data read michael@0: */ michael@0: if (memcmp(in_buf->data, out_buf->data, bytes) != 0) { michael@0: fprintf(stderr,"%s: ERROR - data corruption\n", program_name); michael@0: failed_already=1; michael@0: return; michael@0: } michael@0: } michael@0: /* michael@0: * shutdown reads and writes michael@0: */ michael@0: if (PR_Shutdown(sockfd, PR_SHUTDOWN_BOTH) < 0) { michael@0: fprintf(stderr,"%s: ERROR - PR_Shutdown\n", program_name); michael@0: failed_already=1; michael@0: } michael@0: PR_Close(sockfd); michael@0: } michael@0: michael@0: PR_DELETE(out_buf); michael@0: PR_DELETE(in_buf); michael@0: michael@0: /* michael@0: * Decrement exit_counter and notify parent thread michael@0: */ michael@0: michael@0: PR_EnterMonitor(cp->exit_mon); michael@0: --(*cp->exit_counter); michael@0: PR_Notify(cp->exit_mon); michael@0: PR_ExitMonitor(cp->exit_mon); michael@0: DPRINTF(("TCP_Client exiting\n")); michael@0: } michael@0: michael@0: /* michael@0: * TCP_Socket_Client_Server_Test - concurrent server test michael@0: * michael@0: * Each client connects to the server and sends a chunk of data michael@0: * For each connection, server reads the data michael@0: * from the client and sends it back to the client, unmodified. michael@0: * Each client checks that data received from server is same as the michael@0: * data it sent to the server. michael@0: * michael@0: */ michael@0: michael@0: static PRInt32 michael@0: TCP_Socket_Client_Server_Test(void) michael@0: { michael@0: int i; michael@0: Client_Param *cparamp; michael@0: PRMonitor *mon2; michael@0: PRInt32 datalen; michael@0: PRInt32 connections = 0; michael@0: PRThread *thr; michael@0: michael@0: datalen = tcp_mesg_size; michael@0: connections = 0; michael@0: michael@0: mon2 = PR_NewMonitor(); michael@0: if (mon2 == NULL) { michael@0: fprintf(stderr,"%s: PR_NewMonitor failed\n", program_name); michael@0: failed_already=1; michael@0: return -1; michael@0: } michael@0: michael@0: /* michael@0: * Start client jobs michael@0: */ michael@0: cparamp = PR_NEW(Client_Param); michael@0: if (cparamp == NULL) { michael@0: fprintf(stderr,"%s: PR_NEW failed\n", program_name); michael@0: failed_already=1; michael@0: return -1; michael@0: } michael@0: cparamp->server_addr.inet.family = PR_AF_INET; michael@0: cparamp->server_addr.inet.port = PR_htons(server_port); michael@0: cparamp->server_addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK); michael@0: cparamp->exit_mon = mon2; michael@0: cparamp->exit_counter = &connections; michael@0: cparamp->datalen = datalen; michael@0: for (i = 0; i < num_tcp_clients; i++) { michael@0: thr = PR_CreateThread(PR_USER_THREAD, TCP_Client, (void *)cparamp, michael@0: PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_UNJOINABLE_THREAD, 0); michael@0: if (NULL == thr) { michael@0: fprintf(stderr,"%s: PR_CreateThread failed\n", program_name); michael@0: failed_already=1; michael@0: return -1; michael@0: } michael@0: PR_EnterMonitor(mon2); michael@0: connections++; michael@0: PR_ExitMonitor(mon2); michael@0: DPRINTF(("Created TCP client = 0x%lx\n", thr)); michael@0: } michael@0: /* Wait for client jobs to exit */ michael@0: PR_EnterMonitor(mon2); michael@0: while (0 != connections) { michael@0: PR_Wait(mon2, PR_INTERVAL_NO_TIMEOUT); michael@0: DPRINTF(("Client job count = %d\n", connections)); michael@0: } michael@0: PR_ExitMonitor(mon2); michael@0: printf("%30s","TCP_Socket_Client_Server_Test:"); michael@0: printf("%2ld Server %2ld Clients %2ld connections_per_client\n",1l, michael@0: num_tcp_clients, num_tcp_connections_per_client); michael@0: printf("%30s %2ld messages_per_connection %4ld bytes_per_message\n",":", michael@0: num_tcp_mesgs_per_connection, tcp_mesg_size); michael@0: michael@0: PR_DELETE(cparamp); michael@0: return 0; michael@0: } michael@0: michael@0: /************************************************************************/ michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: /* michael@0: * -d debug mode michael@0: */ michael@0: PLOptStatus os; michael@0: PLOptState *opt; michael@0: program_name = argv[0]; michael@0: michael@0: opt = PL_CreateOptState(argc, argv, "dp:"); michael@0: while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) michael@0: { michael@0: if (PL_OPT_BAD == os) continue; michael@0: switch (opt->option) michael@0: { michael@0: case 'd': /* debug mode */ michael@0: _debug_on = 1; michael@0: break; michael@0: case 'p': michael@0: server_port = atoi(opt->value); michael@0: break; michael@0: default: michael@0: break; michael@0: } michael@0: } michael@0: PL_DestroyOptState(opt); michael@0: michael@0: PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0); michael@0: PR_STDIO_INIT(); michael@0: michael@0: PR_SetConcurrency(4); michael@0: michael@0: TCP_Socket_Client_Server_Test(); michael@0: michael@0: PR_Cleanup(); michael@0: if (failed_already) michael@0: return 1; michael@0: else michael@0: return 0; michael@0: }