00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
00033 #include "mmscore/mmsimportscheduler.h"
00034 
00035 #define SCHEDULER_SLEEP_TIME 10 
00036 
00037 MMSImportScheduler::MMSImportScheduler(MMSPluginManager *pluginManager) {
00038 
00039     MMSConfigData *config = new MMSConfigData();
00040     DataSource    *source = new DataSource(config->getConfigDBDBMS(),
00041                                            config->getConfigDBDatabase(),
00042                                            config->getConfigDBAddress(),
00043                                            config->getConfigDBPort(),
00044                                            config->getConfigDBUser(),
00045                                            config->getConfigDBPassword());
00046     delete config;
00047 
00048     
00049 
00050 
00051     
00052     this->pluginManager = pluginManager;
00053 
00054     
00055     this->pluginService = new MMSPluginService(source);
00056     this->importPropertyService = new MMSImportPropertyService(source);
00057 }
00058 
00059 MMSImportScheduler::~MMSImportScheduler() {
00060     for(vector<IMPORT_PLUGINS*>::iterator it(importPlugins.begin()); it != importPlugins.end(); ++it) {
00061         delete(((IMPORT_PLUGINS*)(*it))->plugin);
00062         delete(((IMPORT_PLUGINS*)(*it))->importProperty);
00063         delete(((IMPORT_PLUGINS*)(*it))->pluginHandler);
00064         delete(*it);
00065     }
00066     delete this->pluginService;
00067     delete this->importPropertyService;
00068 }
00069 
00070 void MMSImportScheduler::getImportPlugins() {
00071     vector<MMSPluginData *> pluginList;
00072 
00073     DEBUGMSG("IMPORTSCHEDULER", "getImportPlugins()");
00074     
00075     pluginList = this->pluginService->getImportPlugins();
00076 
00077     int base_time = time(NULL);
00078 
00079     
00080     for(unsigned i=0; i<importPlugins.size(); i++) {
00081         
00082         bool found=false;
00083         unsigned j=0;
00084 
00085         DEBUGMSG("IMPORTSCHEDULER", "Work with %s", importPlugins.at(i)->plugin->getName().c_str());
00086         while(j<pluginList.size()) {
00087             if (importPlugins.at(i)->plugin->getId() == pluginList.at(j)->getId()) {
00088                 found=true;
00089                 break;
00090             }
00091             j++;
00092         }
00093 
00094         if (!found) {
00095 
00096             
00097             DEBUGMSG("IMPORTSCHEDULER", "delete %s", importPlugins.at(i)->plugin->getName().c_str());
00098             importPlugins.erase(importPlugins.begin()+i);
00099         }
00100     }
00101 
00102     
00103     for(unsigned i=0; i<pluginList.size(); i++) {
00104         
00105         bool found=false;
00106         unsigned j=0;
00107         while(j<importPlugins.size()) {
00108             if (pluginList.at(i)->getId() == importPlugins.at(j)->plugin->getId()) {
00109                 found=true;
00110                 break;
00111             }
00112             j++;
00113         }
00114 
00115         if (!pluginList.at(i)->getActive()) {
00116             
00117             continue;
00118         }
00119 
00120         if (!found) {
00121             
00122             IMPORT_PLUGINS *ip = new IMPORT_PLUGINS;
00123 
00124             
00125             ip->plugin = pluginList.at(i);
00126 
00127             
00128             ip->importProperty = this->importPropertyService->getImportPropertyByPlugin(ip->plugin);
00129 
00130             
00131             ip->pluginHandler = this->pluginManager->getImportPluginHandler(ip->plugin->getId());
00132 
00133             
00134             ip->nextTime = base_time;
00135             if (!ip->importProperty->getOnStartUp()) {
00136                 if (ip->importProperty->getTime())
00137                     ip->nextTime += ip->importProperty->getTime();
00138                 else
00139                     ip->nextTime = 0;
00140             }
00141             base_time += SCHEDULER_SLEEP_TIME;
00142 
00143             
00144             importPlugins.push_back(ip);
00145         }
00146         else {
00147             
00148             
00149             importPlugins.at(j)->plugin = pluginList.at(i);
00150 
00151             
00152             importPlugins.at(j)->importProperty = this->importPropertyService->getImportPropertyByPlugin(importPlugins.at(j)->plugin);
00153 
00154             
00155             importPlugins.at(j)->pluginHandler = this->pluginManager->getImportPluginHandler(importPlugins.at(j)->plugin->getId());
00156         }
00157     }
00158 }
00159 
00160 void MMSImportScheduler::threadMain() {
00161     time_t curr_time;
00162 
00163     
00164     sleep(2);
00165 
00166     try {
00167         while(1) {
00168                 
00169                 getImportPlugins();
00170 
00171                 
00172                 curr_time = time(NULL);
00173 
00174                 
00175                 for(unsigned int i=0; i<importPlugins.size(); i++) {
00176                     
00177                     if (!importPlugins.at(i)->nextTime) continue;
00178                     if (importPlugins.at(i)->nextTime > curr_time) continue;
00179                     if (importPlugins.at(i)->importProperty->getInterval() <= 0)
00180                         importPlugins.at(i)->nextTime = 0;
00181                     else
00182                         importPlugins.at(i)->nextTime = curr_time + importPlugins.at(i)->importProperty->getInterval();
00183 
00184                     
00185                     
00186                     try {
00187                         importPlugins.at(i)->pluginHandler->invokeExecute(NULL);
00188                     } catch(MMSError &error) {
00189                         DEBUGMSG("IMPORTSCHEDULER", "Abort import due to: %s", error.getMessage().c_str());
00190                     }
00191 
00192                 }
00193 
00194                 sleep(SCHEDULER_SLEEP_TIME);
00195         }
00196     } catch(MMSError &error) {
00197         DEBUGMSG("IMPORTSCHEDULER", "Abort import due to: %s", error.getMessage().c_str());
00198     }
00199 
00200     
00201     delete this;
00202 }
00203 
00204 void MMSImportScheduler::executeImport(int pluginID) {
00205     for(unsigned int i = 0;i<importPlugins.size();i++) {
00206         if(importPlugins.at(i)->plugin->getId()==pluginID) {
00207             importPlugins.at(i)->pluginHandler->invokeExecute();
00208             return;
00209         }
00210     }
00211 }
00212