Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
tmModuleTest.cpp File Reference
#include "ipcITransactionService.h"
#include "ipcITransactionObserver.h"
#include "ipcIService.h"
#include "nsDebug.h"
#include "nsIEventQueueService.h"
#include "nsIServiceManager.h"
#include "nsIComponentRegistrar.h"
#include "nsString.h"
#include "prmem.h"
#include "plgetopt.h"
#include "nspr.h"
#include "prlog.h"

Go to the source code of this file.

Classes

class  myTransactionObserver

Defines

#define RETURN_IF_FAILED(rv, step)

Functions

static NS_DEFINE_CID (kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID)
int main (PRInt32 argc, char *argv[])

Variables

const int NameSize = 1024
PRIntn optDebug = 0
char optMode = 's'
char * profileName = new char[NameSize]
char * queueName = new char[NameSize]
char * data = new char[NameSize]
PRUint32 dataLen = 10
static nsIEventQueuegEventQ = nsnull
static PRBool gKeepRunning = PR_TRUE
static ipcIServicegIpcServ = nsnull
static ipcITransactionServicegTransServ = nsnull

Define Documentation

#define RETURN_IF_FAILED (   rv,
  step 
)
Value:
PR_BEGIN_MACRO \
    if (NS_FAILED(rv)) { \
        printf("*** %s failed: rv=%x\n", step, rv); \
        return rv;\
    } \
    PR_END_MACRO

Definition at line 75 of file tmModuleTest.cpp.


Function Documentation

int main ( PRInt32  argc,
char *  argv[] 
)

Definition at line 130 of file tmModuleTest.cpp.

