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

Last change on this file since 1021 was 1021, checked in by oabramkina, 7 years ago

Intermeadiate version for merging with new server functionalities.

  • 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: 7.2 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       int nbSrvPools = (context->hasServer) ? context->clientPrimServer.size() : 1;
89       for (int i = 0; i < nbSrvPools; ++i)
90       {
91//       CContextClient* contextClientTmp = (0 != context->clientPrimServer) ? context->clientPrimServer
92         CContextClient* contextClientTmp = (context->hasServer) ? context->clientPrimServer[i]
93                                                                             : context->client;
94
95         CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
96         if (contextClientTmp->isServerLeader())
97         {
98           CMessage msg ;
99           msg<<this->getId() ;
100           msg<<content ;
101           const std::list<int>& ranks = contextClientTmp->getRanksServerLeader();
102           for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
103             event.push(*itRank,1,msg);
104           contextClientTmp->sendEvent(event) ;
105         }
106         else contextClientTmp->sendEvent(event) ;
107      }
108     }
109
110    //  if (!context->hasServer)
111    //  {
112    //    CContextClient* client=context->client ;
113
114    //    CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
115    //    if (client->isServerLeader())
116    //    {
117    //      CMessage msg ;
118    //      msg<<this->getId() ;
119    //      msg<<content ;
120    //      const std::list<int>& ranks = client->getRanksServerLeader();
121    //      for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
122    //        event.push(*itRank,1,msg);
123    //      client->sendEvent(event) ;
124    //    }
125    //    else client->sendEvent(event) ;
126    // }
127   }
128
129   void CVariable::sendValue(CContextClient* client)
130   {
131     CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
132     if (client->isServerLeader())
133     {
134       CMessage msg ;
135       msg<<this->getId() ;
136       msg<<content ;
137       const std::list<int>& ranks = client->getRanksServerLeader();
138       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
139         event.push(*itRank,1,msg);
140       client->sendEvent(event) ;
141     }
142     else client->sendEvent(event) ;
143   }
144
145   /*
146   *\brief Receive value of a variable with its id from client to server
147   *
148   */
149   void CVariable::recvValue(CEventServer& event)
150   {
151      CBufferIn* buffer=event.subEvents.begin()->buffer;
152      string id;
153      *buffer>>id ;
154      get(id)->recvValue(*buffer);
155   }
156
157   /*
158   *\brief Receive value of a variable with its id from client to server
159   *
160   */
161   void CVariable::recvValue(CBufferIn& buffer)
162   {
163      string str ;
164      buffer>>str;
165      setContent(str);
166   }
167
168   bool CVariable::dispatchEvent(CEventServer& event)
169   {
170    if (SuperClass::dispatchEvent(event)) return true ;
171    else
172    {
173      switch(event.type)
174      {
175        case EVENT_ID_VARIABLE_VALUE :
176          recvValue(event) ;
177          return true ;
178          break ;
179
180        default :
181          ERROR("bool CVariable::dispatchEvent(CEventServer& event)",<<"Unknown Event") ;
182          return false ;
183      }
184    }
185   }
186
187/*
188   void CVariable::toBinary(StdOStream & os) const
189   {
190     const StdString & content = this->content;
191     const StdSize size        =  content.size();
192     SuperClass::toBinary(os);
193
194     os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
195     os.write (content.data(), size * sizeof(char));
196   }
197
198   void CVariable::fromBinary(StdIStream & is)
199   {
200      SuperClass::fromBinary(is);
201      StdSize size  = 0;
202      is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
203      this->content.assign(size, ' ');
204      is.read (const_cast<char *>(this->content.data()), size * sizeof(char));
205   }
206*/
207   void CVariableGroup::parse(xml::CXMLNode & node, bool withAttr)
208   {
209      CVariableGroup* group_ptr = (this->hasId())
210         ? CVariableGroup::get(this->getId()) : CVariableGroup::get(this);
211
212      StdString content;
213      if (this->getId().compare(CVariableGroup::GetDefName()) != 0 && node.getContent(content))
214      {
215        StdSize beginid = 0, endid = 0, begindata = 0, enddata = 0;
216        StdString subdata, subid;
217
218        while ((beginid = content.find_first_not_of ( " \r\n\t;", enddata)) != StdString::npos)
219        {
220           endid   = content.find_first_of ( " \r\n\t=", beginid );
221           subid   = content.substr ( beginid, endid-beginid);
222           subid   = boost::to_lower_copy(boost::trim_copy(subid)) ;
223
224           begindata = content.find_first_of ( "=", endid ) + 1;
225           enddata   = content.find_first_of ( ";", begindata );
226           subdata   = content.substr ( begindata, enddata-begindata);
227           subdata   = boost::trim_copy(subdata) ;
228           group_ptr->createChild(subid)->content = subdata ;
229        }
230      }
231      else
232      {
233         SuperClass::parse(node, withAttr);
234      }
235      //SuperClass::parse(node, withAttr);
236
237   }
238
239} // namespace xios
Note: See TracBrowser for help on using the repository browser.