Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Enumerations | Functions
tmoacc.c File Reference
#include "nspr.h"
#include <stdlib.h>
#include <string.h>
#include "plerror.h"
#include "plgetopt.h"

Go to the source code of this file.

Classes

struct  Shared

Defines

#define BASE_PORT   9867
#define DEFAULT_THREADS   1
#define DEFAULT_BACKLOG   10
#define DEFAULT_TIMEOUT   10
#define RANDOM_RANGE   100 /* should be significantly smaller than RAND_MAX */

Typedefs

typedef struct Shared Shared

Enumerations

enum  Status { running, stopped }

Functions

static PRIntervalTime Timeout (const Shared *shared)
static void Accept (void *arg)
PRIntn Tmoacc (PRIntn argc, char **argv)
int main (int argc, char **argv)
 The Xalan testcases app.

Define Documentation

#define BASE_PORT   9867

Definition at line 51 of file tmoacc.c.

Definition at line 53 of file tmoacc.c.

Definition at line 52 of file tmoacc.c.

Definition at line 54 of file tmoacc.c.

#define RANDOM_RANGE   100 /* should be significantly smaller than RAND_MAX */

Definition at line 55 of file tmoacc.c.


Typedef Documentation

typedef struct Shared Shared

Enumeration Type Documentation

enum Status
Enumerator:
running 
stopped 

Definition at line 57 of file tmoacc.c.


Function Documentation

static void Accept ( void arg) [static]

Definition at line 85 of file tmoacc.c.

{
    PRStatus rv;
    char *buffer = NULL;
    PRNetAddr clientAddr;
    Shared *shared = (Shared*)arg;
    PRInt32 recv_length = 0, flags = 0;
    PRFileDesc *clientSock;
    PRIntn toread, byte, bytes, loop = 0;
    struct Descriptor { PRInt32 length; PRUint32 checksum; } descriptor;

    do
    {
        PRUint32 checksum = 0;
        if (NULL != shared->debug)
            PR_fprintf(shared->debug, "[%d]accepting ... ", loop++);
        clientSock = PR_Accept(
            shared->listenSock, &clientAddr, Timeout(shared));
        if (clientSock != NULL)
        {
            if (NULL != shared->debug)
                PR_fprintf(shared->debug, "reading length ... ");
            bytes = PR_Recv(
                clientSock, &descriptor, sizeof(descriptor),
                flags, Timeout(shared));
            if (sizeof(descriptor) == bytes)
            {
                /* and, before doing something stupid ... */
                descriptor.length = PR_ntohl(descriptor.length);
                descriptor.checksum = PR_ntohl(descriptor.checksum);
                if (NULL != shared->debug)
                    PR_fprintf(shared->debug, "%d bytes ... ", descriptor.length);
                toread = descriptor.length;
                if (recv_length < descriptor.length)
                {
                    if (NULL != buffer) PR_DELETE(buffer);
                    buffer = (char*)PR_MALLOC(descriptor.length);
                    recv_length = descriptor.length;
                }
                for (toread = descriptor.length; toread > 0; toread -= bytes)
                {
                    bytes = PR_Recv(
                        clientSock, &buffer[descriptor.length - toread],
                        toread, flags, Timeout(shared));
                    if (-1 == bytes)
                    {
                        if (NULL != shared->debug)
                            PR_fprintf(shared->debug, "read data failed...");
                        bytes = 0;
                    }
                }
            }
            else if (NULL != shared->debug)
            {
                PR_fprintf(shared->debug, "read desciptor failed...");
                descriptor.length = -1;
            }
            if (NULL != shared->debug)
                PR_fprintf(shared->debug, "closing");
            rv = PR_Shutdown(clientSock, PR_SHUTDOWN_BOTH);
            if ((PR_FAILURE == rv) && (NULL != shared->debug))
            {
                PR_fprintf(shared->debug, " failed");
                shared->passed = PR_FALSE;
            }
            rv = PR_Close(clientSock);
            if (PR_FAILURE == rv) if (NULL != shared->debug)
            {
                PR_fprintf(shared->debug, " failed");
                shared->passed = PR_FALSE;
            }
            if (descriptor.length > 0)
            {
                for (byte = 0; byte < descriptor.length; ++byte)
                {
                    PRUint32 overflow = checksum & 0x80000000;
                    checksum = (checksum << 1);
                    if (0x00000000 != overflow) checksum += 1;
                    checksum += buffer[byte];
                }
                if ((descriptor.checksum != checksum) && (NULL != shared->debug))
                {
                    PR_fprintf(shared->debug, " ... data mismatch");
                    shared->passed = PR_FALSE;
                }
            }
            else if (0 == descriptor.length)
            {
                PR_Lock(shared->ml);
                shared->status = stopped;
                PR_NotifyCondVar(shared->cv);
                PR_Unlock(shared->ml);
            }
            if (NULL != shared->debug)
                PR_fprintf(shared->debug, "\n");
        }
        else
        {
            if (PR_PENDING_INTERRUPT_ERROR != PR_GetError())
            {
                if (NULL != shared->debug) PL_PrintError("Accept");
                shared->passed = PR_FALSE;
            }
        }        
    } while (running == shared->status);
    if (NULL != buffer) PR_DELETE(buffer);
}  /* Accept */

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char **  argv 
)

The Xalan testcases app.

Definition at line 327 of file tmoacc.c.

{
    return (PR_VersionCheck(PR_VERSION)) ?
        PR_Initialize(Tmoacc, argc, argv, 4) : -1;
}  /* main */

