OMNeT++ Simulation Library  5.6.1
cfingerprint.h
1 //==========================================================================
2 // CFINGERPRINT.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2008 Andras Varga
10  Copyright (C) 2006-2008 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __CFINGERPRINT_H
17 #define __CFINGERPRINT_H
18 
19 #include <string.h>
20 #include "simkerneldefs.h"
21 #include "cevent.h"
22 #include "cmessage.h"
23 #include "chasher.h"
24 #include "cmatchexpression.h"
25 
26 namespace omnetpp {
27 
35 {
36  public:
39  virtual ~cFingerprintCalculator() {}
41 
45  virtual void initialize(const char *expectedFingerprints, cConfiguration *cfg, int index=-1) = 0;
46 
49  virtual void addEvent(cEvent *event) = 0;
50  virtual void addScalarResult(const cComponent *component, const char *name, double value) = 0;
51  virtual void addStatisticResult(const cComponent *component, const char *name, const cStatistic *value) = 0;
52  virtual void addVectorResult(const cComponent *component, const char *name, const simtime_t& t, double value) = 0;
53 
54  virtual void addExtraData(const char *data, size_t length) = 0;
55  virtual void addExtraData(char data) = 0;
56  virtual void addExtraData(short data) = 0;
57  virtual void addExtraData(int data) = 0;
58  virtual void addExtraData(long data) = 0;
59  virtual void addExtraData(long long data) = 0;
60  virtual void addExtraData(unsigned char data) = 0;
61  virtual void addExtraData(unsigned short data) = 0;
62  virtual void addExtraData(unsigned int data) = 0;
63  virtual void addExtraData(unsigned long data) = 0;
64  virtual void addExtraData(unsigned long long data) = 0;
65  virtual void addExtraData(double data) = 0;
66  virtual void addExtraData(const char *data) = 0;
68 
73  virtual bool checkFingerprint() const = 0;
74 };
75 
76 #ifdef USE_OMNETPP4x_FINGERPRINTS
77 
87 class SIM_API cOmnetpp4xFingerprintCalculator : public cFingerprintCalculator
88 {
89  protected:
90  std::string expectedFingerprints;
91  cHasher *hasher;
92 
93  public:
94  cOmnetpp4xFingerprintCalculator();
95  virtual ~cOmnetpp4xFingerprintCalculator();
96 
97  virtual cOmnetpp4xFingerprintCalculator *dup() const override { return new cOmnetpp4xFingerprintCalculator(); }
98  virtual std::string str() const override { return hasher->str(); }
99  virtual void initialize(const char *expectedFingerprints, cConfiguration *cfg, int index=-1) override;
100 
101  virtual void addEvent(cEvent *event) override;
102  virtual void addScalarResult(const cComponent *component, const char *name, double value) override {}
103  virtual void addStatisticResult(const cComponent *component, const char *name, const cStatistic *value) override {}
104  virtual void addVectorResult(const cComponent *component, const char *name, const simtime_t& t, double value) override {}
105 
106  virtual void addExtraData(const char *buffer, size_t length) override { hasher->add(buffer, length); }
107  virtual void addExtraData(char data) override { hasher->add(data); }
108  virtual void addExtraData(short data) override { hasher->add(data); }
109  virtual void addExtraData(int data) override { hasher->add(data); }
110  virtual void addExtraData(long data) override { hasher->add(data); }
111  virtual void addExtraData(long long data) override { hasher->add(data); }
112  virtual void addExtraData(unsigned char data) override { hasher->add(data); }
113  virtual void addExtraData(unsigned short data) override { hasher->add(data); }
114  virtual void addExtraData(unsigned int data) override { hasher->add(data); }
115  virtual void addExtraData(unsigned long data) override { hasher->add(data); }
116  virtual void addExtraData(unsigned long long data) override { hasher->add(data); }
117  virtual void addExtraData(double data) override { hasher->add(data); }
118  virtual void addExtraData(const char *data) override { hasher->add(data); }
119 
120  virtual bool checkFingerprint() const override;
121 
122 };
123 
124 #else // if !USE_OMNETPP4x_FINGERPRINTS
125 
156 {
157  protected:
158  enum FingerprintIngredient {
159  EVENT_NUMBER = 'e',
160  SIMULATION_TIME = 't',
161  MESSAGE_FULL_NAME = 'n',
162  MESSAGE_CLASS_NAME = 'c',
163  MESSAGE_KIND = 'k',
164  MESSAGE_BIT_LENGTH = 'l',
165  MESSAGE_CONTROL_INFO_CLASS_NAME = 'o',
166  MESSAGE_DATA = 'd',
167  MODULE_ID = 'i',
168  MODULE_FULL_NAME = 'm',
169  MODULE_FULL_PATH = 'p',
170  MODULE_CLASS_NAME = 'a',
171  RANDOM_NUMBERS_DRAWN = 'r',
172  RESULT_SCALAR = 's',
173  RESULT_STATISTIC = 'z',
174  RESULT_VECTOR = 'v',
175  EXTRA_DATA = 'x',
176  CLEAN_HASHER = '0'
177  };
178 
179  class MatchableObject : public cMatchExpression::Matchable
180  {
181  private:
182  const cObject *object;
183  mutable std::string attributeValue;
184 
185  public:
186  MatchableObject(const cObject *object) : object(object) {}
187 
188  virtual const char *getAsString() const;
189  virtual const char *getAsString(const char *attribute) const;
190  };
191 
192  protected:
193  std::string expectedFingerprints;
194  std::string ingredients;
195  cMatchExpression *eventMatcher;
196  cMatchExpression *moduleMatcher;
197  cMatchExpression *resultMatcher;
198  cHasher *hasher;
199  bool addEvents;
200  bool addScalarResults;
201  bool addStatisticResults;
202  bool addVectorResults;
203  bool addExtraData_;
204 
205  protected:
206  virtual FingerprintIngredient validateIngredient(char ch);
207  virtual void parseIngredients(const char *s);
208  virtual void parseEventMatcher(const char *s);
209  virtual void parseModuleMatcher(const char *s);
210  virtual void parseResultMatcher(const char *s);
211  virtual bool addEventIngredient(cEvent *event, FingerprintIngredient ingredient);
212 
213  public:
215  virtual ~cSingleFingerprintCalculator();
216 
217  virtual cSingleFingerprintCalculator *dup() const override { return new cSingleFingerprintCalculator(); }
218  virtual std::string str() const override;
219  virtual void initialize(const char *expectedFingerprints, cConfiguration *cfg, int index=-1) override;
220 
221  virtual void addEvent(cEvent *event) override;
222  virtual void addScalarResult(const cComponent *component, const char *name, double value) override;
223  virtual void addStatisticResult(const cComponent *component, const char *name, const cStatistic *value) override;
224  virtual void addVectorResult(const cComponent *component, const char *name, const simtime_t& t, double value) override;
225 
226  virtual void addExtraData(const char *buffer, size_t length) override { if (addExtraData_) hasher->add(buffer, length); }
227  virtual void addExtraData(char data) override { if (addExtraData_) hasher->add(data); }
228  virtual void addExtraData(short data) override { if (addExtraData_) hasher->add(data); }
229  virtual void addExtraData(int data) override { if (addExtraData_) hasher->add(data); }
230  virtual void addExtraData(long data) override { if (addExtraData_) hasher->add(data); }
231  virtual void addExtraData(long long data) override { if (addExtraData_) hasher->add(data); }
232  virtual void addExtraData(unsigned char data) override { if (addExtraData_) hasher->add(data); }
233  virtual void addExtraData(unsigned short data) override { if (addExtraData_) hasher->add(data); }
234  virtual void addExtraData(unsigned int data) override { if (addExtraData_) hasher->add(data); }
235  virtual void addExtraData(unsigned long data) override { if (addExtraData_) hasher->add(data); }
236  virtual void addExtraData(unsigned long long data) override { if (addExtraData_) hasher->add(data); }
237  virtual void addExtraData(double data) override { if (addExtraData_) hasher->add(data); }
238  virtual void addExtraData(const char *data) override { if (addExtraData_) hasher->add(data); }
239 
240  virtual bool checkFingerprint() const override;
241 
242 };
243 
244 
254 {
255  protected:
256  cFingerprintCalculator *prototype;
257  std::vector<cFingerprintCalculator *> elements;
258 
259  public:
261  virtual ~cMultiFingerprintCalculator();
262 
263  virtual cMultiFingerprintCalculator *dup() const override { return new cMultiFingerprintCalculator(static_cast<cFingerprintCalculator *>(prototype->dup())); }
264  virtual std::string str() const override;
265  virtual void initialize(const char *expectedFingerprints, cConfiguration *cfg, int index=-1) override;
266 
267  virtual void addEvent(cEvent *event) override;
268  virtual void addScalarResult(const cComponent *component, const char *name, double value) override;
269  virtual void addStatisticResult(const cComponent *component, const char *name, const cStatistic *value) override;
270  virtual void addVectorResult(const cComponent *component, const char *name, const simtime_t& t, double value) override;
271 
272 #define for_each_element(CODE) for (std::vector<cFingerprintCalculator *>::iterator it = elements.begin(); it != elements.end(); ++it) { cFingerprintCalculator *element = *it; CODE; }
273  virtual void addExtraData(const char *data, size_t length) override { for_each_element(element->addExtraData(data, length)); }
274  virtual void addExtraData(char data) override { for_each_element(element->addExtraData(data)); }
275  virtual void addExtraData(short data) override { for_each_element(element->addExtraData(data)); }
276  virtual void addExtraData(int data) override { for_each_element(element->addExtraData(data)); }
277  virtual void addExtraData(long data) override { for_each_element(element->addExtraData(data)); }
278  virtual void addExtraData(long long data) override { for_each_element(element->addExtraData(data)); }
279  virtual void addExtraData(unsigned char data) override { for_each_element(element->addExtraData(data)); }
280  virtual void addExtraData(unsigned short data) override { for_each_element(element->addExtraData(data)); }
281  virtual void addExtraData(unsigned int data) override { for_each_element(element->addExtraData(data)); }
282  virtual void addExtraData(unsigned long data) override { for_each_element(element->addExtraData(data)); }
283  virtual void addExtraData(unsigned long long data) override { for_each_element(element->addExtraData(data)); }
284  virtual void addExtraData(double data) override { for_each_element(element->addExtraData(data)); }
285  virtual void addExtraData(const char *data) override { for_each_element(element->addExtraData(data)); }
286 #undef for_each_element
287 
288  virtual bool checkFingerprint() const override;
289 };
290 
291 #endif // !USE_OMNETPP4x_FINGERPRINTS
292 
293 
294 } // namespace omnetpp
295 
296 #endif
297 
Common base for module and channel classes.
Definition: ccomponent.h:48
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
int64_t-based, base-10 fixed-point simulation time.
Definition: simtime.h:66
Objects to be matched must implement this interface.
Definition: cmatchexpression.h:64
This class calculates the "fingerprint" of a simulation.
Definition: cfingerprint.h:155
Represents an event in the discrete event simulator.
Definition: cevent.h:43
virtual cSingleFingerprintCalculator * dup() const override
Definition: cfingerprint.h:217
virtual cObject * dup() const
virtual cMultiFingerprintCalculator * dup() const override
Definition: cfingerprint.h:263
std::string str() const
Utility class to calculate the hash of some data.
Definition: chasher.h:37
Utility class, to make it impossible to call the operator= and copy constructor of any class derived ...
Definition: cobject.h:311
This class defines the interface for fingerprint calculators.
Definition: cfingerprint.h:34
Definition: cabstracthistogram.h:21
cStatistic is an abstract class for computing statistical properties of a random variable.
Definition: cstatistic.h:34
This class calculates multiple fingerprints simultaneously.
Definition: cfingerprint.h:253
Represents the configuration, as accessed by the simulation kernel.
Definition: cconfiguration.h:70
Decides whether an object matches an expression.
Definition: cmatchexpression.h:57