reSIProcate/repro  9694
Dispatcher.cxx
Go to the documentation of this file.
00001 #include "repro/Dispatcher.hxx"
00002 #include "resip/stack/Message.hxx"
00003 #include "resip/stack/SipStack.hxx"
00004 #include "rutil/WinLeakCheck.hxx"
00005 
00006 
00007 namespace repro
00008 {
00009 
00010 Dispatcher::Dispatcher(std::auto_ptr<Worker> prototype,
00011                         resip::SipStack* stack,
00012                         int workers, 
00013                         bool startImmediately):
00014    mStack(stack),
00015    mFifo(0,0),
00016    mAcceptingWork(false),
00017    mShutdown(false),
00018    mStarted(false),
00019    mWorkerPrototype(prototype.release())
00020 {
00021    for(int i=0; i<workers;i++)
00022    {
00023       mWorkerThreads.push_back(new WorkerThread(mWorkerPrototype->clone(),mFifo,mStack));
00024    }
00025    
00026    if(startImmediately)
00027    {
00028       startAll();
00029    }
00030 }
00031 
00032 Dispatcher::~Dispatcher()
00033 {
00034    shutdownAll();
00035    
00036    std::vector<WorkerThread*>::iterator i;
00037    for(i=mWorkerThreads.begin(); i!=mWorkerThreads.end(); ++i)
00038    {
00039       delete *i;
00040    }
00041 
00042    mWorkerThreads.clear();
00043 
00044    while(!mFifo.empty())
00045    {
00046       delete mFifo.getNext();
00047    }
00048    
00049    delete mWorkerPrototype;
00050    
00051 }
00052 
00053 bool
00054 Dispatcher::post(std::auto_ptr<resip::ApplicationMessage>& work)
00055 {
00056    resip::ReadLock r(mMutex);
00057    if(mAcceptingWork)
00058    {
00059       mFifo.add(work.release(),
00060                   resip::TimeLimitFifo<resip::ApplicationMessage>::InternalElement);
00061       return true;
00062    }
00063    
00064    return false;
00065    
00066    //If we aren't accepting work, the auto ptr is not released. (We don't
00067    // take ownership, and the caller gets to handle the contents of the 
00068    // auto_ptr)
00069 }
00070 
00071 size_t
00072 Dispatcher::fifoCountDepth() const 
00073 {
00074    return mFifo.getCountDepth();
00075 }
00076 
00077 time_t
00078 Dispatcher::fifoTimeDepth() const 
00079 {
00080    return mFifo.getTimeDepth();
00081 }
00082 
00083 int
00084 Dispatcher::workPoolSize() const 
00085 {
00086    return (int)mWorkerThreads.size();
00087 }
00088 
00089 void
00090 Dispatcher::stop()
00091 {
00092    resip::WriteLock w(mMutex);
00093    mAcceptingWork=false;
00094 }
00095 
00096 void
00097 Dispatcher::resume()
00098 {
00099    resip::WriteLock w(mMutex);
00100    mAcceptingWork = !mShutdown;
00101 }
00102 
00103 void
00104 Dispatcher::shutdownAll()
00105 {
00106    resip::WriteLock w(mMutex);
00107    if(!mShutdown)
00108    {
00109       mAcceptingWork=false;
00110       mShutdown=true;
00111       
00112       std::vector<WorkerThread*>::iterator i;
00113       for(i=mWorkerThreads.begin(); i!=mWorkerThreads.end(); ++i)
00114       {
00115          (*i)->shutdown();
00116          (*i)->join();
00117       }
00118    }
00119 }
00120 
00121 void 
00122 Dispatcher::startAll()
00123 {
00124    resip::WriteLock w(mMutex);
00125    if(!mShutdown && !mStarted)
00126    {
00127       std::vector<WorkerThread*>::iterator i;
00128       for(i=mWorkerThreads.begin(); i!=mWorkerThreads.end(); ++i)
00129       {
00130          (*i)->run();
00131       }
00132       mStarted=true;
00133       mAcceptingWork=true;
00134    }
00135 }
00136 
00137 } //namespace repro
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  */