AbstractServerDeviceClass.cpp

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #include <fesa-core/Server/AbstractServerDeviceClass.h>
00004 
00005 #include <fesa-core/Server/AbstractServerEquipment.h>
00006 #include <fesa-core/Server/Property.h>
00007 #include <fesa-core/Server/AbstractServerActionFactory.h>
00008 #include <fesa-core/Server/AbstractServerAction.h>
00009 #include <fesa-core/DataStore/EquipmentData.h>
00010 #include <fesa-core/DataStore/FieldSynchroManager.h>
00011 #include <fesa-core/DataStore/AbstractDeviceFactory.h>
00012 #include <fesa-core/DataStore/AbstractDevice.h>
00013 #include <fesa-core/DataStore/GlobalDevice.h>
00014 #include <fesa-core/Diagnostic/FesaStream.h>
00015 
00016 #include <cmw-log/Logger.h>
00017 
00018 #include <iostream>
00019 
00020 namespace
00021 {
00022 
00023 CMW::Log::Logger& logger = CMW::Log::LoggerFactory::getLogger("FESA.FWK.fesa-core.Server.AbstractServerDeviceClass");
00024 
00025 }
00026 
00027 namespace fesa
00028 {
00029 
00030 AbstractServerDeviceClass::AbstractServerDeviceClass(const std::string& name, const std::string& version) :
00031     AbstractDeviceClass(name, version)
00032 {
00033     // Register itself
00034     AbstractServerEquipment::getInstance()->registerDeviceClass(this);
00035 }
00036 
00037 AbstractServerDeviceClass::~AbstractServerDeviceClass()
00038 {
00039     std::map<std::string, Property*>::iterator properties;
00040 
00041     for (properties = propertiesCol_.begin(); properties != propertiesCol_.end(); properties++)
00042         if (properties->second != NULL)
00043         {
00044             delete properties->second;
00045             properties->second = NULL;
00046         }
00047 
00048     relatedClassCol_.clear();
00049     propertiesCol_.clear();
00050 
00051     try
00052     {
00053         AbstractServerEquipment::getInstance()->deregisterDeviceClass(name_);
00054     }
00055     // the class might have already been deleted
00056     catch (FesaException& ex)
00057     {
00058 
00059     }
00060 }
00061 
00062 void AbstractServerDeviceClass::initialize()
00063 {
00064     // configure the properties
00065     configurePropertiesActions();
00066 
00067     *(EquipmentData::getInstance()->srvProcessPid_) = getpid();
00068 
00069     // The user's init is now called
00070     specificInit();
00071 
00072     // Synchronize the setting fields
00073     FieldSynchroManager* pFieldSynchroManager = FieldSynchroManager::getInstance();
00074     pFieldSynchroManager->commitModifiedFieldSynchronization();
00075 
00076     AbstractDeviceFactory* pAbstractDeviceFactory = getDeviceFactory();
00077     const std::vector<AbstractDevice*>& abstractDevCol = pAbstractDeviceFactory->getAbstractDeviceCollection();
00078     for (uint32_t i = 0; i < abstractDevCol.size(); ++i)
00079     {
00080         abstractDevCol[i]->synchronizeSettingFields();
00081     }
00082 
00083     GlobalDevice* pGlobalDevice = pAbstractDeviceFactory->getGlobalDevice();
00084     if (pGlobalDevice != NULL)
00085     {
00086         pGlobalDevice->synchronizeSettingFields();
00087     }
00088 
00089 }
00090 
00091 void AbstractServerDeviceClass::configurePropertiesActions()
00092 {
00093     // get the properties information from the design document
00094     std::vector<PropertyInfo*>* propertiesConfig = fillProperties();
00095     std::map<std::string, std::vector<std::string> >* actionsConfig = fillActions();
00096 
00097     for (uint32_t i = 0; i < propertiesConfig->size(); i++)
00098     {
00099         // CRFESA-764 put this verbose option
00100         if(logger.isLoggable(CMW::Log::Level::LL_DEBUG))
00101         {
00102             std::ostringstream msg;
00103             msg << "setting property " << (*propertiesConfig)[i]->name_;
00104             LOG_DEBUG_IF(logger, msg.str());
00105         }
00106 
00107         AbstractServerAction* getServerAction = NULL;
00108         AbstractServerAction* setServerAction = NULL;
00109 
00110         if (!(((*propertiesConfig)[i])->getActionProperty_.name_.empty()))
00111         {
00112             ServerActionConfig config = (*propertiesConfig)[i]->getActionProperty_;
00113             getServerAction = actionFactory_->createServerAction(config);
00114         }
00115 
00116         if (!(*propertiesConfig)[i]->setActionProperty_.name_.empty())
00117         {
00118             ServerActionConfig config = (*propertiesConfig)[i]->setActionProperty_;
00119             //fill the notified properties of Server Set-actions
00120             std::map<std::string, std::vector<std::string> >::iterator iter = actionsConfig->find(config.name_);
00121             if (iter != actionsConfig->end())
00122                 config.notifiedPropertiesCol_ = (*iter).second;
00123             setServerAction = actionFactory_->createServerAction(config);
00124         }
00125 
00126         //create the property. Exceptions will be caught in the main
00127         Property* prop = new Property((*propertiesConfig)[i]->name_, (*propertiesConfig)[i]->isGlobal_,
00128                                       (*propertiesConfig)[i]->isOnChange_, (*propertiesConfig)[i]->isSubscribable_,
00129                                       (*propertiesConfig)[i]->type_, (*propertiesConfig)[i]->multiplexed_,
00130                                       (*propertiesConfig)[i]->priorityOffset_, (*propertiesConfig)[i]->notificationThreadKey_, getServerAction, setServerAction, (*propertiesConfig)[i]->className_);
00131         propertiesCol_.insert(std::make_pair(prop->getName(), prop));
00132     }// end loop the properties
00133 
00134     /* To avoid memory leak, content of propertiesConfig has to be deleted!
00135      * This does not work if we use an iterator in the loop and call
00136      * properties = propertiesConfig.erase(properties)
00137      */
00138     if (propertiesConfig != NULL)
00139     {
00140         for (uint32_t i = 0; i < propertiesConfig->size(); i++)
00141         {
00142             delete (*propertiesConfig)[i];
00143         }
00144         delete propertiesConfig;
00145         propertiesConfig = NULL;
00146     }
00147     if (actionsConfig != NULL)
00148     {
00149         delete actionsConfig;
00150         actionsConfig = NULL;
00151     }
00152 }
00153 
00154 Property* AbstractServerDeviceClass::getProperty(const std::string& propertyName)
00155 {
00156     std::map<std::string, Property*>::iterator itr = propertiesCol_.find(propertyName);
00157     if (itr != propertiesCol_.end())
00158     {
00159         return (*itr).second;
00160     }
00161     else if (baseClass_ != NULL)
00162     {
00163         return ((AbstractServerDeviceClass*) baseClass_)->getProperty(propertyName);
00164     }
00165 
00166     return NULL;
00167 }
00168 
00169 // why is this service here?
00170 void AbstractServerDeviceClass::reportErrorToSubscribers(std::string Message)
00171 {
00172 }
00173 
00174 void AbstractServerDeviceClass::printConfig(FesaStream* configStream)
00175 {
00176     *configStream << "\t\t<class name=\"" << name_ << "\" version=\"" << version_ << "\">" << std::endl;
00177     *configStream << "\t\t\t<relationship>" << std::endl;
00178     if(baseClass_ != NULL)
00179     {
00180         *configStream << "\t\t\t\t<inheritance name=\"" << baseClass_->getName() << "\" version=\"" << baseClass_->getVersion() << "\"/>" << std::endl;
00181     }
00182     if (!relatedClassCol_.empty())
00183     {
00184         for(std::vector<AbstractDeviceClass*>::iterator it=relatedClassCol_.begin(); it!=relatedClassCol_.end(); ++it)
00185         {
00186             AbstractDeviceClass* pADC = (AbstractDeviceClass*)*it;
00187             *configStream << "\t\t\t\t<composition name=\"" << pADC->getName() << "\" version=\"" << pADC->getVersion() << "\"/>" << std::endl;
00188         }
00189     }
00190     *configStream << "\t\t\t</relationship>" << std::endl;
00191     *configStream << "\t\t\t<interface>" << std::endl;
00192     *configStream << "\t\t\t\t<device-interface>" << std::endl;
00193     std::map<std::string, Property*>::iterator itProp;
00194     // iterate trough the collection properties and extract first the device ones
00195     for (itProp = propertiesCol_.begin(); itProp != propertiesCol_.end(); ++itProp)
00196     {
00197         if (!itProp->second->isGlobal()) //device property
00198         {
00199             itProp->second->printConfig(configStream);
00200         }
00201     }
00202     *configStream << "\t\t\t\t</device-interface>" << std::endl;
00203     *configStream << "\t\t\t\t<global-interface>" << std::endl;
00204     // iterate trough the collection properties and extract first the global ones
00205     for (itProp = propertiesCol_.begin(); itProp != propertiesCol_.end(); ++itProp)
00206     {
00207         if (itProp->second->isGlobal()) //global property
00208         {
00209             itProp->second->printConfig(configStream);
00210         }
00211     }
00212     *configStream << "\t\t\t\t</global-interface>" << std::endl;
00213 }
00214 
00215 void AbstractServerDeviceClass::printState(FesaStream* fesaStream, double elapsedTime)
00216 {
00217 }
00218 
00219 } // fesa
00220 

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1