home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / prpoll.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  7.8 KB  |  274 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. #include "prinit.h"
  20. #include "prio.h"
  21. #include "prlog.h"
  22. #include "prprf.h"
  23. #include "prnetdb.h"
  24.  
  25. #ifndef XP_MAC
  26. #include "private/pprio.h"
  27. #else
  28. #include "pprio.h"
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34.  
  35. static void
  36. clientThreadFunc(void *arg)
  37. {
  38.     PRUint16 port = (PRUint16) arg;
  39.     PRFileDesc *sock;
  40.     PRNetAddr addr;
  41.     char buf[128];
  42.     int i;
  43.  
  44.     addr.inet.family = AF_INET;
  45.     addr.inet.port = PR_htons(port);
  46.     addr.inet.ip = PR_htonl(INADDR_LOOPBACK);
  47.     PR_snprintf(buf, sizeof(buf), "%hu", port);
  48.  
  49.     for (i = 0; i < 5; i++) {
  50.     sock = PR_NewTCPSocket();
  51.         PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
  52.  
  53.     PR_Write(sock, buf, sizeof(buf));
  54.     PR_Close(sock);
  55.     }
  56. }
  57.  
  58. int main(int argc, char **argv)
  59. {
  60.     PRFileDesc *listenSock1, *listenSock2;
  61.     PRFileDesc *badFD;
  62.     PRUint16 listenPort1, listenPort2;
  63.     PRNetAddr addr;
  64.     char buf[128];
  65.     PRThread *clientThread;
  66.     PRPollDesc pds0[10], pds1[10], *pds, *other_pds;
  67.     PRIntn npds;
  68.     PRInt32 retVal;
  69.     PRIntn i, j;
  70.  
  71.     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
  72.     PR_STDIO_INIT();
  73.  
  74.     printf("This program tests PR_Poll with sockets.\n");
  75.     printf("Timeout, error reporting, and normal operation are tested.\n\n");
  76.  
  77.     /* Create two listening sockets */
  78.     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
  79.     fprintf(stderr, "Can't create a new TCP socket\n");
  80.     exit(1);
  81.     }
  82.     addr.inet.family = AF_INET;
  83.     addr.inet.ip = PR_htonl(INADDR_ANY);
  84.     addr.inet.port = PR_htons(0);
  85.     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
  86.     fprintf(stderr, "Can't bind socket\n");
  87.     exit(1);
  88.     }
  89.     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
  90.     fprintf(stderr, "PR_GetSockName failed\n");
  91.     exit(1);
  92.     }
  93.     listenPort1 = PR_ntohs(addr.inet.port);
  94.     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
  95.     fprintf(stderr, "Can't listen on a socket\n");
  96.     exit(1);
  97.     }
  98.  
  99.     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
  100.     fprintf(stderr, "Can't create a new TCP socket\n");
  101.     exit(1);
  102.     }
  103.     addr.inet.family = AF_INET;
  104.     addr.inet.ip = PR_htonl(INADDR_ANY);
  105.     addr.inet.port = PR_htons(0);
  106.     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
  107.     fprintf(stderr, "Can't bind socket\n");
  108.     exit(1);
  109.     }
  110.     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
  111.     fprintf(stderr, "PR_GetSockName failed\n");
  112.     exit(1);
  113.     }
  114.     listenPort2 = PR_ntohs(addr.inet.port);
  115.     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
  116.     fprintf(stderr, "Can't listen on a socket\n");
  117.     exit(1);
  118.     }
  119.     PR_snprintf(buf, sizeof(buf),
  120.         "The server thread is listening on ports %hu and %hu\n\n",
  121.         listenPort1, listenPort2);
  122.     printf("%s", buf);
  123.  
  124.     /* Set up the poll descriptor array */
  125.     pds = pds0;
  126.     other_pds = pds1;
  127.     memset(pds, 0, sizeof(pds));
  128.     pds[0].fd = listenSock1;
  129.     pds[0].in_flags = PR_POLL_READ;
  130.     pds[1].fd = listenSock2;
  131.     pds[1].in_flags = PR_POLL_READ;
  132.     npds = 2;
  133.  
  134.     /* Testing timeout */
  135.     printf("PR_Poll should time out in 5 seconds\n");
  136.     retVal = PR_Poll(pds, npds, PR_SecondsToInterval(5));
  137.     if (retVal != 0) {
  138.     PR_snprintf(buf, sizeof(buf),
  139.         "PR_Poll should time out and return 0, but it returns %ld\n",
  140.         retVal);
  141.     fprintf(stderr, "%s", buf);
  142.     exit(1);
  143.     }
  144.     printf("PR_Poll timed out.  Test passed.\n\n");
  145.  
  146.     /* Testing bad fd */
  147.     printf("PR_Poll should detect a bad file descriptor\n");
  148.     if ((badFD = PR_NewTCPSocket()) == NULL) {
  149.     fprintf(stderr, "Can't create a TCP socket\n");
  150.     exit(1);
  151.     }
  152.  
  153.     pds[2].fd = badFD;
  154.     pds[2].in_flags = PR_POLL_READ;
  155.     npds = 3;
  156.     PR_Close(badFD);  /* make the fd bad */
  157.     retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
  158.     if (retVal != 1 || (unsigned short) pds[2].out_flags != PR_POLL_NVAL) {
  159.     fprintf(stderr, "Failed to detect the bad fd: "
  160.         "PR_Poll returns %d, out_flags is 0x%hx\n",
  161.         retVal, pds[2].out_flags);
  162.     exit(1);
  163.     }
  164.     printf("PR_Poll detected the bad fd.  Test passed.\n\n");
  165.     npds = 2;
  166.  
  167.     clientThread = PR_CreateThread(PR_USER_THREAD,
  168.         clientThreadFunc, (void *) listenPort1,
  169.         PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
  170.         PR_UNJOINABLE_THREAD, 0);
  171.     if (clientThread == NULL) {
  172.     fprintf(stderr, "can't create thread\n");
  173.     exit(1);
  174.     }
  175.  
  176.     clientThread = PR_CreateThread(PR_USER_THREAD,
  177.         clientThreadFunc, (void *) listenPort2,
  178.         PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
  179.         PR_UNJOINABLE_THREAD, 0);
  180.     if (clientThread == NULL) {
  181.     fprintf(stderr, "can't create thread\n");
  182.     exit(1);
  183.     }
  184.  
  185.     printf("Two client threads are created.  Each of them will\n");
  186.     printf("send data to one of the two ports the server is listening on.\n");
  187.     printf("The data they send is the port number.  Each of them send\n");
  188.     printf("the data five times, so you should see ten lines below,\n");
  189.     printf("interleaved in an arbitrary order.\n");
  190.  
  191.     /* 20 events total */
  192.     i = 0;
  193.     while (i < 20) {
  194.     PRPollDesc *tmp;
  195.     int nextIndex;
  196.     int nEvents = 0;
  197.  
  198.     retVal = PR_Poll(pds, npds, PR_INTERVAL_NO_TIMEOUT);
  199.     PR_ASSERT(retVal != 0);  /* no timeout */
  200.     if (retVal == -1) {
  201.         fprintf(stderr, "PR_Poll failed\n");
  202.         exit(1);
  203.     }
  204.  
  205.     nextIndex = 2;
  206.     /* the two listening sockets */
  207.     for (j = 0; j < 2; j++) {
  208.         other_pds[j] = pds[j];
  209.         PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
  210.             && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
  211.         if (pds[j].out_flags & PR_POLL_READ) {
  212.         PRFileDesc *sock;
  213.  
  214.         nEvents++;
  215.         sock = PR_Accept(pds[j].fd, NULL, PR_INTERVAL_NO_TIMEOUT);
  216.         if (sock == NULL) {
  217.             fprintf(stderr, "PR_Accept() failed\n");
  218.             exit(1);
  219.         }
  220.         other_pds[nextIndex].fd = sock;
  221.         other_pds[nextIndex].in_flags = PR_POLL_READ;
  222.         nextIndex++;
  223.         } else if (pds[j].out_flags & PR_POLL_ERR) {
  224.         fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
  225.         exit(1);
  226.         } else if (pds[j].out_flags & PR_POLL_NVAL) {
  227.         fprintf(stderr, "PR_Poll() indicates that fd %d is invalid\n",
  228.             PR_FileDesc2NativeHandle(pds[j].fd));
  229.         exit(1);
  230.         }
  231.     }
  232.  
  233.     for (j = 2; j < npds; j++) {
  234.         PR_ASSERT((pds[j].out_flags & PR_POLL_WRITE) == 0
  235.             && (pds[j].out_flags & PR_POLL_EXCEPT) == 0);
  236.         if (pds[j].out_flags & PR_POLL_READ) {
  237.         PRInt32 nBytes;
  238.  
  239.         nEvents++;
  240.         nBytes = PR_Read(pds[j].fd, buf, sizeof(buf));
  241.         if (nBytes == -1) {
  242.             fprintf(stderr, "PR_Read() failed\n");
  243.             exit(1);
  244.         }
  245.         /* Just to be safe */
  246.         buf[127] = '\0';
  247.         PR_Close(pds[j].fd);
  248.         printf("The server received \"%s\" from a client\n", buf);
  249.         } else if (pds[j].out_flags & PR_POLL_ERR) {
  250.         fprintf(stderr, "PR_Poll() indicates that an fd has error\n");
  251.         exit(1);
  252.         } else if (pds[j].out_flags & PR_POLL_NVAL) {
  253.         fprintf(stderr, "PR_Poll() indicates that an fd is invalid\n");
  254.         exit(1);
  255.         } else {
  256.         other_pds[nextIndex] = pds[j];
  257.         nextIndex++;
  258.         }
  259.     }
  260.  
  261.     PR_ASSERT(retVal == nEvents);
  262.     /* swap */
  263.     tmp = pds;
  264.     pds = other_pds;
  265.     other_pds = tmp;
  266.     npds = nextIndex;
  267.     i += nEvents;
  268.     }
  269.  
  270.     printf("All tests finished\n");
  271.     PR_Cleanup();
  272.     return 0;
  273. }
  274.