source: XIOS/trunk/src/node/variable.cpp @ 773

Last change on this file since 773 was 773, checked in by rlacroix, 8 years ago

File/Variable?: Add an helper function to get the output name.

  • Property copyright set to
    Software name : XIOS (Xml I/O Server)
    http://forge.ipsl.jussieu.fr/ioserver
    Creation date : January 2009
    Licence : CeCCIL version2
    see license file in root directory : Licence_CeCILL_V2-en.txt
    or http://www.cecill.info/licences/Licence_CeCILL_V2-en.html
    Holder : CEA/LSCE (Laboratoire des Sciences du CLimat et de l'Environnement)
    CNRS/IPSL (Institut Pierre Simon Laplace)
    Project Manager : Yann Meurdesoif
    yann.meurdesoif@cea.fr
File size: 5.5 KB
RevLine 
[268]1#include "variable.hpp"
2
[352]3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
[268]6#include "object_factory.hpp"
[591]7#include "xios_spl.hpp"
[352]8#include "type.hpp"
[489]9#include "context.hpp"
10#include "context_client.hpp"
[388]11#include <boost/algorithm/string.hpp>
[268]12
[335]13namespace xios {
[268]14
15   /// ////////////////////// Définitions ////////////////////// ///
16
17   CVariable::CVariable(void)
18      : CObjectTemplate<CVariable>()
19      , CVariableAttributes()
[274]20      , content()
[268]21   { /* Ne rien faire de plus */ }
22
23   CVariable::CVariable(const StdString & id)
24      : CObjectTemplate<CVariable>(id)
25      , CVariableAttributes()
[274]26      , content()
[268]27   { /* Ne rien faire de plus */ }
28
29   CVariable::~CVariable(void)
30   { /* Ne rien faire de plus */ }
31
32   StdString CVariable::GetName(void)   { return (StdString("variable")); }
33   StdString CVariable::GetDefName(void){ return (CVariable::GetName()); }
34   ENodeType CVariable::GetType(void)   { return (eVariable); }
35
[274]36   void CVariable::parse(xml::CXMLNode & node)
37   {
38      SuperClass::parse(node);
39      StdString id = (this->hasId()) ? this->getId() : StdString("undefined");
40      if (!node.getContent(this->content))
41      {
42         ERROR("CVariable::parse(xml::CXMLNode & node)",
43               << "[ variable id = " << id
44               << " ] variable is not defined !");
45      }
[477]46      content = boost::trim_copy(content) ;
[274]47   }
48
[773]49   const StdString& CVariable::getVariableOutputName(void) const
50   {
51     return name.isEmpty() ? getId() : name;
52   }
53
[274]54   const StdString & CVariable::getContent (void) const
55   {
56      return (this->content);
57   }
58
[489]59   void CVariable::setContent(const StdString& contentStr)
60   {
61     this->content = contentStr;
62   }
63
[274]64   StdString CVariable::toString(void) const
65   {
66      StdOStringStream oss;
67
68      oss << "<" << CVariable::GetName() << " ";
69      if (this->hasId())
70         oss << " id=\"" << this->getId() << "\" ";
71      oss << SuperClassAttribute::toString() << ">" << std::endl
72          << this->content /*<< std::endl*/;
73      oss << "</" << CVariable::GetName() << " >";
74      return (oss.str());
[489]75   }
76
77   /*
78   *\brief Sending value of a variable with its id from client to server
79   *
80   */
81   void CVariable::sendValue()
82   {
83     CContext* context=CContext::getCurrent() ;
84     if (!context->hasServer)
85     {
86       CContextClient* client=context->client ;
87
88       CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
89       if (client->isServerLeader())
90       {
91         CMessage msg ;
92         msg<<this->getId() ;
93         msg<<content ;
[595]94         const std::list<int>& ranks = client->getRanksServerLeader();
95         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
96           event.push(*itRank,1,msg);
[489]97         client->sendEvent(event) ;
98       }
99       else client->sendEvent(event) ;
100    }
101   }
102
103   /*
104   *\brief Receive value of a variable with its id from client to server
105   *
106   */
107   void CVariable::recvValue(CEventServer& event)
108   {
109      CBufferIn* buffer=event.subEvents.begin()->buffer;
110      string id;
111      *buffer>>id ;
112      get(id)->recvValue(*buffer);
113   }
114
115   /*
116   *\brief Receive value of a variable with its id from client to server
117   *
118   */
119   void CVariable::recvValue(CBufferIn& buffer)
120   {
121      string str ;
122      buffer>>str;
123      setContent(str);
124   }
125
126   bool CVariable::dispatchEvent(CEventServer& event)
127   {
128    if (SuperClass::dispatchEvent(event)) return true ;
129    else
130    {
131      switch(event.type)
132      {
133        case EVENT_ID_VARIABLE_VALUE :
134          recvValue(event) ;
135          return true ;
136          break ;
137
138        default :
139          ERROR("bool CVariable::dispatchEvent(CEventServer& event)",<<"Unknown Event") ;
140          return false ;
141      }
142    }
143   }
144
[369]145/*
[274]146   void CVariable::toBinary(StdOStream & os) const
147   {
148     const StdString & content = this->content;
149     const StdSize size        =  content.size();
150     SuperClass::toBinary(os);
151
152     os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
153     os.write (content.data(), size * sizeof(char));
154   }
155
156   void CVariable::fromBinary(StdIStream & is)
157   {
158      SuperClass::fromBinary(is);
159      StdSize size  = 0;
160      is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
161      this->content.assign(size, ' ');
162      is.read (const_cast<char *>(this->content.data()), size * sizeof(char));
163   }
[369]164*/
[274]165   void CVariableGroup::parse(xml::CXMLNode & node, bool withAttr)
166   {
[347]167      CVariableGroup* group_ptr = (this->hasId())
[346]168         ? CVariableGroup::get(this->getId()) : CVariableGroup::get(this);
[274]169
170      StdString content;
171      if (this->getId().compare(CVariableGroup::GetDefName()) != 0 && node.getContent(content))
172      {
173        StdSize beginid = 0, endid = 0, begindata = 0, enddata = 0;
174        StdString subdata, subid;
175
176        while ((beginid = content.find_first_not_of ( " \r\n\t;", enddata)) != StdString::npos)
177        {
178           endid   = content.find_first_of ( " \r\n\t=", beginid );
179           subid   = content.substr ( beginid, endid-beginid);
[387]180           subid   = boost::to_lower_copy(boost::trim_copy(subid)) ;
[274]181
182           begindata = content.find_first_of ( "=", endid ) + 1;
183           enddata   = content.find_first_of ( ";", begindata );
184           subdata   = content.substr ( begindata, enddata-begindata);
[477]185           subdata   = boost::trim_copy(subdata) ;
[346]186           group_ptr->createChild(subid)->content = subdata ;
[274]187        }
188      }
189      else
190      {
191         SuperClass::parse(node, withAttr);
192      }
193      //SuperClass::parse(node, withAttr);
194
195   }
196
[335]197} // namespace xios
Note: See TracBrowser for help on using the repository browser.