Back to index

lightning-sunbird  0.9+nobinonly
Defines | Enumerations | Functions | Variables
ntioto.c File Reference
#include <plgetopt.h>
#include <nspr.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Defines

#define JITTER_DEFAULT   100000
#define BASE_PORT   9867
#define ACCEPT_READ_DATASIZE   10
#define ACCEPT_READ_BUFSIZE   (PR_ACCEPT_READ_BUF_OVERHEAD + ACCEPT_READ_DATASIZE)

Enumerations

enum  { RunJitter, RunAcceptRead, AllDone }

Functions

static void Help (void)
static void AcceptThread (void *arg)
static void JitterThread (void *arg)
static void ConnectThread (void *arg)
PRIntn main (PRIntn argc, char *argv[])

Variables

PRLogModuleInfolm
PRLogModuleLevel msgLevel = PR_LOG_NONE
PRIntn debug = 0
PRIntn verbose = 0
PRUint32 failed_already = 0
PRIntervalTime timeout
PRNetAddr listenAddr
PRFileDesclistenSock
PRLockml
PRCondVarcv
enum { ... }  state
PRFileDescfile1
PRIntn iCounter = 0
PRIntn jitter = JITTER_DEFAULT
PRBool resume = PR_FALSE

Define Documentation

Definition at line 123 of file ntioto.c.

Definition at line 122 of file ntioto.c.

#define BASE_PORT   9867

Definition at line 92 of file ntioto.c.

#define JITTER_DEFAULT   100000

Definition at line 91 of file ntioto.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
RunJitter 
RunAcceptRead 
AllDone 

Definition at line 99 of file ntioto.c.


Function Documentation

static void AcceptThread ( void arg) [static]

Definition at line 125 of file ntioto.c.

{
    PRIntn bytesRead;
    char dataBuf[ACCEPT_READ_BUFSIZE];
    PRFileDesc  *arSock;
    PRNetAddr   *arAddr;

    bytesRead = PR_AcceptRead( listenSock, 
        &arSock,
        &arAddr,
        dataBuf,
        ACCEPT_READ_DATASIZE,
        PR_SecondsToInterval(1));

    if ( bytesRead == -1 && PR_GetError() == PR_IO_TIMEOUT_ERROR ) {
        if ( debug ) printf("AcceptRead timed out\n");
    } else {
        if ( debug ) printf("Oops! read: %d, error: %d\n", bytesRead, PR_GetError());
    }

    while( state != AllDone )  {
        PR_Lock( ml );
        while( state != RunAcceptRead )
            PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT );
        if ( ++iCounter >= jitter )
            state = AllDone;
        else
            state = RunJitter;
        if ( verbose ) printf(".");
        PR_NotifyCondVar( cv );
        PR_Unlock( ml );
        PR_Write( file1, ".", 1 );
    }

    return;
} /* end AcceptThread() */

Here is the call graph for this function:

Here is the caller graph for this function:

static void ConnectThread ( void arg) [static]

Definition at line 178 of file ntioto.c.

{
    PRStatus    rv;
    PRFileDesc  *clientSock;
    PRNetAddr   serverAddress;
    clientSock = PR_NewTCPSocket();

    PR_ASSERT(clientSock);

    if ( resume ) {
        if ( debug ) printf("pausing 3 seconds before connect\n");
        PR_Sleep( PR_SecondsToInterval(3));
    }

    memset(&serverAddress, 0, sizeof(serverAddress));
    rv = PR_InitializeNetAddr(PR_IpAddrLoopback, BASE_PORT, &serverAddress);
    PR_ASSERT( PR_SUCCESS == rv );
    rv = PR_Connect( clientSock, 
        &serverAddress, 
        PR_SecondsToInterval(1));
    PR_ASSERT( PR_SUCCESS == rv );

    /* that's all we do. ... Wait for the acceptread() to timeout */
    while( state != AllDone )
        PR_Sleep( PR_SecondsToInterval(1));
    return;
} /* end ConnectThread() */

Here is the call graph for this function:

Here is the caller graph for this function:

static void Help ( void  ) [static]

Definition at line 112 of file ntioto.c.

{
    printf("Template: Help(): display your help message(s) here");
    exit(1);
} /* end Help() */

Here is the call graph for this function:

static void JitterThread ( void arg) [static]

Definition at line 162 of file ntioto.c.

