util/Timeout.h

Go to the documentation of this file.
00001 /*-----------------------------------------------------------------------------
00002 Name:      Timeout.h
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Allows you be called back after a given delay.
00006 -----------------------------------------------------------------------------*/
00007 
00008 #ifndef _UTIL_TIMEOUT_H
00009 #define _UTIL_TIMEOUT_H
00010 
00011 #include <util/xmlBlasterDef.h>
00012 #include <util/I_Timeout.h>
00013 #include <util/Timestamp.h>
00014 
00015 #include <string>
00016 #include <map>
00017 #include <util/thread/ThreadImpl.h>
00018 
00019 namespace org { namespace xmlBlaster { namespace util {
00020 
00021    typedef std::pair<I_Timeout*, void*> Container;
00022    typedef std::map<org::xmlBlaster::util::Timestamp, Container> TimeoutMap;
00023 
00024 
00082 class Dll_Export Timeout : public org::xmlBlaster::util::thread::Thread
00083 {
00084 
00085  private: 
00087    std::string ME; //  = "Timeout";
00088    std::string threadName_;
00089 //   boost::thread* runningThread_;
00091    TimeoutMap timeoutMap_;
00092    // private TreeMap std::map = null;
00094    bool isRunning_; //  = true;
00096    bool isReady_; //  = false;
00097 
00099    bool mapHasNewEntry_;
00100 
00102    bool isActive_;
00104    const bool isDebug_; //  = false;
00105 
00106    const bool detached_;
00107 
00108    org::xmlBlaster::util::TimestampFactory& timestampFactory_;
00109 
00110    org::xmlBlaster::util::Global& global_;
00111    org::xmlBlaster::util::I_Log&    log_;
00112 
00114    org::xmlBlaster::util::thread::Mutex invocationMutex_;   
00115    org::xmlBlaster::util::thread::Mutex waitForTimeoutMutex_;
00116    org::xmlBlaster::util::thread::Condition waitForTimeoutCondition_;
00117 
00118    size_t getTimeoutMapSize();
00119 
00123     bool start(bool detached);
00124 
00125 //   friend class TimeoutRunner;
00126  public:
00127 
00131    Timeout(org::xmlBlaster::util::Global& global);
00132 
00136    Timeout(org::xmlBlaster::util::Global& global, const std::string &name);
00137 
00138     ~Timeout();
00139 
00144    void join();
00145 
00147 //   void operator()();
00148 
00153    int getSize() const {
00154       return (int)timeoutMap_.size();
00155    }
00156 
00175     org::xmlBlaster::util::Timestamp addTimeoutListener(I_Timeout *listener, long delay, void *userData);
00176 
00198    org::xmlBlaster::util::Timestamp refreshTimeoutListener(org::xmlBlaster::util::Timestamp key, long delay); 
00199 
00206     org::xmlBlaster::util::Timestamp addOrRefreshTimeoutListener(I_Timeout *listener, 
00207                                                   long delay, void *userData, org::xmlBlaster::util::Timestamp key);
00208    // throws org::xmlBlaster::util::XmlBlasterException
00209 
00217    void removeTimeoutListener(org::xmlBlaster::util::Timestamp key);
00218 
00227    bool isExpired(org::xmlBlaster::util::Timestamp key);
00228 
00237    long spanToTimeout(org::xmlBlaster::util::Timestamp key);
00238 
00248    long getTimeout(org::xmlBlaster::util::Timestamp key);
00249 
00253    void removeAll();
00254 
00258    void shutdown();
00259 
00260    void run();
00261 
00262 };
00263 
00264 }}} // namespaces
00265 
00266 #endif
00267 
00268