Subversion Repositories HomeAutomation

Rev

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