00001 // Copyright CERN 2012 - Developed in collaboration with GSI 00002 00003 #include <fesa-core/DataStore/FieldSynchroManager.h> 00004 00005 #include <fesa-core/DataStore/FieldValue.h> 00006 #include <fesa-core/Synchronization/MultiplexingContext.h> 00007 #include <fesa-core/Utilities/Lock.h> 00008 00009 00010 namespace fesa 00011 { 00012 00013 FieldSynchroManager* FieldSynchroManager::theInstance_ = NULL; 00014 00015 FieldSynchroManager::FieldSynchroManager() 00016 { 00017 00018 } 00019 00020 FieldSynchroManager* FieldSynchroManager::getInstance() 00021 { 00022 if (!theInstance_) 00023 { 00024 theInstance_ = new FieldSynchroManager(); 00025 } 00026 return theInstance_; 00027 } 00028 00029 void FieldSynchroManager::registerModifiedField(MultiplexingContext* pContext, DataStore* pDataStore, 00030 bool isShared, FieldValueBase* pFieldValue) 00031 { 00032 Lock lock(mutex_); 00033 PairFieldValueShared pairFieldValueShared(pFieldValue, isShared); 00034 MapModifiedFields::iterator itModifiedFields = recentlyModifiedFields_.find(pContext); 00035 if (itModifiedFields != recentlyModifiedFields_.end()) 00036 { 00037 MapDataStoreSetOfPairs::iterator itr = itModifiedFields->second.find(pDataStore); 00038 if (itr != itModifiedFields->second.end()) 00039 { 00040 itr->second.insert(pairFieldValueShared); 00041 } 00042 else 00043 { 00044 (itModifiedFields->second)[pDataStore].insert(pairFieldValueShared); 00045 } 00046 } 00047 else 00048 { 00049 MapDataStoreSetOfPairs mapDataStoreSetOfPairs; 00050 mapDataStoreSetOfPairs[pDataStore].insert(pairFieldValueShared); 00051 recentlyModifiedFields_.insert(make_pair(pContext, mapDataStoreSetOfPairs)); 00052 } 00053 } 00054 00055 void FieldSynchroManager::commitModifiedFieldSynchronization(MultiplexingContext* context) 00056 { 00057 /* 00058 * itModifiedFields->first = MultiplexingContext* 00059 * itModifiedFields->second = MapDataStoreSetOfPairs 00060 * itDataStore->first = DataStore* 00061 * itDataStore->second = SetPairFieldValueShared 00062 * itPairsFieldValueShared->first = FieldValue* 00063 * itPairsFieldValueShared->second = bool (isShared) 00064 */ 00065 Lock lock(mutex_); 00066 MapModifiedFields::iterator itModifiedFields = recentlyModifiedFields_.find(context); 00067 if (itModifiedFields != recentlyModifiedFields_.end()) 00068 { 00069 MapDataStoreSetOfPairs::iterator itDataStore(itModifiedFields->second.begin()); 00070 00071 for (; itDataStore != itModifiedFields->second.end(); ++itDataStore) 00072 { 00073 SetPairFieldValueShared::iterator itPairFieldValueShared(itDataStore->second.begin()); 00074 for (; itPairFieldValueShared != itDataStore->second.end(); ++itPairFieldValueShared) 00075 { 00076 if(itPairFieldValueShared->second) {// shared = true 00077 itPairFieldValueShared->first->setToBeSync(true); 00078 } 00079 else // Field not shared needs to be comitted immediately 00080 itPairFieldValueShared->first->commit(true); 00081 } 00082 } 00083 recentlyModifiedFields_.erase(itModifiedFields); 00084 } 00085 } 00086 00087 void FieldSynchroManager::abortModifiedFieldSynchronization(MultiplexingContext* context) 00088 { 00089 /* 00090 * itModifiedFields->first = MultiplexingContext* 00091 * itModifiedFields->second = MapDataStoreSetOfPairs 00092 * itDataStore->first = DataStore* 00093 * itDataStore->second = SetPairFieldValueShared 00094 * itPairsFieldValueShared->first = FieldValue* 00095 * itPairsFieldValueShared->second = bool (isShared) 00096 */ 00097 Lock lock(mutex_); 00098 MapModifiedFields::iterator itModifiedFields = recentlyModifiedFields_.find(context); 00099 if (itModifiedFields != recentlyModifiedFields_.end()) 00100 { 00101 MapDataStoreSetOfPairs::iterator itDataStore(itModifiedFields->second.begin()); 00102 00103 for (; itDataStore != itModifiedFields->second.end(); ++itDataStore) 00104 { 00105 // itDataStore->first->lock(); 00106 SetPairFieldValueShared::iterator itPairFieldValueShared(itDataStore->second.begin()); 00107 for (; itPairFieldValueShared != itDataStore->second.end(); ++itPairFieldValueShared) 00108 { 00109 itPairFieldValueShared->first->setPendingChanged(false); 00110 } 00111 // itDataStore->first->unlock(); 00112 } 00113 recentlyModifiedFields_.erase(itModifiedFields); 00114 } 00115 } 00116 00117 void FieldSynchroManager::commitModifiedFieldSynchronization() 00118 { 00119 Lock lock(mutex_); 00120 MapModifiedFields::iterator itModifiedFields = recentlyModifiedFields_.begin(); 00121 for (; itModifiedFields != recentlyModifiedFields_.end(); ++itModifiedFields) 00122 { 00123 MapDataStoreSetOfPairs::iterator itDataStore(itModifiedFields->second.begin()); 00124 00125 for (; itDataStore != itModifiedFields->second.end(); ++itDataStore) 00126 { 00127 SetPairFieldValueShared::iterator itPairFieldValueShared(itDataStore->second.begin()); 00128 for (; itPairFieldValueShared != itDataStore->second.end(); ++itPairFieldValueShared) 00129 { 00130 if(itPairFieldValueShared->second) 00131 { 00132 // shared = true 00133 itPairFieldValueShared->first->setToBeSync(true); 00134 } 00135 else 00136 { 00137 // Field not shared needs to be comitted immediately 00138 itPairFieldValueShared->first->commit(true); 00139 } 00140 } 00141 } 00142 recentlyModifiedFields_.erase(itModifiedFields); 00143 } 00144 } 00145 00146 } // fesa