introduced MessageMap, nicer method names
This commit is contained in:
+132
-16
@@ -27,28 +27,50 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
Message::Message(const string clazz, const string name, const bool isSet,
|
||||
const bool isActive, const string comment,
|
||||
const unsigned char srcAddress, const unsigned char dstAddress,
|
||||
const vector<unsigned char> id, DataField* data,
|
||||
const unsigned int pollPriority)
|
||||
: m_class(clazz), m_name(name), m_isSet(isSet),
|
||||
m_isActive(isActive), m_comment(comment),
|
||||
m_srcAddress(srcAddress), m_dstAddress(dstAddress),
|
||||
m_id(id), m_data(data), m_pollPriority(pollPriority)
|
||||
{
|
||||
int exp = 7;
|
||||
unsigned long long key = (unsigned long long)(id.size()-2) << (8 * exp + 5);
|
||||
if (isActive == true)
|
||||
key |= 0x1fLL << (8 * exp--);
|
||||
else
|
||||
key |= (unsigned long long)getMasterNumber(srcAddress) << (8 * exp--);
|
||||
key |= (unsigned long long)dstAddress << (8 * exp--);
|
||||
for (vector<unsigned char>::const_iterator it=id.begin(); it<id.end(); it++)
|
||||
key |= (unsigned long long)(*it) << (8 * exp--);
|
||||
m_key = key;
|
||||
}
|
||||
|
||||
result_t Message::create(vector<string>::iterator& it, const vector<string>::iterator end,
|
||||
const map<string, DataField*> templates, Message*& returnValue)
|
||||
{
|
||||
// [type];[class];name;[comment];[QQ];ZZ;id;fields...
|
||||
result_t result;
|
||||
// [type];class;name;[comment];[QQ];ZZ;id;fields...
|
||||
bool isSet, isActive;
|
||||
unsigned int pollPriority = 0;
|
||||
if (it == end)
|
||||
return RESULT_ERR_EOF;
|
||||
|
||||
const char* str = (*it++).c_str();
|
||||
if (it == end)
|
||||
return RESULT_ERR_EOF;
|
||||
bool isSetMessage, isActiveMessage;
|
||||
unsigned int pollPriority = 0;
|
||||
if (strcasecmp(str, "W") == 0) {
|
||||
isActiveMessage = true;
|
||||
isSetMessage = true;
|
||||
isActive = true;
|
||||
isSet = true;
|
||||
} else if (str[0] == 'C' || str[0] == 'c') {
|
||||
isActiveMessage = false;
|
||||
isSetMessage = str[1] == 'W' || str[1] == 'w';
|
||||
isActive = false;
|
||||
isSet = str[1] == 'W' || str[1] == 'w';
|
||||
} else if (str[0] == 'P' || str[0] == 'p') {
|
||||
isActiveMessage = true;
|
||||
isSetMessage = false;
|
||||
isActive = true;
|
||||
isSet = false;
|
||||
if (str[1] == 0)
|
||||
pollPriority = 1;
|
||||
else {
|
||||
@@ -58,8 +80,8 @@ result_t Message::create(vector<string>::iterator& it, const vector<string>::ite
|
||||
return result;
|
||||
}
|
||||
} else {
|
||||
isActiveMessage = true;
|
||||
isSetMessage = false;
|
||||
isActive = true;
|
||||
isSet = false;
|
||||
}
|
||||
|
||||
string clazz = *it++;
|
||||
@@ -80,7 +102,7 @@ result_t Message::create(vector<string>::iterator& it, const vector<string>::ite
|
||||
if (it == end)
|
||||
return RESULT_ERR_EOF;
|
||||
unsigned char srcAddress;
|
||||
if (*str == 0 || isActiveMessage == true)
|
||||
if (*str == 0 || isActive == true)
|
||||
srcAddress = SYN; // no specific source defined, or ignore for active message
|
||||
else {
|
||||
srcAddress = parseInt(str, 16, 0, 0xff, result);
|
||||
@@ -125,17 +147,17 @@ result_t Message::create(vector<string>::iterator& it, const vector<string>::ite
|
||||
return RESULT_ERR_INVALID_ARG; // missing/too short/too long ID
|
||||
|
||||
DataField* data = NULL;
|
||||
result = DataField::create(it, end, templates, data, isSetMessage, dstAddress);
|
||||
result = DataField::create(it, end, templates, data, isSet, dstAddress);
|
||||
if (result != RESULT_OK)
|
||||
return result;
|
||||
|
||||
returnValue = new Message(clazz, name, isSetMessage, isActiveMessage, comment, srcAddress, dstAddress, id, data, pollPriority);
|
||||
returnValue = new Message(clazz, name, isSet, isActive, comment, srcAddress, dstAddress, id, data, pollPriority);
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
result_t Message::prepare(const unsigned char srcAddress, SymbolString& masterData, istringstream& input, char separator)
|
||||
{
|
||||
if (m_isActiveMessage == true) {
|
||||
if (m_isActive == true) {
|
||||
masterData.clear();
|
||||
masterData.push_back(srcAddress, false);
|
||||
masterData.push_back(m_dstAddress, false);
|
||||
@@ -157,7 +179,7 @@ result_t Message::prepare(const unsigned char srcAddress, SymbolString& masterDa
|
||||
result_t Message::handle(SymbolString& masterData, SymbolString& slaveData,
|
||||
ostringstream& output, char separator, bool answer)
|
||||
{
|
||||
if (m_isActiveMessage == true) {
|
||||
if (m_isActive == true) {
|
||||
result_t result = m_data->read(masterData, m_id.size() - 2, slaveData, 0, output, false, separator);
|
||||
if (result != RESULT_OK)
|
||||
return result;
|
||||
@@ -170,3 +192,97 @@ result_t Message::handle(SymbolString& masterData, SymbolString& slaveData,
|
||||
}
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
|
||||
result_t MessageMap::add(Message* message)
|
||||
{
|
||||
string key = message->getClass().append(";").append(message->getName());
|
||||
if (message->isActive() == true)
|
||||
key.append(message->isSet() ? ";W" : ";R");
|
||||
else
|
||||
key.append(";C");
|
||||
map<string, Message*>::iterator nameIt = m_messagesByName.find(key);
|
||||
if (nameIt != m_messagesByName.end())
|
||||
return RESULT_ERR_INVALID_ARG; // duplicate key
|
||||
|
||||
if (message->isActive() == false) {
|
||||
unsigned long long pkey = message->getKey();
|
||||
map<unsigned long long, Message*>::iterator keyIt = m_passiveMessagesByKey.find(pkey);
|
||||
if (keyIt != m_passiveMessagesByKey.end())
|
||||
return RESULT_ERR_INVALID_ARG; // duplicate key
|
||||
|
||||
unsigned char idLength = message->getId().size() - 2;
|
||||
if (idLength > m_maxIdLength)
|
||||
m_maxIdLength = idLength;
|
||||
m_passiveMessagesByKey[pkey] = message;
|
||||
}
|
||||
|
||||
m_messagesByName[key] = message;
|
||||
|
||||
return RESULT_OK;
|
||||
}
|
||||
|
||||
Message* MessageMap::find(const string clazz, const string name, const bool isActive, const bool isSet)
|
||||
{
|
||||
string key = clazz;
|
||||
for (int i=0; i<2; i++) {
|
||||
key.append(";").append(name);
|
||||
if (isActive == true)
|
||||
key.append(isSet ? ";W" : ";R");
|
||||
else
|
||||
key.append(";C");
|
||||
map<string, Message*>::iterator it = m_messagesByName.find(key);
|
||||
if (it != m_messagesByName.end())
|
||||
return it->second;
|
||||
key.clear(); // try again without class name
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Message* MessageMap::find(SymbolString master) {
|
||||
if (master.size() < 5)
|
||||
return NULL;
|
||||
unsigned char maxIdLength = master[4];
|
||||
if (maxIdLength > m_maxIdLength)
|
||||
maxIdLength = m_maxIdLength;
|
||||
if (master.size() < 5+maxIdLength)
|
||||
return NULL;
|
||||
|
||||
unsigned long long sourceMask = 0x1fLL << (8 * 7);
|
||||
for (int idLength=maxIdLength; idLength>=0; idLength--) {
|
||||
int exp = 7;
|
||||
unsigned long long key = (unsigned long long)idLength << (8 * exp + 5);
|
||||
key |= (unsigned long long)getMasterNumber(master[0]) << (8 * exp--);
|
||||
key |= (unsigned long long)master[1] << (8 * exp--);
|
||||
key |= (unsigned long long)master[2] << (8 * exp--);
|
||||
key |= (unsigned long long)master[3] << (8 * exp--);
|
||||
for (unsigned char i=0; i<idLength; i++)
|
||||
key |= (unsigned long long)master[5 + i] << (8 * exp--);
|
||||
|
||||
map<unsigned long long , Message*>::iterator it = m_passiveMessagesByKey.find(key);
|
||||
if (it != m_passiveMessagesByKey.end())
|
||||
return it->second;
|
||||
|
||||
if ((key & sourceMask) != 0) {
|
||||
key &= ~sourceMask; // try again without specific source master
|
||||
it = m_passiveMessagesByKey.find(key);
|
||||
if (it != m_passiveMessagesByKey.end())
|
||||
return it->second;
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void MessageMap::clear()
|
||||
{
|
||||
for (map<string, Message*>::iterator it=m_messagesByName.begin(); it!=m_messagesByName.end(); it++) {
|
||||
delete it->second;
|
||||
it->second = NULL;
|
||||
}
|
||||
m_messagesByName.clear();
|
||||
m_passiveMessagesByKey.clear();
|
||||
m_maxIdLength = 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user