00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef GRID_CLIENT_TOPOLOGY_REFRESH_THREAD_HPP_INCLUDED
00011 #define GRID_CLIENT_TOPOLOGY_REFRESH_THREAD_HPP_INCLUDED
00012
00013 #include <boost/asio.hpp>
00014 #include <boost/thread/thread.hpp>
00015 #include <boost/bind.hpp>
00016 #include <boost/date_time/posix_time/posix_time.hpp>
00017
00021 class GridClientRecurringEventThread {
00022 public:
00024 class EventListener {
00025 public:
00027 virtual ~EventListener() {
00028 }
00029
00031 virtual void onTimerEvent() = 0;
00032 };
00033
00040 GridClientRecurringEventThread(long refreshIntervalInMillis, EventListener& pLstnr)
00041 : work(new boost::asio::io_service::work(ioSrv)),
00042 thread(boost::bind(&boost::asio::io_service::run, &ioSrv)),
00043 refreshIntervalInMillis(refreshIntervalInMillis),
00044 timer(ioSrv, boost::posix_time::millisec(refreshIntervalInMillis)),
00045 lstnr(pLstnr) {
00046 timer.async_wait(boost::bind(&GridClientRecurringEventThread::onTimerHandler, this,
00047 boost::asio::placeholders::error));
00048 }
00049
00053 virtual ~GridClientRecurringEventThread() {
00054 timer.cancel();
00055
00056 delete work;
00057
00058 thread.interrupt();
00059 thread.join();
00060 }
00061
00062 protected:
00064 void onTimerHandler(const boost::system::error_code& error) {
00065 if (error == boost::asio::error::operation_aborted || thread.interruption_requested())
00066 return;
00067
00068 lstnr.onTimerEvent();
00069
00070 timer.expires_at(timer.expires_at() + boost::posix_time::milliseconds(refreshIntervalInMillis));
00071
00072 timer.async_wait(boost::bind(&GridClientRecurringEventThread::onTimerHandler, this,
00073 boost::asio::placeholders::error));
00074 }
00075
00076 private:
00078 boost::asio::io_service ioSrv;
00079
00080 boost::asio::io_service::work* work;
00081
00083 boost::thread thread;
00084
00086 long refreshIntervalInMillis;
00087
00089 boost::asio::deadline_timer timer;
00090
00092 EventListener& lstnr;
00093 };
00094
00095 #endif