Subversion Repositories HomeAutomation

Rev

Rev 1323 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  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(Header header)
  14. {
  15.     LOG.setName("Message");
  16.  
  17.     this->myHeader = header;
  18.  
  19.     xml::Node messageNode = Protocol::getInstance()->getRootNode().selectFirst("messagetypes").selectFirst("messagetype", xml::Node::attributePair("name", this->myHeader.getMessageType()));
  20.  
  21.     // Load variables
  22.     xml::Node::nodeList variableNodes = messageNode.select("variable");
  23.  
  24.     for (unsigned int n = 0; n < variableNodes.size(); n++)
  25.     {
  26.         Variable variable(variableNodes[n]);
  27.         this->myVariables[variableNodes[n]["name"]] = variable;
  28.     }
  29.  
  30.     // Load responses
  31.     xml::Node::nodeList responseNodes = messageNode.select("response");
  32.  
  33.     for (unsigned int n = 0; n < responseNodes.size(); n++)
  34.     {
  35.         this->myResponses.push_back(responseNodes[n]["code"]); // TODO Validate the code
  36.     }
  37.  
  38.     // Check if valid for this module
  39.     xml::Node moduleNode = Protocol::getInstance()->getRootNode().selectFirst("modules").selectFirst("module", xml::Node::attributePair("name", this->myHeader.getModuleType()));
  40.  
  41.     if (moduleNode.selectFirst("in").select("message", xml::Node::attributePair("name", this->myHeader.getMessageType())).size() == 0 &&
  42.         moduleNode.selectFirst("out").select("message", xml::Node::attributePair("name", this->myHeader.getMessageType())).size() == 0)
  43.     {
  44.         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.");
  45.         // TODO throw exception
  46.     }
  47. }
  48.  
  49. Message::Message()
  50. {
  51.     LOG.setName("Message");
  52. }
  53.  
  54. Message::~Message()
  55. {
  56. }
  57.  
  58. void Message::setOrigin(const void *origin)
  59. {
  60.     //LOG.debug("setOrigin(" + itos((unsigned long) origin) + ")");
  61.     this->myOrigin = origin;
  62. }
  63.  
  64. bool Message::isOrigin(const void *origin)
  65. {
  66.     //LOG.debug("isOrigin(" + itos((unsigned long)origin) + "==" + itos((unsigned long) this->myOrigin) + ")");
  67.     return this->myOrigin == origin;
  68. }
  69.  
  70.  
  71. Header & Message::getHeader()
  72. {
  73.     return this->myHeader;
  74. }
  75.  
  76. Variable & Message::getVariable(string name)
  77. {
  78.     return this->myVariables[name];
  79.  
  80.     // TODO Check if it does not exist, throw exception?
  81. }
  82.  
  83. Message::variableMap & Message::getVariables()
  84. {
  85.     return this->myVariables;
  86. }
  87.  
  88. Message::responseList Message::getResponses()
  89. {
  90.     return this->myResponses;
  91. }
  92.  
  93.  
  94. void Message::readBits(BitBuffer & buffer)
  95. {
  96.     for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
  97.     {
  98.         iter->second.readBits(buffer);
  99.     }
  100. }
  101.  
  102. void Message::writeBits(BitBuffer & buffer)
  103. {
  104.     for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
  105.     {
  106.         iter->second.writeBits(buffer);
  107.     }
  108. }
  109.  
  110. string Message::toString()
  111. {
  112.     string buffer = this->myHeader.toString() + "::Message(";
  113.  
  114.     for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
  115.     {
  116.         buffer += iter->second.toString() + ",";
  117.     }
  118.  
  119.     trim_if(buffer, is_any_of(","));
  120.  
  121.     buffer += ")";
  122.  
  123.     return buffer;
  124. }
  125.  
  126. }
  127. }
  128.