[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 | { |
---|
[1158] | 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 | |
---|
[274] | 57 | ERROR("CVariable::parse(xml::CXMLNode & node)", |
---|
| 58 | << "[ variable id = " << id |
---|
[1158] | 59 | << " ] variable is not defined !. It does not have any content. See error log for more details."); |
---|
[274] | 60 | } |
---|
[477] | 61 | content = boost::trim_copy(content) ; |
---|
[274] | 62 | } |
---|
| 63 | |
---|
[773] | 64 | const StdString& CVariable::getVariableOutputName(void) const |
---|
| 65 | { |
---|
| 66 | return name.isEmpty() ? getId() : name; |
---|
| 67 | } |
---|
| 68 | |
---|
[274] | 69 | const StdString & CVariable::getContent (void) const |
---|
| 70 | { |
---|
| 71 | return (this->content); |
---|
| 72 | } |
---|
| 73 | |
---|
[489] | 74 | void CVariable::setContent(const StdString& contentStr) |
---|
| 75 | { |
---|
| 76 | this->content = contentStr; |
---|
| 77 | } |
---|
| 78 | |
---|
[274] | 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()); |
---|
[489] | 90 | } |
---|
| 91 | |
---|
| 92 | /* |
---|
| 93 | *\brief Sending value of a variable with its id from client to server |
---|
| 94 | * |
---|
| 95 | */ |
---|
[987] | 96 | |
---|
[2019] | 97 | void CVariable::sendValue(CContextClient* client) |
---|
[1009] | 98 | { |
---|
| 99 | CEventClient event(this->getType(),EVENT_ID_VARIABLE_VALUE) ; |
---|
[1021] | 100 | if (client->isServerLeader()) |
---|
[1009] | 101 | { |
---|
| 102 | CMessage msg ; |
---|
| 103 | msg<<this->getId() ; |
---|
| 104 | msg<<content ; |
---|
[1021] | 105 | const std::list<int>& ranks = client->getRanksServerLeader(); |
---|
[1009] | 106 | for (std::list<int>::const_iterator itRank = ranks.begin(), itRankEnd = ranks.end(); itRank != itRankEnd; ++itRank) |
---|
| 107 | event.push(*itRank,1,msg); |
---|
[1021] | 108 | client->sendEvent(event) ; |
---|
[1009] | 109 | } |
---|
[1021] | 110 | else client->sendEvent(event) ; |
---|
[1009] | 111 | } |
---|
| 112 | |
---|
[489] | 113 | /* |
---|
| 114 | *\brief Receive value of a variable with its id from client to server |
---|
| 115 | * |
---|
| 116 | */ |
---|
| 117 | void CVariable::recvValue(CEventServer& event) |
---|
| 118 | { |
---|
| 119 | CBufferIn* buffer=event.subEvents.begin()->buffer; |
---|
| 120 | string id; |
---|
| 121 | *buffer>>id ; |
---|
| 122 | get(id)->recvValue(*buffer); |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | /* |
---|
| 126 | *\brief Receive value of a variable with its id from client to server |
---|
| 127 | * |
---|
| 128 | */ |
---|
| 129 | void CVariable::recvValue(CBufferIn& buffer) |
---|
| 130 | { |
---|
| 131 | string str ; |
---|
| 132 | buffer>>str; |
---|
| 133 | setContent(str); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | bool CVariable::dispatchEvent(CEventServer& event) |
---|
| 137 | { |
---|
| 138 | if (SuperClass::dispatchEvent(event)) return true ; |
---|
| 139 | else |
---|
| 140 | { |
---|
| 141 | switch(event.type) |
---|
| 142 | { |
---|
| 143 | case EVENT_ID_VARIABLE_VALUE : |
---|
| 144 | recvValue(event) ; |
---|
| 145 | return true ; |
---|
| 146 | break ; |
---|
| 147 | |
---|
| 148 | default : |
---|
| 149 | ERROR("bool CVariable::dispatchEvent(CEventServer& event)",<<"Unknown Event") ; |
---|
| 150 | return false ; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
[369] | 155 | /* |
---|
[274] | 156 | void CVariable::toBinary(StdOStream & os) const |
---|
| 157 | { |
---|
| 158 | const StdString & content = this->content; |
---|
| 159 | const StdSize size = content.size(); |
---|
| 160 | SuperClass::toBinary(os); |
---|
| 161 | |
---|
| 162 | os.write (reinterpret_cast<const char*>(&size), sizeof(StdSize)); |
---|
| 163 | os.write (content.data(), size * sizeof(char)); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | void CVariable::fromBinary(StdIStream & is) |
---|
| 167 | { |
---|
| 168 | SuperClass::fromBinary(is); |
---|
| 169 | StdSize size = 0; |
---|
| 170 | is.read (reinterpret_cast<char*>(&size), sizeof(StdSize)); |
---|
| 171 | this->content.assign(size, ' '); |
---|
| 172 | is.read (const_cast<char *>(this->content.data()), size * sizeof(char)); |
---|
| 173 | } |
---|
[369] | 174 | */ |
---|
[274] | 175 | void CVariableGroup::parse(xml::CXMLNode & node, bool withAttr) |
---|
| 176 | { |
---|
[347] | 177 | CVariableGroup* group_ptr = (this->hasId()) |
---|
[346] | 178 | ? CVariableGroup::get(this->getId()) : CVariableGroup::get(this); |
---|
[274] | 179 | |
---|
| 180 | StdString content; |
---|
| 181 | if (this->getId().compare(CVariableGroup::GetDefName()) != 0 && node.getContent(content)) |
---|
| 182 | { |
---|
| 183 | StdSize beginid = 0, endid = 0, begindata = 0, enddata = 0; |
---|
| 184 | StdString subdata, subid; |
---|
| 185 | |
---|
| 186 | while ((beginid = content.find_first_not_of ( " \r\n\t;", enddata)) != StdString::npos) |
---|
| 187 | { |
---|
| 188 | endid = content.find_first_of ( " \r\n\t=", beginid ); |
---|
| 189 | subid = content.substr ( beginid, endid-beginid); |
---|
[387] | 190 | subid = boost::to_lower_copy(boost::trim_copy(subid)) ; |
---|
[274] | 191 | |
---|
| 192 | begindata = content.find_first_of ( "=", endid ) + 1; |
---|
| 193 | enddata = content.find_first_of ( ";", begindata ); |
---|
| 194 | subdata = content.substr ( begindata, enddata-begindata); |
---|
[477] | 195 | subdata = boost::trim_copy(subdata) ; |
---|
[346] | 196 | group_ptr->createChild(subid)->content = subdata ; |
---|
[274] | 197 | } |
---|
| 198 | } |
---|
| 199 | else |
---|
| 200 | { |
---|
| 201 | SuperClass::parse(node, withAttr); |
---|
| 202 | } |
---|
| 203 | //SuperClass::parse(node, withAttr); |
---|
| 204 | |
---|
| 205 | } |
---|
| 206 | |
---|
[335] | 207 | } // namespace xios |
---|