00001 #ifndef FILE_CONFIGURATION_THREAD_HH
00002 #define FILE_CONFIGURATION_THREAD_HH
00003 #include "../core/ConfigurationThread.h"
00004 #include <OpenTracker/misc/xml/XMLWriter.h>
00005 #include <ace/Thread.h>
00006 #include <string>
00007 #include <fstream>
00008
00009
00010
00011 #ifdef PLATFORM_WIN32
00012 # define WIN32_LEAN_AND_MEAN
00013 # include <windows.h>
00014 # undef WIN32_LEAN_AND_MEAN
00015 # define DIR_HDL HANDLE
00016 # define FILE_TIME FILETIME
00017 # define CLOSEDIR(blah) FindClose(blah)
00018 # define COMPARE_FTIME(later, earlier) CompareFileTime(&later, &earlier)
00019 # define SEP "\\"
00020 #else
00021 # include <dirent.h>
00022 # define DIR_HDL DIR*
00023 # define FILE_TIME time_t
00024 # define CLOSEDIR(blah) closedir(blah)
00025 # define COMPARE_FTIME(later, earlier) later > earlier
00026 # define SEP "/"
00027 #endif //__WIN32__
00028
00029
00030 namespace ot{
00031
00032 class FileConfigurationThread: public ConfigurationThread{
00033
00034 protected :
00035
00036 Configurator * config;
00037 void * thread;
00038 bool finishflag;
00039 std::string filename;
00040 FILE_TIME last;
00041 FILE_TIME current;
00042
00043 DIR_HDL hFile;
00044
00045 void backupCurrentGraph(){
00046 XMLWriter writer(*(config->getContext()));
00047 logPrintI( "CONFIG_FUNC::writing backup \n");
00048 writer.write("backup.xml");
00049 };
00050
00051 void initializeFileTime(){
00052 # ifdef PLATFORM_WIN32
00053 if (! timestamp(filename.c_str(), &last)){
00054 last.dwLowDateTime =0;
00055 last.dwHighDateTime=0;
00056 }
00057 # else
00058 if ( !timestamp(filename.c_str(), &last)){
00059 last =0;
00060 }
00061 # endif
00062 current = last;
00063 }
00064
00065 bool timestamp(const char *file, FILE_TIME * time){
00066 bool result = false;
00067 # ifdef PLATFORM_WIN32
00068
00069 struct _WIN32_FIND_DATAA data;
00070 DIR_HDL dir = FindFirstFile(file, &data);
00071 if (dir != INVALID_HANDLE_VALUE){
00072 (*time) = data.ftLastWriteTime;
00073 FindClose(dir);
00074 result = true;
00075 }
00076
00077
00078 # else
00079 struct stat info;
00080 if (stat(file, &info)==0){
00081 *time =info.st_mtime;
00082 result = true;
00083 }
00084 # endif
00085 return result;
00086 }
00087
00088
00089 std::string readFileIntoString(const char * fname){
00090 std::string line, result;
00091 std::ifstream in;
00092 in.open(fname);
00093 while(getline(in, line)){
00094 result += line + "\n";
00095 }
00096 return result;
00097 }
00098
00099 public:
00100 FileConfigurationThread( const char * fname):
00101 thread(0), finishflag(false), filename(fname){
00102 config = Configurator::instance();
00103 initializeFileTime();
00104
00105 }
00106 ~FileConfigurationThread(){
00107 closeThread();
00108
00109 }
00110
00111
00112
00113 void start()
00114 {
00115 logPrintI( "START_THREAD::starting FileConfiguration thread\n");
00116 thread = new ACE_thread_t;
00117 ACE_Thread::spawn((ACE_THR_FUNC)ConfigurationThread::thread_func,
00118 this,
00119 THR_NEW_LWP | THR_JOINABLE,
00120 (ACE_thread_t *)thread );
00121 }
00122
00123
00124
00125 void configTask(){
00126 logPrintI("CONFIG_FUNC::starting the new thread\n");
00127
00128
00129 # ifdef WIN32
00130 logPrintI( "CONFIG_FUNC:: initial timestamp: %d %d\n", last.dwLowDateTime ,last.dwHighDateTime );
00131
00132 # endif //WIN32
00133
00134 while (!finishflag){
00135
00136
00137 timestamp(filename.c_str(), ¤t);
00138
00139 if (COMPARE_FTIME(current, last)){
00140
00141
00142 #ifdef OT_BACKUP_ON_RECONFIG
00143 backupCurrentGraph();
00144 #endif //OT_BACKUP_ON_RECONFIG
00145 logPrintI( "CONFIG_FUNC::changing configuration\n");
00146
00147 config->changeConfiguration(filename);
00148 logPrintI( "CONFIG_FUNC::configuration changed\n");
00149 last = current;
00150
00151
00152
00153 }
00154 OSUtils::sleep(1000);
00155 }
00156 }
00157
00158
00159 void closeThread()
00160 {
00161 # ifdef WIN32
00162 ACE_Thread::join( (ACE_thread_t*)thread );
00163 # else
00164 ACE_Thread::join( (ACE_thread_t)thread );
00165 # endif
00166
00167 delete ((ACE_thread_t *)thread);
00168 }
00169
00170 void finish(){
00171 finishflag = true;
00172 }
00173
00174 };
00175
00176
00177
00178 }
00179
00180 #endif //FILE_CONFIGURATION_THREAD_HH
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196