source: XIOS/dev/dev_olga/src/node/variable.cpp @ 987

Last change on this file since 987 was 987, checked in by mhnguyen, 7 years ago

First working version.
There are more things to do with it

  • 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: 6.4 KB
Line 
1#include "variable.hpp"
2
3#include "attribute_template.hpp"
4#include "object_template.hpp"
5#include "group_template.hpp"
6#include "object_factory.hpp"
7#include "xios_spl.hpp"
8#include "type.hpp"
9#include "context.hpp"
10#include "context_client.hpp"
11#include <boost/algorithm/string.hpp>
12
13namespace xios {
14
15   /// ////////////////////// Définitions ////////////////////// ///
16
17   CVariable::CVariable(void)
18      : CObjectTemplate<CVariable>()
19      , CVariableAttributes()
20      , content()
21   { /* Ne rien faire de plus */ }
22
23   CVariable::CVariable(const StdString & id)
24      : CObjectTemplate<CVariable>(id)
25      , CVariableAttributes()
26      , content()
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
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      }
46      content = boost::trim_copy(content) ;
47   }
48
49   const StdString& CVariable::getVariableOutputName(void) const
50   {
51     return name.isEmpty() ? getId() : name;
52   }
53
54   const StdString & CVariable::getContent (void) const
55   {
56      return (this->content);
57   }
58
59   void CVariable::setContent(const StdString& contentStr)
60   {
61     this->content = contentStr;
62   }
63
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());
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
85     if (context->hasClient)
86     {
87           // Use correct context client to send message
88       CContextClient* contextClientTmp = (0 != context->clientPrimServer) ? context->clientPrimServer
89                                                                           : context->client;
90
91       CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
92       if (contextClientTmp->isServerLeader())
93       {
94         CMessage msg ;
95         msg<<this->getId() ;
96         msg<<content ;
97         const std::list<int>& ranks = contextClientTmp->getRanksServerLeader();
98         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
99           event.push(*itRank,1,msg);
100         contextClientTmp->sendEvent(event) ;
101       }
102       else contextClientTmp->sendEvent(event) ;
103    }
104
105
106    //  if (!context->hasServer)
107    //  {
108    //    CContextClient* client=context->client ;
109
110    //    CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
111    //    if (client->isServerLeader())
112    //    {
113    //      CMessage msg ;
114    //      msg<<this->getId() ;
115    //      msg<<content ;
116    //      const std::list<int>& ranks = client->getRanksServerLeader();
117    //      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
118    //        event.push(*itRank,1,msg);
119    //      client->sendEvent(event) ;
120    //    }
121    //    else client->sendEvent(event) ;
122    // }
123   }
124
125   /*
126   *\brief Receive value of a variable with its id from client to server
127   *
128   */
129   void CVariable::recvValue(CEventServer& event)
130   {
131      CBufferIn* buffer=event.subEvents.begin()->buffer;
132      string id;
133      *buffer>>id ;
134      get(id)->recvValue(*buffer);
135   }
136
137   /*
138   *\brief Receive value of a variable with its id from client to server
139   *
140   */
141   void CVariable::recvValue(CBufferIn& buffer)
142   {
143      string str ;
144      buffer>>str;
145      setContent(str);
146   }
147
148   bool CVariable::dispatchEvent(CEventServer& event)
149   {
150    if (SuperClass::dispatchEvent(event)) return true ;
151    else
152    {
153      switch(event.type)
154      {
155        case EVENT_ID_VARIABLE_VALUE :
156          recvValue(event) ;
157          return true ;
158          break ;
159
160        default :
161          ERROR("bool CVariable::dispatchEvent(CEventServer& event)",<<"Unknown Event") ;
162          return false ;
163      }
164    }
165   }
166
167/*
168   void CVariable::toBinary(StdOStream & os) const
169   {
170     const StdString & content = this->content;
171     const StdSize size        =  content.size();
172     SuperClass::toBinary(os);
173
174     os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
175     os.write (content.data(), size * sizeof(char));
176   }
177
178   void CVariable::fromBinary(StdIStream & is)
179   {
180      SuperClass::fromBinary(is);
181      StdSize size  = 0;
182      is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
183      this->content.assign(size, ' ');
184      is.read (const_cast<char *>(this->content.data()), size * sizeof(char));
185   }
186*/
187   void CVariableGroup::parse(xml::CXMLNode & node, bool withAttr)
188   {
189      CVariableGroup* group_ptr = (this->hasId())
190         ? CVariableGroup::get(this->getId()) : CVariableGroup::get(this);
191
192      StdString content;
193      if (this->getId().compare(CVariableGroup::GetDefName()) != 0 && node.getContent(content))
194      {
195        StdSize beginid = 0, endid = 0, begindata = 0, enddata = 0;
196        StdString subdata, subid;
197
198        while ((beginid = content.find_first_not_of ( " \r\n\t;", enddata)) != StdString::npos)
199        {
200           endid   = content.find_first_of ( " \r\n\t=", beginid );
201           subid   = content.substr ( beginid, endid-beginid);
202           subid   = boost::to_lower_copy(boost::trim_copy(subid)) ;
203
204           begindata = content.find_first_of ( "=", endid ) + 1;
205           enddata   = content.find_first_of ( ";", begindata );
206           subdata   = content.substr ( begindata, enddata-begindata);
207           subdata   = boost::trim_copy(subdata) ;
208           group_ptr->createChild(subid)->content = subdata ;
209        }
210      }
211      else
212      {
213         SuperClass::parse(node, withAttr);
214      }
215      //SuperClass::parse(node, withAttr);
216
217   }
218
219} // namespace xios
Note: See TracBrowser for help on using the repository browser.