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

Last change on this file since 595 was 595, checked in by rlacroix, 9 years ago

Allow using more servers than clients.

This will be useful later when implementing server to client communications.

  • 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.4 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
49   const StdString & CVariable::getContent (void) const
50   {
51      return (this->content);
52   }
53
[489]54   void CVariable::setContent(const StdString& contentStr)
55   {
56     this->content = contentStr;
57   }
58
[274]59   StdString CVariable::toString(void) const
60   {
61      StdOStringStream oss;
62
63      oss << "<" << CVariable::GetName() << " ";
64      if (this->hasId())
65         oss << " id=\"" << this->getId() << "\" ";
66      oss << SuperClassAttribute::toString() << ">" << std::endl
67          << this->content /*<< std::endl*/;
68      oss << "</" << CVariable::GetName() << " >";
69      return (oss.str());
[489]70   }
71
72   /*
73   *\brief Sending value of a variable with its id from client to server
74   *
75   */
76   void CVariable::sendValue()
77   {
78     CContext* context=CContext::getCurrent() ;
79     if (!context->hasServer)
80     {
81       CContextClient* client=context->client ;
82
83       CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
84       if (client->isServerLeader())
85       {
86         CMessage msg ;
87         msg<<this->getId() ;
88         msg<<content ;
[595]89         const std::list<int>& ranks = client->getRanksServerLeader();
90         for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
91           event.push(*itRank,1,msg);
[489]92         client->sendEvent(event) ;
93       }
94       else client->sendEvent(event) ;
95    }
96   }
97
98   /*
99   *\brief Receive value of a variable with its id from client to server
100   *
101   */
102   void CVariable::recvValue(CEventServer& event)
103   {
104      CBufferIn* buffer=event.subEvents.begin()->buffer;
105      string id;
106      *buffer>>id ;
107      get(id)->recvValue(*buffer);
108   }
109
110   /*
111   *\brief Receive value of a variable with its id from client to server
112   *
113   */
114   void CVariable::recvValue(CBufferIn& buffer)
115   {
116      string str ;
117      buffer>>str;
118      setContent(str);
119   }
120
121   bool CVariable::dispatchEvent(CEventServer& event)
122   {
123    if (SuperClass::dispatchEvent(event)) return true ;
124    else
125    {
126      switch(event.type)
127      {
128        case EVENT_ID_VARIABLE_VALUE :
129          recvValue(event) ;
130          return true ;
131          break ;
132
133        default :
134          ERROR("bool CVariable::dispatchEvent(CEventServer& event)",<<"Unknown Event") ;
135          return false ;
136      }
137    }
138   }
139
[369]140/*
[274]141   void CVariable::toBinary(StdOStream & os) const
142   {
143     const StdString & content = this->content;
144     const StdSize size        =  content.size();
145     SuperClass::toBinary(os);
146
147     os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
148     os.write (content.data(), size * sizeof(char));
149   }
150
151   void CVariable::fromBinary(StdIStream & is)
152   {
153      SuperClass::fromBinary(is);
154      StdSize size  = 0;
155      is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
156      this->content.assign(size, ' ');
157      is.read (const_cast<char *>(this->content.data()), size * sizeof(char));
158   }
[369]159*/
[274]160   void CVariableGroup::parse(xml::CXMLNode & node, bool withAttr)
161   {
[347]162      CVariableGroup* group_ptr = (this->hasId())
[346]163         ? CVariableGroup::get(this->getId()) : CVariableGroup::get(this);
[274]164
165      StdString content;
166      if (this->getId().compare(CVariableGroup::GetDefName()) != 0 && node.getContent(content))
167      {
168        StdSize beginid = 0, endid = 0, begindata = 0, enddata = 0;
169        StdString subdata, subid;
170
171        while ((beginid = content.find_first_not_of ( " \r\n\t;", enddata)) != StdString::npos)
172        {
173           endid   = content.find_first_of ( " \r\n\t=", beginid );
174           subid   = content.substr ( beginid, endid-beginid);
[387]175           subid   = boost::to_lower_copy(boost::trim_copy(subid)) ;
[274]176
177           begindata = content.find_first_of ( "=", endid ) + 1;
178           enddata   = content.find_first_of ( ";", begindata );
179           subdata   = content.substr ( begindata, enddata-begindata);
[477]180           subdata   = boost::trim_copy(subdata) ;
[346]181           group_ptr->createChild(subid)->content = subdata ;
[274]182        }
183      }
184      else
185      {
186         SuperClass::parse(node, withAttr);
187      }
188      //SuperClass::parse(node, withAttr);
189
190   }
191
[335]192} // namespace xios
Note: See TracBrowser for help on using the repository browser.