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