1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 2001
20
* the Initial Developer. All Rights Reserved.
23
* Darin Fisher <darin@netscape.com> (original author)
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the NPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
41
#include "nsIServiceManager.h"
42
#include "nsIEventQueueService.h"
43
#include "nsIOutputStream.h"
44
#include "nsIStreamListener.h"
45
#include "nsITransport.h"
46
#include "nsIInputStream.h"
47
#include "nsIOutputStream.h"
52
#ifndef USE_CREATE_INSTANCE
53
#include "nsICacheService.h"
54
#include "nsICacheSession.h"
55
#include "nsICacheEntryDescriptor.h"
57
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
58
static nsICacheSession *session = nsnull;
59
static nsICacheEntryDescriptor *desc = nsnull;
63
* This test program exercises the memory cache's nsITransport implementation.
65
* This test program loads a file into the memory cache (using OpenOutputStream),
66
* and then reads the file back out (using AsyncRead). The data read from the
67
* memory cache is written to a new file (with .out added as a suffix to the file
71
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
72
static nsIEventQueue *gEventQ = nsnull;
74
class TestListener : public nsIStreamListener
78
NS_DECL_NSISTREAMLISTENER
79
NS_DECL_NSIREQUESTOBSERVER
82
virtual ~TestListener();
89
NS_IMPL_ISUPPORTS2(TestListener,
93
TestListener::TestListener(char *filename)
99
TestListener::~TestListener()
104
TestListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)
106
printf("OnStartRequest\n");
108
mFile = fopen(mFilename, "w");
110
return NS_ERROR_FAILURE;
116
TestListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
118
printf("OnStopRequest: status=%x\n", status);
127
TestListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
129
PRUint32 offset, PRUint32 count)
131
printf("OnDataAvailable: offset=%u count=%u\n", offset, count);
133
if (!mFile) return NS_ERROR_FAILURE;
140
PRUint32 amount = PR_MIN(count, sizeof(buf));
142
rv = is->Read(buf, amount, &nread);
143
if (NS_FAILED(rv)) return rv;
145
fwrite(buf, nread, 1, mFile);
151
nsresult TestMCTransport(const char *filename)
154
nsCOMPtr<nsITransport> transport;
156
#ifdef USE_CREATE_INSTANCE
157
rv = nsComponentManager::CreateInstance(
158
"@mozilla.org/network/memory-cache-transport;1",
160
NS_GET_IID(nsITransport),
161
getter_AddRefs(transport));
165
nsCOMPtr<nsICacheService> serv(do_GetService(kCacheServiceCID, &rv));
166
if (NS_FAILED(rv)) return rv;
168
rv = serv->CreateSession("TestMCTransport",
169
nsICache::STORE_IN_MEMORY, PR_TRUE,
171
if (NS_FAILED(rv)) return rv;
173
rv = session->OpenCacheEntry(filename,
174
nsICache::ACCESS_READ_WRITE,
177
if (NS_FAILED(rv)) return rv;
179
rv = desc->MarkValid();
180
if (NS_FAILED(rv)) return rv;
182
rv = desc->GetTransport(getter_AddRefs(transport));
183
if (NS_FAILED(rv)) return rv;
186
nsCOMPtr<nsIOutputStream> os;
187
rv = transport->OpenOutputStream(0, (PRUint32) -1, 0, getter_AddRefs(os));
188
if (NS_FAILED(rv)) return rv;
190
char *out = PR_smprintf("%s.out", filename);
191
nsCOMPtr<nsIStreamListener> listener = new TestListener(out);
193
return NS_ERROR_OUT_OF_MEMORY;
195
nsCOMPtr<nsIRequest> req;
196
rv = transport->AsyncRead(listener, nsnull, 0, (PRUint32) -1, 0, getter_AddRefs(req));
197
if (NS_FAILED(rv)) return rv;
199
FILE *file = fopen(filename, "r");
201
return NS_ERROR_FILE_NOT_FOUND;
204
PRUint32 count, total=0;
206
while ((count = fread(buf, 1, sizeof(buf), file)) > 0) {
207
printf("writing %u bytes\n", count);
209
rv = os->Write(buf, count, &count);
210
if (NS_FAILED(rv)) return rv;
213
PLEvent *event = nsnull;
214
gEventQ->GetEvent(&event);
215
if (event) gEventQ->HandleEvent(event);
218
printf("wrote %u bytes\n", total);
223
int main(int argc, char **argv)
228
printf("usage: %s filename\n", argv[0]);
232
nsCOMPtr<nsIEventQueueService> eqs =
233
do_GetService(kEventQueueServiceCID, &rv);
235
printf("failed to create event queue service: rv=%x\n", rv);
239
rv = eqs->CreateMonitoredThreadEventQueue();
241
printf("failed to create monitored event queue: rv=%x\n", rv);
245
rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
247
printf("failed to get thread event queue: %x\n", rv);
251
rv = TestMCTransport(argv[1]);
252
printf("TestMCTransport returned %x\n", rv);
254
gEventQ->ProcessPendingEvents();
256
#ifndef USE_CREATE_INSTANCE
258
NS_IF_RELEASE(session);