reSIProcate/repro  9694
Public Member Functions | Public Attributes | Protected Attributes | Private Member Functions
repro::Dispatcher Class Reference

A generic thread-bank class. More...

#include <Dispatcher.hxx>

Collaboration diagram for repro::Dispatcher:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 Dispatcher (std::auto_ptr< Worker > prototype, resip::SipStack *stack, int workers=2, bool startImmediately=true)
virtual ~Dispatcher ()
virtual bool post (std::auto_ptr< resip::ApplicationMessage > &work)
 Posts a message to this thread bank.
size_t fifoCountDepth () const
time_t fifoTimeDepth () const
int workPoolSize () const
void stop ()
 This Dispatcher will stop accepting new work, but processing will continue normally on the messages already in the queue.
void resume ()
 Resumes accepting work.
void shutdownAll ()
 Shuts down the thread-bank.
void startAll ()
 Starts the thread bank.

Public Attributes

resip::SipStackmStack

Protected Attributes

resip::TimeLimitFifo
< resip::ApplicationMessage
mFifo
bool mAcceptingWork
bool mShutdown
bool mStarted
WorkermWorkerPrototype
resip::RWMutex mMutex
std::vector< WorkerThread * > mWorkerThreads

Private Member Functions

 Dispatcher (const Dispatcher &toCopy)
Dispatcheroperator= (const Dispatcher &toCopy)

Detailed Description

A generic thread-bank class.

This is intended to be a generic thread-bank class that operates under the paradigm of accepting messages, modifying those messages, and posting the messages back to the originator through the SipStack. (Since this uses ApplicationMessages, the message will automatically get back to the correct TransactionUser; what happens from there is the coder's business.)

This class gets generality by using prototyping of a very simple class, Worker. All the user must do is subclass Worker to do the work needed, and pass an instance of this class when constructing the Dispatcher. Dispatcher will clone this Worker as many times as needed to fill the thread bank.

Note:
The functions in this class are intended to be thread-safe.

Definition at line 41 of file Dispatcher.hxx.


Constructor & Destructor Documentation

repro::Dispatcher::Dispatcher ( std::auto_ptr< Worker prototype,
resip::SipStack stack,
int  workers = 2,
bool  startImmediately = true 
)
Parameters:
prototypeThe prototypical instance of Worker.
stackThe stack to post messages to.
workersThe number of threads in this bank.
startImmediatelyWhether to start this thread bank on construction.

Definition at line 10 of file Dispatcher.cxx.

                                              :
   mStack(stack),
   mFifo(0,0),
   mAcceptingWork(false),
   mShutdown(false),
   mStarted(false),
   mWorkerPrototype(prototype.release())
{
   for(int i=0; i<workers;i++)
   {
      mWorkerThreads.push_back(new WorkerThread(mWorkerPrototype->clone(),mFifo,mStack));
   }
   
   if(startImmediately)
   {
      startAll();
   }
}
repro::Dispatcher::~Dispatcher ( ) [virtual]

Definition at line 32 of file Dispatcher.cxx.

{
   shutdownAll();
   
   std::vector<WorkerThread*>::iterator i;
   for(i=mWorkerThreads.begin(); i!=mWorkerThreads.end(); ++i)
   {
      delete *i;
   }

   mWorkerThreads.clear();

   while(!mFifo.empty())
   {
      delete mFifo.getNext();
   }
   
   delete mWorkerPrototype;
   
}
repro::Dispatcher::Dispatcher ( const Dispatcher toCopy) [private]

Member Function Documentation

size_t repro::Dispatcher::fifoCountDepth ( ) const
Returns:
The number of messages in this Dispatcher's queue

Definition at line 72 of file Dispatcher.cxx.

{
   return mFifo.getCountDepth();
}
time_t repro::Dispatcher::fifoTimeDepth ( ) const
Returns:
The time between which the front of the queue was posted and the back of the queue was posted.

Definition at line 78 of file Dispatcher.cxx.

{
   return mFifo.getTimeDepth();
}
Dispatcher& repro::Dispatcher::operator= ( const Dispatcher toCopy) [private]
bool repro::Dispatcher::post ( std::auto_ptr< resip::ApplicationMessage > &  work) [virtual]

Posts a message to this thread bank.

Parameters:
workThe message that conveys the work that needs to be done. It is understood that any information that needs to be conveyed back to the originator will be placed in the message by the Workers in the thread bank.
Returns:
true iff this message was successfully posted. (This may not be the case if this Dispatcher is in the process of shutting down)

Definition at line 54 of file Dispatcher.cxx.

{
   resip::ReadLock r(mMutex);
   if(mAcceptingWork)
   {
      mFifo.add(work.release(),
                  resip::TimeLimitFifo<resip::ApplicationMessage>::InternalElement);
      return true;
   }
   
   return false;
   
   //If we aren't accepting work, the auto ptr is not released. (We don't
   // take ownership, and the caller gets to handle the contents of the 
   // auto_ptr)
}
void repro::Dispatcher::resume ( )

Resumes accepting work.

Definition at line 97 of file Dispatcher.cxx.

void repro::Dispatcher::shutdownAll ( )

Shuts down the thread-bank.

Definition at line 104 of file Dispatcher.cxx.

{
   resip::WriteLock w(mMutex);
   if(!mShutdown)
   {
      mAcceptingWork=false;
      mShutdown=true;
      
      std::vector<WorkerThread*>::iterator i;
      for(i=mWorkerThreads.begin(); i!=mWorkerThreads.end(); ++i)
      {
         (*i)->shutdown();
         (*i)->join();
      }
   }
}
void repro::Dispatcher::startAll ( )

Starts the thread bank.

Definition at line 122 of file Dispatcher.cxx.

{
   resip::WriteLock w(mMutex);
   if(!mShutdown && !mStarted)
   {
      std::vector<WorkerThread*>::iterator i;
      for(i=mWorkerThreads.begin(); i!=mWorkerThreads.end(); ++i)
      {
         (*i)->run();
      }
      mStarted=true;
      mAcceptingWork=true;
   }
}
void repro::Dispatcher::stop ( )

This Dispatcher will stop accepting new work, but processing will continue normally on the messages already in the queue.

Definition at line 90 of file Dispatcher.cxx.

int repro::Dispatcher::workPoolSize ( ) const
Returns:
The number of workers in this thread bank.

Definition at line 84 of file Dispatcher.cxx.

{
   return (int)mWorkerThreads.size();
}

Member Data Documentation

Definition at line 120 of file Dispatcher.hxx.

Definition at line 119 of file Dispatcher.hxx.

Definition at line 125 of file Dispatcher.hxx.

bool repro::Dispatcher::mShutdown [protected]

Definition at line 121 of file Dispatcher.hxx.

Definition at line 114 of file Dispatcher.hxx.

bool repro::Dispatcher::mStarted [protected]

Definition at line 122 of file Dispatcher.hxx.

Definition at line 123 of file Dispatcher.hxx.

Definition at line 127 of file Dispatcher.hxx.


The documentation for this class was generated from the following files: