Rev 1323 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1309 | runge | 1 | /* |
| 2 | * Header.cpp |
||
| 3 | * |
||
| 4 | * Created on: Jul 23, 2009 |
||
| 5 | * Author: Mattias Runge |
||
| 6 | */ |
||
| 7 | |||
| 8 | #include "Header.h" |
||
| 9 | |||
| 1323 | runge | 10 | #include "message/types/UnsignedInteger.hpp" |
| 11 | |||
| 1309 | runge | 12 | namespace atom { |
| 13 | namespace message { |
||
| 14 | |||
| 15 | Header::Header() |
||
| 16 | { |
||
| 1326 | runge | 17 | LOG.setName("Header"); |
| 18 | this->loadVariables(); |
||
| 1323 | runge | 19 | } |
| 20 | |||
| 21 | Header::Header(BitBuffer & buffer) |
||
| 22 | { |
||
| 1326 | runge | 23 | LOG.setName("Header"); |
| 24 | this->loadVariables(); |
||
| 1323 | runge | 25 | this->readBits(buffer); |
| 26 | } |
||
| 27 | |||
| 28 | Header::Header(unsigned long moduleId, string moduleType, string messageType) |
||
| 29 | { |
||
| 1326 | runge | 30 | LOG.setName("Header"); |
| 31 | |||
| 32 | LOG.debug("Start of constructor..."); |
||
| 33 | |||
| 34 | this->loadVariables(); |
||
| 35 | |||
| 36 | LOG.debug("Done loading variables..."); |
||
| 37 | |||
| 1323 | runge | 38 | if (types::UnsignedInteger *moduleIdDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleId"].getDatatype().get())) |
| 39 | { |
||
| 40 | moduleIdDatatype->setValue(moduleId); |
||
| 41 | } |
||
| 42 | else |
||
| 43 | { |
||
| 1326 | runge | 44 | throw new Exception("moduleId is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification"); |
| 1323 | runge | 45 | } |
| 46 | |||
| 1326 | runge | 47 | LOG.debug("checkpoint 1..."); |
| 48 | |||
| 1323 | runge | 49 | if (types::UnsignedInteger *moduleTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleType"].getDatatype().get())) |
| 50 | { |
||
| 1326 | runge | 51 | unsigned long moduleTypeId = convert::string2uint(Protocol::getInstance()->getRootCacheNode().selectFirst("module", xml::Node::attributePair("name", moduleType))["id"]); |
| 1323 | runge | 52 | |
| 53 | moduleTypeDatatype->setValue(moduleTypeId); |
||
| 54 | } |
||
| 55 | else |
||
| 56 | { |
||
| 1326 | runge | 57 | throw new Exception("moduletype is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification"); |
| 1323 | runge | 58 | } |
| 59 | |||
| 1326 | runge | 60 | LOG.debug("checkpoint 2..."); |
| 61 | |||
| 1323 | runge | 62 | if (types::UnsignedInteger *messageTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["messageType"].getDatatype().get())) |
| 63 | { |
||
| 1326 | runge | 64 | unsigned long messageTypeId = convert::string2uint(Protocol::getInstance()->getRootCacheNode().selectFirst("messagetype", xml::Node::attributePair("name", messageType))["id"]); |
| 1323 | runge | 65 | |
| 66 | messageTypeDatatype->setValue(messageTypeId); |
||
| 67 | } |
||
| 68 | else |
||
| 69 | { |
||
| 1326 | runge | 70 | throw new Exception("messageType is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification"); |
| 1323 | runge | 71 | } |
| 1326 | runge | 72 | |
| 73 | LOG.debug("End of contructor..."); |
||
| 1323 | runge | 74 | } |
| 75 | |||
| 76 | Header::~Header() |
||
| 77 | { |
||
| 78 | } |
||
| 79 | |||
| 80 | void Header::loadVariables() |
||
| 81 | { |
||
| 1311 | runge | 82 | xml::Node::nodeList nodes = Protocol::getInstance()->getRootNode().selectFirst("header").select("variable"); |
| 1310 | runge | 83 | |
| 84 | for (unsigned int n = 0; n < nodes.size(); n++) |
||
| 85 | { |
||
| 1323 | runge | 86 | Variable variable(nodes[n]); |
| 1310 | runge | 87 | this->myVariables[nodes[n]["name"]] = variable; |
| 88 | } |
||
| 1323 | runge | 89 | |
| 90 | // TODO check that moduleType, moduleId and messageId are present |
||
| 1309 | runge | 91 | } |
| 92 | |||
| 1323 | runge | 93 | unsigned long Header::getModuleId() |
| 1309 | runge | 94 | { |
| 1323 | runge | 95 | if (types::UnsignedInteger *moduleIdDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleId"].getDatatype().get())) |
| 96 | { |
||
| 97 | return moduleIdDatatype->getValue(); |
||
| 98 | } |
||
| 1326 | runge | 99 | |
| 100 | throw new Exception("moduleIdDatatype is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification"); |
||
| 1309 | runge | 101 | } |
| 102 | |||
| 1323 | runge | 103 | string Header::getModuleType() |
| 1309 | runge | 104 | { |
| 1323 | runge | 105 | if (types::UnsignedInteger *moduleTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["moduleType"].getDatatype().get())) |
| 106 | { |
||
| 107 | unsigned long moduleTypeId = moduleTypeDatatype->getValue(); |
||
| 1309 | runge | 108 | |
| 1326 | runge | 109 | string moduleType = Protocol::getInstance()->getRootCacheNode().selectFirst("module", xml::Node::attributePair("id", convert::uint2string(moduleTypeId)))["name"]; |
| 1323 | runge | 110 | |
| 111 | return moduleType; |
||
| 112 | } |
||
| 1326 | runge | 113 | |
| 114 | throw new Exception("moduleType is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification"); |
||
| 1309 | runge | 115 | } |
| 116 | |||
| 1323 | runge | 117 | string Header::getMessageType() |
| 118 | { |
||
| 119 | if (types::UnsignedInteger *messageTypeDatatype = dynamic_cast<types::UnsignedInteger *>(this->myVariables["messageType"].getDatatype().get())) |
||
| 120 | { |
||
| 121 | unsigned long messageTypeId = messageTypeDatatype->getValue(); |
||
| 122 | |||
| 1326 | runge | 123 | string messageType = Protocol::getInstance()->getRootCacheNode().selectFirst("messagetype", xml::Node::attributePair("id", convert::uint2string(messageTypeId)))["name"]; |
| 1323 | runge | 124 | |
| 125 | return messageType; |
||
| 126 | } |
||
| 1326 | runge | 127 | |
| 128 | throw new Exception("messageType is not of the type \"unsigned integer\", this is a fatal error, correct the protocol specification"); |
||
| 1323 | runge | 129 | } |
| 130 | |||
| 1309 | runge | 131 | void Header::readBits(BitBuffer & buffer) |
| 132 | { |
||
| 1323 | runge | 133 | // Header is only 29 bits but our buffer is whole bytes, padded in front |
| 134 | buffer.incrementPointer(3); // Increment 3 bits |
||
| 135 | |||
| 1309 | runge | 136 | for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++) |
| 137 | { |
||
| 138 | iter->second.readBits(buffer); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 1323 | runge | 142 | void Header::writeBits(BitBuffer & buffer) |
| 143 | { |
||
| 144 | // Header is only 29 bits but our buffer is whole bytes, padded in front |
||
| 1326 | runge | 145 | buffer.write(3, (long) 0); // Write 3 bits |
| 1323 | runge | 146 | |
| 147 | for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++) |
||
| 148 | { |
||
| 149 | iter->second.writeBits(buffer); |
||
| 150 | } |
||
| 1309 | runge | 151 | } |
| 1323 | runge | 152 | |
| 1326 | runge | 153 | string Header::toString() |
| 154 | { |
||
| 155 | string buffer = "Header("; |
||
| 156 | |||
| 157 | for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++) |
||
| 158 | { |
||
| 159 | buffer += iter->second.toString() + ","; |
||
| 160 | } |
||
| 161 | |||
| 162 | trim_if(buffer, is_any_of(",")); |
||
| 163 | |||
| 164 | buffer += ")"; |
||
| 165 | |||
| 166 | return buffer; |
||
| 1309 | runge | 167 | } |
| 1326 | runge | 168 | |
| 1323 | runge | 169 | } |
| 1326 | runge | 170 | } |