Plugin.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Institute for Artificial Intelligence,
00005  *  Universität Bremen.
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/or other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the Institute for Artificial Intelligence,
00019  *     Universität Bremen, nor the names of its contributors may be
00020  *     used to endorse or promote products derived from this software
00021  *     without specific prior written permission.
00022  *
00023  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00024  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00025  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00026  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00027  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00028  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00029  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00030  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00032  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00033  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00034  *  POSSIBILITY OF SUCH DAMAGE.
00035  *********************************************************************/
00036 
00040 #include <Plugin.h>
00041 
00042 
00043 namespace beliefstate {
00044   namespace plugins {
00045     Plugin::Plugin() {
00046       this->setPluginID(createPluginID());
00047       m_bRunCycle = true;
00048       m_bDevelopmentPlugin = false;
00049       m_strVersion = "";
00050       
00051       ConfigSettings cfgsetCurrent = configSettings();
00052       if(cfgsetCurrent.bOnlyDisplayImportant) {
00053         this->setOnlyDisplayImportant(true);
00054       }
00055     }
00056     
00057     Plugin::~Plugin() {
00058       freePluginID(m_nID);
00059     }
00060     
00061     CDesignator* Plugin::getIndividualConfig() {
00062       return getPluginConfig(this->pluginName());
00063     }
00064     
00065     void Plugin::setDevelopmentPlugin(bool bDevelopmentPlugin) {
00066       m_bDevelopmentPlugin = bDevelopmentPlugin;
00067     }
00068     
00069     bool Plugin::developmentPlugin() {
00070       return m_bDevelopmentPlugin;
00071     }
00072     
00073     void Plugin::setPluginID(int nID) {
00074       m_nID = nID;
00075       
00076       std::stringstream sts;
00077       sts << this->pluginName();
00078       sts << "/";
00079       sts << this->pluginID();
00080       
00081       this->setMessagePrefixLabel(sts.str());
00082     }
00083     
00084     int Plugin::pluginID() {
00085       return m_nID;
00086     }
00087     
00088     Result Plugin::init(int argc, char** argv) {
00089       // Dummy.
00090       return defaultResult();
00091     }
00092     
00093     Result Plugin::deinit() {
00094       // Dummy.
00095       return defaultResult();
00096     }
00097     
00098     Result Plugin::cycle() {
00099       // Dummy.
00100       return defaultResult();
00101     }
00102     
00103     void Plugin::setSubscribedToEvent(std::string strEventName, bool bSubscribed) {
00104       m_lstSubscribedEventNames.remove(strEventName);
00105       
00106       if(bSubscribed) {
00107         m_lstSubscribedEventNames.push_back(strEventName);
00108       }
00109     }
00110     
00111     bool Plugin::subscribedToEvent(std::string strEventName) {
00112       for(string strCurrentName : m_lstSubscribedEventNames) {
00113         if(strCurrentName == strEventName) {
00114           return true;
00115         }
00116       }
00117       
00118       return false;
00119     }
00120     
00121     void Plugin::consumeEvent(Event evEvent) {
00122       // Dummy.
00123     }
00124     
00125     void Plugin::setOffersService(std::string strServiceName, bool bOffering) {
00126       m_lstOfferedServices.remove(strServiceName);
00127       
00128       if(bOffering) {
00129         m_lstOfferedServices.push_back(strServiceName);
00130       }
00131     }
00132     
00133     bool Plugin::offersService(std::string strServiceName) {
00134       for(string strCurrentName : m_lstOfferedServices) {
00135         if(strCurrentName == strServiceName) {
00136           return true;
00137         }
00138       }
00139       
00140       return false;
00141     }
00142     
00143     Event Plugin::consumeServiceEvent(ServiceEvent seServiceEvent) {
00144       Event evReturn = defaultEvent();
00145       
00146       if(seServiceEvent.siServiceIdentifier == SI_RESPONSE) {
00147         m_mtxReceivedServiceEventResponses.lock();
00148         m_lstReceivedServiceEventResponses.push_back(seServiceEvent);
00149         m_mtxReceivedServiceEventResponses.unlock();
00150       }
00151       
00152       return evReturn;
00153     }
00154     
00155     void Plugin::addDependency(std::string strPluginName) {
00156       m_lstDependencies.remove(strPluginName);
00157       m_lstDependencies.push_back(strPluginName);
00158     }
00159     
00160     bool Plugin::dependsOn(std::string strPluginName) {
00161       for(string strDepName : m_lstDependencies) {
00162         if(strDepName == strPluginName) {
00163           return true;
00164         }
00165       }
00166       
00167       return false;
00168     }
00169     
00170     std::list<std::string> Plugin::dependencies() {
00171       return m_lstDependencies;
00172     }
00173     
00174     void Plugin::deployCycleData(Result& resDeployTo) {
00175       m_mtxEventsStore.lock();
00176 
00177       if(m_lstEvents.size() > 0) {
00178         resDeployTo.lstEvents = m_lstEvents;
00179         m_lstEvents.clear();
00180       }
00181       
00182       m_mtxEventsStore.unlock();
00183       
00184       m_mtxServiceEventsStore.lock();
00185       
00186       if(m_lstServiceEvents.size() > 0) {
00187         resDeployTo.lstServiceEvents = m_lstServiceEvents;
00188         m_lstServiceEvents.clear();
00189       }
00190       
00191       m_mtxServiceEventsStore.unlock();
00192     }
00193     
00194     void Plugin::deployEvent(Event evDeploy, bool bWaitForEvent) {
00195       evDeploy.nOriginID = this->pluginID();
00196       
00197       m_mtxEventsStore.lock();
00198       m_lstEvents.push_back(evDeploy);
00199       m_mtxEventsStore.unlock();
00200       
00201       if(bWaitForEvent) {
00202         this->waitForEvent(evDeploy);
00203       }
00204     }
00205     
00206     ServiceEvent Plugin::deployServiceEvent(ServiceEvent seDeploy, bool bWaitForEvent) {
00207       seDeploy.nRequesterID = this->pluginID();
00208       
00209       if(seDeploy.siServiceIdentifier == SI_REQUEST) {
00210         seDeploy.nServiceEventID = rand();
00211       }
00212       
00213       m_mtxServiceEventsStore.lock();
00214       m_lstServiceEvents.push_back(seDeploy);
00215       m_mtxServiceEventsStore.unlock();
00216       
00217       if(bWaitForEvent) {
00218         return this->waitForEvent(seDeploy);
00219       } else {
00220         return seDeploy;
00221       }
00222     }
00223     
00224     void Plugin::setPluginName(std::string strName) {
00225       m_strName = strName;
00226       
00227       this->setPluginID(m_nID);
00228     }
00229     
00230     std::string Plugin::pluginName() {
00231       return m_strName;
00232     }
00233     
00234     void Plugin::setPluginVersion(std::string strVersion) {
00235       m_strVersion = strVersion;
00236     }
00237     
00238     std::string Plugin::pluginVersion() {
00239       return m_strVersion;
00240     }
00241     
00242     std::string Plugin::pluginIdentifierString(bool bBold) {
00243       std::stringstream sts;
00244       sts << colorSpecifierForID(this->pluginID(), bBold);
00245       sts << "[" << this->pluginName() << "/";
00246       sts << this->pluginID();
00247       sts << "]";
00248       
00249       return sts.str();
00250     }
00251     
00252     void Plugin::unimplemented(std::string strMessage) {
00253       std::cout << this->pluginIdentifierString(false) << " NOT IMPLEMENTED: " << strMessage << normalColorSpecifier() << std::endl;
00254     }
00255     
00256     int Plugin::openNewRequestID() {
00257       int nID = 0;
00258       
00259       while(this->isRequestIDOpen(nID)) {
00260         nID++;
00261       }
00262       
00263       m_lstOpenRequestIDs.push_back(nID);
00264       
00265       return nID;
00266     }
00267     
00268     bool Plugin::isRequestIDOpen(int nID) {
00269       for(int nCurrentID : m_lstOpenRequestIDs) {
00270         if(nCurrentID == nID) {
00271           return true;
00272         }
00273       }
00274       
00275       return false;
00276     }
00277     
00278     void Plugin::closeRequestID(int nID) {
00279       m_lstOpenRequestIDs.remove(nID);
00280     }
00281     
00282     bool Plugin::isAnyRequestIDOpen() {
00283       return (m_lstOpenRequestIDs.size() > 0);
00284     }
00285     
00286     void Plugin::setRunning(bool bRunCycle) {
00287       m_mtxRunCycle.lock();
00288       m_bRunCycle = bRunCycle;
00289       m_mtxRunCycle.unlock();
00290     }
00291     
00292     bool Plugin::running() {
00293       m_mtxRunCycle.lock();
00294       bool bReturn = m_bRunCycle;
00295       m_mtxRunCycle.unlock();
00296       
00297       return bReturn;
00298     }
00299     
00300     void Plugin::waitForEvent(Event evWait) {
00301       if(evWait.nOpenRequestID != -1) {
00302         while(this->isRequestIDOpen(evWait.nOpenRequestID) && this->running()) {
00303           // Do nothing and wait.
00304         }
00305       }
00306     }
00307     
00308     ServiceEvent Plugin::waitForEvent(ServiceEvent seWait) {
00309       bool bGoon = true;
00310       ServiceEvent seReturn = defaultServiceEvent(seWait.strServiceName);
00311       
00312       while(bGoon && this->running()) {
00313         m_mtxReceivedServiceEventResponses.lock();
00314         
00315         for(list<ServiceEvent>::iterator itSE = m_lstReceivedServiceEventResponses.begin();
00316             itSE != m_lstReceivedServiceEventResponses.end(); itSE++) {
00317           if(((*itSE).nServiceEventID == seWait.nServiceEventID) && (*itSE).siServiceIdentifier == SI_RESPONSE) {
00318             bGoon = false;
00319             seReturn = *itSE;
00320             
00321             m_lstReceivedServiceEventResponses.erase(itSE);
00322             
00323             break;
00324           }
00325         }
00326         
00327         m_mtxReceivedServiceEventResponses.unlock();
00328       }
00329       
00330       return seReturn;
00331     }
00332     
00333     void Plugin::success(std::string strMessage, bool bImportant) {
00334       this->coloredText(strMessage, colorSpecifierForID(this->pluginID()), false, bImportant);
00335     }
00336     
00337     void Plugin::info(std::string strMessage, bool bImportant) {
00338       this->coloredText(strMessage, colorSpecifierForID(this->pluginID()), false, bImportant);
00339     }
00340     
00341     void Plugin::warn(std::string strMessage, bool bImportant) {
00342       this->coloredText(strMessage, colorSpecifierForID(this->pluginID()), true, bImportant);
00343     }
00344     
00345     void Plugin::fail(std::string strMessage, bool bImportant) {
00346       this->coloredText(strMessage, colorSpecifierForID(this->pluginID()), true, bImportant);
00347     }
00348   }
00349 }


beliefstate
Author(s): Jan Winkler
autogenerated on Sun Oct 5 2014 22:30:15