gecko-dev/netwerk/test/TestFileTransport.cpp

298 lines
9.1 KiB
C++
Raw Normal View History

1999-09-16 02:33:31 +00:00
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* The contents of this file are subject to the Netscape Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/NPL/
1999-09-16 02:33:31 +00:00
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
1999-09-16 02:33:31 +00:00
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Netscape
1999-09-16 02:33:31 +00:00
* Communications Corporation. Portions created by Netscape are
* Copyright (C) 1998 Netscape Communications Corporation. All
* Rights Reserved.
*
* Contributor(s):
1999-09-16 02:33:31 +00:00
*/
#include "nsIFileTransportService.h"
#include "nsIChannel.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsCOMPtr.h"
#include "nsMemory.h"
1999-09-16 02:33:31 +00:00
#include "nsString.h"
2000-01-24 21:28:28 +00:00
#include "nsIFileStreams.h"
1999-09-16 02:33:31 +00:00
#include "nsIStreamListener.h"
#include "nsIEventQueueService.h"
#include "nsIEventQueue.h"
static NS_DEFINE_CID(kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID);
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
1999-11-23 07:03:04 +00:00
static NS_DEFINE_CID(kEventQueueCID, NS_EVENTQUEUE_CID);
1999-09-16 02:33:31 +00:00
PRBool gDone = PR_FALSE;
nsIEventQueue* gEventQ = nsnull;
////////////////////////////////////////////////////////////////////////////////
class MyListener : public nsIStreamListener {
public:
NS_DECL_ISUPPORTS
NS_IMETHOD OnStartRequest(nsIChannel *channel, nsISupports *ctxt) {
printf("starting\n");
return NS_OK;
}
NS_IMETHOD OnStopRequest(nsIChannel *channel, nsISupports *ctxt,
nsresult status, const PRUnichar *errorMsg) {
printf("ending status=%0x total=%d\n", status, mTotal);
if (--mStopCount == 0)
gDone = PR_TRUE;
1999-09-16 02:33:31 +00:00
return NS_OK;
}
NS_IMETHOD OnDataAvailable(nsIChannel *channel, nsISupports *ctxt,
nsIInputStream *inStr, PRUint32 sourceOffset,
PRUint32 count) {
printf("receiving %d bytes\n", count);
char buf[256];
PRUint32 writeCount;
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");
nsresult 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;
}
return NS_OK;
}
MyListener(PRUint32 stopCount = 1) : mTotal(0), mStopCount(stopCount) {
1999-09-16 02:33:31 +00:00
NS_INIT_REFCNT();
}
nsresult Init(const char* origFile) {
1999-09-16 02:33:31 +00:00
nsresult rv;
2000-01-24 21:28:28 +00:00
nsCOMPtr<nsILocalFile> file;
rv = NS_NewLocalFile(origFile, PR_FALSE, getter_AddRefs(file));
if (NS_FAILED(rv)) return rv;
char* name;
rv = file->GetLeafName(&name);
if (NS_FAILED(rv)) return rv;
nsCAutoString str(name);
nsMemory::Free(name);
str.Append(".bak");
rv = file->SetLeafName(str);
if (NS_FAILED(rv)) return rv;
rv = NS_NewLocalFileOutputStream(getter_AddRefs(mOut),
file,
PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
0664);
2000-01-24 21:28:28 +00:00
return rv;
1999-09-16 02:33:31 +00:00
}
virtual ~MyListener() {
nsresult rv = mOut->Close();
NS_ASSERTION(NS_SUCCEEDED(rv), "Close failed");
}
protected:
nsCOMPtr<nsIOutputStream> mOut;
PRUint32 mTotal;
PRUint32 mStopCount;
1999-09-16 02:33:31 +00:00
};
NS_IMPL_THREADSAFE_ISUPPORTS2(MyListener, nsIStreamListener, nsIStreamObserver);
1999-09-16 02:33:31 +00:00
////////////////////////////////////////////////////////////////////////////////
nsresult
TestAsyncRead(const char* fileName, PRUint32 offset, PRInt32 length)
{
nsresult rv;
NS_WITH_SERVICE(nsIFileTransportService, fts, kFileTransportServiceCID, &rv);
if (NS_FAILED(rv)) return rv;
nsIChannel* fileTrans;
2000-01-24 21:28:28 +00:00
nsCOMPtr<nsILocalFile> file;
rv = NS_NewLocalFile(fileName, PR_FALSE, getter_AddRefs(file));
2000-01-24 21:28:28 +00:00
if (NS_FAILED(rv)) return rv;
rv = fts->CreateTransport(file, PR_RDONLY, 0, &fileTrans);
1999-09-16 02:33:31 +00:00
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);
1999-09-16 02:33:31 +00:00
if (NS_FAILED(rv)) return rv;
gDone = PR_FALSE;
rv = fileTrans->SetTransferOffset(offset);
if (NS_FAILED(rv)) return rv;
rv = fileTrans->SetTransferCount(length);
if (NS_FAILED(rv)) return rv;
rv = fileTrans->AsyncRead(listener, nsnull);
1999-09-16 02:33:31 +00:00
if (NS_FAILED(rv)) return rv;
while (!gDone) {
PLEvent* event;
rv = gEventQ->GetEvent(&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;
}
////////////////////////////////////////////////////////////////////////////////
nsresult
TestAsyncWrite(const char* fileName, PRUint32 offset, PRInt32 length)
{
nsresult rv;
NS_WITH_SERVICE(nsIFileTransportService, fts, kFileTransportServiceCID, &rv);
if (NS_FAILED(rv)) return rv;
nsCAutoString outFile(fileName);
outFile.Append(".out");
1999-09-16 02:33:31 +00:00
nsIChannel* fileTrans;
2000-01-24 21:28:28 +00:00
nsCOMPtr<nsILocalFile> file;
rv = NS_NewLocalFile(outFile, PR_FALSE, getter_AddRefs(file));
2000-01-24 21:28:28 +00:00
if (NS_FAILED(rv)) return rv;
rv = fts->CreateTransport(file,
PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
0664, &fileTrans);
1999-09-16 02:33:31 +00:00
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);
1999-09-16 02:33:31 +00:00
if (NS_FAILED(rv)) return rv;
2000-01-24 21:28:28 +00:00
nsCOMPtr<nsILocalFile> f;
rv = NS_NewLocalFile(fileName, PR_FALSE, getter_AddRefs(f));
2000-01-24 21:28:28 +00:00
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIInputStream> inStr;
rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), f);
1999-09-16 02:33:31 +00:00
if (NS_FAILED(rv)) return rv;
gDone = PR_FALSE;
rv = fileTrans->SetTransferOffset(offset);
if (NS_FAILED(rv)) return rv;
rv = fileTrans->SetTransferCount(length);
if (NS_FAILED(rv)) return rv;
rv = fileTrans->AsyncWrite(inStr, listener, nsnull);
1999-09-16 02:33:31 +00:00
if (NS_FAILED(rv)) return rv;
while (!gDone) {
PLEvent* event;
rv = gEventQ->GetEvent(&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;
}
////////////////////////////////////////////////////////////////////////////////
class MyOpenObserver : public nsIStreamObserver
{
public:
NS_DECL_ISUPPORTS
NS_IMETHOD OnStartRequest(nsIChannel *channel, nsISupports *ctxt) {
nsresult rv;
char* contentType;
rv = channel->GetContentType(&contentType);
if (NS_FAILED(rv)) return rv;
PRInt32 length;
rv = channel->GetContentLength(&length);
if (NS_FAILED(rv)) return rv;
printf("stream opened: content type = %s, length = %d\n",
contentType, length);
nsCRT::free(contentType);
return NS_OK;
}
NS_IMETHOD OnStopRequest(nsIChannel *channel, nsISupports *ctxt,
nsresult status, const PRUnichar *errorMsg) {
printf("stream closed: status %x\n", status);
return NS_OK;
}
MyOpenObserver() { NS_INIT_REFCNT(); }
virtual ~MyOpenObserver() {}
};
NS_IMPL_ISUPPORTS1(MyOpenObserver, nsIStreamObserver);
////////////////////////////////////////////////////////////////////////////////
1999-09-16 02:33:31 +00:00
nsresult
NS_AutoregisterComponents()
{
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
return rv;
}
int
main(int argc, char* argv[])
{
nsresult rv;
if (argc < 2) {
printf("usage: %s <file-to-read>\n", argv[0]);
return -1;
}
char* fileName = argv[1];
rv = NS_AutoregisterComponents();
if (NS_FAILED(rv)) return rv;
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
if (NS_FAILED(rv)) return rv;
rv = eventQService->CreateThreadEventQueue();
if (NS_FAILED(rv)) return rv;
1999-11-30 00:32:43 +00:00
rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
if (NS_FAILED(rv)) return rv;
1999-09-16 02:33:31 +00:00
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);
1999-09-16 02:33:31 +00:00
return NS_OK;
}