Back to index

lightning-sunbird  0.9+nobinonly
TestIPC.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla IPC.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 2002
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@netscape.com>
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 
00038 #include "ipcIService.h"
00039 #include "ipcIMessageObserver.h"
00040 #include "ipcILockService.h"
00041 #include "ipcCID.h"
00042 #include "ipcLockCID.h"
00043 
00044 #include "nsIEventQueueService.h"
00045 #include "nsIServiceManager.h"
00046 #include "nsIComponentRegistrar.h"
00047 
00048 #include "nsString.h"
00049 #include "prmem.h"
00050 
00051 static const nsID kIPCMTargetID =
00052 { /* 753ca8ff-c8c2-4601-b115-8c2944da1150 */
00053     0x753ca8ff,
00054     0xc8c2,
00055     0x4601,
00056     {0xb1, 0x15, 0x8c, 0x29, 0x44, 0xda, 0x11, 0x50}
00057 };
00058 
00059 static const nsID kTestTargetID =
00060 { /* e628fc6e-a6a7-48c7-adba-f241d1128fb8 */
00061     0xe628fc6e,
00062     0xa6a7,
00063     0x48c7,
00064     {0xad, 0xba, 0xf2, 0x41, 0xd1, 0x12, 0x8f, 0xb8}
00065 };
00066 
00067 #define RETURN_IF_FAILED(rv, step) \
00068     PR_BEGIN_MACRO \
00069     if (NS_FAILED(rv)) { \
00070         printf("*** %s failed: rv=%x\n", step, rv); \
00071         return rv;\
00072     } \
00073     PR_END_MACRO
00074 
00075 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00076 static nsIEventQueue* gEventQ = nsnull;
00077 static PRBool gKeepRunning = PR_TRUE;
00078 static ipcIService *gIpcServ = nsnull;
00079 static ipcILockService *gIpcLockServ = nsnull;
00080 
00081 static void
00082 SendMsg(ipcIService *ipc, PRUint32 cID, const nsID &target, const char *data, PRUint32 dataLen, PRBool sync = PR_FALSE)
00083 {
00084     printf("*** sending message: [to-client=%u dataLen=%u]\n", cID, dataLen);
00085 
00086     nsresult rv;
00087 
00088     rv = ipc->SendMessage(cID, target, (const PRUint8 *) data, dataLen);
00089     if (NS_FAILED(rv)) {
00090         printf("*** sending message failed: rv=%x\n", rv);
00091         return;
00092     }
00093 
00094     if (sync) {
00095         rv = ipc->WaitMessage(cID, target, nsnull, PR_UINT32_MAX);
00096         if (NS_FAILED(rv))
00097             printf("*** waiting for message failed: rv=%x\n", rv);
00098     }
00099 }
00100 
00101 //-----------------------------------------------------------------------------
00102 
00103 class myIpcMessageObserver : public ipcIMessageObserver
00104 {
00105 public:
00106     NS_DECL_ISUPPORTS
00107     NS_DECL_IPCIMESSAGEOBSERVER
00108 };
00109 NS_IMPL_ISUPPORTS1(myIpcMessageObserver, ipcIMessageObserver)
00110 
00111 NS_IMETHODIMP
00112 myIpcMessageObserver::OnMessageAvailable(PRUint32 sender, const nsID &target, const PRUint8 *data, PRUint32 dataLen)
00113 {
00114     printf("*** got message: [sender=%u data=%s]\n", sender, (const char *) data);
00115     return NS_OK;
00116 }
00117 
00118 //-----------------------------------------------------------------------------
00119 
00120 #if 0
00121 class myIpcClientQueryHandler : public ipcIClientQueryHandler
00122 {
00123 public:
00124     NS_DECL_ISUPPORTS
00125     NS_DECL_IPCICLIENTQUERYHANDLER
00126 };
00127 
00128 NS_IMPL_ISUPPORTS1(myIpcClientQueryHandler, ipcIClientQueryHandler)
00129 
00130 NS_IMETHODIMP
00131 myIpcClientQueryHandler::OnQueryComplete(PRUint32 aQueryID,
00132                                          nsresult aStatus,
00133                                          PRUint32 aClientID,
00134                                          const char **aNames,
00135                                          PRUint32 aNameCount,
00136                                          const nsID **aTargets,
00137                                          PRUint32 aTargetCount)
00138 {
00139     printf("*** query complete [queryID=%u status=0x%x clientID=%u]\n",
00140             aQueryID, aStatus, aClientID);
00141 
00142     PRUint32 i;
00143     printf("***  names:\n");
00144     for (i = 0; i < aNameCount; ++i)
00145         printf("***    %d={%s}\n", i, aNames[i]);
00146     printf("***  targets:\n");
00147     for (i = 0; i < aTargetCount; ++i) {
00148         const char *trailer;
00149         if (aTargets[i]->Equals(kTestTargetID))
00150             trailer = " (TEST_TARGET_ID)";
00151         else if (aTargets[i]->Equals(kIPCMTargetID))
00152             trailer = " (IPCM_TARGET_ID)";
00153         else
00154             trailer = " (unknown)";
00155         char *str = aTargets[i]->ToString();
00156         printf("***    %d=%s%s\n", i, str, trailer);
00157         PR_Free(str);
00158     }
00159 
00160     if (aClientID != 0) {
00161         const char hello[] = "hello friend!";
00162         SendMsg(gIpcServ, aClientID, kTestTargetID, hello, sizeof(hello));
00163     }
00164 
00165     return NS_OK;
00166 }
00167 #endif
00168 
00169 //-----------------------------------------------------------------------------
00170 
00171 #if 0
00172 class myIpcLockNotify : public ipcILockNotify
00173 {
00174 public:
00175     NS_DECL_ISUPPORTS
00176     NS_DECL_IPCILOCKNOTIFY
00177 };
00178 
00179 NS_IMPL_ISUPPORTS1(myIpcLockNotify, ipcILockNotify)
00180 
00181 NS_IMETHODIMP
00182 myIpcLockNotify::OnAcquireLockComplete(const char *lockName, nsresult status)
00183 {
00184     printf("*** OnAcquireLockComplete [lock=%s status=%x]\n", lockName, status);
00185     gIpcLockServ->ReleaseLock(lockName);
00186     return NS_OK;
00187 }
00188 #endif
00189 
00190 //-----------------------------------------------------------------------------
00191 
00192 int main(int argc, char **argv)
00193 {
00194     nsresult rv;
00195 
00196     {
00197         nsCOMPtr<nsIServiceManager> servMan;
00198         NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00199         nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00200         NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00201         if (registrar)
00202             registrar->AutoRegister(nsnull);
00203 
00204         // Create the Event Queue for this thread...
00205         nsCOMPtr<nsIEventQueueService> eqs =
00206                  do_GetService(kEventQueueServiceCID, &rv);
00207         RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
00208 
00209         rv = eqs->CreateMonitoredThreadEventQueue();
00210         RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
00211 
00212         rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00213         RETURN_IF_FAILED(rv, "GetThreadEventQueue");
00214 
00215         printf("*** getting ipc service\n");
00216         nsCOMPtr<ipcIService> ipcServ(do_GetService(IPC_SERVICE_CONTRACTID, &rv));
00217         RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
00218         NS_ADDREF(gIpcServ = ipcServ);
00219 
00220         if (argc > 1) {
00221             printf("*** using client name [%s]\n", argv[1]);
00222             gIpcServ->AddName(argv[1]);
00223         }
00224 
00225         ipcServ->DefineTarget(kTestTargetID, new myIpcMessageObserver(), PR_TRUE);
00226 
00227         const char data[] =
00228                 "01 this is a really long message.\n"
00229                 "02 this is a really long message.\n"
00230                 "03 this is a really long message.\n"
00231                 "04 this is a really long message.\n"
00232                 "05 this is a really long message.\n"
00233                 "06 this is a really long message.\n"
00234                 "07 this is a really long message.\n"
00235                 "08 this is a really long message.\n"
00236                 "09 this is a really long message.\n"
00237                 "10 this is a really long message.\n"
00238                 "11 this is a really long message.\n"
00239                 "12 this is a really long message.\n"
00240                 "13 this is a really long message.\n"
00241                 "14 this is a really long message.\n"
00242                 "15 this is a really long message.\n"
00243                 "16 this is a really long message.\n"
00244                 "17 this is a really long message.\n"
00245                 "18 this is a really long message.\n"
00246                 "19 this is a really long message.\n"
00247                 "20 this is a really long message.\n"
00248                 "21 this is a really long message.\n"
00249                 "22 this is a really long message.\n"
00250                 "23 this is a really long message.\n"
00251                 "24 this is a really long message.\n"
00252                 "25 this is a really long message.\n"
00253                 "26 this is a really long message.\n"
00254                 "27 this is a really long message.\n"
00255                 "28 this is a really long message.\n"
00256                 "29 this is a really long message.\n"
00257                 "30 this is a really long message.\n"
00258                 "31 this is a really long message.\n"
00259                 "32 this is a really long message.\n"
00260                 "33 this is a really long message.\n"
00261                 "34 this is a really long message.\n"
00262                 "35 this is a really long message.\n"
00263                 "36 this is a really long message.\n"
00264                 "37 this is a really long message.\n"
00265                 "38 this is a really long message.\n"
00266                 "39 this is a really long message.\n"
00267                 "40 this is a really long message.\n"
00268                 "41 this is a really long message.\n"
00269                 "42 this is a really long message.\n"
00270                 "43 this is a really long message.\n"
00271                 "44 this is a really long message.\n"
00272                 "45 this is a really long message.\n"
00273                 "46 this is a really long message.\n"
00274                 "47 this is a really long message.\n"
00275                 "48 this is a really long message.\n"
00276                 "49 this is a really long message.\n"
00277                 "50 this is a really long message.\n"
00278                 "51 this is a really long message.\n"
00279                 "52 this is a really long message.\n"
00280                 "53 this is a really long message.\n"
00281                 "54 this is a really long message.\n"
00282                 "55 this is a really long message.\n"
00283                 "56 this is a really long message.\n"
00284                 "57 this is a really long message.\n"
00285                 "58 this is a really long message.\n"
00286                 "59 this is a really long message.\n"
00287                 "60 this is a really long message.\n";
00288         SendMsg(ipcServ, 0, kTestTargetID, data, sizeof(data), PR_TRUE);
00289 
00290 //        PRUint32 queryID;
00291 //        nsCOMPtr<ipcIClientQueryHandler> handler(new myIpcClientQueryHandler());
00292 //        ipcServ->QueryClientByName("foopy", handler, PR_FALSE, &queryID);
00293 
00294         PRUint32 foopyID;
00295         nsresult foopyRv = ipcServ->ResolveClientName("foopy", &foopyID);
00296         printf("*** query for 'foopy' returned [rv=%x id=%u]\n", foopyRv, foopyID);
00297 
00298         if (NS_SUCCEEDED(foopyRv)) {
00299             const char hello[] = "hello friend!";
00300             SendMsg(ipcServ, foopyID, kTestTargetID, hello, sizeof(hello));
00301         }
00302 
00303         //
00304         // test lock service
00305         //
00306         nsCOMPtr<ipcILockService> lockService = do_GetService(IPC_LOCKSERVICE_CONTRACTID, &rv);
00307         RETURN_IF_FAILED(rv, "do_GetService(ipcLockServ)");
00308         NS_ADDREF(gIpcLockServ = lockService);
00309 
00310         //nsCOMPtr<ipcILockNotify> notify(new myIpcLockNotify());
00311         gIpcLockServ->AcquireLock("blah", PR_TRUE);
00312 
00313         rv = gIpcLockServ->AcquireLock("foo", PR_TRUE);
00314         printf("*** sync AcquireLock returned [rv=%x]\n", rv);
00315 
00316         PLEvent *ev;
00317         while (gKeepRunning) {
00318             gEventQ->WaitForEvent(&ev);
00319             gEventQ->HandleEvent(ev);
00320         }
00321 
00322         NS_RELEASE(gIpcServ);
00323 
00324         printf("*** processing remaining events\n");
00325 
00326         // process any remaining events
00327         while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
00328             gEventQ->HandleEvent(ev);
00329 
00330         printf("*** done\n");
00331     } // this scopes the nsCOMPtrs
00332 
00333     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00334     rv = NS_ShutdownXPCOM(nsnull);
00335     NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00336 
00337     return 0;
00338 }