Subversion Repositories HomeAutomation

Rev

Rev 1314 | Rev 1326 | 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
 
1323 runge 13
Message::Message(Header header)
1309 runge 14
{
15
    LOG.setName("Message");
16
 
1323 runge 17
    this->myHeader = header;
1309 runge 18
 
1323 runge 19
    xml::Node messageNode = Protocol::getInstance()->getRootNode().selectFirst("messagetypes").selectFirst("messagetype", xml::Node::attributePair("name", this->myHeader.getMessageType()));
1309 runge 20
 
1323 runge 21
    // Load variables
1310 runge 22
    xml::Node::nodeList variableNodes = messageNode.select("variable");
1309 runge 23
 
1310 runge 24
    for (unsigned int n = 0; n < variableNodes.size(); n++)
25
    {
1323 runge 26
        Variable variable(variableNodes[n]);
1310 runge 27
        this->myVariables[variableNodes[n]["name"]] = variable;
28
    }
29
 
1323 runge 30
    // Load responses
1310 runge 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
 
1323 runge 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.getModuleType())).size() == 0 &&
42
        moduleNode.selectFirst("out").select("message", xml::Node::attributePair("name", this->myHeader.getModuleType())).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
    }
1309 runge 47
}
48
 
49
Message::Message()
50
{
1323 runge 51
    LOG.setName("Message");
1309 runge 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
 
1323 runge 71
Header & Message::getHeader()
1309 runge 72
{
1323 runge 73
    return this->myHeader;
1309 runge 74
}
75
 
1323 runge 76
Variable & Message::getVariable(string name)
1309 runge 77
{
1323 runge 78
    return this->myVariables[name];
1309 runge 79
 
1323 runge 80
    // TODO Check if it does not exist, throw exception?
1309 runge 81
}
82
 
1323 runge 83
Message::responseList Message::getResponses()
1309 runge 84
{
1323 runge 85
    return this->myResponses;
1309 runge 86
}
87
 
88
 
89
void Message::readBits(BitBuffer & buffer)
90
{
91
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
92
    {
93
        iter->second.readBits(buffer);
94
    }
95
}
96
 
1323 runge 97
void Message::writeBits(BitBuffer & buffer)
1309 runge 98
{
1323 runge 99
    for (variableMap::iterator iter = this->myVariables.begin(); iter != this->myVariables.end(); iter++)
100
    {
101
        iter->second.writeBits(buffer);
102
    }
1309 runge 103
}
104
 
1323 runge 105
 
1309 runge 106
}
107
}