UniSet  1.4.0
TimerService.h
См. документацию.
00001 /* This file is part of the UniSet project
00002  * Copyright (c) 2002 Free Software Foundation, Inc.
00003  * Copyright (c) 2002 Pavel Vainerman
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018  */
00019 // --------------------------------------------------------------------------
00024 //---------------------------------------------------------------------------
00025 #ifndef TimerService_H_
00026 #define TimerService_H_
00027 //---------------------------------------------------------------------------
00028 #include <list>
00029 #include <functional>
00030 #include <omnithread.h>
00031 #include <string>
00032 #include "UniSetTypes.h"
00033 #include "UniSetObject.h"
00034 #include "TimerService_i.hh"
00035 #include "PassiveTimer.h"
00036 //#include "OmniThreadCreator.h"
00037 #include "ThreadCreator.h"
00038 
00039 //-----------------------------------------------------------------------------------
00097 class TimerService: 
00098     public POA_TimerService_i,
00099     public UniSetObject
00100 {
00101     public:
00102         
00103         TimerService( UniSetTypes::ObjectId id, const std::string confNodeName="LocalTimerService");
00104         ~TimerService();
00105 
00107         virtual void askTimer( const TimerService_i::Timer& ti, const UniSetTypes::ConsumerInfo& ci);
00108         void printList();
00109 
00110     protected:
00111         TimerService(const std::string confNodeName="LocalTimerService");
00112 
00114         struct TimerInfo
00115         {
00116             TimerInfo():id(0), curTimeMS(0){};
00117             TimerInfo(CORBA::Short id, CORBA::Long timeMS, const UniSetTypes::ConsumerInfo& cinf, 
00118                         CORBA::Short cnt, unsigned int askLifeTime, UniSetTypes::Message::Priority p):
00119                 cinf(cinf),
00120                 ref(0),
00121                 id(id), 
00122                 curTimeMS(timeMS),
00123                 priority(p),
00124                 curTick(cnt-1),
00125                 not_ping(false)
00126             {
00127                 tmr.setTiming(timeMS);
00128                 lifetmr.setTiming(askLifeTime*1000); // [сек]
00129             };
00130             
00131             inline void reset()
00132             {
00133                 curTimeMS = tmr.getInterval();
00134                 tmr.reset();
00135             }
00136             
00137             UniSetTypes::ConsumerInfo cinf;     
00138             UniSetObject_i_var ref;             
00139             UniSetTypes::TimerId id;            
00140             timeout_t curTimeMS;                        
00141             UniSetTypes::Message::Priority priority;    
00147             CORBA::Short curTick; 
00148             
00149             // заказчик с меньшим временем ожидания имеет больший приоритет
00150             bool operator < ( const TimerInfo& ti ) const
00151             { 
00152                 return curTimeMS > ti.curTimeMS; 
00153             }
00154 
00155             PassiveTimer tmr;
00156             PassiveTimer lifetmr;   
00157             bool not_ping;          /* признак недоступности заказчика */
00158         };
00159 
00160         typedef std::list<TimerInfo> TimersList;            
00161         
00163         virtual bool send(TimerInfo& ti);
00164 
00166         virtual bool disactivateObject();
00167         virtual bool activateObject();
00168         virtual void sigterm( int signo );
00169 
00170 
00171         unsigned int MaxCountTimers;    
00172         unsigned int AskLifeTimeSEC;    
00174         void init(const std::string& confnode);
00175         void work();
00176 
00177     private:
00178 
00179         bool terminate;
00180         bool isSleep;
00181         
00182         UniSetTimer* sleepTimer;    
00183         class Timer_eq: public std::unary_function<TimerInfo, bool>
00184         {
00185             public:         
00186                 Timer_eq(const UniSetTypes::ConsumerInfo& coi, CORBA::Short t):tid(t),ci(coi){}
00187 
00188             inline bool operator()(const TimerInfo& ti) const
00189             {
00190                 return ( ti.cinf.id == ci.id && ti.cinf.node == ci.node && ti.id == tid );
00191             }
00192 
00193             protected:
00194                 UniSetTypes::TimerId tid;
00195                 UniSetTypes::ConsumerInfo ci;
00196         };
00197 
00198         TimersList tlst;                
00200         UniSetTypes::uniset_mutex lstMutex; 
00201         int execute_pid; 
00202         ThreadCreator<TimerService>* exthread;      
00203 };
00204 
00205 #endif