[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] | 13 | namespace 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 | |
---|
[773] | 49 | const StdString& CVariable::getVariableOutputName(void) const |
---|
| 50 | { |
---|
| 51 | return name.isEmpty() ? getId() : name; |
---|
| 52 | } |
---|
| 53 | |
---|
[274] | 54 | const StdString & CVariable::getContent (void) const |
---|
| 55 | { |
---|
| 56 | return (this->content); |
---|
| 57 | } |
---|
| 58 | |
---|
[489] | 59 | void CVariable::setContent(const StdString& contentStr) |
---|
| 60 | { |
---|
| 61 | this->content = contentStr; |
---|
| 62 | } |
---|
| 63 | |
---|
[274] | 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()); |
---|
[489] | 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() ; |
---|
[987] | 84 | |
---|
| 85 | if (context->hasClient) |
---|
[489] | 86 | { |
---|
[1009] | 87 | // Use correct context client to send message |
---|
[1030] | 88 | // int nbSrvPools = (context->hasServer) ? context->clientPrimServer.size() : 1; |
---|
| 89 | int nbSrvPools = (context->hasServer) ? (context->hasClient ? context->clientPrimServer.size() : 0) : 1; |
---|
[1009] | 90 | for (int i = 0; i < nbSrvPools; ++i) |
---|
[489] | 91 | { |
---|
[1009] | 92 | // CContextClient* contextClientTmp = (0 != context->clientPrimServer) ? context->clientPrimServer |
---|
| 93 | CContextClient* contextClientTmp = (context->hasServer) ? context->clientPrimServer[i] |
---|
| 94 | : context->client; |
---|
[987] | 95 | |
---|
[1009] | 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 | } |
---|
[987] | 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 | // } |
---|
[489] | 128 | } |
---|
| 129 | |
---|
[1071] | 130 | void CVariable::sendValue(CContextClient* client, bool clientPrim /*= false*/) |
---|
[1009] | 131 | { |
---|
| 132 | CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ; |
---|
[1021] | 133 | if (client->isServerLeader()) |
---|
[1009] | 134 | { |
---|
| 135 | CMessage msg ; |
---|
| 136 | msg<<this->getId() ; |
---|
| 137 | msg<<content ; |
---|
[1021] | 138 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
[1009] | 139 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 140 | event.push(*itRank,1,msg); |
---|
[1021] | 141 | client->sendEvent(event) ; |
---|
[1009] | 142 | } |
---|
[1021] | 143 | else client->sendEvent(event) ; |
---|
[1009] | 144 | } |
---|
| 145 | |
---|
[489] | 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 | |
---|
[369] | 188 | /* |
---|
[274] | 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 | } |
---|
[369] | 207 | */ |
---|
[274] | 208 | void CVariableGroup::parse(xml::CXMLNode & node, bool withAttr) |
---|
| 209 | { |
---|
[347] | 210 | CVariableGroup* group_ptr = (this->hasId()) |
---|
[346] | 211 | ? CVariableGroup::get(this->getId()) : CVariableGroup::get(this); |
---|
[274] | 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); |
---|
[387] | 223 | subid = boost::to_lower_copy(boost::trim_copy(subid)) ; |
---|
[274] | 224 | |
---|
| 225 | begindata = content.find_first_of ( "=", endid ) + 1; |
---|
| 226 | enddata = content.find_first_of ( ";", begindata ); |
---|
| 227 | subdata = content.substr ( begindata, enddata-begindata); |
---|
[477] | 228 | subdata = boost::trim_copy(subdata) ; |
---|
[346] | 229 | group_ptr->createChild(subid)->content = subdata ; |
---|
[274] | 230 | } |
---|
| 231 | } |
---|
| 232 | else |
---|
| 233 | { |
---|
| 234 | SuperClass::parse(node, withAttr); |
---|
| 235 | } |
---|
| 236 | //SuperClass::parse(node, withAttr); |
---|
| 237 | |
---|
| 238 | } |
---|
| 239 | |
---|
[335] | 240 | } // namespace xios |
---|