INET Framework for OMNeT++/OMNEST
inet::MessageChecker Class Reference

#include <MessageChecker.h>

Inheritance diagram for inet::MessageChecker:

Public Member Functions

 MessageChecker ()
 

Protected Member Functions

void initialize () override
 
void handleMessage (cMessage *msg) override
 
void checkMessage (cMessage *msg)
 
void forwardMessage (cMessage *msg)
 
void finish () override
 
void checkFields (void *object, cClassDescriptor *descriptor, const cXMLElementList &msgPattern) const
 
void checkFieldValue (void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr, int i=0) const
 
void checkFieldObject (void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr, const cXMLElement &pattern, int i=0) const
 
int checkFieldArray (void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr) const
 
void checkFieldValueInArray (void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr) const
 
void checkFieldObjectInArray (void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr, const cXMLElement &pattern) const
 
void checkFieldType (void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attrList, int i=0) const
 
int findFieldIndex (void *object, cClassDescriptor *descriptor, const std::string &fieldName) const
 

Protected Attributes

cXMLElementList m_checkingInfo
 
cXMLElementList::iterator m_iterChk
 List of checking information. More...
 
unsigned forwardedMsg
 Interator of the list of chacking information. More...
 
unsigned checkedMsg
 Number of received and forwarded messages. More...
 

Constructor & Destructor Documentation

inet::MessageChecker::MessageChecker ( )
25  : forwardedMsg(0)
26  , checkedMsg(0)
27 {
28 }
unsigned checkedMsg
Number of received and forwarded messages.
Definition: MessageChecker.h:53
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52

Member Function Documentation

int inet::MessageChecker::checkFieldArray ( void *  object,
cClassDescriptor *  descriptor,
int  field,
cXMLAttributeMap &  attr 
) const
protected

Referenced by checkFieldObjectInArray(), checkFields(), and checkFieldValueInArray().

140 {
141  if (!descriptor->getFieldIsArray(field))
142  throw cRuntimeError("The field \"%s\" in message %d isn't an array", attr["name"].data(), forwardedMsg);
143 
144  // check the size of the field array into the client object
145  int size = atol(attr["size"].data());
146  int fieldSize = descriptor->getFieldArraySize(object, field);
147  if (size != fieldSize)
148  throw cRuntimeError("Field array \"%s\" contains %d element(s) (and not %d) in message %d",
149  attr["name"].data(), fieldSize, size, forwardedMsg);
150 
151  return fieldSize;
152 }
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52
void inet::MessageChecker::checkFieldObject ( void *  object,
cClassDescriptor *  descriptor,
int  field,
cXMLAttributeMap &  attr,
const cXMLElement &  pattern,
int  i = 0 
) const
protected

Referenced by checkFieldObjectInArray(), and checkFields().

127 {
128  // get the client object associated to the field
129  void *obj = descriptor->getFieldStructValuePointer(object, field, i);
130 
131  // get the client object associated to the field, and its descriptor class
132  cClassDescriptor *descr = descriptor->getFieldIsCObject(field) ?
133  cClassDescriptor::getDescriptorFor((cObject *)obj) :
134  cClassDescriptor::getDescriptorFor(descriptor->getFieldStructName(field));
135 
136  checkFields(obj, descr, pattern.getChildren());
137 }
void checkFields(void *object, cClassDescriptor *descriptor, const cXMLElementList &msgPattern) const
Definition: MessageChecker.cc:76
void inet::MessageChecker::checkFieldObjectInArray ( void *  object,
cClassDescriptor *  descriptor,
int  field,
cXMLAttributeMap &  attr,
const cXMLElement &  pattern 
) const
protected

Referenced by checkFields().

169 {
170  int fieldSize = checkFieldArray(object, descriptor, field, attr);
171 
172  // convert attribute "index" into integer
173  int i = atol(attr["index"].data());
174 
175  if (i >= fieldSize)
176  throw cRuntimeError("Field \"%s\" in message %d has no entry for index %d", attr["name"].data(), forwardedMsg, i);
177 
178  // check field object into the client object
179  checkFieldObject(object, descriptor, field, attr, pattern, i);
180 }
int checkFieldArray(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr) const
Definition: MessageChecker.cc:139
void checkFieldObject(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr, const cXMLElement &pattern, int i=0) const
Definition: MessageChecker.cc:126
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52
void inet::MessageChecker::checkFields ( void *  object,
cClassDescriptor *  descriptor,
const cXMLElementList &  msgPattern 
) const
protected

Referenced by checkFieldObject(), and checkMessage().

