Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
TestFileTransport.cpp File Reference
#include "nsIComponentRegistrar.h"
#include "nsIFileTransportService.h"
#include "nsITransport.h"
#include "nsIProgressEventSink.h"
#include "nsIInterfaceRequestor.h"
#include "nsIInterfaceRequestorUtils.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 "prlog.h"

Go to the source code of this file.

Classes

class  MyProgressEventSink

Defines

#define PRINTF(args)
#define PRINTF(args)   printf args

Functions

static NS_DEFINE_CID (kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID)
static NS_DEFINE_CID (kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID)
 NS_IMPL_THREADSAFE_ISUPPORTS2 (MyProgressEventSink, nsIProgressEventSink, nsIInterfaceRequestor) class MyListener
nsresult TestAsyncRead (const char *fileName, PRUint32 offset, PRInt32 length)
nsresult TestAsyncWrite (const char *fileName, PRUint32 offset, PRInt32 length)
int main (int argc, char *argv[])

Variables

PRBool gDone = PR_FALSE
nsIEventQueuegEventQ = nsnull

Define Documentation

#define PRINTF (   args)

Definition at line 68 of file TestFileTransport.cpp.

#define PRINTF (   args)    printf args

Definition at line 68 of file TestFileTransport.cpp.


Function Documentation

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

Definition at line 342 of file TestFileTransport.cpp.

{
    nsresult rv;

#if defined(PR_LOGGING)
    gTestFileTransportLog = PR_NewLogModule("TestFileTransport");
#endif

    if (argc < 2) {
        printf("usage: %s <file-to-read>\n", argv[0]);
        return -1;
    }
    char* fileName = argv[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);

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

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

        rv = TestAsyncRead(fileName, 0, -1);
        NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");

        rv = TestAsyncWrite(fileName, 0, -1);
        NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncWrite failed");

        rv = TestAsyncRead(fileName, 10, 100);
        NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");

        NS_RELEASE(gEventQ);
    } // 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 ( kFileTransportServiceCID  ,
NS_FILETRANSPORTSERVICE_CID   
) [static]

Definition at line 105 of file TestFileTransport.cpp.

                 : public nsIStreamListener {
public:
    NS_DECL_ISUPPORTS

    NS_IMETHOD OnStartRequest(nsIRequest *request, nsISupports *ctxt) {
        PRINTF(("starting\n"));
        return NS_OK;
    }

    NS_IMETHOD OnStopRequest(nsIRequest *request, nsISupports *ctxt, 
                             nsresult aStatus) {
        PRINTF(("ending status=%0x total=%d\n", aStatus, mTotal));
        if (--mStopCount == 0)
            gDone = PR_TRUE;
        return NS_OK;
    }

    NS_IMETHOD OnDataAvailable(nsIRequest *request, nsISupports *ctxt, 
                               nsIInputStream *inStr, PRUint32 sourceOffset, 
                               PRUint32 count) {
        PRINTF(("receiving %d bytes\n", count));
        char buf[256];
        PRUint32 writeCount, givenCount=count;
        nsresult rv;
        while (count > 0) {
            PRUint32 amt = PR_MIN(count, 256);
            PRUint32 readCount;
            rv = inStr->Read(buf, amt, &readCount);
            if (NS_FAILED(rv)) return rv;
            NS_ASSERTION(readCount != 0, "premature EOF");
            rv = mOut->Write(buf, readCount, &writeCount);
            if (NS_FAILED(rv)) return rv;
            NS_ASSERTION(writeCount == readCount, "failed to write all the data");
            count -= readCount;
            mTotal += readCount;
        }
        PRINTF(("done reading data [read %u bytes]\n", givenCount - count));
        //PRINTF("sleeping for 100 ticks\n");FLUSH();
        //PR_Sleep(100);
        return NS_OK;
    }

    MyListener(PRUint32 stopCount = 1) : mTotal(0), mStopCount(stopCount) {
    }

    nsresult Init(const char* origFile) {
        nsresult rv;
        nsCOMPtr<nsILocalFile> file;
        rv = NS_NewNativeLocalFile(nsDependentCString(origFile), PR_FALSE, getter_AddRefs(file));
        if (NS_FAILED(rv)) return rv;
        nsCAutoString name;
        rv = file->GetNativeLeafName(name);
        if (NS_FAILED(rv)) return rv;
        name.AppendLiteral(".bak");
        rv = file->SetNativeLeafName(name);
        if (NS_FAILED(rv)) return rv;
        rv = NS_NewLocalFileOutputStream(getter_AddRefs(mOut),
                                         file, 
                                         PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
                                         0664);
        return rv;
    }

    virtual ~MyListener() {
        nsresult rv = mOut->Close();
        NS_ASSERTION(NS_SUCCEEDED(rv), "Close failed");
    }

protected:
    nsCOMPtr<nsIOutputStream> mOut;
    PRUint32 mTotal;
    PRUint32 mStopCount;
};

