Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsimportscheduler.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #include "mmscore/mmsimportscheduler.h"
00034 
00035 #define SCHEDULER_SLEEP_TIME 10 /* sleep time in seconds */
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     /* run with the minimum priority */
00049 //  this->setSchedulePriority(PRIORITY_MIN);
00050 
00051     /* save access to plugin manager */
00052     this->pluginManager = pluginManager;
00053 
00054     /* initialize the service objects */
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     /* get all import plugins */
00075     pluginList = this->pluginService->getImportPlugins();
00076 
00077     int base_time = time(NULL);
00078 
00079     /* go through the old importPlugins list -> delete entries */
00080     for(unsigned i=0; i<importPlugins.size(); i++) {
00081         /* check if entry is in pluginList */
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             /* delete old entry from importPlugins */
00097             DEBUGMSG("IMPORTSCHEDULER", "delete %s", importPlugins.at(i)->plugin->getName().c_str());
00098             importPlugins.erase(importPlugins.begin()+i);
00099         }
00100     }
00101 
00102     /* go through the new plugin list -> get import properties for each import plugin */
00103     for(unsigned i=0; i<pluginList.size(); i++) {
00104         /* check if entry is already in importPlugins */
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             /* delete incactive plugin */
00117             continue;
00118         }
00119 
00120         if (!found) {
00121             /* have to create new importPlugins entry */
00122             IMPORT_PLUGINS *ip = new IMPORT_PLUGINS;
00123 
00124             /* fill plugin */
00125             ip->plugin = pluginList.at(i);
00126 
00127             /* fill import property */
00128             ip->importProperty = this->importPropertyService->getImportPropertyByPlugin(ip->plugin);
00129 
00130             /* get the handler */
00131             ip->pluginHandler = this->pluginManager->getImportPluginHandler(ip->plugin->getId());
00132 
00133             /* calculate the nextTime */
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             /* add item */
00144             importPlugins.push_back(ip);
00145         }
00146         else {
00147             /* take the old entry and update it */
00148             /* update plugin */
00149             importPlugins.at(j)->plugin = pluginList.at(i);
00150 
00151             /* update import property */
00152             importPlugins.at(j)->importProperty = this->importPropertyService->getImportPropertyByPlugin(importPlugins.at(j)->plugin);
00153 
00154             /* get the handler */
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     /* wait a little so other components can start faster */
00164     sleep(2);
00165 
00166     try {
00167         while(1) {
00168                 /* get all import plugins */
00169                 getImportPlugins();
00170 
00171                 /* get the current time */
00172                 curr_time = time(NULL);
00173 
00174                 /* through all my import plugins */
00175                 for(unsigned int i=0; i<importPlugins.size(); i++) {
00176                     /* check something */
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                     /* invoke execute import */
00185                     /* TODO: open a thread */
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     /* self destruct */
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 

Generated by doxygen