Rev 1309 | Rev 1311 | 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 | * Message.cpp |
||
| 3 | * |
||
| 4 | * Created on: Jul 21, 2009 |
||
| 5 | * Author: Mattias Runge |
||
| 6 | */ |
||
| 7 | |||
| 8 | #include "Message.h" |
||
| 9 | |||
| 10 | namespace atom { |
||
| 11 | namespace message { |
||
| 12 | |||
| 13 | Message::Message(string moduletype, unsigned int moduleId, string type) |
||
| 14 | { |
||
| 15 | LOG.setName("Message"); |
||
| 16 | |||
| 17 | this->myModuletype = moduletype; |
||
| 18 | this->myModuleId = moduleId; |
||
| 19 | this->myType = type; |
||
| 20 | |||
| 1310 | runge | 21 | xml::Node moduleNode = db::Database::getInstance()->getRootNode().selectFirst("modules").selectFirst("module", xml::Node::attributePair("name", moduletype)); |
| 22 | xml::Node messageNode = db::Database::getInstance()->getRootNode().selectFirst("messagetypes").selectFirst("messagetype", xml::Node::attributePair("name", type)); |
||
| 1309 | runge | 23 | |
| 1310 | runge | 24 | xml::Node::nodeList variableNodes = messageNode.select("variable"); |
| 1309 | runge | 25 | |
| 1310 | runge | 26 | for (unsigned int n = 0; n < variableNodes.size(); n++) |
| 27 | { |
||
| 28 | Variable variable(variableNodes[n]["name"], variableNodes[n]["datatype"], stob(variableNodes[n]["required"]), variableNodes[n]["unit"]); |
||
| 29 | this->myVariables[variableNodes[n]["name"]] = variable; |
||
| 30 | } |
||
| 31 | |||
| 32 | if (moduleNode.selectFirst("in").select("message", xml::Node::attributePair("name", type)).size() > 0) |
||
| 33 | { |
||
| 34 | this->myFromModule = false; |
||
| 35 | } |
||
| 36 | else if (moduleNode.selectFirst("out").select("message", xml::Node::attributePair("name", type)).size() > 0) |
||
| 37 | { |
||
| 38 | this->myFromModule = true; |
||
| 39 | } |
||
| 40 | else |
||
| 41 | { |
||
| 42 | LOG.error("This message is not supported by this module type, something is wrong; protocol.xml is old, module code is old or a Atom is trying to send and illegal message."); |
||
| 43 | // TODO throw exception |
||
| 44 | } |
||
| 45 | |||
| 46 | xml::Node::nodeList responseNodes = messageNode.select("response"); |
||
| 47 | |||
| 48 | for (unsigned int n = 0; n < responseNodes.size(); n++) |
||
| 49 | { |
||
| 50 | this->myResponses.push_back(responseNodes[n]["code"]); // TODO Validate the code |
||
| 51 | } |
||
| 52 | |||
| 1309 | runge | 53 | } |
| 54 | |||
| 55 | Message::Message() |
||
| 56 | { |
||
| 57 | } |
||
| 58 | |||
| 59 | Message::~Message() |
||
| 60 | { |
||
| 61 | } |
||
| 62 | |||
| 63 | void Message::setOrigin(const void *origin) |
||
| 64 | { |
||
| 65 | //LOG.debug("setOrigin(" + itos((unsigned long) origin) + ")"); |
||
| 66 | this->myOrigin = origin; |
||
| 67 | } |
||
| 68 | |||
| 69 | bool Message::isOrigin(const void *origin) |
||
| 70 | { |
||
| 71 | //LOG.debug("isOrigin(" + itos((unsigned long)origin) + "==" + itos((unsigned long) this->myOrigin) + ")"); |
||
| 72 | return this->myOrigin == origin; |
||
| 73 | } |
||
| 74 | |||
| 75 | |||
| 76 | bool Message::isFromModule() |
||
| 77 | { |
||
| 78 | return this->myFromModule; |
||
| 79 | } |
||
| 80 | |||
| 81 | string Message::getModuletype() |
||
| 82 | { |
||
| 83 | return this->myModuletype; |
||
| 84 | } |
||
| 85 | |||
| 86 | unsigned int Message::getModuleId() |
||
| 87 | { |
||
| 88 | return this->myModuleId; |
||
| 89 | } |
||
| 90 | |||
| 91 | string Message::getType() |
||
| 92 | { |
||
| 93 | return this->myType; |
||
| 94 | } |
||
| 95 | |||
| 96 | Variable & Message::getVariable(string name) |
||
| 97 | { |
||
| 98 | return this->myVariables[name]; |
||
| 99 | |||
| 100 | // TODO Check if it does not exist, throw exception? |
||
| 101 | } |
||
| 102 | |||
| 103 | void Message::readBits(BitBuffer & buffer) |
||
| 104 | { |
||
| 105 | for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++) |
||
| 106 | { |
||
| 107 | iter->second.readBits(buffer); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | |||
| 112 | Message::responseList Message::getResponses() |
||
| 113 | { |
||
| 114 | return this->myResponses; |
||
| 115 | } |
||
| 116 | |||
| 117 | } |
||
| 118 | } |