{
  nsresult rv;

  // default string values
  strcpy(profileName, "defaultProfile");
  strcpy(queueName, "defaultQueue");
  strcpy(data, "test data");

  { // scope the command line option gathering (needed for some reason)

    // Get command line options
    PLOptStatus os;
    PLOptState *opt = PL_CreateOptState(argc, argv, "bdfhlp:q:");

    while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
    {
           if (PL_OPT_BAD == os) continue;
      switch (opt->option)
      {
        case 'b':  /* broadcast a bunch of messages */
          printf("tmModuleTest: broadcaster\n");
          optMode = 'b';
          break;
        case 'd':  /* debug mode */
          printf("tmModuleTest: debugging baby\n");
          optDebug = 1;
          break;
        case 'f':  /* broadcast and flush */
          printf("tmModuleTest: flusher\n");
          optMode = 'f';
          break;
        case 'h':  /* broadcast and detach */
          printf("tmModuleTest: hit-n-run\n");
          optMode = 'h';
          break;
        case 'l':  /* don't broadcast, just listen */
          printf("tmModuleTest: listener\n");
          optMode = 'l';
          break;
        case 'p':  /* set the profile name */
          strcpy(profileName, opt->value);
          printf("tmModuleTest: profilename:%s\n",profileName);
          break;
        case 'q':  /* set the queue name */
          strcpy(queueName, opt->value);
          printf("tmModuleTest: queuename:%s\n",queueName);
          break;
        default:
          printf("tmModuleTest: default\n");
          break;
      }
    }
    PL_DestroyOptState(opt);
  } // scope the command line option gathering (needed for some reason)

  { // scope the nsCOMPtrs

    printf("tmModuleTest: Starting xpcom\n");
   
    // xpcom startup stuff
    nsCOMPtr<nsIServiceManager> servMan;
    NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
    nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
    NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
    if (registrar)
      registrar->AutoRegister(nsnull);

    // Create the Event Queue for this thread...
    nsCOMPtr<nsIEventQueueService> eqs = do_GetService(kEventQueueServiceCID, &rv);
    RETURN_IF_FAILED(rv, "do_GetService(EventQueueService)");

    rv = eqs->CreateMonitoredThreadEventQueue();
    RETURN_IF_FAILED(rv, "CreateMonitoredThreadEventQueue");

    rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
    RETURN_IF_FAILED(rv, "GetThreadEventQueue");

    // Need to make sure the ipc system has been started
    printf("tmModuleTest: getting ipc service\n");
    nsCOMPtr<ipcIService> ipcServ(do_GetService("@mozilla.org/ipc/service;1", &rv));
    RETURN_IF_FAILED(rv, "do_GetService(ipcServ)");
    NS_ADDREF(gIpcServ = ipcServ);

    // Get the transaction service
    printf("tmModuleTest: getting transaction service\n");
    nsCOMPtr<ipcITransactionService> transServ(do_GetService("@mozilla.org/ipc/transaction-service;1", &rv));
    RETURN_IF_FAILED(rv, "do_GetService(transServ)");
    NS_ADDREF(gTransServ = transServ);

    // transaction specifc startup stuff, done for all cases

    nsCOMPtr<ipcITransactionObserver> observ = new myTransactionObserver();

    // initialize the transaction service with a specific profile
    gTransServ->Init(nsDependentCString(profileName));
    printf("tmModuleTest: using profileName [%s]\n", profileName);

    // attach to the queue in the transaction manager
    gTransServ->Attach(nsDependentCString(queueName), observ, PR_TRUE);
    printf("tmModuleTest: observing queue [%s]\n", queueName);


    // run specific patterns based on the mode 
    int i = 0;       // wasn't working inside the cases
    switch (optMode)
    {
      case 's':
        printf("tmModuleTest: start standard\n");
        // post a couple events
        for (; i < 5 ; i++) {
          gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
        }
        // listen for events
        while (gKeepRunning)
          gEventQ->ProcessPendingEvents();
        printf("tmModuleTest: end standard\n");
        break;
      case 'b':
        printf("tmModuleTest: start broadcast\n");
        // post a BUNCH of messages
        for (; i < 50; i++) {
          gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
        }
        // listen for events
        while (gKeepRunning)
          gEventQ->ProcessPendingEvents();
        printf("tmModuleTest: end broadcast\n");
        break;
      case 'f':
        printf("tmModuleTest: start flush\n");
        // post a couple events
        for (; i < 5; i++) {
          gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
        }
        // flush the queue
        gTransServ->Flush(nsDependentCString(queueName), PR_TRUE);
        // post a couple events
        for (i=0; i < 8; i++) {
          gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
        }
        // listen for events
        while (gKeepRunning)
          gEventQ->ProcessPendingEvents();
        // detach
        gTransServ->Detach(nsDependentCString(queueName));
        printf("tmModuleTest: end flush\n");
        break;
      case 'h':
        printf("tmModuleTest: start hit-n-run\n");
        // post a couple events
        for (; i < 5; i++) {
          gTransServ->PostTransaction(nsDependentCString(queueName), (PRUint8 *)data, dataLen);
        }
        // detach
        gTransServ->Detach(nsDependentCString(queueName));
        printf("tmModuleTest: end hit-n-run\n");
        break;
      case 'l':
        printf("tmModuleTest: start listener\n");
        // listen for events
        while (gKeepRunning)
          gEventQ->ProcessPendingEvents();
        printf("tmModuleTest: end listener\n");
        break;
      default :
        printf("tmModuleTest: start & end default\n");
        break;
    }

    // shutdown process

    NS_RELEASE(gTransServ);
    NS_RELEASE(gIpcServ);

    printf("tmModuleTest: processing remaining events\n");

    // process any remaining events
    PLEvent *ev;
    while (NS_SUCCEEDED(gEventQ->GetEvent(&ev)) && ev)
      gEventQ->HandleEvent(ev);

    printf("tmModuleTest: done\n");
  } // this scopes the nsCOMPtrs

  // helps with shutdown on some cases
  PR_Sleep(PR_SecondsToInterval(4));

  // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  rv = NS_ShutdownXPCOM(nsnull);
  NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");

  return 0;
}

Here is the call graph for this function:


Variable Documentation

char* data = new char[NameSize]

Definition at line 69 of file tmModuleTest.cpp.

Definition at line 70 of file tmModuleTest.cpp.

Definition at line 84 of file tmModuleTest.cpp.

Definition at line 87 of file tmModuleTest.cpp.

Definition at line 85 of file tmModuleTest.cpp.

Definition at line 88 of file tmModuleTest.cpp.

const int NameSize = 1024

Definition at line 61 of file tmModuleTest.cpp.

PRIntn optDebug = 0

Definition at line 64 of file tmModuleTest.cpp.

char optMode = 's'

Definition at line 65 of file tmModuleTest.cpp.

char* profileName = new char[NameSize]

Definition at line 66 of file tmModuleTest.cpp.

char* queueName = new char[NameSize]

Definition at line 67 of file tmModuleTest.cpp.