Back to index

lightning-sunbird  0.9+nobinonly
TestSocketInput.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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 #include <stdio.h>
00038 
00039 #ifdef WIN32
00040 #include <windows.h>
00041 #endif
00042 #ifdef XP_OS2
00043 #include <os2.h>
00044 #endif
00045 
00046 #include "nscore.h"
00047 #include "nsCOMPtr.h"
00048 #include "nsISocketTransportService.h"
00049 #include "nsIEventQueueService.h"
00050 #include "nsIServiceManager.h"
00051 #include "nsIComponentRegistrar.h"
00052 #include "nsITransport.h"
00053 #include "nsIRequest.h"
00054 #include "nsIStreamListener.h"
00055 #include "nsIInputStream.h"
00056 
00057 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
00058 static NS_DEFINE_CID(kEventQueueServiceCID,      NS_EVENTQUEUESERVICE_CID);
00059 
00060 static int gKeepRunning = 1;
00061 
00062 class InputTestConsumer : public nsIStreamListener
00063 {
00064 public:
00065 
00066   InputTestConsumer();
00067   virtual ~InputTestConsumer();
00068 
00069   // ISupports interface...
00070   NS_DECL_ISUPPORTS
00071 
00072   // IStreamListener interface...
00073   NS_IMETHOD OnStartRequest(nsIRequest *request, nsISupports* context);
00074 
00075   NS_IMETHOD OnDataAvailable(nsIRequest *request, nsISupports* context,
00076                              nsIInputStream *aIStream, 
00077                              PRUint32 aSourceOffset,
00078                              PRUint32 aLength);
00079 
00080   NS_IMETHOD OnStopRequest(nsIRequest *request, nsISupports* context,
00081                            nsresult aStatus);
00082 
00083 };
00084 
00085 
00086 InputTestConsumer::InputTestConsumer()
00087 {
00088 }
00089 
00090 InputTestConsumer::~InputTestConsumer()
00091 {
00092 }
00093 
00094 
00095 NS_IMPL_ISUPPORTS2(InputTestConsumer, nsIRequestObserver, nsIStreamListener)
00096 
00097 
00098 NS_IMETHODIMP
00099 InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
00100 {
00101   printf("+++ OnStartRequest +++\n");
00102   return NS_OK;
00103 }
00104 
00105 
00106 NS_IMETHODIMP
00107 InputTestConsumer::OnDataAvailable(nsIRequest *request, 
00108                                    nsISupports* context,
00109                                    nsIInputStream *aIStream, 
00110                                    PRUint32 aSourceOffset,
00111                                    PRUint32 aLength)
00112 {
00113   char buf[1025];
00114   while (aLength > 0) {
00115     PRUint32 amt;
00116     aIStream->Read(buf, 1024, &amt);
00117     if (amt == 0) break;
00118     buf[amt] = '\0';
00119     printf(buf);
00120     aLength -= amt;
00121   }
00122 
00123   return NS_OK;
00124 }
00125 
00126 
00127 NS_IMETHODIMP
00128 InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
00129                                  nsresult aStatus)
00130 {
00131   gKeepRunning = 0;
00132   printf("+++ OnStopRequest status %x +++\n", aStatus);
00133   return NS_OK;
00134 }
00135 
00136 
00137 int
00138 main(int argc, char* argv[])
00139 {
00140   nsresult rv;
00141 
00142   if (argc < 2) {
00143       printf("usage: %s <host>\n", argv[0]);
00144       return -1;
00145   }
00146 
00147   int port;
00148   char* hostName = argv[1];
00149 //nsString portString(argv[2]);
00150 
00151 //port = portString.ToInteger(&rv);
00152   port = 13;
00153   {
00154     nsCOMPtr<nsIServiceManager> servMan;
00155     NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00156     nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00157     NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00158     if (registrar)
00159       registrar->AutoRegister(nsnull);
00160 
00161     // Create the Event Queue for this thread...
00162     nsCOMPtr<nsIEventQueueService> eventQService =
00163              do_GetService(kEventQueueServiceCID, &rv);
00164     if (NS_FAILED(rv)) return rv;
00165 
00166     nsCOMPtr<nsIEventQueue> eventQ;
00167     rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eventQ));
00168     if (NS_FAILED(rv)) return rv;
00169 
00170     nsCOMPtr<nsISocketTransportService> sts =
00171              do_GetService(kSocketTransportServiceCID, &rv);
00172     if (NS_FAILED(rv)) return rv;
00173 
00174     nsITransport* transport;
00175 
00176     rv = sts->CreateTransport(hostName, port, nsnull, 0, 0, &transport);
00177     if (NS_SUCCEEDED(rv)) {
00178       nsCOMPtr<nsIRequest> request;
00179       transport->AsyncRead(new InputTestConsumer, nsnull, 0, -1, 0, getter_AddRefs(request));
00180 
00181       NS_RELEASE(transport);
00182     }
00183 
00184     // Enter the message pump to allow the URL load to proceed.
00185     while ( gKeepRunning ) {
00186       PLEvent *gEvent;
00187       eventQ->WaitForEvent(&gEvent);
00188       eventQ->HandleEvent(gEvent);
00189     }
00190 
00191   } // this scopes the nsCOMPtrs
00192   // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00193   rv = NS_ShutdownXPCOM(nsnull);
00194   NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00195   return 0;
00196 }
00197