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) 1998
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
48 #include "nsISocketTransportService.h"
49 #include "nsIEventQueueService.h"
50 #include "nsIServiceManager.h"
51 #include "nsIComponentRegistrar.h"
52 #include "nsITransport.h"
53 #include "nsIRequest.h"
54 #include "nsIStreamListener.h"
55 #include "nsIInputStream.h"
57 static NS_DEFINE_CID(kSocketTransportServiceCID
, NS_SOCKETTRANSPORTSERVICE_CID
);
58 static NS_DEFINE_CID(kEventQueueServiceCID
, NS_EVENTQUEUESERVICE_CID
);
60 static int gKeepRunning
= 1;
62 class InputTestConsumer
: public nsIStreamListener
67 virtual ~InputTestConsumer();
69 // ISupports interface...
72 // IStreamListener interface...
73 NS_IMETHOD
OnStartRequest(nsIRequest
*request
, nsISupports
* context
);
75 NS_IMETHOD
OnDataAvailable(nsIRequest
*request
, nsISupports
* context
,
76 nsIInputStream
*aIStream
,
77 PRUint32 aSourceOffset
,
80 NS_IMETHOD
OnStopRequest(nsIRequest
*request
, nsISupports
* context
,
86 InputTestConsumer::InputTestConsumer()
90 InputTestConsumer::~InputTestConsumer()
95 NS_IMPL_ISUPPORTS2(InputTestConsumer
, nsIRequestObserver
, nsIStreamListener
)
99 InputTestConsumer::OnStartRequest(nsIRequest
*request
, nsISupports
* context
)
101 printf("+++ OnStartRequest +++\n");
107 InputTestConsumer::OnDataAvailable(nsIRequest
*request
,
108 nsISupports
* context
,
109 nsIInputStream
*aIStream
,
110 PRUint32 aSourceOffset
,
114 while (aLength
> 0) {
116 aIStream
->Read(buf
, 1024, &amt
);
128 InputTestConsumer::OnStopRequest(nsIRequest
*request
, nsISupports
* context
,
132 printf("+++ OnStopRequest status %x +++\n", aStatus
);
138 main(int argc
, char* argv
[])
143 printf("usage: %s <host>\n", argv
[0]);
148 char* hostName
= argv
[1];
149 //nsString portString(argv[2]);
151 //port = portString.ToInteger(&rv);
154 nsCOMPtr
<nsIServiceManager
> servMan
;
155 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
156 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
157 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
159 registrar
->AutoRegister(nsnull
);
161 // Create the Event Queue for this thread...
162 nsCOMPtr
<nsIEventQueueService
> eventQService
=
163 do_GetService(kEventQueueServiceCID
, &rv
);
164 if (NS_FAILED(rv
)) return rv
;
166 nsCOMPtr
<nsIEventQueue
> eventQ
;
167 rv
= eventQService
->GetThreadEventQueue(NS_CURRENT_THREAD
, getter_AddRefs(eventQ
));
168 if (NS_FAILED(rv
)) return rv
;
170 nsCOMPtr
<nsISocketTransportService
> sts
=
171 do_GetService(kSocketTransportServiceCID
, &rv
);
172 if (NS_FAILED(rv
)) return rv
;
174 nsITransport
* transport
;
176 rv
= sts
->CreateTransport(hostName
, port
, nsnull
, 0, 0, &transport
);
177 if (NS_SUCCEEDED(rv
)) {
178 nsCOMPtr
<nsIRequest
> request
;
179 transport
->AsyncRead(new InputTestConsumer
, nsnull
, 0, -1, 0, getter_AddRefs(request
));
181 NS_RELEASE(transport
);
184 // Enter the message pump to allow the URL load to proceed.
185 while ( gKeepRunning
) {
187 eventQ
->WaitForEvent(&gEvent
);
188 eventQ
->HandleEvent(gEvent
);
191 } // this scopes the nsCOMPtrs
192 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
193 rv
= NS_ShutdownXPCOM(nsnull
);
194 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");