Here is the call graph for this function:

static PRIntervalTime Timeout ( const Shared shared) [static]

Definition at line 71 of file tmoacc.c.

{
    PRIntervalTime timeout = shared->timeout;
    if (shared->random)
    {
        PRIntervalTime half = timeout >> 1;  /* one half of the interval */
        PRIntervalTime quarter = half >> 1;  /* one quarter of the interval */
        /* something in [0..timeout / 2) */
        PRUint32 random = (rand() % RANDOM_RANGE) * half / RANDOM_RANGE;
        timeout = (3 * quarter) + random;  /* [75..125)% */
    }
    return timeout;
}  /* Timeout */

Here is the call graph for this function:

Here is the caller graph for this function:

PRIntn Tmoacc ( PRIntn  argc,
char **  argv 
)

Definition at line 193 of file tmoacc.c.

{
    PRStatus rv;
    PRIntn exitStatus;
    PRIntn index;
       Shared *shared;
       PLOptStatus os;
       PRThread **thread;
    PRNetAddr listenAddr;
    PRSocketOptionData sockOpt;
    PRIntn timeout = DEFAULT_TIMEOUT;
    PRIntn threads = DEFAULT_THREADS;
    PRIntn backlog = DEFAULT_BACKLOG;
    PRThreadScope thread_scope = PR_LOCAL_THREAD;

       PLOptState *opt = PL_CreateOptState(argc, argv, "dGb:t:T:R");

    shared = PR_NEWZAP(Shared);

    shared->debug = NULL;
    shared->passed = PR_TRUE;
    shared->random = PR_TRUE;
    shared->status = running;
    shared->ml = PR_NewLock();
    shared->cv = PR_NewCondVar(shared->ml);

       while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    {
        if (PL_OPT_BAD == os) continue;
        switch (opt->option)
        {
        case 'd':  /* debug mode */
            shared->debug = PR_GetSpecialFD(PR_StandardError);
            break;
        case 'G':  /* use global threads */
            thread_scope = PR_GLOBAL_THREAD;
            break;
        case 'b':  /* size of listen backlog */
            backlog = atoi(opt->value);
            break;
        case 't':  /* number of threads doing accept */
            threads = atoi(opt->value);
            break;
        case 'T':  /* timeout used for network operations */
            timeout = atoi(opt->value);
            break;
        case 'R':  /* randomize the timeout values */
            shared->random = PR_TRUE;
            break;
        default:
            break;
        }
    }
       PL_DestroyOptState(opt);
    if (0 == threads) threads = DEFAULT_THREADS;
    if (0 == backlog) backlog = DEFAULT_BACKLOG;
    if (0 == timeout) timeout = DEFAULT_TIMEOUT;
    
    PR_STDIO_INIT();
    memset(&listenAddr, 0, sizeof(listenAddr));
    rv = PR_InitializeNetAddr(PR_IpAddrAny, BASE_PORT, &listenAddr);
    PR_ASSERT(PR_SUCCESS == rv);

    shared->timeout = PR_SecondsToInterval(timeout);

    /* First bind to the socket */
    shared->listenSock = PR_NewTCPSocket();
    if (shared->listenSock)
    {
        sockOpt.option = PR_SockOpt_Reuseaddr;
        sockOpt.value.reuse_addr = PR_TRUE;
        rv = PR_SetSocketOption(shared->listenSock, &sockOpt);
        PR_ASSERT(PR_SUCCESS == rv);
        rv = PR_Bind(shared->listenSock, &listenAddr);
        if (rv != PR_FAILURE)
        {
            rv = PR_Listen(shared->listenSock, threads + backlog);
            if (PR_SUCCESS == rv)
            {
                thread = (PRThread**)PR_CALLOC(threads * sizeof(PRThread*));
                for (index = 0; index < threads; ++index)
                {
                    thread[index] = PR_CreateThread(
                        PR_USER_THREAD, Accept, shared,
                        PR_PRIORITY_NORMAL, thread_scope,
                        PR_JOINABLE_THREAD, 0);
                    PR_ASSERT(NULL != thread[index]);
                }

                PR_Lock(shared->ml);
                while (shared->status == running)
                    PR_WaitCondVar(shared->cv, PR_INTERVAL_NO_TIMEOUT);
                PR_Unlock(shared->ml);
                for (index = 0; index < threads; ++index)
                {
                    rv = PR_Interrupt(thread[index]);
                    PR_ASSERT(PR_SUCCESS== rv);
                    rv = PR_JoinThread(thread[index]);
                    PR_ASSERT(PR_SUCCESS== rv);
                }
                PR_DELETE(thread);
            }
            else
            {
                if (shared->debug) PL_PrintError("Listen");
                shared->passed = PR_FALSE;
            }
        }
        else
        {
            if (shared->debug) PL_PrintError("Bind");
            shared->passed = PR_FALSE;
        }

        PR_Close(shared->listenSock);
    }
    else
    {
        if (shared->debug) PL_PrintError("Create");
        shared->passed = PR_FALSE;
    }

    PR_DestroyCondVar(shared->cv);
    PR_DestroyLock(shared->ml);

    PR_fprintf(
        PR_GetSpecialFD(PR_StandardError), "%s\n",
        ((shared->passed) ? "PASSED" : "FAILED"));

    exitStatus = (shared->passed) ? 0 : 1;
    PR_DELETE(shared);
    return exitStatus;
}

Here is the call graph for this function:

Here is the caller graph for this function: