source: XIOS3/trunk/src/node/variable.cpp @ 2627

Last change on this file since 2627 was 2627, checked in by jderouillat, 3 months ago

Fix some main timers on the client side. 1dd memory_report to the list of available parameters.

  • 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: 8.3 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 (CContext::getCurrent()->getId()=="xios") checkInDictionary(id);
41      if (!node.getContent(this->content))
42      {
43        xml::THashAttributes attributes = node.getAttributes();
44        StdString variableName = attributes["name"];
45
46        node.goToParentElement();
47        StdString parentName = node.getElementName();
48        attributes = node.getAttributes();
49        error << "The variable id = " << id << " and name = " << variableName << " does not have any content. Please define it!" << std::endl
50              << "This variable is inside another element whose attributes are :" << std::endl;
51
52        for (xml::THashAttributes::iterator it = attributes.begin(); it != attributes.end(); ++it)
53        {
54           error << it->first << "=\"" << it->second.c_str() << "\" ";
55        }
56        error << std::endl; 
57
58         ERROR("CVariable::parse(xml::CXMLNode & node)",
59               << "[ variable id = " << id
60               << " ] variable is not defined !. It does not have any content. See error log for more details.");
61      }
62      content = boost::trim_copy(content) ;
63   }
64
65   void CVariable::checkInDictionary(StdString id)
66   {
67     // $ grep -rw getin ../src/|grep -v template|grep \"|awk -F \" '{ print $2}'|sort|uniq
68     set<StdString> dictionary_ = { 
69                                    "buffer_size_factor",
70                                    "call_oasis_enddef",
71                                    "check_event_sync",
72                                    "checksum_recv_fields",
73                                    "checksum_send_fields",
74                                    "clients_code_id",
75                                    "info_level",
76                                    "log_memory",
77                                    "log_type",
78                                    "max_buffer_size",
79                                    "memory_report",
80                                    "memtrack_blocks",
81                                    "memtrack_size",
82                                    "min_buffer_size",
83                                    "number_pools_server2",
84                                    "optimal_buffer_size",
85                                    "print_file",
86                                    "pure_one_sided",
87                                    "ratio_server2",
88                                    "recv_field_timeout",
89                                    "server2_dist_file_memory",
90                                    "server2_dist_file_memory_ratio",
91                                    "server_puplish_timeout",
92                                    "system_stack",
93                                    "transport_protocol",
94                                    "using_oasis",
95                                    "using_server",
96                                    "using_server2",
97                                    "xios_stack"                       
98     };
99     if (dictionary_.find(id)==dictionary_.end())
100     {
101       ERROR("CVariable::checkInDictionary(StdString id)",
102              << "[ variable id = " << id
103              << " ] variable does not exist ! Check the syntax of your context id 'xios' in iodef.xml.");
104     }
105   }
106
107   const StdString& CVariable::getVariableOutputName(void) const
108   {
109     return name.isEmpty() ? getId() : name;
110   }
111
112   const StdString & CVariable::getContent (void) const
113   {
114      return (this->content);
115   }
116
117   void CVariable::setContent(const StdString& contentStr)
118   {
119     this->content = contentStr;
120   }
121
122   StdString CVariable::toString(void) const
123   {
124      StdOStringStream oss;
125
126      oss << "<" << CVariable::GetName() << " ";
127      if (this->hasId())
128         oss << " id=\"" << this->getId() << "\" ";
129      oss << SuperClassAttribute::toString() << ">" << std::endl
130          << this->content /*<< std::endl*/;
131      oss << "</" << CVariable::GetName() << " >";
132      return (oss.str());
133   }
134
135   /*
136   *\brief Sending value of a variable with its id from client to server
137   *
138   */
139
140   void CVariable::sendValue(CContextClient* client)
141   {
142     CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ;
143     if (client->isServerLeader())
144     {
145       CMessage msg ;
146       msg<<this->getId() ;
147       msg<<content ;
148       const std::list<int>& ranks = client->getRanksServerLeader();
149       for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank)
150         event.push(*itRank,1,msg);
151       client->sendEvent(event) ;
152     }
153     else client->sendEvent(event) ;
154   }
155
156   /*
157   *\brief Receive value of a variable with its id from client to server
158   *
159   */
160   void CVariable::recvValue(CEventServer& event)
161   {
162      CBufferIn* buffer=event.subEvents.begin()->buffer;
163      string id;
164      *buffer>>id ;
165      get(id)->recvValue(*buffer);
166   }
167
168   /*
169   *\brief Receive value of a variable with its id from client to server
170   *
171   */
172   void CVariable::recvValue(CBufferIn& buffer)
173   {
174      string str ;
175      buffer>>str;
176      setContent(str);
177   }
178
179   bool CVariable::dispatchEvent(CEventServer& event)
180   {
181    if (SuperClass::dispatchEvent(event)) return true ;
182    else
183    {
184      switch(event.type)
185      {
186        case EVENT_ID_VARIABLE_VALUE :
187          recvValue(event) ;
188          return true ;
189          break ;
190
191        default :
192          ERROR("bool CVariable::dispatchEvent(CEventServer& event)",<<"Unknown Event") ;
193          return false ;
194      }
195    }
196   }
197
198/*
199   void CVariable::toBinary(StdOStream & os) const
200   {
201     const StdString & content = this->content;
202     const StdSize size        =  content.size();
203     SuperClass::toBinary(os);
204
205     os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize));
206     os.write (content.data(), size * sizeof(char));
207   }
208
209   void CVariable::fromBinary(StdIStream & is)
210   {
211      SuperClass::fromBinary(is);
212      StdSize size  = 0;
213      is.read (reinterpret_cast<char*>(&size), sizeof(StdSize));
214      this->content.assign(size, ' ');
215      is.read (const_cast<char *>(this->content.data()), size * sizeof(char));
216   }
217*/
218   void CVariableGroup::parse(xml::CXMLNode & node, bool withAttr)
219   {
220      CVariableGroup* group_ptr = (this->hasId())
221         ? CVariableGroup::get(this->getId()) : CVariableGroup::get(this);
222
223      StdString content;
224      if (this->getId().compare(CVariableGroup::GetDefName()) != 0 && node.getContent(content))
225      {
226        StdSize beginid = 0, endid = 0, begindata = 0, enddata = 0;
227        StdString subdata, subid;
228
229        while ((beginid = content.find_first_not_of ( " \r\n\t;", enddata)) != StdString::npos)
230        {
231           endid   = content.find_first_of ( " \r\n\t=", beginid );
232           subid   = content.substr ( beginid, endid-beginid);
233           subid   = boost::to_lower_copy(boost::trim_copy(subid)) ;
234
235           begindata = content.find_first_of ( "=", endid ) + 1;
236           enddata   = content.find_first_of ( ";", begindata );
237           subdata   = content.substr ( begindata, enddata-begindata);
238           subdata   = boost::trim_copy(subdata) ;
239           group_ptr->createChild(subid)->content = subdata ;
240        }
241      }
242      else
243      {
244         SuperClass::parse(node, withAttr);
245      }
246      //SuperClass::parse(node, withAttr);
247
248   }
249
250} // namespace xios
Note: See TracBrowser for help on using the repository browser.