77 {
78  // fldPatternList contains the list of fields to be inspected
79  cXMLElementList::const_iterator iter = msgPattern.begin();
80  while (iter != msgPattern.end()) {
81  const cXMLElement& pattern = **iter;
82  cXMLAttributeMap attr = pattern.getAttributes();
83  std::string patternType(pattern.getTagName());
84 
85  // find field position into the client object
86  int field = findFieldIndex(object, descriptor, attr["name"]);
87 
88  // check the field type into the client object (if requiered)
89  if (attr.find("type") != attr.end())
90  checkFieldType(object, descriptor, field, attr);
91 
92  if (patternType == "fieldValue")
93  checkFieldValue(object, descriptor, field, attr);
94  else if (patternType == "fieldObject")
95  checkFieldObject(object, descriptor, field, attr, pattern);
96  else if (patternType == "fieldArray")
97  checkFieldArray(object, descriptor, field, attr);
98  else if (patternType == "fieldValueInArray")
99  checkFieldValueInArray(object, descriptor, field, attr);
100  else if (patternType == "fieldObjectInArray")
101  checkFieldObjectInArray(object, descriptor, field, attr, pattern);
102 
103  iter++;
104  }
105 }
int findFieldIndex(void *object, cClassDescriptor *descriptor, const std::string &fieldName) const
Definition: MessageChecker.cc:196
void checkFieldType(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attrList, int i=0) const
Definition: MessageChecker.cc:182
int checkFieldArray(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr) const
Definition: MessageChecker.cc:139
void checkFieldObjectInArray(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr, const cXMLElement &pattern) const
Definition: MessageChecker.cc:168
void checkFieldObject(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr, const cXMLElement &pattern, int i=0) const
Definition: MessageChecker.cc:126
void checkFieldValue(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr, int i=0) const
Definition: MessageChecker.cc:107
void checkFieldValueInArray(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr) const
Definition: MessageChecker.cc:154
void inet::MessageChecker::checkFieldType ( void *  object,
cClassDescriptor *  descriptor,
int  field,
cXMLAttributeMap &  attrList,
int  i = 0 
) const
protected

Referenced by checkFields().

183 {
184  std::string type;
185 
186  if (descriptor->getFieldIsCObject(field))
187  type = ((cObject *)descriptor->getFieldStructValuePointer(object, field, i))->getClassName();
188  else
189  type = descriptor->getFieldTypeString(field);
190 
191  if (type != attr["type"])
192  throw cRuntimeError("Type mismatch for field \"%s\" in message %d (\"%s\" != \"%s\")",
193  attr["name"].data(), forwardedMsg, type.data(), attr["type"].data());
194 }
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52
void inet::MessageChecker::checkFieldValue ( void *  object,
cClassDescriptor *  descriptor,
int  field,
cXMLAttributeMap &  attr,
int  i = 0 
) const
protected

Referenced by checkFields(), and checkFieldValueInArray().

108 {
109  // get the field string value from the client object
110  std::string value = descriptor->getFieldValueAsString(object, field, i);
111 
112  // convert the field value into its name of enum value
113  if (descriptor->getFieldProperty(field, "enum")) {
114  cEnum *enm = cEnum::find(descriptor->getFieldProperty(field, "enum"));
115  if (enm)
116  value = enm->getStringFor(atol(value.c_str()));
117  }
118 
119  // check field value into the client object
120  if (value.find(attr["value"]) != 0) //allow to keep reference values even if
121  //simtime precision changed...
122  throw cRuntimeError("Mismatch: field \"%s\" in the message %d (\"%s\" != \"%s\")",
123  attr["name"].data(), forwardedMsg, value.data(), attr["value"].data());
124 }
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52
std::vector< T >::iterator find(std::vector< T > &v, const T &a)
Definition: stlutils.h:48
void inet::MessageChecker::checkFieldValueInArray ( void *  object,
cClassDescriptor *  descriptor,
int  field,
cXMLAttributeMap &  attr 
) const
protected

Referenced by checkFields().

155 {
156  int fieldSize = checkFieldArray(object, descriptor, field, attr);
157 
158  // convert attribute "index" into integer
159  int i = atol(attr["index"].data());
160 
161  if (i >= fieldSize)
162  throw cRuntimeError("Field \"%s\" in message %d has no entry for index %d", attr["name"].data(), forwardedMsg, i);
163 
164  // check field value into the client object
165  checkFieldValue(object, descriptor, field, attr, i);
166 }
int checkFieldArray(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr) const
Definition: MessageChecker.cc:139
void checkFieldValue(void *object, cClassDescriptor *descriptor, int field, cXMLAttributeMap &attr, int i=0) const
Definition: MessageChecker.cc:107
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52
void inet::MessageChecker::checkMessage ( cMessage *  msg)
protected

Referenced by handleMessage().

