Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
TestSocketTransport.cpp File Reference
#include "TestCommon.h"
#include "nsIComponentRegistrar.h"
#include "nsPISocketTransportService.h"
#include "nsISocketTransport.h"
#include "nsIAsyncInputStream.h"
#include "nsIAsyncOutputStream.h"
#include "nsIProgressEventSink.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.h"
#include "nsIProxyObjectManager.h"
#include "nsIRequest.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsCOMPtr.h"
#include "nsMemory.h"
#include "nsString.h"
#include "nsIFileStreams.h"
#include "nsIStreamListener.h"
#include "nsIEventQueueService.h"
#include "nsIEventQueue.h"
#include "nsILocalFile.h"
#include "nsNetUtil.h"
#include "nsAutoLock.h"
#include "prlog.h"

Go to the source code of this file.

Classes

class  MyHandler

Defines

#define LOG(args)   PR_LOG(gTestLog, PR_LOG_DEBUG, args)

Functions

static NS_DEFINE_CID (kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID)
static NS_DEFINE_CID (kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID)
static void *PR_CALLBACK DoneEvent_Handler (PLEvent *ev)
static void PR_CALLBACK DoneEvent_Cleanup (PLEvent *ev)
static void PostDoneEvent ()
 NS_IMPL_THREADSAFE_ISUPPORTS2 (MyHandler, nsIOutputStreamCallback, nsIInputStreamCallback) static nsresult RunCloseTest(nsISocketTransportService *sts
 create transport, open streams, and close
 LOG (("RunCloseTest\n"))
 if (NS_FAILED(rv)) return rv
 LOG (("waiting 1 second before closing transport and streams...\n"))
 PR_Sleep (PR_SecondsToInterval(1))
static nsresult RunTest (nsISocketTransportService *sts, const char *host, int port, const char *path, PRUint32 inFlags, PRUint32 outFlags)
 asynchronously read socket stream
int main (int argc, char *argv[])

Variables

PRBool gDone = PR_FALSE
nsIEventQueuegEventQ = nsnull
const char * host
const char int port
const char int PRUint32 inFlags
const char int PRUint32
PRUint32 outFlags nsresult 
rv
nsCOMPtr< nsISocketTransporttransport
nsCOMPtr< nsIInputStreamin
nsCOMPtr< nsIAsyncInputStreamasyncIn = do_QueryInterface(in, &rv)
nsCOMPtr< nsIOutputStreamout
nsCOMPtr< nsIAsyncOutputStreamasyncOut = do_QueryInterface(out, &rv)
return NS_OK

Define Documentation

#define LOG (   args)    PR_LOG(gTestLog, PR_LOG_DEBUG, args)

Definition at line 71 of file TestSocketTransport.cpp.


Function Documentation

static void PR_CALLBACK DoneEvent_Cleanup ( PLEvent ev) [static]

Definition at line 91 of file TestSocketTransport.cpp.

{
    delete ev;
}

Here is the caller graph for this function:

static void* PR_CALLBACK DoneEvent_Handler ( PLEvent ev) [static]

Definition at line 84 of file TestSocketTransport.cpp.

{
    gDone = PR_TRUE;
    return nsnull;
}

Here is the caller graph for this function:

if ( NS_FAILED(rv )
LOG ( ("RunCloseTest\n")  )
LOG ( ("waiting 1 second before closing transport and streams...\n")  )
int main ( int  argc,
char *  argv[] 
)

Definition at line 282 of file TestSocketTransport.cpp.

{
    if (test_common_init(&argc, &argv) != 0)
        return -1;

    nsresult rv;

    if (argc < 4) {
        printf("usage: TestSocketTransport <host> <port> <path>\n");
        return -1;
    }

    {
        nsCOMPtr<nsIServiceManager> servMan;
        NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
        nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
        NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
        if (registrar)
            registrar->AutoRegister(nsnull);

#if defined(PR_LOGGING)
        gTestLog = PR_NewLogModule("Test");
#endif

        nsCOMPtr<nsIEventQueueService> eventQService =
                 do_GetService(kEventQueueServiceCID, &rv);
        if (NS_FAILED(rv)) return rv;

        rv = eventQService->CreateMonitoredThreadEventQueue();
        if (NS_FAILED(rv)) return rv;

        rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
        if (NS_FAILED(rv)) return rv;

        nsCOMPtr<nsPISocketTransportService> sts =
            do_GetService(kSocketTransportServiceCID, &rv);
        if (NS_FAILED(rv)) return rv;

        LOG(("phase 1 tests...\n"));

        LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
        rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
                          nsITransport::OPEN_UNBUFFERED,
                          nsITransport::OPEN_UNBUFFERED);
        NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");

        LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
        rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
                          0 /* nsITransport::OPEN_BUFFERED */,
                          nsITransport::OPEN_UNBUFFERED);
        NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");

        LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
        rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
                          nsITransport::OPEN_UNBUFFERED,
                          0 /*nsITransport::OPEN_BUFFERED */);
        NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");

        LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
        rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
                          0 /*nsITransport::OPEN_BUFFERED */,
                          0 /*nsITransport::OPEN_BUFFERED */);
        NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");

        LOG(("calling Shutdown on socket transport service:\n"));
        sts->Shutdown();

        LOG(("calling Init on socket transport service:\n"));
        sts->Init();

        LOG(("phase 2 tests...\n"));

        LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
        rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
                     nsITransport::OPEN_UNBUFFERED,
                     nsITransport::OPEN_UNBUFFERED);
        NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");

        LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
        rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
                     0 /* nsITransport::OPEN_BUFFERED */,
                     nsITransport::OPEN_UNBUFFERED);
        NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");

        LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
        rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
                     nsITransport::OPEN_UNBUFFERED,
                     0 /*nsITransport::OPEN_BUFFERED */);
        NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");

        LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
        rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
                     0 /*nsITransport::OPEN_BUFFERED */,
                     0 /*nsITransport::OPEN_BUFFERED */);
        NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");

        LOG(("waiting 1 second before calling Shutdown...\n"));
        PR_Sleep(PR_SecondsToInterval(1));

        LOG(("calling Shutdown on socket transport service:\n"));
        sts->Shutdown();

        NS_RELEASE(gEventQ);

        // give background threads a chance to finish whatever work they may
        // be doing.
        LOG(("waiting 1 second before exiting...\n"));
        PR_Sleep(PR_SecondsToInterval(1));
    } // this scopes the nsCOMPtrs
    // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
    rv = NS_ShutdownXPCOM(nsnull);
    NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
    return NS_OK;
}

