[219] | 1 | #include "file.hpp" |
---|
| 2 | |
---|
[352] | 3 | #include "attribute_template.hpp" |
---|
| 4 | #include "object_template.hpp" |
---|
| 5 | #include "group_template.hpp" |
---|
[219] | 6 | #include "object_factory.hpp" |
---|
[300] | 7 | #include "data_output.hpp" |
---|
| 8 | #include "context.hpp" |
---|
| 9 | #include "context_server.hpp" |
---|
| 10 | #include "nc4_data_output.hpp" |
---|
[318] | 11 | #include "calendar_util.hpp" |
---|
| 12 | #include "date.hpp" |
---|
[352] | 13 | #include "message.hpp" |
---|
| 14 | #include "type.hpp" |
---|
| 15 | #include "xmlioserver_spl.hpp" |
---|
| 16 | #include "context_client.hpp" |
---|
[382] | 17 | #include "mpi.hpp" |
---|
[219] | 18 | |
---|
[335] | 19 | namespace xios { |
---|
[509] | 20 | |
---|
[219] | 21 | /// ////////////////////// Définitions ////////////////////// /// |
---|
| 22 | |
---|
| 23 | CFile::CFile(void) |
---|
| 24 | : CObjectTemplate<CFile>(), CFileAttributes() |
---|
[379] | 25 | , vFieldGroup(), data_out(), enabledFields(), fileComm(MPI_COMM_NULL) |
---|
[509] | 26 | { |
---|
[472] | 27 | setVirtualFieldGroup() ; |
---|
| 28 | setVirtualVariableGroup() ; |
---|
| 29 | } |
---|
[219] | 30 | |
---|
| 31 | CFile::CFile(const StdString & id) |
---|
| 32 | : CObjectTemplate<CFile>(id), CFileAttributes() |
---|
[379] | 33 | , vFieldGroup(), data_out(), enabledFields(), fileComm(MPI_COMM_NULL) |
---|
[509] | 34 | { |
---|
[472] | 35 | setVirtualFieldGroup() ; |
---|
| 36 | setVirtualVariableGroup() ; |
---|
| 37 | } |
---|
[219] | 38 | |
---|
| 39 | CFile::~CFile(void) |
---|
| 40 | { /* Ne rien faire de plus */ } |
---|
| 41 | |
---|
| 42 | ///--------------------------------------------------------------- |
---|
[509] | 43 | //! Get name of file |
---|
[219] | 44 | StdString CFile::GetName(void) { return (StdString("file")); } |
---|
| 45 | StdString CFile::GetDefName(void){ return (CFile::GetName()); } |
---|
| 46 | ENodeType CFile::GetType(void) { return (eFile); } |
---|
| 47 | |
---|
| 48 | //---------------------------------------------------------------- |
---|
[509] | 49 | /*! |
---|
| 50 | \brief Get data that will be written out. |
---|
| 51 | Each enabled file in xml represents a physical netcdf file. |
---|
| 52 | This function allows to access to data to be written out into netcdf file |
---|
| 53 | \return data written out. |
---|
| 54 | */ |
---|
[337] | 55 | boost::shared_ptr<CDataOutput> CFile::getDataOutput(void) const |
---|
[219] | 56 | { |
---|
| 57 | return (data_out); |
---|
| 58 | } |
---|
| 59 | |
---|
[509] | 60 | /*! |
---|
| 61 | \brief Get virtual field group |
---|
| 62 | In each file, there always exists a field group which is the ancestor of all |
---|
| 63 | fields in the file. This is considered be virtual because it is created automatically during |
---|
| 64 | file initialization and it normally doesn't appear on xml file |
---|
| 65 | \return Pointer to field group |
---|
| 66 | */ |
---|
[347] | 67 | CFieldGroup* CFile::getVirtualFieldGroup(void) const |
---|
[219] | 68 | { |
---|
| 69 | return (this->vFieldGroup); |
---|
| 70 | } |
---|
| 71 | |
---|
[509] | 72 | /*! |
---|
| 73 | \brief Get virtual variable group |
---|
| 74 | In each file, there always exists a variable group which is the ancestor of all |
---|
| 75 | variable in the file. This is considered be virtual because it is created automatically during |
---|
| 76 | file initialization and it normally doesn't appear on xml file |
---|
| 77 | \return Pointer to variable group |
---|
| 78 | */ |
---|
[472] | 79 | CVariableGroup* CFile::getVirtualVariableGroup(void) const |
---|
| 80 | { |
---|
| 81 | return (this->vVariableGroup); |
---|
| 82 | } |
---|
| 83 | |
---|
[509] | 84 | //! Get all fields of a file |
---|
[347] | 85 | std::vector<CField*> CFile::getAllFields(void) const |
---|
[219] | 86 | { |
---|
| 87 | return (this->vFieldGroup->getAllChildren()); |
---|
| 88 | } |
---|
[509] | 89 | |
---|
| 90 | //! Get all variables of a file |
---|
[472] | 91 | std::vector<CVariable*> CFile::getAllVariables(void) const |
---|
| 92 | { |
---|
| 93 | return (this->vVariableGroup->getAllChildren()); |
---|
| 94 | } |
---|
[219] | 95 | |
---|
| 96 | //---------------------------------------------------------------- |
---|
[509] | 97 | /*! |
---|
| 98 | \brief Get all enabled fields of file |
---|
| 99 | A field is considered to be enabled if it fullfil these conditions: it is enabled, inside a enabled file |
---|
| 100 | and its own level is not larger than file output level. |
---|
| 101 | \param [in] default_outputlevel default value output level of file |
---|
| 102 | \param [in] default_level default value level of field |
---|
| 103 | \param [in] default_enabled flag determine by default if field is enabled |
---|
| 104 | \return Vector of pointers of enabled fields |
---|
| 105 | */ |
---|
| 106 | std::vector<CField*> CFile::getEnabledFields(int default_outputlevel, |
---|
[347] | 107 | int default_level, |
---|
| 108 | bool default_enabled) |
---|
[219] | 109 | { |
---|
| 110 | if (!this->enabledFields.empty()) |
---|
| 111 | return (this->enabledFields); |
---|
| 112 | |
---|
| 113 | const int _outputlevel = |
---|
| 114 | (!output_level.isEmpty()) ? output_level.getValue() : default_outputlevel; |
---|
[347] | 115 | std::vector<CField*>::iterator it; |
---|
[219] | 116 | this->enabledFields = this->getAllFields(); |
---|
| 117 | |
---|
[347] | 118 | std::vector<CField*> newEnabledFields; |
---|
[509] | 119 | |
---|
[219] | 120 | for ( it = this->enabledFields.begin() ; it != this->enabledFields.end(); it++ ) |
---|
| 121 | { |
---|
| 122 | if (!(*it)->enabled.isEmpty()) // Si l'attribut 'enabled' est défini ... |
---|
| 123 | { |
---|
[300] | 124 | if (! (*it)->enabled.getValue()) continue; |
---|
| 125 | // { it--; this->enabledFields.erase(it+1); continue; } |
---|
[219] | 126 | } |
---|
| 127 | else // Si l'attribut 'enabled' n'est pas défini ... |
---|
| 128 | { |
---|
[300] | 129 | if (!default_enabled) continue ; |
---|
| 130 | // { it--; this->enabledFields.erase(it+1); continue; } |
---|
[219] | 131 | } |
---|
| 132 | |
---|
| 133 | if (!(*it)->level.isEmpty()) // Si l'attribut 'level' est défini ... |
---|
| 134 | { |
---|
[300] | 135 | if ((*it)->level.getValue() > _outputlevel) continue ; |
---|
| 136 | // { it--; this->enabledFields.erase(it+1); continue; } |
---|
[219] | 137 | } |
---|
| 138 | else // Si l'attribut 'level' n'est pas défini ... |
---|
| 139 | { |
---|
[300] | 140 | if (default_level > _outputlevel) continue ; |
---|
| 141 | // { it--; this->enabledFields.erase(it+1); continue; } |
---|
[219] | 142 | } |
---|
[509] | 143 | |
---|
[300] | 144 | // CField* field_tmp=(*it).get() ; |
---|
| 145 | // shared_ptr<CField> sptfield=*it ; |
---|
| 146 | // field_tmp->refObject.push_back(sptfield) ; |
---|
| 147 | newEnabledFields.push_back(*it) ; |
---|
[286] | 148 | // Le champ est finalement actif, on y ajoute sa propre reference. |
---|
[459] | 149 | // (*it)->refObject.push_back(*it); |
---|
[286] | 150 | // Le champ est finalement actif, on y ajoute la référence au champ de base. |
---|
[346] | 151 | (*it)->setRelFile(CFile::get(this)); |
---|
[459] | 152 | // (*it)->baseRefObject->refObject.push_back(*it); |
---|
[286] | 153 | // A faire, ajouter les references intermediaires... |
---|
[219] | 154 | } |
---|
[300] | 155 | enabledFields=newEnabledFields ; |
---|
[219] | 156 | |
---|
| 157 | return (this->enabledFields); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | //---------------------------------------------------------------- |
---|
[509] | 161 | //! Change virtual field group to a new one |
---|
[347] | 162 | void CFile::setVirtualFieldGroup(CFieldGroup* newVFieldGroup) |
---|
[509] | 163 | { |
---|
| 164 | this->vFieldGroup = newVFieldGroup; |
---|
[219] | 165 | } |
---|
| 166 | |
---|
[509] | 167 | //! Change virtual variable group to new one |
---|
[472] | 168 | void CFile::setVirtualVariableGroup(CVariableGroup* newVVariableGroup) |
---|
[509] | 169 | { |
---|
| 170 | this->vVariableGroup = newVVariableGroup; |
---|
[472] | 171 | } |
---|
| 172 | |
---|
[219] | 173 | //---------------------------------------------------------------- |
---|
[509] | 174 | //! Create virtual field group, which is done normally on initializing file |
---|
[300] | 175 | void CFile::setVirtualFieldGroup(void) |
---|
[219] | 176 | { |
---|
[346] | 177 | this->setVirtualFieldGroup(CFieldGroup::create()); |
---|
[219] | 178 | } |
---|
| 179 | |
---|
[509] | 180 | //! Create virtual variable group, which is done normally on initializing file |
---|
[472] | 181 | void CFile::setVirtualVariableGroup(void) |
---|
| 182 | { |
---|
| 183 | this->setVirtualVariableGroup(CVariableGroup::create()); |
---|
| 184 | } |
---|
| 185 | |
---|
[219] | 186 | //---------------------------------------------------------------- |
---|
[318] | 187 | bool CFile::isSyncTime(void) |
---|
| 188 | { |
---|
[347] | 189 | CContext* context = CContext::getCurrent() ; |
---|
[561] | 190 | const CDate& currentDate = context->calendar->getCurrentDate() ; |
---|
[538] | 191 | if (!sync_freq.isEmpty()) |
---|
[318] | 192 | { |
---|
[538] | 193 | if (*lastSync + sync_freq.getValue() < currentDate) |
---|
[318] | 194 | { |
---|
| 195 | *lastSync=currentDate ; |
---|
| 196 | return true ; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | return false ; |
---|
| 200 | } |
---|
[509] | 201 | |
---|
| 202 | //! Initialize a file in order to write into it |
---|
[321] | 203 | void CFile::initFile(void) |
---|
| 204 | { |
---|
[347] | 205 | CContext* context = CContext::getCurrent() ; |
---|
[561] | 206 | const CDate& currentDate = context->calendar->getCurrentDate() ; |
---|
[379] | 207 | CContextServer* server=context->server ; |
---|
[509] | 208 | |
---|
[343] | 209 | lastSync=new CDate(currentDate) ; |
---|
| 210 | lastSplit=new CDate(currentDate) ; |
---|
[321] | 211 | isOpen=false ; |
---|
[379] | 212 | |
---|
| 213 | allDomainEmpty=true ; |
---|
[567] | 214 | |
---|
[379] | 215 | set<CDomain*> setDomain ; |
---|
| 216 | |
---|
| 217 | std::vector<CField*>::iterator it, end = this->enabledFields.end(); |
---|
| 218 | for (it = this->enabledFields.begin() ;it != end; it++) |
---|
| 219 | { |
---|
| 220 | CField* field = *it; |
---|
[567] | 221 | // allDomainEmpty&=field->grid->domain->isEmpty() ; |
---|
| 222 | allDomainEmpty&=(!field->grid->doGridHaveDataToWrite()); |
---|
| 223 | std::vector<CDomain*> vecDomain = field->grid->getDomains(); |
---|
| 224 | for (int i = 0; i < vecDomain.size(); ++i) |
---|
| 225 | setDomain.insert(vecDomain[i]); |
---|
| 226 | // setDomain.insert(field->grid->domain) ; |
---|
[379] | 227 | } |
---|
| 228 | nbDomain=setDomain.size() ; |
---|
| 229 | |
---|
[509] | 230 | // create sub communicator for file |
---|
[379] | 231 | int color=allDomainEmpty?0:1 ; |
---|
| 232 | MPI_Comm_split(server->intraComm,color,server->intraCommRank,&fileComm) ; |
---|
| 233 | if (allDomainEmpty) MPI_Comm_free(&fileComm) ; |
---|
| 234 | // |
---|
[509] | 235 | |
---|
[321] | 236 | } |
---|
[509] | 237 | |
---|
| 238 | //! Verify state of a file |
---|
[321] | 239 | void CFile::checkFile(void) |
---|
| 240 | { |
---|
| 241 | if (!isOpen) createHeader() ; |
---|
| 242 | checkSync() ; |
---|
| 243 | checkSplit() ; |
---|
| 244 | } |
---|
[509] | 245 | |
---|
| 246 | |
---|
| 247 | /*! |
---|
| 248 | \brief Verify if synchronisation should be done |
---|
| 249 | If syn option is enabled, syn frequence and current time will be used to |
---|
| 250 | calculate the moment to syn file(s) |
---|
| 251 | \return True if it is the moment to synchronize file, otherwise false |
---|
| 252 | */ |
---|
[321] | 253 | bool CFile::checkSync(void) |
---|
| 254 | { |
---|
[347] | 255 | CContext* context = CContext::getCurrent() ; |
---|
[561] | 256 | const CDate& currentDate = context->calendar->getCurrentDate() ; |
---|
[538] | 257 | if (!sync_freq.isEmpty()) |
---|
[321] | 258 | { |
---|
[538] | 259 | if (*lastSync + sync_freq.getValue() <= currentDate) |
---|
[321] | 260 | { |
---|
| 261 | *lastSync=currentDate ; |
---|
| 262 | data_out->syncFile() ; |
---|
| 263 | return true ; |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | return false ; |
---|
| 267 | } |
---|
[509] | 268 | |
---|
| 269 | /*! |
---|
| 270 | \brief Verify if splitting should be done |
---|
| 271 | If split option is enabled, split frequence and current time will be used to |
---|
| 272 | calculate the moment to split file |
---|
| 273 | \return True if it is the moment to split file, otherwise false |
---|
| 274 | */ |
---|
[321] | 275 | bool CFile::checkSplit(void) |
---|
| 276 | { |
---|
[347] | 277 | CContext* context = CContext::getCurrent() ; |
---|
[561] | 278 | const CDate& currentDate = context->calendar->getCurrentDate() ; |
---|
[538] | 279 | if (!split_freq.isEmpty()) |
---|
[321] | 280 | { |
---|
[538] | 281 | if (currentDate > *lastSplit + split_freq.getValue()) |
---|
[321] | 282 | { |
---|
[538] | 283 | *lastSplit = *lastSplit + split_freq.getValue(); |
---|
[347] | 284 | std::vector<CField*>::iterator it, end = this->enabledFields.end(); |
---|
[321] | 285 | for (it = this->enabledFields.begin() ;it != end; it++) (*it)->resetNStep() ; |
---|
| 286 | createHeader() ; |
---|
| 287 | return true ; |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | return false ; |
---|
| 291 | } |
---|
[509] | 292 | |
---|
| 293 | /*! |
---|
| 294 | \brief Create header of netcdf file |
---|
| 295 | There are some information to fill in header of each netcdf. |
---|
| 296 | */ |
---|
[300] | 297 | void CFile::createHeader(void) |
---|
[219] | 298 | { |
---|
[347] | 299 | CContext* context = CContext::getCurrent() ; |
---|
[379] | 300 | CContextServer* server=context->server ; |
---|
[509] | 301 | |
---|
[379] | 302 | if (!allDomainEmpty) |
---|
[219] | 303 | { |
---|
[300] | 304 | StdString filename = (!name.isEmpty()) ? name.getValue() : getId(); |
---|
| 305 | StdOStringStream oss; |
---|
| 306 | oss << filename; |
---|
| 307 | if (!name_suffix.isEmpty()) oss << name_suffix.getValue(); |
---|
[538] | 308 | |
---|
[431] | 309 | if (!split_freq.isEmpty()) |
---|
| 310 | { |
---|
| 311 | string splitFormat ; |
---|
| 312 | if (split_freq_format.isEmpty()) |
---|
| 313 | { |
---|
[538] | 314 | if (split_freq.getValue().second != 0) splitFormat = "%y%mo%d%h%mi%s"; |
---|
| 315 | else if (split_freq.getValue().minute != 0) splitFormat = "%y%mo%d%h%mi"; |
---|
| 316 | else if (split_freq.getValue().hour != 0) splitFormat = "%y%mo%d%h"; |
---|
| 317 | else if (split_freq.getValue().day != 0) splitFormat = "%y%mo%d"; |
---|
| 318 | else if (split_freq.getValue().month != 0) splitFormat = "%y%mo"; |
---|
| 319 | else splitFormat = "%y"; |
---|
[431] | 320 | } |
---|
| 321 | else splitFormat=split_freq_format ; |
---|
[538] | 322 | oss << "_" << lastSplit->getStr(splitFormat) |
---|
[589] | 323 | << "-" << (*lastSplit + split_freq.getValue() - 1 * Second).getStr(splitFormat); |
---|
[431] | 324 | } |
---|
[509] | 325 | |
---|
[528] | 326 | bool append = !this->append.isEmpty() && this->append.getValue(); |
---|
[567] | 327 | |
---|
[517] | 328 | bool useClassicFormat = !format.isEmpty() && format == format_attr::netcdf4_classic; |
---|
| 329 | |
---|
[300] | 330 | bool multifile=true ; |
---|
| 331 | if (!type.isEmpty()) |
---|
| 332 | { |
---|
[369] | 333 | if (type==type_attr::one_file) multifile=false ; |
---|
| 334 | else if (type==type_attr::multiple_file) multifile=true ; |
---|
[379] | 335 | |
---|
[509] | 336 | } |
---|
[379] | 337 | #ifndef USING_NETCDF_PAR |
---|
| 338 | if (!multifile) |
---|
| 339 | { |
---|
| 340 | info(0)<<"!!! Warning -> Using non parallel version of netcdf, switching in multiple_file mode for file : "<<filename<<" ..."<<endl ; |
---|
| 341 | multifile=true ; |
---|
| 342 | } |
---|
| 343 | #endif |
---|
[509] | 344 | if (multifile) |
---|
[300] | 345 | { |
---|
[379] | 346 | int commSize, commRank ; |
---|
| 347 | MPI_Comm_size(fileComm,&commSize) ; |
---|
| 348 | MPI_Comm_rank(fileComm,&commRank) ; |
---|
[509] | 349 | |
---|
| 350 | if (server->intraCommSize > 1) |
---|
[375] | 351 | { |
---|
| 352 | oss << "_" ; |
---|
[379] | 353 | int width=0 ; int n=commSize-1 ; |
---|
[375] | 354 | while(n != 0) { n=n/10 ; width++ ;} |
---|
[509] | 355 | if (!min_digits.isEmpty()) |
---|
[391] | 356 | if (width<min_digits) width=min_digits ; |
---|
[375] | 357 | oss.width(width) ; |
---|
| 358 | oss.fill('0') ; |
---|
[379] | 359 | oss<<right<< commRank; |
---|
[375] | 360 | } |
---|
[300] | 361 | } |
---|
| 362 | oss << ".nc"; |
---|
| 363 | |
---|
[526] | 364 | bool isCollective = par_access.isEmpty() || par_access == par_access_attr::collective; |
---|
| 365 | |
---|
| 366 | if (isOpen) data_out->closeFile(); |
---|
| 367 | |
---|
[528] | 368 | data_out = shared_ptr<CDataOutput>(new CNc4DataOutput(oss.str(), append, useClassicFormat, |
---|
| 369 | fileComm, multifile, isCollective)); |
---|
| 370 | isOpen = true; |
---|
[300] | 371 | |
---|
[528] | 372 | data_out->writeFile(CFile::get(this)); |
---|
| 373 | |
---|
| 374 | // Do not recreate the file structure if opening an existing file |
---|
| 375 | if (!data_out->IsInAppendMode()) |
---|
| 376 | { |
---|
| 377 | std::vector<CField*>::iterator it, end = this->enabledFields.end(); |
---|
| 378 | for (it = this->enabledFields.begin(); it != end; it++) |
---|
| 379 | { |
---|
[347] | 380 | CField* field = *it; |
---|
[300] | 381 | this->data_out->writeFieldGrid(field); |
---|
[528] | 382 | } |
---|
| 383 | this->data_out->writeTimeDimension(); |
---|
[509] | 384 | |
---|
[528] | 385 | for (it = this->enabledFields.begin(); it != end; it++) |
---|
| 386 | { |
---|
[347] | 387 | CField* field = *it; |
---|
[300] | 388 | this->data_out->writeField(field); |
---|
[528] | 389 | } |
---|
[509] | 390 | |
---|
[528] | 391 | vector<CVariable*> listVars = getAllVariables() ; |
---|
| 392 | for (vector<CVariable*>::iterator it = listVars.begin(); it != listVars.end(); it++) |
---|
| 393 | this->data_out->writeAttribute(*it); |
---|
[509] | 394 | |
---|
[528] | 395 | this->data_out->definition_end(); |
---|
| 396 | } |
---|
[219] | 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
[509] | 400 | //! Close file |
---|
[286] | 401 | void CFile::close(void) |
---|
| 402 | { |
---|
[321] | 403 | delete lastSync ; |
---|
| 404 | delete lastSplit ; |
---|
[379] | 405 | if (!allDomainEmpty) |
---|
[509] | 406 | if (isOpen) |
---|
[321] | 407 | { |
---|
| 408 | this->data_out->closeFile(); |
---|
| 409 | } |
---|
[379] | 410 | if (fileComm != MPI_COMM_NULL) MPI_Comm_free(&fileComm) ; |
---|
[286] | 411 | } |
---|
[219] | 412 | //---------------------------------------------------------------- |
---|
| 413 | |
---|
[509] | 414 | /*! |
---|
| 415 | \brief Parse xml file and write information into file object |
---|
| 416 | \param [in] node xmld node corresponding in xml file |
---|
| 417 | */ |
---|
[219] | 418 | void CFile::parse(xml::CXMLNode & node) |
---|
| 419 | { |
---|
| 420 | SuperClass::parse(node); |
---|
[509] | 421 | |
---|
[472] | 422 | if (node.goToChildElement()) |
---|
| 423 | { |
---|
| 424 | do |
---|
| 425 | { |
---|
| 426 | if (node.getElementName()=="field" || node.getElementName()=="field_group") this->getVirtualFieldGroup()->parseChild(node); |
---|
| 427 | else if (node.getElementName()=="variable" || node.getElementName()=="variable_group") this->getVirtualVariableGroup()->parseChild(node); |
---|
| 428 | } while (node.goToNextElement()) ; |
---|
| 429 | node.goToParentElement(); |
---|
[219] | 430 | } |
---|
[472] | 431 | |
---|
[219] | 432 | } |
---|
| 433 | //---------------------------------------------------------------- |
---|
| 434 | |
---|
[509] | 435 | /*! |
---|
| 436 | \brief Represent a file in form of string with all its info |
---|
| 437 | \return String |
---|
| 438 | */ |
---|
[219] | 439 | StdString CFile::toString(void) const |
---|
| 440 | { |
---|
| 441 | StdOStringStream oss; |
---|
| 442 | |
---|
| 443 | oss << "<" << CFile::GetName() << " "; |
---|
| 444 | if (this->hasId()) |
---|
| 445 | oss << " id=\"" << this->getId() << "\" "; |
---|
| 446 | oss << SuperClassAttribute::toString() << ">" << std::endl; |
---|
[347] | 447 | if (this->getVirtualFieldGroup() != NULL) |
---|
[219] | 448 | oss << *this->getVirtualFieldGroup() << std::endl; |
---|
| 449 | oss << "</" << CFile::GetName() << " >"; |
---|
| 450 | return (oss.str()); |
---|
| 451 | } |
---|
| 452 | |
---|
| 453 | //---------------------------------------------------------------- |
---|
[509] | 454 | |
---|
| 455 | /*! |
---|
| 456 | \brief Find all inheritace among objects in a file. |
---|
| 457 | \param [in] apply (true) write attributes of parent into ones of child if they are empty |
---|
| 458 | (false) write attributes of parent into a new container of child |
---|
| 459 | \param [in] parent |
---|
| 460 | */ |
---|
[445] | 461 | void CFile::solveDescInheritance(bool apply, const CAttributeMap * const parent) |
---|
[219] | 462 | { |
---|
[445] | 463 | SuperClassAttribute::setAttributes(parent,apply); |
---|
[509] | 464 | this->getVirtualFieldGroup()->solveDescInheritance(apply, NULL); |
---|
[472] | 465 | this->getVirtualVariableGroup()->solveDescInheritance(apply, NULL); |
---|
[219] | 466 | } |
---|
| 467 | |
---|
| 468 | //---------------------------------------------------------------- |
---|
| 469 | |
---|
[509] | 470 | // void CFile::processEnabledFile(void) |
---|
| 471 | // { |
---|
| 472 | // if (output_freq.isEmpty()) ERROR("void CFile::processEnabledFile(void)", |
---|
| 473 | // <<"File attribute <<output_freq>> is undefined"); |
---|
| 474 | // solveFieldRefInheritance(true) ; |
---|
| 475 | // getEnabledFields() ; |
---|
| 476 | // processEnabledFields() ; |
---|
| 477 | // } |
---|
| 478 | |
---|
| 479 | // void CFile::processEnabledFields(void) |
---|
| 480 | // { |
---|
| 481 | // for (unsigned int i = 0; i < this->enabledFields.size(); i++) |
---|
| 482 | // { |
---|
| 483 | // this->enabledFields[i]->processEnabledField() ; |
---|
| 484 | // } |
---|
| 485 | // } |
---|
| 486 | |
---|
| 487 | /*! |
---|
| 488 | \brief Resolve all reference of active fields. |
---|
| 489 | In order to know exactly which data each active field has, a search for all its |
---|
| 490 | reference to find its parents or/and its base reference object must be done. Moreover |
---|
| 491 | during this search, there are some information that can only be sent to server AFTER |
---|
| 492 | all information of active fields are created on server side, e.g: checking mask or index |
---|
| 493 | \param [in] sendToServer: Send all info to server (true) or only a part of it (false) |
---|
| 494 | */ |
---|
| 495 | void CFile::solveAllRefOfEnabledFields(bool sendToServer) |
---|
[459] | 496 | { |
---|
[509] | 497 | int size = this->enabledFields.size(); |
---|
| 498 | for (int i = 0; i < size; ++i) |
---|
| 499 | { |
---|
| 500 | this->enabledFields[i]->solveAllReferenceEnabledField(sendToServer); |
---|
| 501 | } |
---|
[459] | 502 | } |
---|
[509] | 503 | |
---|
| 504 | /*! |
---|
| 505 | \brief Contruct all expression related to active fields. |
---|
| 506 | Each field can do some expressions which appear on the xml file, and itself can be |
---|
| 507 | a result of an expression among some other fields. This function builds all possible expression |
---|
| 508 | relating to active fields. |
---|
| 509 | */ |
---|
| 510 | void CFile::buildAllExpressionOfEnabledFields() |
---|
[459] | 511 | { |
---|
[509] | 512 | int size = this->enabledFields.size(); |
---|
| 513 | for (int i = 0; i < size; ++i) |
---|
| 514 | { |
---|
| 515 | this->enabledFields[i]->buildAllExpressionEnabledField(); |
---|
| 516 | } |
---|
| 517 | } |
---|
| 518 | |
---|
[445] | 519 | void CFile::solveFieldRefInheritance(bool apply) |
---|
[219] | 520 | { |
---|
| 521 | // Résolution des héritages par référence de chacun des champs contenus dans le fichier. |
---|
[347] | 522 | std::vector<CField*> allF = this->getAllFields(); |
---|
[219] | 523 | for (unsigned int i = 0; i < allF.size(); i++) |
---|
[445] | 524 | allF[i]->solveRefInheritance(apply); |
---|
[219] | 525 | } |
---|
| 526 | |
---|
| 527 | //---------------------------------------------------------------- |
---|
| 528 | |
---|
| 529 | void CFile::solveEFGridRef(void) |
---|
| 530 | { |
---|
| 531 | for (unsigned int i = 0; i < this->enabledFields.size(); i++) |
---|
| 532 | this->enabledFields[i]->solveGridReference(); |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | //---------------------------------------------------------------- |
---|
| 536 | |
---|
| 537 | void CFile::solveEFOperation(void) |
---|
| 538 | { |
---|
| 539 | for (unsigned int i = 0; i < this->enabledFields.size(); i++) |
---|
| 540 | this->enabledFields[i]->solveOperation(); |
---|
| 541 | } |
---|
[509] | 542 | |
---|
[459] | 543 | void CFile::solveEFExpression(void) |
---|
[219] | 544 | { |
---|
[459] | 545 | for (unsigned int i = 0; i < this->enabledFields.size(); i++) |
---|
| 546 | this->enabledFields[i]->buildExpression(); |
---|
[509] | 547 | } |
---|
[369] | 548 | |
---|
[509] | 549 | /*! |
---|
| 550 | \brief Add a field into file. |
---|
| 551 | A field is added into file and it will be written out if the file is enabled and |
---|
| 552 | level of this field is smaller than level_output. A new field won't be created if one |
---|
| 553 | with id has already existed |
---|
| 554 | \param [in] id String identity of new field |
---|
| 555 | \return Pointer to added (or already existed) field |
---|
| 556 | */ |
---|
[347] | 557 | CField* CFile::addField(const string& id) |
---|
[300] | 558 | { |
---|
| 559 | return vFieldGroup->createChild(id) ; |
---|
| 560 | } |
---|
[219] | 561 | |
---|
[509] | 562 | /*! |
---|
| 563 | \brief Add a field group into file. |
---|
| 564 | A field group is added into file and it will play a role as parents for fields. |
---|
| 565 | A new field group won't be created if one with id has already existed |
---|
| 566 | \param [in] id String identity of new field group |
---|
| 567 | \return Pointer to added (or already existed) field group |
---|
| 568 | */ |
---|
[347] | 569 | CFieldGroup* CFile::addFieldGroup(const string& id) |
---|
[300] | 570 | { |
---|
| 571 | return vFieldGroup->createChildGroup(id) ; |
---|
| 572 | } |
---|
[509] | 573 | |
---|
| 574 | /*! |
---|
| 575 | \brief Add a variable into file. |
---|
| 576 | A variable is added into file and if one with id has already existed, pointer to |
---|
| 577 | it will be returned. |
---|
| 578 | Variable as long as attributes are information container of file. |
---|
| 579 | However, whereas attributes are "fixed" information, variables provides a more flexible way to user |
---|
| 580 | to fill in (extra) information for a file. |
---|
| 581 | \param [in] id String identity of new variable |
---|
| 582 | \return Pointer to added (or already existed) variable |
---|
| 583 | */ |
---|
[472] | 584 | CVariable* CFile::addVariable(const string& id) |
---|
| 585 | { |
---|
| 586 | return vVariableGroup->createChild(id) ; |
---|
| 587 | } |
---|
| 588 | |
---|
[509] | 589 | /*! |
---|
| 590 | \brief Add a variable group into file. |
---|
| 591 | A variable group is added into file and it will play a role as parents for variables. |
---|
| 592 | A new variable group won't be created if one with id has already existed |
---|
| 593 | \param [in] id String identity of new variable group |
---|
| 594 | \return Pointer to added (or already existed) variable group |
---|
| 595 | */ |
---|
[472] | 596 | CVariableGroup* CFile::addVariableGroup(const string& id) |
---|
| 597 | { |
---|
| 598 | return vVariableGroup->createChildGroup(id) ; |
---|
| 599 | } |
---|
[509] | 600 | |
---|
| 601 | /*! |
---|
| 602 | \brief Send a message to create a field on server side |
---|
| 603 | \param[in] id String identity of field that will be created on server |
---|
| 604 | */ |
---|
[300] | 605 | void CFile::sendAddField(const string& id) |
---|
| 606 | { |
---|
[347] | 607 | CContext* context=CContext::getCurrent() ; |
---|
[509] | 608 | |
---|
[300] | 609 | if (! context->hasServer ) |
---|
| 610 | { |
---|
| 611 | CContextClient* client=context->client ; |
---|
| 612 | |
---|
[509] | 613 | CEventClient event(this->getType(),EVENT_ID_ADD_FIELD) ; |
---|
[300] | 614 | if (client->isServerLeader()) |
---|
| 615 | { |
---|
| 616 | CMessage msg ; |
---|
| 617 | msg<<this->getId() ; |
---|
| 618 | msg<<id ; |
---|
| 619 | event.push(client->getServerLeader(),1,msg) ; |
---|
| 620 | client->sendEvent(event) ; |
---|
| 621 | } |
---|
| 622 | else client->sendEvent(event) ; |
---|
| 623 | } |
---|
[509] | 624 | |
---|
[300] | 625 | } |
---|
[509] | 626 | |
---|
| 627 | /*! |
---|
| 628 | \brief Send a message to create a field group on server side |
---|
| 629 | \param[in] id String identity of field group that will be created on server |
---|
| 630 | */ |
---|
[300] | 631 | void CFile::sendAddFieldGroup(const string& id) |
---|
| 632 | { |
---|
[347] | 633 | CContext* context=CContext::getCurrent() ; |
---|
[300] | 634 | if (! context->hasServer ) |
---|
| 635 | { |
---|
| 636 | CContextClient* client=context->client ; |
---|
| 637 | |
---|
[509] | 638 | CEventClient event(this->getType(),EVENT_ID_ADD_FIELD_GROUP) ; |
---|
[300] | 639 | if (client->isServerLeader()) |
---|
| 640 | { |
---|
| 641 | CMessage msg ; |
---|
| 642 | msg<<this->getId() ; |
---|
| 643 | msg<<id ; |
---|
| 644 | event.push(client->getServerLeader(),1,msg) ; |
---|
| 645 | client->sendEvent(event) ; |
---|
| 646 | } |
---|
| 647 | else client->sendEvent(event) ; |
---|
| 648 | } |
---|
[509] | 649 | |
---|
[300] | 650 | } |
---|
[509] | 651 | |
---|
| 652 | /*! |
---|
| 653 | \brief Receive a message annoucing the creation of a field on server side |
---|
| 654 | \param[in] event Received event |
---|
| 655 | */ |
---|
[300] | 656 | void CFile::recvAddField(CEventServer& event) |
---|
| 657 | { |
---|
[509] | 658 | |
---|
[300] | 659 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 660 | string id; |
---|
| 661 | *buffer>>id ; |
---|
| 662 | get(id)->recvAddField(*buffer) ; |
---|
| 663 | } |
---|
[509] | 664 | |
---|
| 665 | /*! |
---|
| 666 | \brief Receive a message annoucing the creation of a field on server side |
---|
| 667 | \param[in] buffer Buffer containing message |
---|
| 668 | */ |
---|
[300] | 669 | void CFile::recvAddField(CBufferIn& buffer) |
---|
| 670 | { |
---|
| 671 | string id ; |
---|
| 672 | buffer>>id ; |
---|
| 673 | addField(id) ; |
---|
| 674 | } |
---|
| 675 | |
---|
[509] | 676 | /*! |
---|
| 677 | \brief Receive a message annoucing the creation of a field group on server side |
---|
| 678 | \param[in] event Received event |
---|
| 679 | */ |
---|
[300] | 680 | void CFile::recvAddFieldGroup(CEventServer& event) |
---|
| 681 | { |
---|
[509] | 682 | |
---|
[300] | 683 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 684 | string id; |
---|
| 685 | *buffer>>id ; |
---|
| 686 | get(id)->recvAddFieldGroup(*buffer) ; |
---|
| 687 | } |
---|
[509] | 688 | |
---|
| 689 | /*! |
---|
| 690 | \brief Receive a message annoucing the creation of a field group on server side |
---|
| 691 | \param[in] buffer Buffer containing message |
---|
| 692 | */ |
---|
[300] | 693 | void CFile::recvAddFieldGroup(CBufferIn& buffer) |
---|
| 694 | { |
---|
| 695 | string id ; |
---|
| 696 | buffer>>id ; |
---|
| 697 | addFieldGroup(id) ; |
---|
| 698 | } |
---|
| 699 | |
---|
[509] | 700 | /*! |
---|
| 701 | \brief Send messages to duplicate all variables on server side |
---|
| 702 | Because each variable has also its attributes. So first thing to do is replicate |
---|
| 703 | all these attributes on server side. Because variable can have a value, the second thing |
---|
| 704 | is to duplicate this value on server, too. |
---|
| 705 | */ |
---|
| 706 | void CFile::sendAddAllVariables() |
---|
| 707 | { |
---|
| 708 | if (!getAllVariables().empty()) |
---|
| 709 | { |
---|
| 710 | // Firstly, it's necessary to add virtual variable group |
---|
| 711 | sendAddVariableGroup(getVirtualVariableGroup()->getId()); |
---|
[472] | 712 | |
---|
[509] | 713 | // Okie, now we can add to this variable group |
---|
| 714 | std::vector<CVariable*> allVar = getAllVariables(); |
---|
| 715 | std::vector<CVariable*>::const_iterator it = allVar.begin(); |
---|
| 716 | std::vector<CVariable*>::const_iterator itE = allVar.end(); |
---|
[472] | 717 | |
---|
[509] | 718 | for (; it != itE; ++it) |
---|
| 719 | { |
---|
| 720 | this->sendAddVariable((*it)->getId()); |
---|
| 721 | (*it)->sendAllAttributesToServer(); |
---|
| 722 | (*it)->sendValue(); |
---|
| 723 | } |
---|
| 724 | } |
---|
| 725 | } |
---|
[472] | 726 | |
---|
[509] | 727 | /*! |
---|
| 728 | \brief Send a message to create a variable on server side |
---|
| 729 | A variable always belongs to a variable group |
---|
| 730 | \param[in] id String identity of variable that will be created on server |
---|
| 731 | */ |
---|
[472] | 732 | void CFile::sendAddVariable(const string& id) |
---|
| 733 | { |
---|
| 734 | CContext* context=CContext::getCurrent() ; |
---|
[509] | 735 | |
---|
[472] | 736 | if (! context->hasServer ) |
---|
| 737 | { |
---|
| 738 | CContextClient* client=context->client ; |
---|
| 739 | |
---|
[509] | 740 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE) ; |
---|
[472] | 741 | if (client->isServerLeader()) |
---|
| 742 | { |
---|
| 743 | CMessage msg ; |
---|
| 744 | msg<<this->getId() ; |
---|
| 745 | msg<<id ; |
---|
| 746 | event.push(client->getServerLeader(),1,msg) ; |
---|
| 747 | client->sendEvent(event) ; |
---|
| 748 | } |
---|
| 749 | else client->sendEvent(event) ; |
---|
| 750 | } |
---|
[509] | 751 | |
---|
[472] | 752 | } |
---|
[509] | 753 | |
---|
| 754 | /*! |
---|
| 755 | \brief Send a message to create a variable group on server side |
---|
| 756 | \param[in] id String identity of variable group that will be created on server |
---|
| 757 | */ |
---|
[472] | 758 | void CFile::sendAddVariableGroup(const string& id) |
---|
| 759 | { |
---|
| 760 | CContext* context=CContext::getCurrent() ; |
---|
| 761 | if (! context->hasServer ) |
---|
| 762 | { |
---|
| 763 | CContextClient* client=context->client ; |
---|
| 764 | |
---|
[509] | 765 | CEventClient event(this->getType(),EVENT_ID_ADD_VARIABLE_GROUP) ; |
---|
[472] | 766 | if (client->isServerLeader()) |
---|
| 767 | { |
---|
| 768 | CMessage msg ; |
---|
| 769 | msg<<this->getId() ; |
---|
| 770 | msg<<id ; |
---|
| 771 | event.push(client->getServerLeader(),1,msg) ; |
---|
| 772 | client->sendEvent(event) ; |
---|
| 773 | } |
---|
| 774 | else client->sendEvent(event) ; |
---|
| 775 | } |
---|
[509] | 776 | |
---|
[472] | 777 | } |
---|
[509] | 778 | |
---|
| 779 | /*! |
---|
| 780 | \brief Receive a message annoucing the creation of a variable on server side |
---|
| 781 | \param[in] event Received event |
---|
| 782 | */ |
---|
[472] | 783 | void CFile::recvAddVariable(CEventServer& event) |
---|
| 784 | { |
---|
[509] | 785 | |
---|
[472] | 786 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 787 | string id; |
---|
| 788 | *buffer>>id ; |
---|
| 789 | get(id)->recvAddVariable(*buffer) ; |
---|
| 790 | } |
---|
[509] | 791 | |
---|
| 792 | /*! |
---|
| 793 | \brief Receive a message annoucing the creation of a variable on server side |
---|
| 794 | \param[in] buffer Buffer containing message |
---|
| 795 | */ |
---|
[472] | 796 | void CFile::recvAddVariable(CBufferIn& buffer) |
---|
| 797 | { |
---|
| 798 | string id ; |
---|
| 799 | buffer>>id ; |
---|
| 800 | addVariable(id) ; |
---|
| 801 | } |
---|
| 802 | |
---|
[509] | 803 | /*! |
---|
| 804 | \brief Receive a message annoucing the creation of a variable group on server side |
---|
| 805 | \param[in] event Received event |
---|
| 806 | */ |
---|
[472] | 807 | void CFile::recvAddVariableGroup(CEventServer& event) |
---|
| 808 | { |
---|
[509] | 809 | |
---|
[472] | 810 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 811 | string id; |
---|
| 812 | *buffer>>id ; |
---|
| 813 | get(id)->recvAddVariableGroup(*buffer) ; |
---|
| 814 | } |
---|
[509] | 815 | |
---|
| 816 | /*! |
---|
| 817 | \brief Receive a message annoucing the creation of a variable group on server side |
---|
| 818 | \param[in] buffer Buffer containing message |
---|
| 819 | */ |
---|
[472] | 820 | void CFile::recvAddVariableGroup(CBufferIn& buffer) |
---|
| 821 | { |
---|
| 822 | string id ; |
---|
| 823 | buffer>>id ; |
---|
| 824 | addVariableGroup(id) ; |
---|
| 825 | } |
---|
| 826 | |
---|
[509] | 827 | /*! |
---|
| 828 | \brief Sending all active (enabled) fields from client to server. |
---|
| 829 | Each field is identified uniquely by its string identity. Not only should we |
---|
| 830 | send the id to server but also we need to send ids of reference domain and reference axis. |
---|
| 831 | With these two id, it's easier to make reference to grid where all data should be written. |
---|
| 832 | Remark: This function must be called AFTER all active (enabled) files have been created on the server side |
---|
| 833 | */ |
---|
| 834 | void CFile::sendEnabledFields() |
---|
| 835 | { |
---|
| 836 | int size = this->enabledFields.size(); |
---|
| 837 | CField* fieldPtr(0); |
---|
| 838 | for (int i = 0; i < size; ++i) |
---|
| 839 | { |
---|
| 840 | fieldPtr = this->enabledFields[i]; |
---|
| 841 | if (fieldPtr->name.isEmpty()) fieldPtr->name.setValue(fieldPtr->getBaseFieldReference()->getId()); |
---|
| 842 | this->sendAddField(fieldPtr->getId()); |
---|
| 843 | fieldPtr->sendAllAttributesToServer(); |
---|
| 844 | fieldPtr->sendAddAllVariables(); |
---|
| 845 | } |
---|
| 846 | } |
---|
[472] | 847 | |
---|
[509] | 848 | /*! |
---|
| 849 | \brief Dispatch event received from client |
---|
| 850 | Whenever a message is received in buffer of server, it will be processed depending on |
---|
| 851 | its event type. A new event type should be added in the switch list to make sure |
---|
| 852 | it processed on server side. |
---|
| 853 | \param [in] event: Received message |
---|
| 854 | */ |
---|
[300] | 855 | bool CFile::dispatchEvent(CEventServer& event) |
---|
| 856 | { |
---|
| 857 | if (SuperClass::dispatchEvent(event)) return true ; |
---|
| 858 | else |
---|
| 859 | { |
---|
| 860 | switch(event.type) |
---|
| 861 | { |
---|
| 862 | case EVENT_ID_ADD_FIELD : |
---|
| 863 | recvAddField(event) ; |
---|
| 864 | return true ; |
---|
| 865 | break ; |
---|
[509] | 866 | |
---|
[300] | 867 | case EVENT_ID_ADD_FIELD_GROUP : |
---|
| 868 | recvAddFieldGroup(event) ; |
---|
| 869 | return true ; |
---|
[509] | 870 | break ; |
---|
| 871 | |
---|
[472] | 872 | case EVENT_ID_ADD_VARIABLE : |
---|
| 873 | recvAddVariable(event) ; |
---|
| 874 | return true ; |
---|
| 875 | break ; |
---|
[509] | 876 | |
---|
[472] | 877 | case EVENT_ID_ADD_VARIABLE_GROUP : |
---|
| 878 | recvAddVariableGroup(event) ; |
---|
| 879 | return true ; |
---|
[509] | 880 | break ; |
---|
[300] | 881 | default : |
---|
| 882 | ERROR("bool CFile::dispatchEvent(CEventServer& event)", <<"Unknown Event") ; |
---|
| 883 | return false ; |
---|
| 884 | } |
---|
| 885 | } |
---|
| 886 | } |
---|
[509] | 887 | |
---|
| 888 | |
---|
| 889 | |
---|
| 890 | |
---|
[219] | 891 | ///--------------------------------------------------------------- |
---|
| 892 | |
---|
[335] | 893 | } // namespace xios |
---|