use default destination and circuit in condition, check destination address for scan conditions and improved error message

This commit is contained in:
john30
2015-11-08 14:00:03 +01:00
parent 181ba4d3c1
commit dc3796959e
2 changed files with 30 additions and 25 deletions
+24 -21
View File
@@ -651,20 +651,19 @@ Message* getFirstAvailable(vector<Message*> &messages) {
} }
result_t Condition::create(vector<string>::iterator& it, const vector<string>::iterator end, SimpleCondition*& returnValue) result_t Condition::create(vector<string>::iterator& it, const vector<string>::iterator end, string defaultDest, string defaultCircuit, SimpleCondition*& returnValue)
{ {
// name,circuit,messagename,[comment],[fieldname],[ZZ],values (name already skipped by caller) // name,circuit,messagename,[comment],[fieldname],[ZZ],values (name already skipped by caller)
if (it+2>end) { // at least everything including messagename string circuit = it==end ? "" : *(it++); // circuit
it = end; // for error reporting
return RESULT_ERR_EOF;
}
string circuit = *(it++); // circuit
string name = it==end ? "" : *(it++); // messagename string name = it==end ? "" : *(it++); // messagename
if (it<end)
it++; // comment it++; // comment
string field = it==end ? "" : *(it++); // fieldname string field = it==end ? "" : *(it++); // fieldname
string zz = it==end ? "" : *(it++); // ZZ string zz = it==end ? "" : *(it++); // ZZ
unsigned char dstAddress = SYN; unsigned char dstAddress = SYN;
result_t result = RESULT_OK; result_t result = RESULT_OK;
if (zz.length()==0)
zz = defaultDest;
if (zz.length()>0) { if (zz.length()>0) {
dstAddress = (unsigned char)parseInt(zz.c_str(), 16, 0, 0xff, result); dstAddress = (unsigned char)parseInt(zz.c_str(), 16, 0, 0xff, result);
if (result != RESULT_OK) if (result != RESULT_OK)
@@ -672,6 +671,12 @@ result_t Condition::create(vector<string>::iterator& it, const vector<string>::i
if (dstAddress!=SYN && !isValidAddress(dstAddress, false)) if (dstAddress!=SYN && !isValidAddress(dstAddress, false))
return RESULT_ERR_INVALID_ADDR; return RESULT_ERR_INVALID_ADDR;
} }
if (name.length()==0) {
if (!isValidAddress(dstAddress, false) || isMaster(dstAddress))
return RESULT_ERR_INVALID_ADDR;
} else if (circuit.length()==0) {
circuit = defaultCircuit;
}
istringstream stream(it==end ? "" : *(it++)); istringstream stream(it==end ? "" : *(it++));
string str; string str;
vector<unsigned int> valueRanges; vector<unsigned int> valueRanges;
@@ -730,22 +735,24 @@ result_t SimpleCondition::resolve(MessageMap* messages, ostringstream& errorMess
Message* message; Message* message;
if (m_name.length()==0) { if (m_name.length()==0) {
message = messages->getScanMessage(m_dstAddress); message = messages->getScanMessage(m_dstAddress);
errorMessage << "scan condition " << nouppercase << setw(2) << hex << setfill('0') << static_cast<unsigned>(m_dstAddress);
} else { } else {
message = messages->find(m_circuit, m_name, false); message = messages->find(m_circuit, m_name, false);
if (!message) if (!message)
message = messages->find(m_circuit, m_name, false, true); message = messages->find(m_circuit, m_name, false, true);
errorMessage << "condition " << m_circuit << " " << m_name;
} }
if (!message) { if (!message) {
errorMessage << "condition " << m_circuit << " " << m_name << ": message not found"; errorMessage << ": message not found";
return RESULT_ERR_NOTFOUND; return RESULT_ERR_NOTFOUND;
} }
if (message->getDstAddress()==SYN) { if (message->getDstAddress()==SYN) {
if (message->isPassive()) { if (message->isPassive()) {
errorMessage << "condition " << m_circuit << " " << m_name << ": invalid passive message"; errorMessage << ": invalid passive message";
return RESULT_ERR_INVALID_ARG; return RESULT_ERR_INVALID_ARG;
} }
if (m_dstAddress==SYN) { if (m_dstAddress==SYN) {
errorMessage << "condition " << m_circuit << " " << m_name << ": destination address missing"; errorMessage << ": destination address missing";
return RESULT_ERR_INVALID_ADDR; return RESULT_ERR_INVALID_ADDR;
} }
// clone the message with dedicated dstAddress if necessary // clone the message with dedicated dstAddress if necessary
@@ -757,7 +764,7 @@ result_t SimpleCondition::resolve(MessageMap* messages, ostringstream& errorMess
} else { } else {
message = getFirstAvailable(*derived); message = getFirstAvailable(*derived);
if (message==NULL) { if (message==NULL) {
errorMessage << "condition " << m_circuit << " " << m_name << ": conditional derived message"; errorMessage << ": conditional derived message";
return RESULT_ERR_INVALID_ARG; return RESULT_ERR_INVALID_ARG;
} }
} }
@@ -765,7 +772,7 @@ result_t SimpleCondition::resolve(MessageMap* messages, ostringstream& errorMess
if (!m_valueRanges.empty()) { if (!m_valueRanges.empty()) {
if (!message->hasField(m_field.length()>0 ? m_field.c_str() : NULL, true)) { if (!message->hasField(m_field.length()>0 ? m_field.c_str() : NULL, true)) {
errorMessage << "condition " << m_circuit << " " << m_name << ": numeric field " << m_field << " not found"; errorMessage << ": numeric field " << m_field << " not found";
return RESULT_ERR_NOTFOUND; return RESULT_ERR_NOTFOUND;
} }
} }
@@ -803,13 +810,15 @@ bool SimpleCondition::isTrue()
result_t CombinedCondition::resolve(MessageMap* messages, ostringstream& errorMessage) result_t CombinedCondition::resolve(MessageMap* messages, ostringstream& errorMessage)
{ {
ostringstream dummy;
for (vector<Condition*>::iterator it = m_conditions.begin(); it!=m_conditions.end(); it++) { for (vector<Condition*>::iterator it = m_conditions.begin(); it!=m_conditions.end(); it++) {
Condition* condition = *it; Condition* condition = *it;
ostringstream dummy;
result_t ret = condition->resolve(messages, dummy); result_t ret = condition->resolve(messages, dummy);
if (ret!=RESULT_OK) if (ret!=RESULT_OK) {
errorMessage << dummy.str();
return ret; return ret;
} }
}
return RESULT_OK; return RESULT_OK;
} }
@@ -896,18 +905,12 @@ result_t MessageMap::addDefaultFromFile(vector< vector<string> >& defaults, vect
m_lastError = "condition "+type+" already defined"; m_lastError = "condition "+type+" already defined";
return RESULT_ERR_DUPLICATE_NAME; return RESULT_ERR_DUPLICATE_NAME;
} }
if (row.size()>1 && defaultCircuit.length()>0 && row[1].length()==0)
row[1] = defaultCircuit; // set default circuit
SimpleCondition* condition = NULL; SimpleCondition* condition = NULL;
result_t result = Condition::create(++begin, row.end(), condition); result_t result = Condition::create(++begin, row.end(), defaultDest, defaultCircuit, condition);
if (result!=RESULT_OK) { if (condition==NULL || result!=RESULT_OK) {
if (condition)
delete condition;
m_lastError = "invalid condition"; m_lastError = "invalid condition";
return result; return result;
} }
if (!condition)
return RESULT_ERR_INVALID_ARG;
m_conditions[key] = condition; m_conditions[key] = condition;
return RESULT_OK; return RESULT_OK;
} }
+5 -3
View File
@@ -455,10 +455,12 @@ public:
* Factory method for creating a new instance. * Factory method for creating a new instance.
* @param it the iterator to traverse for the definition parts. * @param it the iterator to traverse for the definition parts.
* @param end the iterator pointing to the end of the definition parts. * @param end the iterator pointing to the end of the definition parts.
* @param defaultDest the valid destination address extracted from the file name (from ZZ part), or empty.
* @param defaultCircuit the valid circuit name extracted from the file name (from IDENT part), or empty.
* @param returnValue the variable in which to store the created instance. * @param returnValue the variable in which to store the created instance.
* @return @a RESULT_OK on success, or an error code. * @return @a RESULT_OK on success, or an error code.
*/ */
static result_t create(vector<string>::iterator& it, const vector<string>::iterator end, SimpleCondition*& returnValue); static result_t create(vector<string>::iterator& it, const vector<string>::iterator end, string defaultDest, string defaultCircuit, SimpleCondition*& returnValue);
/** /**
* Combine this condition with another instance using a logical and. * Combine this condition with another instance using a logical and.
@@ -509,8 +511,8 @@ public:
*/ */
SimpleCondition(const string circuit, const string name, const unsigned char dstAddress, const string field, const vector<unsigned int> valueRanges) SimpleCondition(const string circuit, const string name, const unsigned char dstAddress, const string field, const vector<unsigned int> valueRanges)
: Condition(), : Condition(),
m_circuit(circuit), m_name(name), m_dstAddress(dstAddress), m_field(field), m_valueRanges(valueRanges), m_circuit(circuit), m_name(name), m_dstAddress(dstAddress), m_field(field),
m_message(NULL) { } m_valueRanges(valueRanges), m_message(NULL) { }
/** /**
* Destructor. * Destructor.