Here is the call graph for this function:

static NS_DEFINE_CID ( kSocketTransportServiceCID  ,
NS_SOCKETTRANSPORTSERVICE_CID   
) [static]

create transport, open streams, and close

static void PostDoneEvent ( ) [static]

Definition at line 97 of file TestSocketTransport.cpp.

{
    LOG(("PostDoneEvent\n"));

    PLEvent *ev = new PLEvent();

    PL_InitEvent(ev, nsnull, 
            DoneEvent_Handler,
            DoneEvent_Cleanup);

    gEventQ->PostEvent(ev);
}

Here is the call graph for this function:

Here is the caller graph for this function:

PR_Sleep ( PR_SecondsToInterval(1)  )
static nsresult RunTest ( nsISocketTransportService sts,
const char *  host,
int  port,
const char *  path,
PRUint32  inFlags,
PRUint32  outFlags 
) [static]

asynchronously read socket stream

Definition at line 232 of file TestSocketTransport.cpp.

{
    nsresult rv;

    LOG(("RunTest\n"));

    nsCOMPtr<nsISocketTransport> transport;
    rv = sts->CreateTransport(nsnull, 0,
                              nsDependentCString(host), port, nsnull,
                              getter_AddRefs(transport));
    if (NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIInputStream> in;
    rv = transport->OpenInputStream(inFlags, 0, 0, getter_AddRefs(in));
    nsCOMPtr<nsIAsyncInputStream> asyncIn = do_QueryInterface(in, &rv);
    if (NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIOutputStream> out;
    rv = transport->OpenOutputStream(outFlags, 0, 0, getter_AddRefs(out));
    nsCOMPtr<nsIAsyncOutputStream> asyncOut = do_QueryInterface(out, &rv);
    if (NS_FAILED(rv)) return rv;

    MyHandler *handler = new MyHandler(path, asyncIn, asyncOut);
    if (handler == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;
    NS_ADDREF(handler);

    rv = asyncOut->AsyncWait(handler, 0, 0, nsnull);

    if (NS_SUCCEEDED(rv)) {
        PLEvent* event;
        gDone = PR_FALSE;
        while (!gDone) {
            rv = gEventQ->WaitForEvent(&event);
            if (NS_FAILED(rv)) return rv;
            rv = gEventQ->HandleEvent(event);
            if (NS_FAILED(rv)) return rv;
        }
    }

    NS_RELEASE(handler);

    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 212 of file TestSocketTransport.cpp.

Definition at line 217 of file TestSocketTransport.cpp.

Definition at line 78 of file TestSocketTransport.cpp.

Definition at line 79 of file TestSocketTransport.cpp.

const char* host

Definition at line 197 of file TestSocketTransport.cpp.

Definition at line 210 of file TestSocketTransport.cpp.

Definition at line 197 of file TestSocketTransport.cpp.

Definition at line 224 of file TestSocketTransport.cpp.

Definition at line 215 of file TestSocketTransport.cpp.

const char int port

Definition at line 197 of file TestSocketTransport.cpp.

rv
Initial value:

Definition at line 197 of file TestSocketTransport.cpp.

Definition at line 204 of file TestSocketTransport.cpp.