Back to index

lightning-sunbird  0.9+nobinonly
selct_nm.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /***********************************************************************
00039 **  1997 - Netscape Communications Corporation
00040 **
00041 ** Name: prselect_norm.c
00042 **
00043 ** Description: tests PR_Select with sockets - Normal operations.
00044 **
00045 ** Modification History:
00046 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
00047 **              The debug mode will print all of the printfs associated with this test.
00048 **                    The regress mode will be the default mode. Since the regress tool limits
00049 **           the output to a one line status:PASS or FAIL,all of the printf statements
00050 **                    have been handled with an if (debug_mode) statement.
00051 ** 04-June-97 AGarcia removed the Test_Result function. Regress tool has been updated to
00052 **                   recognize the return code from tha main program.
00053 ***********************************************************************/
00054 
00055 /***********************************************************************
00056 ** Includes
00057 ***********************************************************************/
00058 /* Used to get the command line option */
00059 #include "plgetopt.h"
00060 
00061 #include "prinit.h"
00062 #include "prio.h"
00063 #include "prlog.h"
00064 #include "prprf.h"
00065 #include "prerror.h"
00066 #include "prnetdb.h"
00067 
00068 #ifdef XP_MAC
00069 #include "probslet.h"
00070 #else
00071 #include "obsolete/probslet.h"
00072 #endif
00073 
00074 #include <stdio.h>
00075 #include <string.h>
00076 #include <stdlib.h>
00077 
00078 PRIntn failed_already=0;
00079 PRIntn debug_mode;
00080 
00081 static void
00082 clientThreadFunc(void *arg)
00083 {
00084     PRUintn port = (PRUintn) arg;
00085     PRFileDesc *sock;
00086     PRNetAddr addr;
00087     char buf[128];
00088     int i;
00089 
00090     addr.inet.family = PR_AF_INET;
00091     addr.inet.port = PR_htons((PRUint16)port);
00092     addr.inet.ip = PR_htonl(PR_INADDR_LOOPBACK);
00093     PR_snprintf(buf, sizeof(buf), "%hu", addr.inet.port);
00094 
00095     for (i = 0; i < 5; i++) {
00096        sock = PR_NewTCPSocket();
00097         PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
00098        PR_Write(sock, buf, sizeof(buf));
00099        PR_Close(sock);
00100     }
00101 }
00102 
00103 int main(int argc, char **argv)
00104 {
00105     PRFileDesc *listenSock1, *listenSock2;
00106     PRFileDesc *fds0[10], *fds1[10], **fds, **other_fds;
00107     PRIntn nfds;
00108     PRUint16 listenPort1, listenPort2;
00109     PRNetAddr addr;
00110     PR_fd_set readFdSet;
00111     char buf[128];
00112     PRThread *clientThread;
00113     PRInt32 retVal;
00114     PRIntn i, j;
00115 
00116        /* The command line argument: -d is used to determine if the test is being run
00117        in debug mode. The regress tool requires only one line output:PASS or FAIL.
00118        All of the printfs associated with this test has been handled with a if (debug_mode)
00119        test.
00120        Usage: test_name -d
00121        */
00122        PLOptStatus os;
00123        PLOptState *opt = PL_CreateOptState(argc, argv, "d:");
00124        while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00125     {
00126               if (PL_OPT_BAD == os) continue;
00127         switch (opt->option)
00128         {
00129         case 'd':  /* debug mode */
00130                      debug_mode = 1;
00131             break;
00132          default:
00133             break;
00134         }
00135     }
00136        PL_DestroyOptState(opt);
00137 
00138  /* main test */
00139        
00140     PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
00141     PR_STDIO_INIT();
00142 
00143     if (debug_mode) {
00144               printf("This program tests PR_Select with sockets.  \n");
00145               printf(" Normal operation are tested.\n\n");
00146        }
00147 
00148     /* Create two listening sockets */
00149     if ((listenSock1 = PR_NewTCPSocket()) == NULL) {
00150        fprintf(stderr, "Can't create a new TCP socket\n");
00151        failed_already=1;
00152        goto exit_now;
00153     }
00154     addr.inet.family = PR_AF_INET;
00155     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
00156     addr.inet.port = PR_htons(0);
00157     if (PR_Bind(listenSock1, &addr) == PR_FAILURE) {
00158        fprintf(stderr, "Can't bind socket\n");
00159        failed_already=1;
00160        goto exit_now;
00161     }
00162     if (PR_GetSockName(listenSock1, &addr) == PR_FAILURE) {
00163        fprintf(stderr, "PR_GetSockName failed\n");
00164        failed_already=1;
00165        goto exit_now;
00166     }
00167     listenPort1 = PR_ntohs(addr.inet.port);
00168     if (PR_Listen(listenSock1, 5) == PR_FAILURE) {
00169        fprintf(stderr, "Can't listen on a socket\n");
00170        failed_already=1;
00171        goto exit_now;
00172     }
00173 
00174     if ((listenSock2  = PR_NewTCPSocket()) == NULL) {
00175        fprintf(stderr, "Can't create a new TCP socket\n");
00176        failed_already=1;
00177        goto exit_now;
00178     }
00179     addr.inet.family = PR_AF_INET;
00180     addr.inet.ip = PR_htonl(PR_INADDR_ANY);
00181     addr.inet.port = PR_htons(0);
00182     if (PR_Bind(listenSock2, &addr) == PR_FAILURE) {
00183        fprintf(stderr, "Can't bind socket\n");
00184        failed_already=1;
00185        goto exit_now;
00186     }
00187     if (PR_GetSockName(listenSock2, &addr) == PR_FAILURE) {
00188        fprintf(stderr, "PR_GetSockName failed\n");
00189        failed_already=1;
00190        goto exit_now;
00191     }
00192     listenPort2 = PR_ntohs(addr.inet.port);
00193     if (PR_Listen(listenSock2, 5) == PR_FAILURE) {
00194        fprintf(stderr, "Can't listen on a socket\n");
00195 failed_already=1;
00196        goto exit_now;
00197     }
00198     PR_snprintf(buf, sizeof(buf),
00199            "The server thread is listening on ports %hu and %hu\n\n",
00200            listenPort1, listenPort2);
00201     if (debug_mode) printf("%s", buf);
00202 
00203     clientThread = PR_CreateThread(PR_USER_THREAD,
00204            clientThreadFunc, (void *) listenPort1,
00205            PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
00206            PR_UNJOINABLE_THREAD, 0);
00207     if (clientThread == NULL) {
00208        fprintf(stderr, "can't create thread\n");
00209        failed_already=1;
00210        goto exit_now;
00211     }
00212 
00213     clientThread = PR_CreateThread(PR_USER_THREAD,
00214            clientThreadFunc, (void *) listenPort2,
00215            PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
00216            PR_UNJOINABLE_THREAD, 0);
00217     if (clientThread == NULL) {
00218        fprintf(stderr, "can't create thread\n");
00219        failed_already=1;
00220        goto exit_now;
00221     }
00222 
00223     if (debug_mode) {
00224               printf("Two client threads are created.  Each of them will\n");
00225               printf("send data to one of the two ports the server is listening on.\n");
00226               printf("The data they send is the port number.  Each of them send\n");
00227               printf("the data five times, so you should see ten lines below,\n");
00228               printf("interleaved in an arbitrary order.\n");
00229        }
00230     /* set up the fd array */
00231     fds = fds0;
00232     other_fds = fds1;
00233     fds[0] = listenSock1;
00234     fds[1] = listenSock2;
00235     nfds = 2;
00236     /* Set up the fd set */
00237     PR_FD_ZERO(&readFdSet);
00238     PR_FD_SET(listenSock1, &readFdSet);
00239     PR_FD_SET(listenSock2, &readFdSet);
00240 
00241     /* 20 events total */
00242     i = 0;
00243     while (i < 20) {
00244        PRFileDesc **tmp;
00245        int nextIndex;
00246        int nEvents = 0;
00247 
00248        retVal = PR_Select(0 /* unused */, &readFdSet, NULL, NULL,
00249               PR_INTERVAL_NO_TIMEOUT);
00250        PR_ASSERT(retVal != 0);  /* no timeout */
00251        if (retVal == -1) {
00252            fprintf(stderr, "PR_Select failed (%d, %d)\n", PR_GetError(),
00253                   PR_GetOSError());
00254        failed_already=1;
00255            goto exit_now;
00256        }
00257 
00258        nextIndex = 2;
00259        /* the two listening sockets */
00260        for (j = 0; j < 2; j++) {
00261            other_fds[j] = fds[j];
00262            if (PR_FD_ISSET(fds[j], &readFdSet)) {
00263               PRFileDesc *sock;
00264 
00265               nEvents++;
00266               sock = PR_Accept(fds[j], NULL, PR_INTERVAL_NO_TIMEOUT);
00267               if (sock == NULL) {
00268                   fprintf(stderr, "PR_Accept() failed\n");
00269               failed_already=1;
00270                   goto exit_now;
00271               }
00272               other_fds[nextIndex] = sock;
00273               PR_FD_SET(sock, &readFdSet);
00274               nextIndex++;
00275            }
00276            PR_FD_SET(fds[j], &readFdSet);
00277        }
00278 
00279        for (j = 2; j < nfds; j++) {
00280            if (PR_FD_ISSET(fds[j], &readFdSet)) {
00281               PRInt32 nBytes;
00282 
00283               PR_FD_CLR(fds[j], &readFdSet);
00284               nEvents++;
00285               nBytes = PR_Read(fds[j], buf, sizeof(buf));
00286               if (nBytes == -1) {
00287                   fprintf(stderr, "PR_Read() failed\n");
00288               failed_already=1;
00289                   goto exit_now;
00290               }
00291               /* Just to be safe */
00292               buf[127] = '\0';
00293               PR_Close(fds[j]);
00294               if (debug_mode) printf("The server received \"%s\" from a client\n", buf);
00295            } else {
00296               PR_FD_SET(fds[j], &readFdSet);
00297               other_fds[nextIndex] = fds[j];
00298               nextIndex++;
00299            }
00300        }
00301 
00302        PR_ASSERT(retVal == nEvents);
00303        /* swap */
00304        tmp = fds;
00305        fds = other_fds;
00306        other_fds = tmp;
00307        nfds = nextIndex;
00308        i += nEvents;
00309     }
00310 
00311     if (debug_mode) printf("Test passed\n");
00312 
00313     PR_Cleanup();
00314        goto exit_now;
00315 exit_now:
00316        if(failed_already)   
00317               return 1;
00318        else
00319               return 0;
00320 }