home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / selct_nm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.4 KB  |  297 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /***********************************************************************
  20. **  1997 - Netscape Communications Corporation
  21. **
  22. ** Name: prselect_norm.c
  23. **
  24. ** Description: tests PR_Select with sockets - Normal operations.
  25. **
  26. ** Modification History:
  27. ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
  28. **             The debug mode will print all of the printfs associated with this test.
  29. **             The regress mode will be the default mode. Since the regress tool limits
  30. **           the output to a one line status:PASS or FAIL,all of the printf statements
  31. **             have been handled with an if (debug_mode) statement.
  32. ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
  33. **            recognize the return code from tha main program.
  34. ***********************************************************************/
  35.  
  36. /***********************************************************************
  37. ** Includes
  38. ***********************************************************************/
  39. /* Used to get the command line option */
  40. #include "plgetopt.h"
  41.  
  42. #include "prinit.h"
  43. #include "prio.h"
  44. #include "prlog.h"
  45. #include "prprf.h"
  46. #include "prerror.h"
  47. #include "prnetdb.h"
  48.  
  49. #include "probslet.h"
  50.  
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <stdlib.h>
  54. PRIntn failed_already=0;
  55. PRIntn debug_mode;
  56.  
  57. static void
  58. clientThreadFunc(void *arg)
  59. {
  60.     PRUintn port = (PRUintn) arg;
  61.     PRFileDesc *sock;
  62.     PRNetAddr addr;
  63.     char buf[128];
  64.     int i;
  65.  
  66.     addr.inet.family = AF_INET;
  67.     addr.inet.port = PR_htons((PRUint16)port);
  68.     addr.inet.ip = PR_htonl(INADDR_LOOPBACK);
  69.     PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.port);
  70.  
  71.     for (i = 0; i < 5; i++) {
  72.     sock = PR_NewTCPSocket();
  73.         PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
  74.     PR_Write(sock, buf, sizeof(buf));
  75.     PR_Close(sock);
  76.     }
  77. }
  78.  
  79. int main(int argc, char **argv)
  80. {
  81.     PRFileDesc *listenSock1, *listenSock2;
  82.     PRFileDesc *fds0[10], *fds1[10], **fds, **other_fds;
  83.     PRIntn nfds;
  84.     PRUint16 listenPort1, listenPort2;
  85.     PRNetAddr addr;
  86.     PR_fd_set readFdSet;
  87.     char buf[128];
  88.     PRThread *clientThread;
  89.     PRInt32 retVal;
  90.     PRIntn i, j;
  91.  
  92.     /* The command line argument: -d is used to determine if the test is being run
  93.     in debug mode. The regress tool requires only one line output:PASS or FAIL.
  94.     All of the printfs associated with this test has been handled with a if (debug_mode)
  95.     test.
  96.     Usage: test_name -d
  97.     */
  98.     PLOptStatus os;
  99.     PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
  100.     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
  101.     {
  102.         if (PL_OPT_BAD == os) continue;
  103.         switch (opt->option)
  104.         {
  105.         case 'd':  /* debug mode */
  106.             debug_mode = 1;
  107.             break;
  108.          default:
  109.             break;
  110.         }
  111.     }
  112.     PL_DestroyOptState(opt);
  113.  
  114.  /* main test */
  115.     
  116.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  117.     PR_STDIO_INIT();
  118.  
  119.     if (debug_mode) {
  120.         printf("This program tests PR_Select with sockets.  \n");
  121.         printf(" Normal operation are tested.\n\n");
  122.     }
  123.  
  124.     /* Create two listening sockets */
  125.     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
  126.     fprintf(stderr, "Can't create a new TCP socket\n");
  127.     failed_already=1;
  128.     goto exit_now;
  129.     }
  130.     addr.inet.family = AF_INET;
  131.     addr.inet.ip = PR_htonl(INADDR_ANY);
  132.     addr.inet.port = PR_htons(0);
  133.     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
  134.     fprintf(stderr, "Can't bind socket\n");
  135.     failed_already=1;
  136.     goto exit_now;
  137.     }
  138.     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
  139.     fprintf(stderr, "PR_GetSockName failed\n");
  140.     failed_already=1;
  141.     goto exit_now;
  142.     }
  143.     listenPort1 = PR_ntohs(addr.inet.port);
  144.     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
  145.     fprintf(stderr, "Can't listen on a socket\n");
  146.     failed_already=1;
  147.     goto exit_now;
  148.     }
  149.  
  150.     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
  151.     fprintf(stderr, "Can't create a new TCP socket\n");
  152.     failed_already=1;
  153.     goto exit_now;
  154.     }
  155.     addr.inet.family = AF_INET;
  156.     addr.inet.ip = PR_htonl(INADDR_ANY);
  157.     addr.inet.port = PR_htons(0);
  158.     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
  159.     fprintf(stderr, "Can't bind socket\n");
  160.     failed_already=1;
  161.     goto exit_now;
  162.     }
  163.     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
  164.     fprintf(stderr, "PR_GetSockName failed\n");
  165.     failed_already=1;
  166.     goto exit_now;
  167.     }
  168.     listenPort2 = PR_ntohs(addr.inet.port);
  169.     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
  170.     fprintf(stderr, "Can't listen on a socket\n");
  171. failed_already=1;
  172.     goto exit_now;
  173.     }
  174.     PR_snprintf(buf, sizeof(buf),
  175.         "The server thread is listening on ports %hu and %hu\n\n",
  176.         listenPort1, listenPort2);
  177.     if (debug_mode) printf("%s", buf);
  178.  
  179.     clientThread = PR_CreateThread(PR_USER_THREAD,
  180.         clientThreadFunc, (void *) listenPort1,
  181.         PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
  182.         PR_UNJOINABLE_THREAD, 0);
  183.     if (clientThread == NULL) {
  184.     fprintf(stderr, "can't create thread\n");
  185.     failed_already=1;
  186.     goto exit_now;
  187.     }
  188.  
  189.     clientThread = PR_CreateThread(PR_USER_THREAD,
  190.         clientThreadFunc, (void *) listenPort2,
  191.         PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
  192.         PR_UNJOINABLE_THREAD, 0);
  193.     if (clientThread == NULL) {
  194.     fprintf(stderr, "can't create thread\n");
  195.     failed_already=1;
  196.     goto exit_now;
  197.     }
  198.  
  199.     if (debug_mode) {
  200.         printf("Two client threads are created.  Each of them will\n");
  201.         printf("send data to one of the two ports the server is listening on.\n");
  202.         printf("The data they send is the port number.  Each of them send\n");
  203.         printf("the data five times, so you should see ten lines below,\n");
  204.         printf("interleaved in an arbitrary order.\n");
  205.     }
  206.     /* set up the fd array */
  207.     fds = fds0;
  208.     other_fds = fds1;
  209.     fds[0] = listenSock1;
  210.     fds[1] = listenSock2;
  211.     nfds = 2;
  212.     /* Set up the fd set */
  213.     PR_FD_ZERO(&readFdSet);
  214.     PR_FD_SET(listenSock1, &readFdSet);
  215.     PR_FD_SET(listenSock2, &readFdSet);
  216.  
  217.     /* 20 events total */
  218.     i = 0;
  219.     while (i < 20) {
  220.     PRFileDesc **tmp;
  221.     int nextIndex;
  222.     int nEvents = 0;
  223.  
  224.     retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
  225.         PR_INTERVAL_NO_TIMEOUT);
  226.     PR_ASSERT(retVal != 0);  /* no timeout */
  227.     if (retVal == -1) {
  228.         fprintf(stderr, "PR_Select failed (%d, %d)\n", PR_GetError(),
  229.             PR_GetOSError());
  230.     failed_already=1;
  231.         goto exit_now;
  232.     }
  233.  
  234.     nextIndex = 2;
  235.     /* the two listening sockets */
  236.     for (j = 0; j < 2; j++) {
  237.         other_fds[j] = fds[j];
  238.         if (PR_FD_ISSET(fds[j], &readFdSet)) {
  239.         PRFileDesc *sock;
  240.  
  241.         nEvents++;
  242.         sock = PR_Accept(fds[j], NULL, PR_INTERVAL_NO_TIMEOUT);
  243.         if (sock == NULL) {
  244.             fprintf(stderr, "PR_Accept() failed\n");
  245.         failed_already=1;
  246.             goto exit_now;
  247.         }
  248.         other_fds[nextIndex] = sock;
  249.         PR_FD_SET(sock, &readFdSet);
  250.         nextIndex++;
  251.         }
  252.         PR_FD_SET(fds[j], &readFdSet);
  253.     }
  254.  
  255.     for (j = 2; j < nfds; j++) {
  256.         if (PR_FD_ISSET(fds[j], &readFdSet)) {
  257.         PRInt32 nBytes;
  258.  
  259.         PR_FD_CLR(fds[j], &readFdSet);
  260.         nEvents++;
  261.         nBytes = PR_Read(fds[j], buf, sizeof(buf));
  262.         if (nBytes == -1) {
  263.             fprintf(stderr, "PR_Read() failed\n");
  264.         failed_already=1;
  265.             goto exit_now;
  266.         }
  267.         /* Just to be safe */
  268.         buf[127] = '\0';
  269.         PR_Close(fds[j]);
  270.         if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
  271.         } else {
  272.         PR_FD_SET(fds[j], &readFdSet);
  273.         other_fds[nextIndex] = fds[j];
  274.         nextIndex++;
  275.         }
  276.     }
  277.  
  278.     PR_ASSERT(retVal == nEvents);
  279.     /* swap */
  280.     tmp = fds;
  281.     fds = other_fds;
  282.     other_fds = tmp;
  283.     nfds = nextIndex;
  284.     i += nEvents;
  285.     }
  286.  
  287.     if (debug_mode) printf("Test passed\n");
  288.  
  289.     PR_Cleanup();
  290.     goto exit_now;
  291. exit_now:
  292.     if(failed_already)    
  293.         return 1;
  294.     else
  295.         return 0;
  296. }
  297.