Here is the call graph for this function:

nsresult TestAsyncRead ( const char *  fileName,
PRUint32  offset,
PRInt32  length 
)

Definition at line 190 of file TestFileTransport.cpp.

{
    nsresult rv;

    PRINTF(("TestAsyncRead\n"));

    nsCOMPtr<nsIFileTransportService> fts = 
             do_GetService(kFileTransportServiceCID, &rv);
    if (NS_FAILED(rv)) return rv;

    nsITransport* fileTrans;
    nsCOMPtr<nsILocalFile> file;
    rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(file));
    if (NS_FAILED(rv)) return rv;
    rv = fts->CreateTransport(file, PR_RDONLY, 0, PR_TRUE, &fileTrans);
    if (NS_FAILED(rv)) return rv;

    MyListener* listener = new MyListener();
    if (listener == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;
    NS_ADDREF(listener);
    rv = listener->Init(fileName);
    if (NS_FAILED(rv)) return rv;

    MyProgressEventSink* progressSink = new MyProgressEventSink();
    if (progressSink == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;
    NS_ADDREF(progressSink);
    rv = fileTrans->SetNotificationCallbacks(progressSink, PR_FALSE);
    if (NS_FAILED(rv)) return rv;

    gDone = PR_FALSE;
    nsCOMPtr<nsIRequest> request;
    rv = fileTrans->AsyncRead(listener, nsnull, offset, length, 0, getter_AddRefs(request));
    if (NS_FAILED(rv)) return rv;

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

    NS_RELEASE(listener);
    NS_RELEASE(fileTrans);
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult TestAsyncWrite ( const char *  fileName,
PRUint32  offset,
PRInt32  length 
)

Definition at line 242 of file TestFileTransport.cpp.

{
    nsresult rv;

    PRINTF(("TestAsyncWrite\n"));

    nsCOMPtr<nsIFileTransportService> fts = 
             do_GetService(kFileTransportServiceCID, &rv);
    if (NS_FAILED(rv)) return rv;

    nsCAutoString outFile(fileName);
    outFile.AppendLiteral(".out");
    nsITransport* fileTrans;
    nsCOMPtr<nsILocalFile> file;
    rv = NS_NewNativeLocalFile(outFile, PR_FALSE, getter_AddRefs(file));
    if (NS_FAILED(rv)) return rv;
    rv = fts->CreateTransport(file,
                              PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
                              0664, PR_TRUE, &fileTrans);
    if (NS_FAILED(rv)) return rv;

    MyListener* listener = new MyListener();
    if (listener == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;
    NS_ADDREF(listener);
    rv = listener->Init(outFile.get());
    if (NS_FAILED(rv)) return rv;

    MyProgressEventSink* progressSink = new MyProgressEventSink();
    if (progressSink == nsnull)
        return NS_ERROR_OUT_OF_MEMORY;
    NS_ADDREF(progressSink);
    rv = fileTrans->SetNotificationCallbacks(progressSink, PR_FALSE);
    if (NS_FAILED(rv)) return rv;

    nsCOMPtr<nsILocalFile> f;
    rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(f));
    if (NS_FAILED(rv)) return rv;
    nsCOMPtr<nsIInputStream> inStr;
    rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), f);
    if (NS_FAILED(rv)) return rv;

    gDone = PR_FALSE;
    nsCOMPtr<nsIRequest> request;
    rv = NS_AsyncWriteFromStream(getter_AddRefs(request), fileTrans, inStr, offset, length, 0, listener);
    if (NS_FAILED(rv)) return rv;

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

    NS_RELEASE(listener);
    NS_RELEASE(fileTrans);
    return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 75 of file TestFileTransport.cpp.

Definition at line 76 of file TestFileTransport.cpp.