AbstractRTDeviceClass.cpp

Go to the documentation of this file.
00001 // Copyright CERN 2012 - Developed in collaboration with GSI
00002 
00003 #include <fesa-core/RealTime/AbstractRTDeviceClass.h>
00004 #include <fesa-core/RealTime/AbstractRTEquipment.h>
00005 #include <fesa-core/DataStore/FieldSynchroManager.h>
00006 #include <fesa-core/DataStore/AbstractDevice.h>
00007 #include <fesa-core/DataStore/GlobalDevice.h>
00008 #include <fesa-core/DataStore/AbstractDeviceFactory.h>
00009 #include <fesa-core/Synchronization/NoneContext.h>
00010 #include <fesa-core/Core/AbstractDeviceClass.h>
00011 
00012 namespace fesa
00013 {
00014     AbstractRTDeviceClass::AbstractRTDeviceClass(const std::string& name, const std::string& version) :
00015         AbstractDeviceClass(name, version)
00016     {
00017         // Register itself
00018         //TODO move this call out of the constructor, or at least use dependency injection
00019         AbstractRTEquipment::getInstance()->registerDeviceClass(this);
00020     }
00021 
00022     AbstractRTDeviceClass::AbstractRTDeviceClass() :
00023         AbstractDeviceClass("", "")
00024     {
00025         //This constructor is only used for Mocking
00026         //TODO fix first constructor
00027     }
00028 
00029     AbstractRTDeviceClass::~AbstractRTDeviceClass()
00030     {
00031         //TODO
00032         // clean all the collections rtLayerInfoCol_, rtActionInfoCol_, rtSchedulingInfoCol_
00033         // But it's an intricate problem:
00034         // But those collections can contain pointer on elements
00035         // which are managed by the base class(es) and more complex, in the context of
00036         // a complex inheritance tree a base class A can be extended by several classes and therefore
00037         // the class A should keep track of the object that point to it (kind of smart pointer)
00038         // For the time being only the collection are automatically deleted.
00039         // The resources allocated for the elements (just memory)
00040         // are freed by the OS when the program exit.
00041 
00042 
00043         try
00044         {
00045             AbstractRTEquipment::getInstance()->deregisterDeviceClass(name_);
00046         }
00047         // the class might have already been deleted
00048         catch (FesaException ex)
00049         {
00050 
00051         }
00052     }
00053 
00054     void AbstractRTDeviceClass::initialize()
00055     {
00056         // The user's init is now called
00057         specificInit();
00058 
00059         // Synchronize the setting fields
00060         FieldSynchroManager* pFieldSynchroManager = FieldSynchroManager::getInstance();
00061         pFieldSynchroManager->commitModifiedFieldSynchronization();
00062 
00063         AbstractDeviceFactory* pAbstractDeviceFactory = getDeviceFactory();
00064         const std::vector<AbstractDevice*>& abstractDevCol = pAbstractDeviceFactory->getAbstractDeviceCollection();
00065         for (uint32_t i = 0; i < abstractDevCol.size(); ++i)
00066         {
00067             abstractDevCol[i]->synchronizeSettingFields();
00068         }
00069 
00070         GlobalDevice* pGlobalDevice = pAbstractDeviceFactory->getGlobalDevice();
00071         if (pGlobalDevice != NULL)
00072         {
00073             pGlobalDevice->synchronizeSettingFields();
00074         }
00075     }
00076 
00077     void AbstractRTDeviceClass::addDiagnosticSchedulingUnitInfo(std::vector<RTSchedulingUnitInfo*>& schedulingUnitInfo)
00078     {
00079         //Attention!!!! Every developer who uses the same names will produce undefined behavior!!
00080 
00082         RTSchedulingUnitInfo* temp = new RTSchedulingUnitInfo();
00083 
00084         temp->name_ = DIAGNOSTIC_LAYER;
00085         temp->actionName_ = "RTDiagnosticSetting";
00086         temp->className_ = name_;
00087         temp->selectionCriterion_ = "";
00088         temp->eventName_ = "diagnosticEvent";
00089         temp->eventField_ = "diagnosticEvent";
00090 
00091         schedulingUnitInfo.push_back(temp);
00092     }
00093 
00094     void AbstractRTDeviceClass::addDiagnosticRTActionInfo(RTActionInfoCol& actionInfoCol)
00095     {
00096         //Attention!!!! Every developer who uses the same names will produce undefined behavior!!
00097 
00098         RTActionInfo* actionInfo = new RTActionInfo();
00099         actionInfo->className_ = name_;
00100         std::string s = "RTDiagnosticSetting";
00101         actionInfoCol.insert(std::make_pair(s, actionInfo));
00102     }
00103 
00104     void AbstractRTDeviceClass::addDiagnosticEvents(ElementXML* xmlElementClass)
00105     {
00106         //Attention!!!! Every developer who uses the same names will produce undefined behavior!!
00107 
00108         //add to events mapping
00109         ElementXML* eventsMapping =  getOrCreateXMLElement(xmlElementClass,"events-mapping");
00110         ElementXML* logicalEvent =  getOrCreateXMLElement(eventsMapping,"diagnosticEvent");
00111         ElementXML* eventConfiguration =  getOrCreateXMLElement(logicalEvent,"event-configuration");
00112         addXMLAttribute(eventConfiguration,"name","diag-event-configuration");
00113         ElementXML* eventSource =  getOrCreateXMLElement(eventConfiguration,"OnDemand");
00114         ElementXML* event =  getOrCreateXMLElement(eventSource,"on-demand-event-source-ref");
00115         addXMLAttribute(event,"name",DIAG_EVENT_SOURCE_NAME);
00116 
00117         //add to global device
00118         ElementXML* globalInstance =  getOrCreateXMLElement(xmlElementClass,"global-instance");
00119         ElementXML* globalInstanceEventsMapping =  getOrCreateXMLElement(globalInstance,"events-mapping");
00120         ElementXML* globalInstanceDiagEvent =  getOrCreateXMLElement(globalInstanceEventsMapping,"diagnosticEvent");
00121         ElementXML* globalInstanceDiagEventConfigRef =  getOrCreateXMLElement(globalInstanceDiagEvent,"event-configuration-ref");
00122         addXMLAttribute(globalInstanceDiagEventConfigRef,"name","diag-event-configuration");
00123     }
00124 
00125     ElementXML* AbstractRTDeviceClass::getOrCreateXMLElement(ElementXML* baseElement,std::string name)
00126     {
00127         std::vector<ElementXML*>::iterator iter;
00128         for (iter=baseElement->childList_.begin();iter!=baseElement->childList_.end();iter++)
00129         {
00130                 if((*iter)->name_ == name)
00131                 {
00132                         return *iter;
00133                 }
00134         }
00135 
00136         //notfound
00137                 ElementXML* element =  new ElementXML;
00138                 element->name_ = name;
00139                 baseElement->childList_.push_back(element);
00140                 return element;
00141     }
00142 
00143     void AbstractRTDeviceClass::addXMLAttribute(ElementXML* baseElement,std::string name,std::string value)
00144     {
00145         AttributeXML* attr =  new AttributeXML;
00146         attr->name_ = name;
00147         attr->value_ = value;
00148                 baseElement->attributeList_.push_back(attr);
00149     }
00150 }

Generated on 18 Jan 2013 for Fesa by  doxygen 1.6.1