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