{
    while( state != AllDone )  {
        PR_Lock( ml );
        while( state != RunJitter && state != AllDone )
            PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT );
        if ( state != AllDone)
            state = RunAcceptRead;
        if ( verbose ) printf("+");
        PR_NotifyCondVar( cv );
        PR_Unlock( ml );
        PR_Write( file1, "+", 1 );
    }
    return;
} /* end Goofy() */

Here is the call graph for this function:

Here is the caller graph for this function:

PRIntn main ( PRIntn  argc,
char *  argv[] 
)

Definition at line 207 of file ntioto.c.

{
    PRThread *tJitter;
    PRThread *tAccept;
    PRThread *tConnect;
    PRStatus rv;
    /* This test if valid for WinNT only! */

#if !defined(WINNT)
    return 0;
#endif

    {
        /*
        ** Get command line options
        */
        PLOptStatus os;
        PLOptState *opt = PL_CreateOptState(argc, argv, "hdrvj:");

           while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
        {
                  if (PL_OPT_BAD == os) continue;
            switch (opt->option)
            {
            case 'd':  /* debug */
                debug = 1;
                         msgLevel = PR_LOG_ERROR;
                break;
            case 'v':  /* verbose mode */
                verbose = 1;
                         msgLevel = PR_LOG_DEBUG;
                break;
            case 'j':
                jitter = atoi(opt->value);
                if ( jitter == 0)
                    jitter = JITTER_DEFAULT;
                break;
            case 'r':
                resume = PR_TRUE;
                break;
            case 'h':  /* help message */
                         Help();
                break;
             default:
                break;
            }
        }
           PL_DestroyOptState(opt);
    }

    lm = PR_NewLogModule("Test");       /* Initialize logging */

    /* set concurrency */
    PR_SetConcurrency( 4 );

    /* setup thread synchronization mechanics */
    ml = PR_NewLock();
    cv = PR_NewCondVar( ml );

    /* setup a tcp socket */
    memset(&listenAddr, 0, sizeof(listenAddr));
    rv = PR_InitializeNetAddr(PR_IpAddrAny, BASE_PORT, &listenAddr);
    PR_ASSERT( PR_SUCCESS == rv );

    listenSock = PR_NewTCPSocket();
    PR_ASSERT( listenSock );

    rv = PR_Bind( listenSock, &listenAddr);
    PR_ASSERT( PR_SUCCESS == rv );

    rv = PR_Listen( listenSock, 5 );
    PR_ASSERT( PR_SUCCESS == rv );

    /* open a file for writing, provoke bug */
    file1 = PR_Open("xxxTestFile", PR_CREATE_FILE | PR_RDWR, 666);

    /* create Connect thread */
    tConnect = PR_CreateThread(
        PR_USER_THREAD, ConnectThread, NULL,
        PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
        PR_JOINABLE_THREAD, 0 );
    PR_ASSERT( tConnect );

    /* create jitter off thread */
    tJitter = PR_CreateThread(
        PR_USER_THREAD, JitterThread, NULL,
        PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
        PR_JOINABLE_THREAD, 0 );
    PR_ASSERT( tJitter );

    /* create acceptread thread */
    tAccept = PR_CreateThread(
        PR_USER_THREAD, AcceptThread, NULL,
        PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
        PR_JOINABLE_THREAD, 0 );
    PR_ASSERT( tAccept );

    /* wait for all threads to quit, then terminate gracefully */
    PR_JoinThread( tConnect );
    PR_JoinThread( tAccept );
    PR_JoinThread( tJitter );
    PR_Close( listenSock );
    PR_DestroyCondVar(cv);
    PR_DestroyLock(ml);
    PR_Close( file1 );
    PR_Delete( "xxxTestFile");

    /* test return and exit */
    if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS");
    return( (failed_already == PR_TRUE )? 1 : 0 );
}  /* main() */

Here is the call graph for this function:


Variable Documentation

Definition at line 98 of file ntioto.c.

PRIntn debug = 0

Definition at line 85 of file ntioto.c.

Definition at line 87 of file ntioto.c.

Definition at line 104 of file ntioto.c.

PRIntn iCounter = 0

Definition at line 105 of file ntioto.c.

Definition at line 106 of file ntioto.c.

Definition at line 95 of file ntioto.c.

Definition at line 96 of file ntioto.c.

Definition at line 83 of file ntioto.c.

Definition at line 97 of file ntioto.c.

Definition at line 84 of file ntioto.c.

Definition at line 107 of file ntioto.c.

enum { ... } state

Definition at line 94 of file ntioto.c.

PRIntn verbose = 0

Definition at line 86 of file ntioto.c.