Part of bug 382647 (move xpfe bookmarks to suite) - do the xpfe removals. r=KaiRo...
[mozilla-central.git] / netwerk / test / TestSocketInput.cpp
blob7925fbdfda2e6f2042ec1b635e1e7187a4133597
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
13 * License.
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.
22 * Contributor(s):
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 ***** */
37 #include <stdio.h>
39 #ifdef WIN32
40 #include <windows.h>
41 #endif
42 #ifdef XP_OS2
43 #include <os2.h>
44 #endif
46 #include "nscore.h"
47 #include "nsCOMPtr.h"
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
64 public:
66 InputTestConsumer();
67 virtual ~InputTestConsumer();
69 // ISupports interface...
70 NS_DECL_ISUPPORTS
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,
78 PRUint32 aLength);
80 NS_IMETHOD OnStopRequest(nsIRequest *request, nsISupports* context,
81 nsresult aStatus);
86 InputTestConsumer::InputTestConsumer()
90 InputTestConsumer::~InputTestConsumer()
95 NS_IMPL_ISUPPORTS2(InputTestConsumer, nsIRequestObserver, nsIStreamListener)
98 NS_IMETHODIMP
99 InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
101 printf("+++ OnStartRequest +++\n");
102 return NS_OK;
106 NS_IMETHODIMP
107 InputTestConsumer::OnDataAvailable(nsIRequest *request,
108 nsISupports* context,
109 nsIInputStream *aIStream,
110 PRUint32 aSourceOffset,
111 PRUint32 aLength)
113 char buf[1025];
114 while (aLength > 0) {
115 PRUint32 amt;
116 aIStream->Read(buf, 1024, &amt);
117 if (amt == 0) break;
118 buf[amt] = '\0';
119 printf(buf);
120 aLength -= amt;
123 return NS_OK;
127 NS_IMETHODIMP
128 InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
129 nsresult aStatus)
131 gKeepRunning = 0;
132 printf("+++ OnStopRequest status %x +++\n", aStatus);
133 return NS_OK;
138 main(int argc, char* argv[])
140 nsresult rv;
142 if (argc < 2) {
143 printf("usage: %s <host>\n", argv[0]);
144 return -1;
147 int port;
148 char* hostName = argv[1];
149 //nsString portString(argv[2]);
151 //port = portString.ToInteger(&rv);
152 port = 13;
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");
158 if (registrar)
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 ) {
186 PLEvent *gEvent;
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");
195 return 0;