1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
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 MPL, 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 MPL, 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 transport
= do_CreateInstance(
158 "@mozilla.org/network/memory-cache-transport;1", &rv
);
162 nsCOMPtr
<nsICacheService
> serv(do_GetService(kCacheServiceCID
, &rv
));
163 if (NS_FAILED(rv
)) return rv
;
165 rv
= serv
->CreateSession("TestMCTransport",
166 nsICache::STORE_IN_MEMORY
, PR_TRUE
,
168 if (NS_FAILED(rv
)) return rv
;
170 rv
= session
->OpenCacheEntry(nsDependentCString(filename
),
171 nsICache::ACCESS_READ_WRITE
,
174 if (NS_FAILED(rv
)) return rv
;
176 rv
= desc
->MarkValid();
177 if (NS_FAILED(rv
)) return rv
;
179 rv
= desc
->GetTransport(getter_AddRefs(transport
));
180 if (NS_FAILED(rv
)) return rv
;
183 nsCOMPtr
<nsIOutputStream
> os
;
184 rv
= transport
->OpenOutputStream(0, (PRUint32
) -1, 0, getter_AddRefs(os
));
185 if (NS_FAILED(rv
)) return rv
;
187 char *out
= PR_smprintf("%s.out", filename
);
188 nsCOMPtr
<nsIStreamListener
> listener
= new TestListener(out
);
190 return NS_ERROR_OUT_OF_MEMORY
;
192 nsCOMPtr
<nsIRequest
> req
;
193 rv
= transport
->AsyncRead(listener
, nsnull
, 0, (PRUint32
) -1, 0, getter_AddRefs(req
));
194 if (NS_FAILED(rv
)) return rv
;
196 FILE *file
= fopen(filename
, "r");
198 return NS_ERROR_FILE_NOT_FOUND
;
201 PRUint32 count
, total
=0;
203 while ((count
= fread(buf
, 1, sizeof(buf
), file
)) > 0) {
204 printf("writing %u bytes\n", count
);
206 rv
= os
->Write(buf
, count
, &count
);
207 if (NS_FAILED(rv
)) return rv
;
210 PLEvent
*event
= nsnull
;
211 gEventQ
->GetEvent(&event
);
212 if (event
) gEventQ
->HandleEvent(event
);
215 printf("wrote %u bytes\n", total
);
220 int main(int argc
, char **argv
)
225 printf("usage: %s filename\n", argv
[0]);
229 nsCOMPtr
<nsIEventQueueService
> eqs
=
230 do_GetService(kEventQueueServiceCID
, &rv
);
232 printf("failed to create event queue service: rv=%x\n", rv
);
236 rv
= eqs
->CreateMonitoredThreadEventQueue();
238 printf("failed to create monitored event queue: rv=%x\n", rv
);
242 rv
= eqs
->GetThreadEventQueue(NS_CURRENT_THREAD
, &gEventQ
);
244 printf("failed to get thread event queue: %x\n", rv
);
248 rv
= TestMCTransport(argv
[1]);
249 printf("TestMCTransport returned %x\n", rv
);
251 gEventQ
->ProcessPendingEvents();
253 #ifndef USE_CREATE_INSTANCE
255 NS_IF_RELEASE(session
);