Back to index

lightning-sunbird  0.9+nobinonly
TestStreamPump.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 "nsISeekableStream.h"
00050 #include "nsCOMPtr.h"
00051 #include "nsMemory.h"
00052 #include "nsString.h"
00053 #include "nsIFileStreams.h"
00054 #include "nsIStreamListener.h"
00055 #include "nsIEventQueueService.h"
00056 #include "nsIEventQueue.h"
00057 #include "nsILocalFile.h"
00058 #include "nsNetUtil.h"
00059 #include "nsAutoLock.h"
00060 #include "prlog.h"
00061 #include "prprf.h"
00062 
00064 
00065 #if defined(PR_LOGGING)
00066 //
00067 // set NSPR_LOG_MODULES=Test:5
00068 //
00069 static PRLogModuleInfo *gTestLog = nsnull;
00070 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
00071 #else
00072 #define LOG(args)
00073 #endif
00074 
00076 
00077 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00078 
00079 PRBool gDone = PR_FALSE;
00080 nsIEventQueue *gEventQ = nsnull;
00081 
00083 
00084 static void *PR_CALLBACK
00085 DoneEvent_Handler(PLEvent *ev)
00086 {
00087     gDone = PR_TRUE;
00088     return nsnull;
00089 }
00090 
00091 static void PR_CALLBACK
00092 DoneEvent_Cleanup(PLEvent *ev)
00093 {
00094     delete ev;
00095 }
00096 
00097 static void
00098 PostDoneEvent()
00099 {
00100     LOG(("PostDoneEvent\n"));
00101 
00102     PLEvent *ev = new PLEvent();
00103 
00104     PL_InitEvent(ev, nsnull, 
00105             DoneEvent_Handler,
00106             DoneEvent_Cleanup);
00107 
00108     gEventQ->PostEvent(ev);
00109 }
00110 
00112 
00113 class MyListener : public nsIStreamListener
00114 {
00115 public:
00116     NS_DECL_ISUPPORTS
00117 
00118     MyListener() {}
00119     virtual ~MyListener() {}
00120 
00121     NS_IMETHOD OnStartRequest(nsIRequest *req, nsISupports *ctx)
00122     {
00123         LOG(("MyListener::OnStartRequest\n"));
00124         return NS_OK;
00125     }
00126 
00127     NS_IMETHOD OnDataAvailable(nsIRequest *req, nsISupports *ctx,
00128                                nsIInputStream *stream,
00129                                PRUint32 offset, PRUint32 count)
00130     {
00131         LOG(("MyListener::OnDataAvailable [offset=%u count=%u]\n", offset, count));
00132 
00133         char buf[500];
00134         nsresult rv;
00135 
00136         while (count) {
00137             PRUint32 n, amt = PR_MIN(count, sizeof(buf));
00138 
00139             rv = stream->Read(buf, amt, &n);
00140             if (NS_FAILED(rv)) {
00141                 LOG(("  read returned 0x%08x\n", rv));
00142                 return rv;
00143             }
00144 
00145             fwrite(buf, n, 1, stdout);
00146             printf("\n");
00147 
00148             LOG(("  read %u bytes\n", n));
00149             count -= n;
00150         }
00151 
00152         return NS_OK;
00153     }
00154 
00155     NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
00156     {
00157         LOG(("MyListener::OnStopRequest [status=%x]\n", status));
00158         PostDoneEvent();
00159         return NS_OK;
00160     }
00161 };
00162 
00163 NS_IMPL_ISUPPORTS2(MyListener,
00164                    nsIRequestObserver,
00165                    nsIStreamListener)
00166 
00167 
00168 
00172 static nsresult
00173 RunTest(nsIFile *file, PRInt64 offset, PRInt64 length)
00174 {
00175     nsresult rv;
00176 
00177     LOG(("RunTest\n"));
00178 
00179     nsCOMPtr<nsIInputStream> stream;
00180     rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file);
00181     if (NS_FAILED(rv)) return rv;
00182 
00183     nsCOMPtr<nsIInputStreamPump> pump;
00184     rv = NS_NewInputStreamPump(getter_AddRefs(pump), stream, offset, length);
00185     if (NS_FAILED(rv)) return rv;
00186 
00187     rv = pump->AsyncRead(new MyListener(), nsnull);
00188     if (NS_FAILED(rv)) return rv;
00189 
00190     gDone = PR_FALSE;
00191     while (!gDone) {
00192         PLEvent *event;
00193         rv = gEventQ->WaitForEvent(&event);
00194         if (NS_FAILED(rv)) return rv;
00195         rv = gEventQ->HandleEvent(event);
00196         if (NS_FAILED(rv)) return rv;
00197     }
00198 
00199     return NS_OK;
00200 }
00201 
00203 
00204 int
00205 main(int argc, char* argv[])
00206 {
00207     if (test_common_init(&argc, &argv) != 0)
00208         return -1;
00209 
00210     nsresult rv;
00211 
00212     if (argc < 4) {
00213         printf("usage: %s <file-to-read> <start-offset> <read-length>\n", argv[0]);
00214         return -1;
00215     }
00216     char* fileName = argv[1];
00217     PRInt64 offset, length;
00218     int err = PR_sscanf(argv[2], "%lld", &offset);
00219     if (err == -1) {
00220       printf("Start offset must be an integer!\n");
00221       return 1;
00222     }
00223     err = PR_sscanf(argv[3], "%lld", &length);
00224     if (err == -1) {
00225       printf("Length must be an integer!\n");
00226       return 1;
00227     }
00228 
00229     {
00230         nsCOMPtr<nsIServiceManager> servMan;
00231         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00232         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00233         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00234         if (registrar)
00235             registrar->AutoRegister(nsnull);
00236 
00237 #if defined(PR_LOGGING)
00238         gTestLog = PR_NewLogModule("Test");
00239 #endif
00240 
00241         nsCOMPtr<nsIEventQueueService> eventQService =
00242                  do_GetService(kEventQueueServiceCID, &rv);
00243         if (NS_FAILED(rv)) return rv;
00244 
00245         rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00246         if (NS_FAILED(rv)) return rv;
00247 
00248         nsCOMPtr<nsILocalFile> file;
00249         rv = NS_NewNativeLocalFile(nsDependentCString(fileName), PR_FALSE, getter_AddRefs(file));
00250         if (NS_FAILED(rv)) return rv;
00251 
00252         rv = RunTest(file, offset, length);
00253         NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
00254 
00255         NS_RELEASE(gEventQ);
00256 
00257         // give background threads a chance to finish whatever work they may
00258         // be doing.
00259         PR_Sleep(PR_SecondsToInterval(1));
00260     } // this scopes the nsCOMPtrs
00261     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00262     rv = NS_ShutdownXPCOM(nsnull);
00263     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00264     return NS_OK;
00265 }