Back to index

lightning-sunbird  0.9+nobinonly
tmModuleTest.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 Transaction Manager.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corp.
00018  * Portions created by the Initial Developer are Copyright (C) 2003
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   John Gaunt <jgaunt@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 // transaction manager includes
00039 #include "ipcITransactionService.h"
00040 #include "ipcITransactionObserver.h"
00041 
00042 // ipc daemon includes
00043 #include "ipcIService.h"
00044 
00045 // core & xpcom ns includes
00046 #include "nsDebug.h"
00047 #include "nsIEventQueueService.h"
00048 #include "nsIServiceManager.h"
00049 #include "nsIComponentRegistrar.h"
00050 #include "nsString.h"
00051 
00052 // nspr includes
00053 #include "prmem.h"
00054 #include "plgetopt.h"
00055 #include "nspr.h"
00056 #include "prlog.h"
00057 
00059 // Testing/Debug/Logging BEGIN
00060 
00061 const int NameSize = 1024;
00062 
00063 /* command line options */
00064 PRIntn      optDebug = 0;
00065 char        optMode = 's';
00066 char        *profileName = new char[NameSize];
00067 char        *queueName = new char[NameSize];
00068 
00069 char        *data = new char[NameSize];
00070 PRUint32    dataLen = 10;         // includes the null terminator for "test data"
00071 
00072 // Testing/Debug/Logging END
00074 
00075 #define RETURN_IF_FAILED(rv, step) \
00076     PR_BEGIN_MACRO \
00077     if (NS_FAILED(rv)) { \
00078         printf("*** %s failed: rv=%x\n", step, rv); \
00079         return rv;\
00080     } \
00081     PR_END_MACRO
00082 
00083 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
00084 static nsIEventQueue* gEventQ = nsnull;
00085 static PRBool gKeepRunning = PR_TRUE;
00086 //static PRInt32 gMsgCount = 0;
00087 static ipcIService *gIpcServ = nsnull;
00088 static ipcITransactionService *gTransServ = nsnull;
00089 
00090 //-----------------------------------------------------------------------------
00091 
00092 class myTransactionObserver : public ipcITransactionObserver
00093 {
00094 public:
00095     NS_DECL_ISUPPORTS
00096     NS_DECL_IPCITRANSACTIONOBSERVER
00097 
00098     myTransactionObserver() { }
00099 };
00100 
00101 NS_IMPL_ISUPPORTS1(myTransactionObserver, ipcITransactionObserver)
00102 
00103 NS_IMETHODIMP myTransactionObserver::OnTransactionAvailable(PRUint32 aQueueID, const PRUint8 *aData, PRUint32 aDataLen)
00104 {
00105     printf("tmModuleTest: myTransactionObserver::OnTransactionAvailable [%s]\n", aData);
00106     return NS_OK;
00107 }
00108 
00109 NS_IMETHODIMP myTransactionObserver::OnAttachReply(PRUint32 aQueueID, PRUint32 aStatus)
00110 {
00111     printf("tmModuleTest: myTransactionObserver::OnAttachReply [%d]\n", aStatus);
00112     return NS_OK;
00113 }
00114 
00115 NS_IMETHODIMP myTransactionObserver::OnDetachReply(PRUint32 aQueueID, PRUint32 aStatus)
00116 {
00117     printf("tmModuleTest: myTransactionObserver::OnDetachReply [%d]\n", aStatus);
00118     return NS_OK;
00119 }
00120 
00121 NS_IMETHODIMP myTransactionObserver::OnFlushReply(PRUint32 aQueueID, PRUint32 aStatus)
00122 {
00123     printf("tmModuleTest: myTransactionObserver::OnFlushReply [%d]\n", aStatus);
00124     return NS_OK;
00125 }
00126 
00127 
00128 //-----------------------------------------------------------------------------
00129 
00130 int main(PRInt32 argc, char *argv[])
00131 {
00132   nsresult rv;
00133 
00134   // default string values
00135   strcpy(profileName, "defaultProfile");
00136   strcpy(queueName, "defaultQueue");
00137   strcpy(data, "test data");
00138 
00139   { // scope the command line option gathering (needed for some reason)
00140 
00141     // Get command line options
00142     PLOptStatus os;
00143     PLOptState *opt = PL_CreateOptState(argc, argv, "bdfhlp:q:");
00144 
00145     while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00146     {
00147            if (PL_OPT_BAD == os) continue;
00148       switch (opt->option)
00149       {
00150         case 'b':  /* broadcast a bunch of messages */
00151           printf("tmModuleTest: broadcaster\n");
00152           optMode = 'b';
00153           break;
00154         case 'd':  /* debug mode */
00155           printf("tmModuleTest: debugging baby\n");
00156           optDebug = 1;
00157           break;
00158         case 'f':  /* broadcast and flush */
00159           printf("tmModuleTest: flusher\n");
00160           optMode = 'f';
00161           break;
00162         case 'h':  /* broadcast and detach */
00163           printf("tmModuleTest: hit-n-run\n");
00164           optMode = 'h';
00165           break;
00166         case 'l':  /* don't broadcast, just listen */
00167           printf("tmModuleTest: listener\n");
00168           optMode = 'l';
00169           break;
00170         case 'p':  /* set the profile name */
00171           strcpy(profileName, opt->value);
00172           printf("tmModuleTest: profilename:%s\n",profileName);
00173           break;
00174         case 'q':  /* set the queue name */
00175           strcpy(queueName, opt->value);
00176           printf("tmModuleTest: queuename:%s\n",queueName);
00177           break;
00178         default:
00179           printf("tmModuleTest: default\n");
00180           break;
00181       }
00182     }
00183     PL_DestroyOptState(opt);
00184   } // scope the command line option gathering (needed for some reason)
00185 
00186   { // scope the nsCOMPtrs
00187 
00188     printf("tmModuleTest: Starting xpcom\n");
00189    
00190     // xpcom startup stuff
00191     nsCOMPtr<nsIServiceManager> servMan;
00192     NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
00193     nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
00194     NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
00195     if (registrar)
00196       registrar->AutoRegister(nsnull);
00197 
00198     // Create the Event Queue for this thread...
00199     nsCOMPtr<nsIEventQueueService> eqs = do_GetService(kEventQueueServiceCID, &rv);
00200     RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");
00201 
00202     rv = eqs->CreateMonitoredThreadEventQueue();
00203     RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");
00204 
00205     rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
00206     RETURN_IF_FAILED(rv, "GetThreadEventQueue");
00207 
00208     // Need to make sure the ipc system has been started
00209     printf("tmModuleTest: getting ipc service\n");
00210     nsCOMPtr<ipcIService> ipcServ(do_GetService("@mozilla.org/ipc/service;1", &rv));
00211     RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
00212     NS_ADDREF(gIpcServ = ipcServ);
00213 
00214     // Get the transaction service
00215     printf("tmModuleTest: getting transaction service\n");
00216     nsCOMPtr<ipcITransactionService> transServ(do_GetService("@mozilla.org/ipc/transaction-service;1", &rv));
00217     RETURN_IF_FAILED(rv, "do_GetService(transServ)");
00218     NS_ADDREF(gTransServ = transServ);
00219 
00220     // transaction specifc startup stuff, done for all cases
00221 
00222     nsCOMPtr<ipcITransactionObserver> observ = new myTransactionObserver();
00223 
00224     // initialize the transaction service with a specific profile
00225     gTransServ->Init(nsDependentCString(profileName));
00226     printf("tmModuleTest: using profileName [%s]\n", profileName);
00227 
00228     // attach to the queue in the transaction manager
00229     gTransServ->Attach(nsDependentCString(queueName), observ, PR_TRUE);
00230     printf("tmModuleTest: observing queue [%s]\n", queueName);
00231 
00232 
00233     // run specific patterns based on the mode 
00234     int i = 0;       // wasn't working inside the cases
00235     switch (optMode)
00236     {
00237       case 's':
00238         printf("tmModuleTest: start standard\n");
00239         // post a couple events
00240         for (; i < 5 ; i++) {
00241           gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
00242         }
00243         // listen for events
00244         while (gKeepRunning)
00245           gEventQ->ProcessPendingEvents();
00246         printf("tmModuleTest: end standard\n");
00247         break;
00248       case 'b':
00249         printf("tmModuleTest: start broadcast\n");
00250         // post a BUNCH of messages
00251         for (; i < 50; i++) {
00252           gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
00253         }
00254         // listen for events
00255         while (gKeepRunning)
00256           gEventQ->ProcessPendingEvents();
00257         printf("tmModuleTest: end broadcast\n");
00258         break;
00259       case 'f':
00260         printf("tmModuleTest: start flush\n");
00261         // post a couple events
00262         for (; i < 5; i++) {
00263           gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
00264         }
00265         // flush the queue
00266         gTransServ->Flush(nsDependentCString(queueName), PR_TRUE);
00267         // post a couple events
00268         for (i=0; i < 8; i++) {
00269           gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
00270         }
00271         // listen for events
00272         while (gKeepRunning)
00273           gEventQ->ProcessPendingEvents();
00274         // detach
00275         gTransServ->Detach(nsDependentCString(queueName));
00276         printf("tmModuleTest: end flush\n");
00277         break;
00278       case 'h':
00279         printf("tmModuleTest: start hit-n-run\n");
00280         // post a couple events
00281         for (; i < 5; i++) {
00282           gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
00283         }
00284         // detach
00285         gTransServ->Detach(nsDependentCString(queueName));
00286         printf("tmModuleTest: end hit-n-run\n");
00287         break;
00288       case 'l':
00289         printf("tmModuleTest: start listener\n");
00290         // listen for events
00291         while (gKeepRunning)
00292           gEventQ->ProcessPendingEvents();
00293         printf("tmModuleTest: end listener\n");
00294         break;
00295       default :
00296         printf("tmModuleTest: start & end default\n");
00297         break;
00298     }
00299 
00300     // shutdown process
00301 
00302     NS_RELEASE(gTransServ);
00303     NS_RELEASE(gIpcServ);
00304 
00305     printf("tmModuleTest: processing remaining events\n");
00306 
00307     // process any remaining events
00308     PLEvent *ev;
00309     while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
00310       gEventQ->HandleEvent(ev);
00311 
00312     printf("tmModuleTest: done\n");
00313   } // this scopes the nsCOMPtrs
00314 
00315   // helps with shutdown on some cases
00316   PR_Sleep(PR_SecondsToInterval(4));
00317 
00318   // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
00319   rv = NS_ShutdownXPCOM(nsnull);
00320   NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
00321 
00322   return 0;
00323 }