Back to index

lightning-sunbird  0.9+nobinonly
TestStreamTransport.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@netscape.com>
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 "TestCommon.h"
00039 #include "nsIComponentRegistrar.h"
00040 #include "nsIStreamTransportService.h"
00041 #include "nsIAsyncInputStream.h"
00042 #include "nsIProgressEventSink.h"
00043 #include "nsIInterfaceRequestor.h"
00044 #include "nsIInterfaceRequestorUtils.h"
00045 #include "nsIProxyObjectManager.h"
00046 #include "nsIRequest.h"
00047 #include "nsIServiceManager.h"
00048 #include "nsIComponentManager.h"
00049 #include "nsCOMPtr.h"
00050 #include "nsMemory.h"
00051 #include "nsString.h"
00052 #include "nsIFileStreams.h"
00053 #include "nsIStreamListener.h"
00054 #include "nsIEventQueueService.h"
00055 #include "nsIEventQueue.h"
00056 #include "nsILocalFile.h"
00057 #include "nsNetUtil.h"
00058 #include "nsAutoLock.h"
00059 #include "prlog.h"
00060 #include "prenv.h"
00061 
00063 
00064 #if defined(PR_LOGGING)
00065 //
00066 // set NSPR_LOG_MODULES=Test:5
00067 //
00068 static PRLogModuleInfo *gTestLog = nsnull;
00069 #endif
00070 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
00071 
00073 
00074 static NS_DEFINE_CID(kStreamTransportServiceCID, NS_STREAMTRANSPORTSERVICE_CID);
00075 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00076 
00077 PRBool gDone = PR_FALSE;
00078 nsIEventQueue* gEventQ = nsnull;
00079 
00081 
00082 static void *PR_CALLBACK
00083 DoneEvent_Handler(PLEvent *ev)
00084 {
00085     gDone = PR_TRUE;
00086     return nsnull;
00087 }
00088 
00089 static void PR_CALLBACK
00090 DoneEvent_Cleanup(PLEvent *ev)
00091 {
00092     delete ev;
00093 }
00094 
00095 static void
00096 PostDoneEvent()
00097 {
00098     LOG(("PostDoneEvent\n"));
00099 
00100     PLEvent *ev = new PLEvent();
00101 
00102     PL_InitEvent(ev, nsnull, 
00103             DoneEvent_Handler,
00104             DoneEvent_Cleanup);
00105 
00106     gEventQ->PostEvent(ev);
00107 }
00108 
00110 
00111 #define CHUNK_SIZE 500
00112 
00113 class MyCopier : public nsIInputStreamCallback
00114                , public nsIOutputStreamCallback
00115 {
00116 public:
00117     NS_DECL_ISUPPORTS
00118 
00119     MyCopier()
00120         : mLock(nsnull)
00121         , mInputCondition(NS_OK)
00122     {
00123     }
00124 
00125     virtual ~MyCopier()
00126     {
00127         if (mLock)
00128             PR_DestroyLock(mLock);
00129         if (mInput)
00130             mInput->Close();
00131         if (mOutput)
00132             mOutput->Close();
00133     }
00134 
00135     // called on any thread
00136     NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream *inStr)
00137     {
00138         LOG(("OnInputStreamReady\n"));
00139         nsAutoLock lock(mLock);
00140         NS_ASSERTION(inStr == mInput, "unexpected stream");
00141         Process_Locked();
00142         return NS_OK;
00143     }
00144 
00145     // called on any thread
00146     NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream *outStr)
00147     {
00148         LOG(("OnOutputStreamReady\n"));
00149         nsAutoLock lock(mLock);
00150         NS_ASSERTION(outStr == mOutput, "unexpected stream");
00151         Process_Locked();
00152         return NS_OK;
00153     }
00154 
00155     void Close_Locked()
00156     {
00157         LOG(("Close_Locked\n"));
00158 
00159         mOutput->Close();
00160         mOutput = 0;
00161         mInput->Close();
00162         mInput = 0;
00163 
00164         // post done copying event
00165         PostDoneEvent();
00166     }
00167 
00168     void Process_Locked()
00169     {
00170         while (1) {
00171             mInputCondition = NS_OK; // reset
00172 
00173             PRUint32 n;
00174             nsresult rv = mOutput->WriteSegments(FillOutputBuffer, this, CHUNK_SIZE, &n);
00175             if (NS_FAILED(rv) || (n == 0)) {
00176                 if (rv == NS_BASE_STREAM_WOULD_BLOCK)
00177                     mOutput->AsyncWait(this, 0, 0, nsnull);
00178                 else if (mInputCondition == NS_BASE_STREAM_WOULD_BLOCK)
00179                     mInput->AsyncWait(this, 0, 0, nsnull);
00180                 else
00181                     Close_Locked();
00182                 break;
00183             }
00184         }
00185     }
00186 
00187     nsresult AsyncCopy(nsITransport *srcTrans, nsITransport *destTrans)
00188     {
00189         mLock = PR_NewLock();
00190         if (!mLock)
00191             return NS_ERROR_OUT_OF_MEMORY;
00192 
00193         nsresult rv;
00194 
00195         nsCOMPtr<nsIInputStream> inStr;
00196         rv = srcTrans->OpenInputStream(0, 0, 0, getter_AddRefs(inStr));
00197         if (NS_FAILED(rv)) return rv;
00198 
00199         nsCOMPtr<nsIOutputStream> outStr;
00200         rv = destTrans->OpenOutputStream(0, 0, 0, getter_AddRefs(outStr));
00201         if (NS_FAILED(rv)) return rv;
00202 
00203         mInput = do_QueryInterface(inStr);
00204         mOutput = do_QueryInterface(outStr);
00205 
00206         return mInput->AsyncWait(this, 0, 0, nsnull);
00207     }
00208 
00209     static NS_METHOD FillOutputBuffer(nsIOutputStream *outStr,
00210                                       void *closure,
00211                                       char *buffer,
00212                                       PRUint32 offset,
00213                                       PRUint32 count,
00214                                       PRUint32 *countRead)
00215     {
00216         MyCopier *self = (MyCopier *) closure;
00217 
00218         nsresult rv = self->mInput->Read(buffer, count, countRead);
00219         if (NS_FAILED(rv))
00220             self->mInputCondition = rv;
00221         else if (*countRead == 0)
00222             self->mInputCondition = NS_BASE_STREAM_CLOSED;
00223 
00224         return self->mInputCondition;
00225     }
00226 
00227 protected:
00228     PRLock                        *mLock;
00229     nsCOMPtr<nsIAsyncInputStream>  mInput;
00230     nsCOMPtr<nsIAsyncOutputStream> mOutput;
00231     nsresult                       mInputCondition;
00232 };
00233 
00234 NS_IMPL_THREADSAFE_ISUPPORTS2(MyCopier,
00235                               nsIInputStreamCallback,
00236                               nsIOutputStreamCallback)
00237 
00238 
00239 
00243 static nsresult
00244 RunTest(nsIFile *srcFile, nsIFile *destFile)
00245 {
00246     nsresult rv;
00247 
00248     LOG(("RunTest\n"));
00249 
00250     nsCOMPtr<nsIStreamTransportService> sts =
00251         do_GetService(kStreamTransportServiceCID, &rv);
00252     if (NS_FAILED(rv)) return rv;
00253 
00254     nsCOMPtr<nsIInputStream> srcStr;
00255     rv = NS_NewLocalFileInputStream(getter_AddRefs(srcStr), srcFile);
00256     if (NS_FAILED(rv)) return rv;
00257 
00258     nsCOMPtr<nsIOutputStream> destStr;
00259     rv = NS_NewLocalFileOutputStream(getter_AddRefs(destStr), destFile);
00260     if (NS_FAILED(rv)) return rv;
00261 
00262     nsCOMPtr<nsITransport> srcTransport;
00263     rv = sts->CreateInputTransport(srcStr, nsInt64(-1), nsInt64(-1), PR_TRUE,
00264                                    getter_AddRefs(srcTransport));
00265     if (NS_FAILED(rv)) return rv;
00266 
00267     nsCOMPtr<nsITransport> destTransport;
00268     rv = sts->CreateOutputTransport(destStr, nsInt64(-1), nsInt64(-1), PR_TRUE,
00269                                     getter_AddRefs(destTransport));
00270     if (NS_FAILED(rv)) return rv;
00271 
00272     MyCopier *copier = new MyCopier();
00273     if (copier == nsnull)
00274         return NS_ERROR_OUT_OF_MEMORY;
00275     NS_ADDREF(copier);
00276 
00277     rv = copier->AsyncCopy(srcTransport, destTransport);
00278     if (NS_FAILED(rv)) return rv;
00279 
00280     PLEvent* event;
00281 
00282     gDone = PR_FALSE;
00283     while (!gDone) {
00284         rv = gEventQ->WaitForEvent(&event);
00285         if (NS_FAILED(rv)) return rv;
00286         rv = gEventQ->HandleEvent(event);
00287         if (NS_FAILED(rv)) return rv;
00288     }
00289 
00290     NS_RELEASE(copier);
00291     return NS_OK;
00292 }
00293 
00295 
00296 static nsresult
00297 RunBlockingTest(nsIFile *srcFile, nsIFile *destFile)
00298 {
00299     nsresult rv;
00300 
00301     LOG(("RunBlockingTest\n"));
00302 
00303     nsCOMPtr<nsIStreamTransportService> sts =
00304         do_GetService(kStreamTransportServiceCID, &rv);
00305     if (NS_FAILED(rv)) return rv;
00306 
00307     nsCOMPtr<nsIInputStream> srcIn;
00308     rv = NS_NewLocalFileInputStream(getter_AddRefs(srcIn), srcFile);
00309     if (NS_FAILED(rv)) return rv;
00310 
00311     nsCOMPtr<nsIOutputStream> fileOut;
00312     rv = NS_NewLocalFileOutputStream(getter_AddRefs(fileOut), destFile);
00313     if (NS_FAILED(rv)) return rv;
00314     
00315     nsCOMPtr<nsITransport> destTransport;
00316     rv = sts->CreateOutputTransport(fileOut, nsInt64(-1), nsInt64(-1),
00317                                     PR_TRUE, getter_AddRefs(destTransport));
00318     if (NS_FAILED(rv)) return rv;
00319 
00320     nsCOMPtr<nsIOutputStream> destOut;
00321     rv = destTransport->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(destOut));
00322     if (NS_FAILED(rv)) return rv;
00323 
00324     char buf[120];
00325     PRUint32 n;
00326     for (;;) {
00327         rv = srcIn->Read(buf, sizeof(buf), &n);
00328         if (NS_FAILED(rv) || (n == 0)) return rv;
00329 
00330         rv = destOut->Write(buf, n, &n);
00331         if (NS_FAILED(rv)) return rv;
00332     }
00333 
00334     return NS_OK;
00335 }
00336 
00338 
00339 int
00340 main(int argc, char* argv[])
00341 {
00342     if (test_common_init(&argc, &argv) != 0)
00343         return -1;
00344 
00345     nsresult rv;
00346 
00347     if (argc < 2) {
00348         printf("usage: %s <file-to-read>\n", argv[0]);
00349         return -1;
00350     }
00351     char* fileName = argv[1];
00352     {
00353         nsCOMPtr<nsIServiceManager> servMan;
00354         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00355         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00356         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00357         if (registrar)
00358             registrar->AutoRegister(nsnull);
00359 
00360 #if defined(PR_LOGGING)
00361         gTestLog = PR_NewLogModule("Test");
00362 #endif
00363 
00364         nsCOMPtr<nsIEventQueueService> eventQService =
00365                  do_GetService(kEventQueueServiceCID, &rv);
00366         if (NS_FAILED(rv)) return rv;
00367 
00368         rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00369         if (NS_FAILED(rv)) return rv;
00370 
00371         nsCOMPtr<nsILocalFile> srcFile;
00372         rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(srcFile));
00373         if (NS_FAILED(rv)) return rv;
00374 
00375         nsCOMPtr<nsIFile> destFile;
00376         rv = srcFile->Clone(getter_AddRefs(destFile));
00377         if (NS_FAILED(rv)) return rv;
00378 
00379         nsCAutoString leafName;
00380         rv = destFile->GetNativeLeafName(leafName);
00381         if (NS_FAILED(rv)) return rv;
00382 
00383         nsCAutoString newName;
00384         newName = leafName + NS_LITERAL_CSTRING(".1");
00385         rv = destFile->SetNativeLeafName(newName);
00386         if (NS_FAILED(rv)) return rv;
00387 
00388         rv = RunTest(srcFile, destFile);
00389         NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
00390 
00391         newName = leafName + NS_LITERAL_CSTRING(".2");
00392         rv = destFile->SetNativeLeafName(newName);
00393         if (NS_FAILED(rv)) return rv;
00394 
00395         rv = RunBlockingTest(srcFile, destFile);
00396         NS_ASSERTION(NS_SUCCEEDED(rv), "RunBlockingTest failed");
00397 
00398         NS_RELEASE(gEventQ);
00399 
00400         // give background threads a chance to finish whatever work they may
00401         // be doing.
00402         PR_Sleep(PR_SecondsToInterval(1));
00403     } // this scopes the nsCOMPtrs
00404     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00405     rv = NS_ShutdownXPCOM(nsnull);
00406     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00407     return NS_OK;
00408 }