reSIProcate/repro  9694
Dispatcher.hxx
Go to the documentation of this file.
00001 #ifndef DISPATCHER_HXX
00002 #define DISPATCHER_HXX 1
00003 
00004 #include "repro/WorkerThread.hxx"
00005 #include "repro/Worker.hxx"
00006 #include "resip/stack/ApplicationMessage.hxx"
00007 #include "rutil/TimeLimitFifo.hxx"
00008 #include "rutil/RWMutex.hxx"
00009 #include "rutil/Lock.hxx"
00010 #include <vector>
00011 
00012 namespace resip
00013 { 
00014    class SipStack; 
00015    class ApplicationMessage; 
00016 };
00017 
00018 namespace repro
00019 {
00020 
00021 /**
00022    @class Dispatcher
00023    
00024    @brief A generic thread-bank class.
00025    
00026    This is intended to be a generic thread-bank class that operates under
00027    the paradigm of accepting messages, modifying those messages, and posting
00028    the messages back to the originator through the SipStack. (Since this uses
00029    ApplicationMessages, the message will automatically get back to the correct
00030    TransactionUser; what happens from there is the coder's business.)
00031    
00032    This class gets
00033    generality by using prototyping of a very simple class, Worker. All the
00034    user must do is subclass Worker to do the work needed, and pass an instance
00035    of this class when constructing the Dispatcher. Dispatcher will clone this
00036    Worker as many times as needed to fill the thread bank. 
00037    
00038    @note The functions in this class are intended to be thread-safe.
00039 */
00040 
00041 class Dispatcher
00042 {
00043    public:
00044    
00045       /**
00046          @param prototype The prototypical instance of Worker.
00047          
00048          @param stack The stack to post messages to.
00049          
00050          @param workers The number of threads in this bank.
00051          
00052          @param startImmediately Whether to start this thread bank on 
00053             construction.
00054       */
00055       Dispatcher(std::auto_ptr<Worker> prototype, 
00056                   resip::SipStack* stack,
00057                   int workers=2, 
00058                   bool startImmediately=true);
00059 
00060       virtual ~Dispatcher();
00061       
00062       /**
00063          Posts a message to this thread bank.
00064          
00065          @param work The message that conveys the work that needs to be done.
00066             It is understood that any information that needs to be conveyed
00067             back to the originator will be placed in the message by the Workers
00068             in the thread bank.
00069             
00070          @returns true iff this message was successfully posted. (This may not
00071             be the case if this Dispatcher is in the process of shutting down)
00072       */
00073       virtual bool post(std::auto_ptr<resip::ApplicationMessage>& work);
00074 
00075       /**
00076          @returns The number of messages in this Dispatcher's queue
00077       */
00078       size_t fifoCountDepth() const;
00079       
00080       /**
00081          @returns The time between which the front of the queue was posted and
00082             the back of the queue was posted.
00083       */ 
00084       time_t fifoTimeDepth() const;
00085       
00086       /**
00087          @returns The number of workers in this thread bank.
00088       */
00089       int workPoolSize() const;
00090       
00091       /**
00092          This Dispatcher will stop accepting new
00093          work, but processing will continue normally on the messages already
00094          in the queue.
00095       */
00096       void stop();
00097       
00098       /**
00099          Resumes accepting work.
00100       */
00101       void resume();
00102       
00103       /**
00104          Shuts down the thread-bank.
00105       */
00106       void shutdownAll();
00107       
00108       
00109       /**
00110          Starts the thread bank.
00111       */
00112       void startAll();
00113 
00114       resip::SipStack* mStack;
00115 
00116       
00117    protected:
00118 
00119       resip::TimeLimitFifo<resip::ApplicationMessage> mFifo;
00120       bool mAcceptingWork;
00121       bool mShutdown;
00122       bool mStarted;
00123       Worker* mWorkerPrototype;
00124 
00125       resip::RWMutex mMutex;
00126 
00127       std::vector<WorkerThread*> mWorkerThreads;
00128 
00129    private:
00130       //No copying!
00131       Dispatcher(const Dispatcher& toCopy);
00132       Dispatcher& operator=(const Dispatcher& toCopy);
00133 };
00134 
00135 }
00136 
00137 #endif
00138 
00139 /* ====================================================================
00140  * The Vovida Software License, Version 1.0 
00141  * 
00142  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00143  * 
00144  * Redistribution and use in source and binary forms, with or without
00145  * modification, are permitted provided that the following conditions
00146  * are met:
00147  * 
00148  * 1. Redistributions of source code must retain the above copyright
00149  *    notice, this list of conditions and the following disclaimer.
00150  * 
00151  * 2. Redistributions in binary form must reproduce the above copyright
00152  *    notice, this list of conditions and the following disclaimer in
00153  *    the documentation and/or other materials provided with the
00154  *    distribution.
00155  * 
00156  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00157  *    and "Vovida Open Communication Application Library (VOCAL)" must
00158  *    not be used to endorse or promote products derived from this
00159  *    software without prior written permission. For written
00160  *    permission, please contact vocal@vovida.org.
00161  *
00162  * 4. Products derived from this software may not be called "VOCAL", nor
00163  *    may "VOCAL" appear in their name, without prior written
00164  *    permission of Vovida Networks, Inc.
00165  * 
00166  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00167  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00168  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00169  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00170  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00171  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00172  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00173  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00174  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00175  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00176  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00177  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00178  * DAMAGE.
00179  * 
00180  * ====================================================================
00181  * 
00182  * This software consists of voluntary contributions made by Vovida
00183  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00184  * Inc.  For more information on Vovida Networks, Inc., please see
00185  * <http://www.vovida.org/>.
00186  *
00187  */