Back to index

lightning-sunbird  0.9+nobinonly
TestMCTransport.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Darin Fisher <darin@netscape.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include <stdio.h>
00040 
00041 #include "nsIServiceManager.h"
00042 #include "nsIEventQueueService.h"
00043 #include "nsIOutputStream.h"
00044 #include "nsIStreamListener.h"
00045 #include "nsITransport.h"
00046 #include "nsIInputStream.h"
00047 #include "nsIOutputStream.h"
00048 #include "nsCOMPtr.h"
00049 #include "plstr.h"
00050 #include "prprf.h"
00051 
00052 #ifndef USE_CREATE_INSTANCE
00053 #include "nsICacheService.h"
00054 #include "nsICacheSession.h"
00055 #include "nsICacheEntryDescriptor.h"
00056 #include "nsNetCID.h"
00057 static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
00058 static nsICacheSession *session = nsnull;
00059 static nsICacheEntryDescriptor *desc = nsnull;
00060 #endif
00061 
00071 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00072 static nsIEventQueue *gEventQ = nsnull;
00073 
00074 class TestListener : public nsIStreamListener
00075 {
00076 public:
00077     NS_DECL_ISUPPORTS
00078     NS_DECL_NSISTREAMLISTENER
00079     NS_DECL_NSIREQUESTOBSERVER
00080 
00081     TestListener(char *);
00082     virtual ~TestListener();
00083 
00084 private:
00085     char *mFilename;
00086     FILE *mFile;
00087 };
00088 
00089 NS_IMPL_ISUPPORTS2(TestListener,
00090                    nsIStreamListener,
00091                    nsIRequestObserver)
00092 
00093 TestListener::TestListener(char *filename)
00094     : mFilename(filename)
00095     , mFile(nsnull)
00096 {
00097 }
00098 
00099 TestListener::~TestListener()
00100 {
00101 }
00102 
00103 NS_IMETHODIMP
00104 TestListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)
00105 {
00106     printf("OnStartRequest\n");
00107 
00108     mFile = fopen(mFilename, "w");
00109     if (!mFile)
00110         return NS_ERROR_FAILURE;
00111 
00112     return NS_OK;
00113 }
00114 
00115 NS_IMETHODIMP
00116 TestListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
00117 {
00118     printf("OnStopRequest: status=%x\n", status);
00119 
00120     if (mFile)
00121         fclose(mFile);
00122 
00123     return NS_OK;
00124 }
00125 
00126 NS_IMETHODIMP
00127 TestListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
00128                               nsIInputStream *is,
00129                               PRUint32 offset, PRUint32 count)
00130 {
00131     printf("OnDataAvailable: offset=%u count=%u\n", offset, count);
00132 
00133     if (!mFile) return NS_ERROR_FAILURE;
00134 
00135     char buf[128];
00136     nsresult rv;
00137     PRUint32 nread = 0;
00138 
00139     while (count) {
00140         PRUint32 amount = PR_MIN(count, sizeof(buf));
00141 
00142         rv = is->Read(buf, amount, &nread);
00143         if (NS_FAILED(rv)) return rv;
00144 
00145         fwrite(buf, nread, 1, mFile);
00146         count -= nread;
00147     }
00148     return NS_OK;
00149 }
00150 
00151 nsresult TestMCTransport(const char *filename)
00152 {
00153     nsresult rv = NS_OK;
00154     nsCOMPtr<nsITransport> transport;
00155 
00156 #ifdef USE_CREATE_INSTANCE
00157     transport = do_CreateInstance(
00158             "@mozilla.org/network/memory-cache-transport;1", &rv);
00159     if (NS_FAILED(rv))
00160         return rv;
00161 #else
00162     nsCOMPtr<nsICacheService> serv(do_GetService(kCacheServiceCID, &rv));
00163     if (NS_FAILED(rv)) return rv;
00164 
00165     rv = serv->CreateSession("TestMCTransport",
00166                              nsICache::STORE_IN_MEMORY, PR_TRUE,
00167                              &session);
00168     if (NS_FAILED(rv)) return rv;
00169 
00170     rv = session->OpenCacheEntry(nsDependentCString(filename),
00171                                  nsICache::ACCESS_READ_WRITE,
00172                                  nsICache::BLOCKING,
00173                                  &desc);
00174     if (NS_FAILED(rv)) return rv;
00175 
00176     rv = desc->MarkValid();
00177     if (NS_FAILED(rv)) return rv;
00178 
00179     rv = desc->GetTransport(getter_AddRefs(transport));
00180     if (NS_FAILED(rv)) return rv;
00181 #endif
00182 
00183     nsCOMPtr<nsIOutputStream> os;
00184     rv = transport->OpenOutputStream(0, (PRUint32) -1, 0, getter_AddRefs(os));
00185     if (NS_FAILED(rv)) return rv;
00186 
00187     char *out = PR_smprintf("%s.out", filename);
00188     nsCOMPtr<nsIStreamListener> listener = new TestListener(out);
00189     if (!listener)
00190         return NS_ERROR_OUT_OF_MEMORY;
00191 
00192     nsCOMPtr<nsIRequest> req;
00193     rv = transport->AsyncRead(listener, nsnull, 0, (PRUint32) -1, 0, getter_AddRefs(req));
00194     if (NS_FAILED(rv)) return rv;
00195 
00196     FILE *file = fopen(filename, "r");
00197     if (!file)
00198         return NS_ERROR_FILE_NOT_FOUND;
00199 
00200     char buf[256];
00201     PRUint32 count, total=0;
00202 
00203     while ((count = fread(buf, 1, sizeof(buf), file)) > 0) {
00204         printf("writing %u bytes\n", count);
00205         total += count;
00206         rv = os->Write(buf, count, &count);
00207         if (NS_FAILED(rv)) return rv;
00208 
00209         // process an event
00210         PLEvent *event = nsnull;
00211         gEventQ->GetEvent(&event);
00212         if (event) gEventQ->HandleEvent(event);
00213     }
00214 
00215     printf("wrote %u bytes\n", total);
00216 
00217     return rv;
00218 }
00219 
00220 int main(int argc, char **argv)
00221 {
00222     nsresult rv;
00223 
00224     if (argc < 2) {
00225         printf("usage: %s filename\n", argv[0]);
00226         return -1;
00227     }
00228 
00229     nsCOMPtr<nsIEventQueueService> eqs = 
00230              do_GetService(kEventQueueServiceCID, &rv);
00231     if (NS_FAILED(rv)) {
00232         printf("failed to create event queue service: rv=%x\n", rv);
00233         return -1;
00234     }
00235 
00236     rv = eqs->CreateMonitoredThreadEventQueue();
00237     if (NS_FAILED(rv)) {
00238         printf("failed to create monitored event queue: rv=%x\n", rv);
00239         return -1;
00240     }
00241 
00242     rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00243     if (NS_FAILED(rv)) {
00244         printf("failed to get thread event queue: %x\n", rv);
00245         return -1;
00246     }
00247 
00248     rv = TestMCTransport(argv[1]);
00249     printf("TestMCTransport returned %x\n", rv);
00250 
00251     gEventQ->ProcessPendingEvents();
00252 
00253 #ifndef USE_CREATE_INSTANCE
00254     NS_IF_RELEASE(desc);
00255     NS_IF_RELEASE(session);
00256 #endif
00257     return 0;
00258 }