1999-04-05 21:06:07 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* 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-04-05 21:06:07 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +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-04-05 21:06:07 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-04-05 21:06:07 +00:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 03:40:37 +00:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
1999-04-05 21:06:07 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsIFileTransportService.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIInputStream.h"
|
1999-05-05 22:41:44 +00:00
|
|
|
#include "nsIEventQueue.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
1999-04-05 21:06:07 +00:00
|
|
|
#include "prinrval.h"
|
1999-05-04 19:33:37 +00:00
|
|
|
#include "prmon.h"
|
1999-06-02 11:42:43 +00:00
|
|
|
#include "prcmon.h"
|
1999-04-06 06:11:23 +00:00
|
|
|
#include "prio.h"
|
2000-01-24 21:28:28 +00:00
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsNetUtil.h"
|
1999-08-27 10:33:37 +00:00
|
|
|
#include "nsIPipe.h"
|
|
|
|
#include "nsIBufferOutputStream.h"
|
1999-06-08 20:57:32 +00:00
|
|
|
#include "nsIBufferInputStream.h"
|
1999-09-15 09:49:18 +00:00
|
|
|
#include "nsIRunnable.h"
|
1999-04-06 06:11:23 +00:00
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsISupportsArray.h"
|
1999-06-07 21:33:30 +00:00
|
|
|
#include "nsIChannel.h"
|
1999-08-27 10:33:37 +00:00
|
|
|
#include "nsCOMPtr.h"
|
1999-04-05 21:06:07 +00:00
|
|
|
#include <stdio.h>
|
2000-01-24 21:28:28 +00:00
|
|
|
#include "nsInt64.h"
|
1999-04-05 21:06:07 +00:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID);
|
1999-06-02 11:42:43 +00:00
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
1999-04-05 21:06:07 +00:00
|
|
|
|
|
|
|
PRIntervalTime gDuration = 0;
|
1999-04-06 06:11:23 +00:00
|
|
|
PRUint32 gVolume = 0;
|
1999-04-05 21:06:07 +00:00
|
|
|
|
|
|
|
class nsReader : public nsIRunnable, public nsIStreamListener {
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
1999-05-09 19:18:25 +00:00
|
|
|
printf("waiting\n");
|
1999-05-04 19:33:37 +00:00
|
|
|
if (!mMonitor)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
PR_EnterMonitor(mMonitor);
|
1999-06-02 11:42:43 +00:00
|
|
|
if (mEventQueue == nsnull)
|
1999-04-05 21:06:07 +00:00
|
|
|
PR_CWait(this, PR_INTERVAL_NO_TIMEOUT);
|
1999-06-02 11:42:43 +00:00
|
|
|
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_ExitMonitor(mMonitor);
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-05-09 19:18:25 +00:00
|
|
|
printf("running\n");
|
1999-05-05 22:41:44 +00:00
|
|
|
|
1999-05-06 19:47:55 +00:00
|
|
|
mEventQueue->EventLoop();
|
1999-05-05 22:41:44 +00:00
|
|
|
|
1999-05-09 19:18:25 +00:00
|
|
|
printf("quitting\n");
|
1999-04-05 21:06:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
nsReader()
|
1999-10-01 23:32:12 +00:00
|
|
|
: mEventQueue(nsnull), mStartTime(0), mThread(nsnull), mBytesRead(0)
|
1999-04-06 06:11:23 +00:00
|
|
|
{
|
1999-04-05 21:06:07 +00:00
|
|
|
NS_INIT_REFCNT();
|
1999-05-04 19:33:37 +00:00
|
|
|
mMonitor = PR_NewMonitor();
|
1999-04-05 21:06:07 +00:00
|
|
|
}
|
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
virtual ~nsReader() {
|
|
|
|
NS_IF_RELEASE(mThread);
|
1999-05-05 22:41:44 +00:00
|
|
|
NS_IF_RELEASE(mEventQueue);
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_DestroyMonitor(mMonitor);
|
1999-04-06 06:11:23 +00:00
|
|
|
}
|
|
|
|
|
1999-04-05 21:06:07 +00:00
|
|
|
nsresult Init(nsIThread* thread) {
|
1999-05-05 22:41:44 +00:00
|
|
|
nsresult rv;
|
1999-04-06 06:11:23 +00:00
|
|
|
mThread = thread;
|
|
|
|
NS_ADDREF(mThread);
|
1999-04-05 21:06:07 +00:00
|
|
|
PRThread* prthread;
|
|
|
|
thread->GetPRThread(&prthread);
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_EnterMonitor(mMonitor);
|
1999-05-09 19:18:25 +00:00
|
|
|
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
|
1999-05-05 22:41:44 +00:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-06-02 11:42:43 +00:00
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-11-30 00:32:43 +00:00
|
|
|
rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &mEventQueue);
|
1999-05-05 22:41:44 +00:00
|
|
|
}
|
1999-06-02 11:42:43 +00:00
|
|
|
|
1999-05-05 22:41:44 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-04-05 21:06:07 +00:00
|
|
|
// wake up event loop
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_Notify(mMonitor);
|
|
|
|
PR_ExitMonitor(mMonitor);
|
1999-04-05 21:06:07 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-07 08:08:40 +00:00
|
|
|
NS_IMETHOD OnStartRequest(nsIChannel* channel,
|
|
|
|
nsISupports* context) {
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_EnterMonitor(mMonitor);
|
1999-05-09 19:18:25 +00:00
|
|
|
printf("start binding\n");
|
1999-04-05 21:06:07 +00:00
|
|
|
mStartTime = PR_IntervalNow();
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_ExitMonitor(mMonitor);
|
1999-04-05 21:06:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-07 08:08:40 +00:00
|
|
|
NS_IMETHOD OnDataAvailable(nsIChannel* channel,
|
|
|
|
nsISupports* context,
|
1999-06-28 20:37:10 +00:00
|
|
|
nsIInputStream *aIStream,
|
1999-04-22 08:47:06 +00:00
|
|
|
PRUint32 aSourceOffset,
|
1999-04-05 21:06:07 +00:00
|
|
|
PRUint32 aLength) {
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_EnterMonitor(mMonitor);
|
1999-04-05 21:06:07 +00:00
|
|
|
char buf[1025];
|
|
|
|
while (aLength > 0) {
|
|
|
|
PRUint32 amt;
|
1999-07-13 02:37:08 +00:00
|
|
|
/*nsresult rv = */aIStream->Read(buf, 1024, &amt);
|
1999-09-09 22:05:05 +00:00
|
|
|
if (amt == 0) break;
|
1999-05-09 19:18:25 +00:00
|
|
|
buf[amt] = '\0';
|
|
|
|
printf(buf);
|
1999-04-05 21:06:07 +00:00
|
|
|
aLength -= amt;
|
1999-10-01 23:32:12 +00:00
|
|
|
mBytesRead += amt;
|
1999-04-06 06:11:23 +00:00
|
|
|
gVolume += amt;
|
1999-04-05 21:06:07 +00:00
|
|
|
}
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_ExitMonitor(mMonitor);
|
1999-04-05 21:06:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-07 08:08:40 +00:00
|
|
|
NS_IMETHOD OnStopRequest(nsIChannel* channel,
|
|
|
|
nsISupports* context,
|
1999-04-05 21:06:07 +00:00
|
|
|
nsresult aStatus,
|
1999-06-11 15:50:14 +00:00
|
|
|
const PRUnichar* aMsg) {
|
1999-04-06 06:11:23 +00:00
|
|
|
nsresult rv;
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_EnterMonitor(mMonitor);
|
1999-06-11 15:50:14 +00:00
|
|
|
PRIntervalTime endTime = PR_IntervalNow();
|
|
|
|
gDuration += (endTime - mStartTime);
|
|
|
|
printf("stop binding, %d\n", aStatus);
|
1999-10-01 23:32:12 +00:00
|
|
|
if (NS_FAILED(aStatus)) printf("channel failed.\n");
|
|
|
|
printf("read %d bytes\n", mBytesRead);
|
1999-05-04 19:33:37 +00:00
|
|
|
PR_ExitMonitor(mMonitor);
|
1999-04-06 06:11:23 +00:00
|
|
|
|
|
|
|
// get me out of my event loop
|
|
|
|
rv = mThread->Interrupt();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return rv;
|
1999-04-05 21:06:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
1999-05-05 22:41:44 +00:00
|
|
|
nsIEventQueue* mEventQueue;
|
1999-04-05 21:06:07 +00:00
|
|
|
PRIntervalTime mStartTime;
|
1999-04-06 06:11:23 +00:00
|
|
|
nsIThread* mThread;
|
1999-10-01 23:32:12 +00:00
|
|
|
PRUint32 mBytesRead;
|
1999-05-04 19:33:37 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
PRMonitor* mMonitor;
|
1999-04-05 21:06:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsReader);
|
|
|
|
NS_IMPL_RELEASE(nsReader);
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsReader::QueryInterface(const nsIID& aIID, void* *aInstancePtr)
|
|
|
|
{
|
|
|
|
if (NULL == aInstancePtr) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
1999-10-01 23:32:12 +00:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIRunnable)) ||
|
|
|
|
aIID.Equals(NS_GET_IID(nsISupports))) {
|
1999-04-05 21:06:07 +00:00
|
|
|
*aInstancePtr = NS_STATIC_CAST(nsIRunnable*, this);
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-10-01 23:32:12 +00:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIStreamListener))) {
|
1999-04-05 21:06:07 +00:00
|
|
|
*aInstancePtr = NS_STATIC_CAST(nsIStreamListener*, this);
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
Simulated_nsFileTransport_Run(nsReader* reader, const char* path)
|
|
|
|
{
|
|
|
|
// duplicate the work of nsFileTransport::Run here
|
|
|
|
|
|
|
|
#define NS_FILE_TRANSPORT_BUFFER_SIZE (4*1024)
|
|
|
|
|
|
|
|
nsresult rv;
|
2000-01-24 21:28:28 +00:00
|
|
|
nsCOMPtr<nsIInputStream> fileStr;
|
1999-06-08 20:57:32 +00:00
|
|
|
nsIBufferInputStream* bufStr = nsnull;
|
1999-04-22 08:47:06 +00:00
|
|
|
PRUint32 sourceOffset = 0;
|
1999-08-27 10:33:37 +00:00
|
|
|
nsCOMPtr<nsIBufferOutputStream> out;
|
2000-01-24 21:28:28 +00:00
|
|
|
nsCOMPtr<nsILocalFile> file;
|
1999-04-06 06:11:23 +00:00
|
|
|
|
1999-07-07 08:08:40 +00:00
|
|
|
rv = reader->OnStartRequest(nsnull, nsnull);
|
1999-04-06 06:11:23 +00:00
|
|
|
if (NS_FAILED(rv)) goto done; // XXX should this abort the transfer?
|
|
|
|
|
2000-01-24 21:28:28 +00:00
|
|
|
rv = NS_NewLocalFile(path, getter_AddRefs(file));
|
1999-04-06 06:11:23 +00:00
|
|
|
if (NS_FAILED(rv)) goto done;
|
|
|
|
|
2000-01-24 21:28:28 +00:00
|
|
|
rv = NS_NewFileInputStream(file, getter_AddRefs(fileStr));
|
1999-04-06 06:11:23 +00:00
|
|
|
if (NS_FAILED(rv)) goto done;
|
|
|
|
|
1999-08-27 10:33:37 +00:00
|
|
|
rv = NS_NewPipe(&bufStr, getter_AddRefs(out), nsnull,
|
|
|
|
NS_FILE_TRANSPORT_BUFFER_SIZE,
|
|
|
|
NS_FILE_TRANSPORT_BUFFER_SIZE);
|
|
|
|
if (NS_FAILED(rv)) goto done;
|
1999-04-06 06:11:23 +00:00
|
|
|
|
1999-06-11 15:50:14 +00:00
|
|
|
/*
|
1999-06-08 12:28:27 +00:00
|
|
|
if ( spec.GetFileSize() == 0) goto done;
|
1999-06-11 15:50:14 +00:00
|
|
|
*/
|
1999-06-08 12:28:27 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
while (PR_TRUE) {
|
|
|
|
PRUint32 amt;
|
1999-06-11 15:50:14 +00:00
|
|
|
/* id'l change to FillFrom... */
|
2000-01-24 21:28:28 +00:00
|
|
|
PRInt64 size;
|
|
|
|
rv = file->GetFileSize(&size);
|
|
|
|
if (NS_FAILED(rv)) break;
|
|
|
|
rv = out->WriteFrom(fileStr, nsInt64(size), &amt);
|
1999-09-09 22:05:05 +00:00
|
|
|
if (NS_FAILED(rv) || amt == 0) break;
|
1999-04-06 06:11:23 +00:00
|
|
|
|
1999-07-07 08:08:40 +00:00
|
|
|
rv = reader->OnDataAvailable(nsnull, nsnull, bufStr, sourceOffset, amt);
|
1999-04-06 06:11:23 +00:00
|
|
|
if (NS_FAILED(rv)) break;
|
1999-04-22 08:47:06 +00:00
|
|
|
|
|
|
|
sourceOffset += amt;
|
1999-04-06 06:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
|
|
|
NS_IF_RELEASE(bufStr);
|
|
|
|
|
1999-07-07 08:08:40 +00:00
|
|
|
rv = reader->OnStopRequest(nsnull, nsnull, rv, nsnull);
|
1999-04-06 06:11:23 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-04-05 21:06:07 +00:00
|
|
|
void
|
1999-04-06 06:11:23 +00:00
|
|
|
SerialReadTest(char* dirName)
|
1999-04-05 21:06:07 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-04-06 06:11:23 +00:00
|
|
|
PRStatus status;
|
|
|
|
|
|
|
|
PRDir* dir = PR_OpenDir(dirName);
|
|
|
|
NS_ASSERTION(dir, "bad dir");
|
|
|
|
|
|
|
|
nsISupportsArray* threads;
|
|
|
|
rv = NS_NewISupportsArray(&threads);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewISupportsArray failed");
|
|
|
|
|
1999-04-05 21:06:07 +00:00
|
|
|
PRIntervalTime startTime = PR_IntervalNow();
|
1999-04-06 06:11:23 +00:00
|
|
|
PRDirEntry* entry;
|
|
|
|
while ((entry = PR_ReadDir(dir, PR_SKIP_BOTH)) != nsnull) {
|
|
|
|
nsFileSpec spec(dirName);
|
|
|
|
spec += entry->name;
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
nsReader* reader = new nsReader();
|
|
|
|
NS_ASSERTION(reader, "out of memory");
|
|
|
|
NS_ADDREF(reader);
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
nsIThread* readerThread;
|
1999-10-01 23:32:12 +00:00
|
|
|
rv = NS_NewThread(&readerThread, reader, 0, PR_JOINABLE_THREAD);
|
1999-04-06 06:11:23 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "new thread failed");
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
rv = reader->Init(readerThread);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "init failed");
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
nsIStreamListener* listener;
|
1999-10-01 23:32:12 +00:00
|
|
|
reader->QueryInterface(NS_GET_IID(nsIStreamListener), (void**)&listener);
|
1999-04-06 06:11:23 +00:00
|
|
|
NS_ASSERTION(listener, "QI failed");
|
|
|
|
|
|
|
|
rv = Simulated_nsFileTransport_Run(reader, spec);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Simulated_nsFileTransport_Run failed");
|
|
|
|
|
|
|
|
// the reader thread will hang on to these objects until it quits
|
|
|
|
NS_RELEASE(listener);
|
|
|
|
NS_RELEASE(readerThread);
|
|
|
|
NS_RELEASE(reader);
|
1999-04-05 21:06:07 +00:00
|
|
|
}
|
|
|
|
PRIntervalTime endTime = PR_IntervalNow();
|
1999-04-06 06:11:23 +00:00
|
|
|
printf("duration %d ms, volume %d\n",
|
|
|
|
PR_IntervalToMilliseconds(endTime - startTime),
|
|
|
|
gVolume);
|
|
|
|
gVolume = 0;
|
|
|
|
|
|
|
|
// now that we've forked all the async requests, wait until they're done
|
1999-05-14 17:25:27 +00:00
|
|
|
PRUint32 threadCount;
|
|
|
|
rv = threads->Count(&threadCount);
|
1999-04-06 06:11:23 +00:00
|
|
|
for (PRUint32 i = 0; i < threadCount; i++) {
|
1999-06-02 11:42:43 +00:00
|
|
|
nsIThread* thread = (nsIThread*)threads->ElementAt(i);
|
1999-04-06 06:11:23 +00:00
|
|
|
thread->Join();
|
|
|
|
NS_RELEASE(thread);
|
|
|
|
}
|
|
|
|
NS_RELEASE(threads);
|
|
|
|
|
|
|
|
status = PR_CloseDir(dir);
|
|
|
|
NS_ASSERTION(status == PR_SUCCESS, "can't close dir");
|
1999-04-05 21:06:07 +00:00
|
|
|
}
|
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
void
|
|
|
|
ParallelReadTest(char* dirName, nsIFileTransportService* fts)
|
1999-04-05 21:06:07 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-04-06 06:11:23 +00:00
|
|
|
PRStatus status;
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
PRDir* dir = PR_OpenDir(dirName);
|
|
|
|
NS_ASSERTION(dir, "bad dir");
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
nsISupportsArray* threads;
|
|
|
|
rv = NS_NewISupportsArray(&threads);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewISupportsArray failed");
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
PRDirEntry* entry;
|
|
|
|
while ((entry = PR_ReadDir(dir, PR_SKIP_BOTH)) != nsnull) {
|
2000-01-24 21:28:28 +00:00
|
|
|
nsCOMPtr<nsILocalFile> file;
|
|
|
|
rv = NS_NewLocalFile(dirName, getter_AddRefs(file));
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_NewLocalFile failed");
|
|
|
|
|
|
|
|
rv = file->Append(entry->name);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "AppendPath failed");
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
nsReader* reader = new nsReader();
|
|
|
|
NS_ASSERTION(reader, "out of memory");
|
|
|
|
NS_ADDREF(reader);
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
nsIThread* readerThread;
|
1999-06-02 11:42:43 +00:00
|
|
|
|
1999-10-01 23:32:12 +00:00
|
|
|
rv = NS_NewThread(&readerThread, reader, 0, PR_JOINABLE_THREAD);
|
1999-06-02 11:42:43 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "new thread failed");
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
rv = reader->Init(readerThread);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "init failed");
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
nsIStreamListener* listener;
|
1999-10-01 23:32:12 +00:00
|
|
|
reader->QueryInterface(NS_GET_IID(nsIStreamListener), (void**)&listener);
|
1999-04-06 06:11:23 +00:00
|
|
|
NS_ASSERTION(listener, "QI failed");
|
|
|
|
|
1999-06-07 21:33:30 +00:00
|
|
|
nsIChannel* trans;
|
2000-01-24 21:28:28 +00:00
|
|
|
rv = fts->CreateTransport(file, PR_RDONLY, "load", 0, 0, &trans);
|
1999-04-14 08:13:35 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "create failed");
|
|
|
|
|
1999-07-20 08:46:33 +00:00
|
|
|
rv = trans->AsyncRead(0, -1, nsnull, listener);
|
1999-04-06 06:11:23 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "AsyncRead failed");
|
|
|
|
|
|
|
|
// the reader thread will hang on to these objects until it quits
|
|
|
|
NS_RELEASE(trans);
|
|
|
|
NS_RELEASE(listener);
|
|
|
|
NS_RELEASE(reader);
|
1999-09-09 22:05:05 +00:00
|
|
|
rv = threads->AppendElement(readerThread) ? NS_OK : NS_ERROR_FAILURE; // XXX this method incorrectly returns a bool
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "AppendElement failed");
|
1999-04-06 06:11:23 +00:00
|
|
|
NS_RELEASE(readerThread);
|
|
|
|
}
|
|
|
|
|
|
|
|
// now that we've forked all the async requests, wait until they're done
|
1999-05-14 17:25:27 +00:00
|
|
|
PRUint32 threadCount;
|
|
|
|
rv = threads->Count(&threadCount);
|
1999-04-06 06:11:23 +00:00
|
|
|
for (PRUint32 i = 0; i < threadCount; i++) {
|
1999-06-02 11:42:43 +00:00
|
|
|
nsIThread* thread = (nsIThread*)threads->ElementAt(i);
|
1999-04-06 06:11:23 +00:00
|
|
|
thread->Join();
|
|
|
|
NS_RELEASE(thread);
|
1999-04-05 21:06:07 +00:00
|
|
|
}
|
1999-04-06 06:11:23 +00:00
|
|
|
NS_RELEASE(threads);
|
|
|
|
|
|
|
|
status = PR_CloseDir(dir);
|
|
|
|
NS_ASSERTION(status == PR_SUCCESS, "can't close dir");
|
|
|
|
}
|
|
|
|
|
1999-06-22 18:24:02 +00:00
|
|
|
nsresult NS_AutoregisterComponents()
|
|
|
|
{
|
|
|
|
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
printf("usage: %s <dir-to-read-all-files-from>\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char* dirName = argv[1];
|
|
|
|
|
1999-06-22 18:24:02 +00:00
|
|
|
rv = NS_AutoregisterComponents();
|
1999-04-06 06:11:23 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
NS_WITH_SERVICE(nsIFileTransportService, fts, kFileTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-02 11:42:43 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
SerialReadTest(dirName);
|
1999-06-02 11:42:43 +00:00
|
|
|
|
1999-10-01 23:32:12 +00:00
|
|
|
//ParallelReadTest(dirName, fts);
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-13 18:10:15 +00:00
|
|
|
fts->ProcessPendingRequests();
|
1999-04-05 21:06:07 +00:00
|
|
|
|
1999-04-06 06:11:23 +00:00
|
|
|
printf("duration %d ms, volume %d\n",
|
|
|
|
PR_IntervalToMilliseconds(gDuration),
|
|
|
|
gVolume);
|
|
|
|
gVolume = 0;
|
1999-04-05 21:06:07 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
1999-06-22 18:24:02 +00:00
|
|
|
|