51 {
52  while (m_iterChk != m_checkingInfo.end()) {
53  cXMLElement& messagePattern = **m_iterChk;
54 
55  if (std::string(messagePattern.getTagName()) == "message" && messagePattern.hasAttributes()) {
56  int occurence = atol(messagePattern.getAttribute("occurence"));
57  if (occurence > 0) {
58  if (messagePattern.hasChildren())
59  checkFields(msg, msg->getDescriptor(), messagePattern.getChildren());
60 
61  occurence--;
62  std::ostringstream occur_str;
63  occur_str << occurence;
64  messagePattern.setAttribute("occurence", occur_str.str().data());
65  checkedMsg++;
66  }
67  if (occurence == 0)
68  m_iterChk++;
69  break;
70  }
71  else
72  m_iterChk++;
73  }
74 }
cXMLElementList m_checkingInfo
Definition: MessageChecker.h:50
unsigned checkedMsg
Number of received and forwarded messages.
Definition: MessageChecker.h:53
cXMLElementList::iterator m_iterChk
List of checking information.
Definition: MessageChecker.h:51
void checkFields(void *object, cClassDescriptor *descriptor, const cXMLElementList &msgPattern) const
Definition: MessageChecker.cc:76
int inet::MessageChecker::findFieldIndex ( void *  object,
cClassDescriptor *  descriptor,
const std::string &  fieldName 
) const
protected

Referenced by checkFields().

197 {
198  std::ostringstream availableFields;
199  for (int i = 0; i < descriptor->getFieldCount(); i++) {
200  availableFields << descriptor->getFieldName(i) << ", ";
201  if (std::string(descriptor->getFieldName(i)) == fieldName)
202  return i;
203  }
204 
205  throw cRuntimeError("Unknown field \"%s\" in message %d\nAvailable fields in \"%s\" are : %s"
206  , fieldName.data(), forwardedMsg, descriptor->getClassName(), availableFields.str().data());
207  return 0;
208 }
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52
void inet::MessageChecker::finish ( )
overrideprotected
223 {
224  if (forwardedMsg > checkedMsg)
225  throw cRuntimeError("%d message(s) has not been checked", forwardedMsg - checkedMsg);
226 
227  if (m_iterChk != m_checkingInfo.end())
228  throw cRuntimeError("Several message(s) have to be checked");
229 }
cXMLElementList m_checkingInfo
Definition: MessageChecker.h:50
unsigned checkedMsg
Number of received and forwarded messages.
Definition: MessageChecker.h:53
cXMLElementList::iterator m_iterChk
List of checking information.
Definition: MessageChecker.h:51
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52
void inet::MessageChecker::forwardMessage ( cMessage *  msg)
protected

Referenced by handleMessage().

211 {
212  cGate *gateOut = gate("out");
213  cChannel *channel = gateOut->getChannel();
214  simtime_t now = simTime();
215  simtime_t endTransmissionTime = channel->getTransmissionFinishTime();
216  simtime_t delayToWait = 0;
217  if (endTransmissionTime > now)
218  delayToWait = endTransmissionTime - now;
219  sendDelayed(msg, delayToWait, "out");
220 }
void inet::MessageChecker::handleMessage ( cMessage *  msg)
overrideprotected
44 {
45  forwardedMsg++;
46  checkMessage(msg);
47  forwardMessage(msg);
48 }
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52
void forwardMessage(cMessage *msg)
Definition: MessageChecker.cc:210
void checkMessage(cMessage *msg)
Definition: MessageChecker.cc:50
void inet::MessageChecker::initialize ( )
overrideprotected
31 {
32  cXMLElement *root = par("config");
33  if (std::string(root->getTagName()) == "checker") {
34  m_checkingInfo = root->getChildren();
35  m_iterChk = m_checkingInfo.begin();
36  }
37  forwardedMsg = 0;
38  checkedMsg = 0;
39  WATCH(forwardedMsg);
40  WATCH(checkedMsg);
41 }
cXMLElementList m_checkingInfo
Definition: MessageChecker.h:50
unsigned checkedMsg
Number of received and forwarded messages.
Definition: MessageChecker.h:53
cXMLElementList::iterator m_iterChk
List of checking information.
Definition: MessageChecker.h:51
unsigned forwardedMsg
Interator of the list of chacking information.
Definition: MessageChecker.h:52

Member Data Documentation

unsigned inet::MessageChecker::checkedMsg
protected

Number of received and forwarded messages.

Referenced by checkMessage(), finish(), and initialize().

unsigned inet::MessageChecker::forwardedMsg
protected
cXMLElementList inet::MessageChecker::m_checkingInfo
protected

Referenced by checkMessage(), finish(), and initialize().

cXMLElementList::iterator inet::MessageChecker::m_iterChk
protected

List of checking information.

Referenced by checkMessage(), finish(), and initialize().


The documentation for this class was generated from the following files: