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

Last change on this file since 2496 was 2496, checked in by jderouillat, 14 months ago

Check that the parameters of the XIOS context are valid

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