Back to index

lightning-sunbird  0.9+nobinonly
TestFileTransport.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsIComponentRegistrar.h"
00039 #include "nsIFileTransportService.h"
00040 #include "nsITransport.h"
00041 #include "nsIProgressEventSink.h"
00042 #include "nsIInterfaceRequestor.h"
00043 #include "nsIInterfaceRequestorUtils.h"
00044 #include "nsIRequest.h"
00045 #include "nsIServiceManager.h"
00046 #include "nsIComponentManager.h"
00047 #include "nsCOMPtr.h"
00048 #include "nsMemory.h"
00049 #include "nsString.h"
00050 #include "nsIFileStreams.h"
00051 #include "nsIStreamListener.h"
00052 #include "nsIEventQueueService.h"
00053 #include "nsIEventQueue.h"
00054 #include "nsILocalFile.h"
00055 #include "nsNetUtil.h"
00056 #include "prlog.h"
00057 
00059 
00060 #if defined(PR_LOGGING)
00061 static PRLogModuleInfo *gTestFileTransportLog = nsnull;
00062 #define PRINTF(args) PR_LOG(gTestFileTransportLog, PR_LOG_ALWAYS, args)
00063 #else
00064 #define PRINTF(args)
00065 #endif
00066 
00067 #undef PRINTF
00068 #define PRINTF(args) printf args
00069 
00071 
00072 static NS_DEFINE_CID(kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID);
00073 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00074 
00075 PRBool gDone = PR_FALSE;
00076 nsIEventQueue* gEventQ = nsnull;
00077 
00079 
00080 class MyProgressEventSink : public nsIProgressEventSink
00081                           , public nsIInterfaceRequestor {
00082 public:
00083     NS_DECL_ISUPPORTS
00084 
00085     MyProgressEventSink() { }
00086 
00087     NS_IMETHOD OnProgress(nsIRequest *request, nsISupports *ctxt,
00088                           PRUint64 progress, PRUint64 progressMax) {
00089         PRINTF(("progress: %llu/%llu\n", progress, progressMax));
00090         return NS_OK;
00091     }
00092 
00093     NS_IMETHOD OnStatus(nsIRequest *request, nsISupports *ctxt,
00094                         nsresult status, const PRUnichar *statusArg) {
00095         return NS_OK;
00096     }
00097 
00098     NS_IMETHOD GetInterface(const nsIID &iid, void **result) {
00099         if (iid.Equals(NS_GET_IID(nsIProgressEventSink)))
00100             return QueryInterface(iid, result);
00101         return NS_ERROR_NO_INTERFACE;
00102     }
00103 };
00104 
00105 NS_IMPL_THREADSAFE_ISUPPORTS2(MyProgressEventSink,
00106                               nsIProgressEventSink,
00107                               nsIInterfaceRequestor)
00108 
00109 
00110 
00111 class MyListener : public nsIStreamListener {
00112 public:
00113     NS_DECL_ISUPPORTS
00114 
00115     NS_IMETHOD OnStartRequest(nsIRequest *request, nsISupports *ctxt) {
00116         PRINTF(("starting\n"));
00117         return NS_OK;
00118     }
00119 
00120     NS_IMETHOD OnStopRequest(nsIRequest *request, nsISupports *ctxt, 
00121                              nsresult aStatus) {
00122         PRINTF(("ending status=%0x total=%d\n", aStatus, mTotal));
00123         if (--mStopCount == 0)
00124             gDone = PR_TRUE;
00125         return NS_OK;
00126     }
00127 
00128     NS_IMETHOD OnDataAvailable(nsIRequest *request, nsISupports *ctxt, 
00129                                nsIInputStream *inStr, PRUint32 sourceOffset, 
00130                                PRUint32 count) {
00131         PRINTF(("receiving %d bytes\n", count));
00132         char buf[256];
00133         PRUint32 writeCount, givenCount=count;
00134         nsresult rv;
00135         while (count > 0) {
00136             PRUint32 amt = PR_MIN(count, 256);
00137             PRUint32 readCount;
00138             rv = inStr->Read(buf, amt, &readCount);
00139             if (NS_FAILED(rv)) return rv;
00140             NS_ASSERTION(readCount != 0, "premature EOF");
00141             rv = mOut->Write(buf, readCount, &writeCount);
00142             if (NS_FAILED(rv)) return rv;
00143             NS_ASSERTION(writeCount == readCount, "failed to write all the data");
00144             count -= readCount;
00145             mTotal += readCount;
00146         }
00147         PRINTF(("done reading data [read %u bytes]\n", givenCount - count));
00148         //PRINTF("sleeping for 100 ticks\n");FLUSH();
00149         //PR_Sleep(100);
00150         return NS_OK;
00151     }
00152 
00153     MyListener(PRUint32 stopCount = 1) : mTotal(0), mStopCount(stopCount) {
00154     }
00155 
00156     nsresult Init(const char* origFile) {
00157         nsresult rv;
00158         nsCOMPtr<nsILocalFile> file;
00159         rv = NS_NewNativeLocalFile(nsDependentCString(origFile), PR_FALSE, getter_AddRefs(file));
00160         if (NS_FAILED(rv)) return rv;
00161         nsCAutoString name;
00162         rv = file->GetNativeLeafName(name);
00163         if (NS_FAILED(rv)) return rv;
00164         name.AppendLiteral(".bak");
00165         rv = file->SetNativeLeafName(name);
00166         if (NS_FAILED(rv)) return rv;
00167         rv = NS_NewLocalFileOutputStream(getter_AddRefs(mOut),
00168                                          file, 
00169                                          PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
00170                                          0664);
00171         return rv;
00172     }
00173 
00174     virtual ~MyListener() {
00175         nsresult rv = mOut->Close();
00176         NS_ASSERTION(NS_SUCCEEDED(rv), "Close failed");
00177     }
00178 
00179 protected:
00180     nsCOMPtr<nsIOutputStream> mOut;
00181     PRUint32 mTotal;
00182     PRUint32 mStopCount;
00183 };
00184 
00185 NS_IMPL_THREADSAFE_ISUPPORTS2(MyListener, nsIStreamListener, nsIRequestObserver)
00186 
00187 
00188 
00189 nsresult
00190 TestAsyncRead(const char* fileName, PRUint32 offset, PRInt32 length)
00191 {
00192     nsresult rv;
00193 
00194     PRINTF(("TestAsyncRead\n"));
00195 
00196     nsCOMPtr<nsIFileTransportService> fts = 
00197              do_GetService(kFileTransportServiceCID, &rv);
00198     if (NS_FAILED(rv)) return rv;
00199 
00200     nsITransport* fileTrans;
00201     nsCOMPtr<nsILocalFile> file;
00202     rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(file));
00203     if (NS_FAILED(rv)) return rv;
00204     rv = fts->CreateTransport(file, PR_RDONLY, 0, PR_TRUE, &fileTrans);
00205     if (NS_FAILED(rv)) return rv;
00206 
00207     MyListener* listener = new MyListener();
00208     if (listener == nsnull)
00209         return NS_ERROR_OUT_OF_MEMORY;
00210     NS_ADDREF(listener);
00211     rv = listener->Init(fileName);
00212     if (NS_FAILED(rv)) return rv;
00213 
00214     MyProgressEventSink* progressSink = new MyProgressEventSink();
00215     if (progressSink == nsnull)
00216         return NS_ERROR_OUT_OF_MEMORY;
00217     NS_ADDREF(progressSink);
00218     rv = fileTrans->SetNotificationCallbacks(progressSink, PR_FALSE);
00219     if (NS_FAILED(rv)) return rv;
00220 
00221     gDone = PR_FALSE;
00222     nsCOMPtr<nsIRequest> request;
00223     rv = fileTrans->AsyncRead(listener, nsnull, offset, length, 0, getter_AddRefs(request));
00224     if (NS_FAILED(rv)) return rv;
00225 
00226     while (!gDone) {
00227         PLEvent* event;
00228         rv = gEventQ->WaitForEvent(&event);
00229         if (NS_FAILED(rv)) return rv;
00230         rv = gEventQ->HandleEvent(event);
00231         if (NS_FAILED(rv)) return rv;
00232     }
00233 
00234     NS_RELEASE(listener);
00235     NS_RELEASE(fileTrans);
00236     return NS_OK;
00237 }
00238 
00240 
00241 nsresult
00242 TestAsyncWrite(const char* fileName, PRUint32 offset, PRInt32 length)
00243 {
00244     nsresult rv;
00245 
00246     PRINTF(("TestAsyncWrite\n"));
00247 
00248     nsCOMPtr<nsIFileTransportService> fts = 
00249              do_GetService(kFileTransportServiceCID, &rv);
00250     if (NS_FAILED(rv)) return rv;
00251 
00252     nsCAutoString outFile(fileName);
00253     outFile.AppendLiteral(".out");
00254     nsITransport* fileTrans;
00255     nsCOMPtr<nsILocalFile> file;
00256     rv = NS_NewNativeLocalFile(outFile, PR_FALSE, getter_AddRefs(file));
00257     if (NS_FAILED(rv)) return rv;
00258     rv = fts->CreateTransport(file,
00259                               PR_CREATE_FILE | PR_WRONLY | PR_TRUNCATE,
00260                               0664, PR_TRUE, &fileTrans);
00261     if (NS_FAILED(rv)) return rv;
00262 
00263     MyListener* listener = new MyListener();
00264     if (listener == nsnull)
00265         return NS_ERROR_OUT_OF_MEMORY;
00266     NS_ADDREF(listener);
00267     rv = listener->Init(outFile.get());
00268     if (NS_FAILED(rv)) return rv;
00269 
00270     MyProgressEventSink* progressSink = new MyProgressEventSink();
00271     if (progressSink == nsnull)
00272         return NS_ERROR_OUT_OF_MEMORY;
00273     NS_ADDREF(progressSink);
00274     rv = fileTrans->SetNotificationCallbacks(progressSink, PR_FALSE);
00275     if (NS_FAILED(rv)) return rv;
00276 
00277     nsCOMPtr<nsILocalFile> f;
00278     rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(f));
00279     if (NS_FAILED(rv)) return rv;
00280     nsCOMPtr<nsIInputStream> inStr;
00281     rv = NS_NewLocalFileInputStream(getter_AddRefs(inStr), f);
00282     if (NS_FAILED(rv)) return rv;
00283 
00284     gDone = PR_FALSE;
00285     nsCOMPtr<nsIRequest> request;
00286     rv = NS_AsyncWriteFromStream(getter_AddRefs(request), fileTrans, inStr, offset, length, 0, listener);
00287     if (NS_FAILED(rv)) return rv;
00288 
00289     while (!gDone) {
00290         PLEvent* event;
00291         rv = gEventQ->WaitForEvent(&event);
00292         if (NS_FAILED(rv)) return rv;
00293         rv = gEventQ->HandleEvent(event);
00294         if (NS_FAILED(rv)) return rv;
00295     }
00296 
00297     NS_RELEASE(listener);
00298     NS_RELEASE(fileTrans);
00299     return NS_OK;
00300 }
00301 
00303 
00304 #if 0
00305 class MyOpenObserver : public nsIRequestObserver
00306 {
00307 public:
00308     NS_DECL_ISUPPORTS
00309 
00310     NS_IMETHOD OnStartRequest(nsIRequest *request, nsISupports *ctxt) {
00311         nsresult rv;
00312         char* contentType;
00313         nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
00314         rv = channel->GetContentType(&contentType);
00315         if (NS_FAILED(rv)) return rv;
00316         PRInt32 length;
00317         rv = cr->GetContentLength(&length);
00318         if (NS_FAILED(rv)) return rv;
00319         PRINTF(("stream opened: content type = %s, length = %d\n",
00320                 contentType, length));
00321         nsCRT::free(contentType);
00322         return NS_OK;
00323     }
00324 
00325     NS_IMETHOD OnStopRequest(nsIRequest *request, nsISupports *ctxt,
00326                              nsresult aStatus) {
00327         PRINTF(("stream closed: status %x\n", aStatus));
00328         return NS_OK;
00329     }
00330 
00331     MyOpenObserver() { }
00332     virtual ~MyOpenObserver() {}
00333 };
00334 
00335 NS_IMPL_ISUPPORTS1(MyOpenObserver, nsIRequestObserver)
00336 #endif
00337 
00339 
00340 
00341 int
00342 main(int argc, char* argv[])
00343 {
00344     nsresult rv;
00345 
00346 #if defined(PR_LOGGING)
00347     gTestFileTransportLog = PR_NewLogModule("TestFileTransport");
00348 #endif
00349 
00350     if (argc < 2) {
00351         printf("usage: %s <file-to-read>\n", argv[0]);
00352         return -1;
00353     }
00354     char* fileName = argv[1];
00355     {
00356         nsCOMPtr<nsIServiceManager> servMan;
00357         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00358         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00359         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00360         if (registrar)
00361             registrar->AutoRegister(nsnull);
00362 
00363         nsCOMPtr<nsIEventQueueService> eventQService =
00364                  do_GetService(kEventQueueServiceCID, &rv);
00365         if (NS_FAILED(rv)) return rv;
00366 
00367         rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00368         if (NS_FAILED(rv)) return rv;
00369 
00370         rv = TestAsyncRead(fileName, 0, -1);
00371         NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");
00372 
00373         rv = TestAsyncWrite(fileName, 0, -1);
00374         NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncWrite failed");
00375 
00376         rv = TestAsyncRead(fileName, 10, 100);
00377         NS_ASSERTION(NS_SUCCEEDED(rv), "TestAsyncRead failed");
00378 
00379         NS_RELEASE(gEventQ);
00380     } // this scopes the nsCOMPtrs
00381     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00382     rv = NS_ShutdownXPCOM(nsnull);
00383     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00384     return NS_OK;
00385 }