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

Simple token bucket meter. More...

#include <TokenBucketMeter.h>

Inheritance diagram for inet::TokenBucketMeter:

Public Member Functions

 TokenBucketMeter ()
 

Protected Member Functions

virtual int numInitStages () const override
 
virtual void initialize (int stage) override
 
virtual void handleMessage (cMessage *msg) override
 
virtual void refreshDisplay () const override
 
virtual int meterPacket (cPacket *packet)
 

Protected Attributes

double CIR = NaN
 
long CBS = 0
 
bool colorAwareMode = false
 
long Tc = 0
 
simtime_t lastUpdateTime
 
int numRcvd = 0
 
int numRed = 0
 

Detailed Description

Simple token bucket meter.

Constructor & Destructor Documentation

inet::TokenBucketMeter::TokenBucketMeter ( )
inline
44 {}

Member Function Documentation

void inet::TokenBucketMeter::handleMessage ( cMessage *  msg)
overrideprotectedvirtual
52 {
53  cPacket *packet = findIPDatagramInPacket(check_and_cast<cPacket *>(msg));
54  if (!packet)
55  throw cRuntimeError("TokenBucketMeter received a packet that does not encapsulate an IP datagram.");
56 
57  numRcvd++;
58  int color = meterPacket(packet);
59  if (color == GREEN) {
60  send(packet, "greenOut");
61  }
62  else {
63  numRed++;
64  send(packet, "redOut");
65  }
66 }
cPacket * findIPDatagramInPacket(cPacket *packet)
Returns the IP datagram encapsulated inside packet, or the packet itself if it is an IPv4/IPv6 datagr...
Definition: DiffservUtil.cc:199
virtual int meterPacket(cPacket *packet)
Definition: TokenBucketMeter.cc:78
Definition: DiffservUtil.h:30
int numRed
Definition: TokenBucketMeter.h:41
int numRcvd
Definition: TokenBucketMeter.h:40
void inet::TokenBucketMeter::initialize ( int  stage)
overrideprotectedvirtual
30 {
31  cSimpleModule::initialize(stage);
32 
33  if (stage == INITSTAGE_LOCAL) {
34  numRcvd = 0;
35  numRed = 0;
36  WATCH(numRcvd);
37  WATCH(numRed);
38 
39  CBS = 8 * (int)par("cbs");
40  colorAwareMode = par("colorAwareMode");
41  Tc = CBS;
42  }
43  else if (stage == INITSTAGE_NETWORK_LAYER) {
44  const char *cirStr = par("cir");
45  IInterfaceTable *ift = findModuleFromPar<IInterfaceTable>(par("interfaceTableModule"), this);
46  CIR = parseInformationRate(cirStr, "cir", ift, *this, 0);
47  lastUpdateTime = simTime();
48  }
49 }
long Tc
Definition: TokenBucketMeter.h:37
simtime_t lastUpdateTime
Definition: TokenBucketMeter.h:38
bool colorAwareMode
Definition: TokenBucketMeter.h:35
Initialization of network-layer protocols, stage 1.
Definition: InitStages.h:72
Local initializations.
Definition: InitStages.h:35
int numRed
Definition: TokenBucketMeter.h:41
double parseInformationRate(const char *attrValue, const char *attrName, IInterfaceTable *ift, cSimpleModule &owner, int defaultValue)
Parses the information rate parameter (bits/sec).
Definition: DiffservUtil.cc:51
int numRcvd
Definition: TokenBucketMeter.h:40
double CIR
Definition: TokenBucketMeter.h:33
long CBS
Definition: TokenBucketMeter.h:34
int inet::TokenBucketMeter::meterPacket ( cPacket *  packet)
protectedvirtual
79 {
80  // update token buckets
81  simtime_t currentTime = simTime();
82  long numTokens = (long)(SIMTIME_DBL(currentTime - lastUpdateTime) * CIR);
83  lastUpdateTime = currentTime;
84  if (Tc + numTokens <= CBS)
85  Tc += numTokens;
86  else
87  Tc = CBS;
88 
89  // update meter state
90  int oldColor = colorAwareMode ? getColor(packet) : -1;
91  int newColor;
92  int packetSizeInBits = packet->getBitLength();
93  if (oldColor <= GREEN && Tc - packetSizeInBits >= 0) {
94  Tc -= packetSizeInBits;
95  newColor = GREEN;
96  }
97  else
98  newColor = RED;
99 
100  setColor(packet, newColor);
101  return newColor;
102 }
long Tc
Definition: TokenBucketMeter.h:37
int getColor(cPacket *packet)
Returns the color of the packet.
Definition: DiffservUtil.cc:227
void setColor(cPacket *packet, int color)
Sets the color of the packet.
Definition: DiffservUtil.cc:233
simtime_t lastUpdateTime
Definition: TokenBucketMeter.h:38
bool colorAwareMode
Definition: TokenBucketMeter.h:35
Definition: DiffservUtil.h:30
Definition: DiffservUtil.h:30
double CIR
Definition: TokenBucketMeter.h:33
long CBS
Definition: TokenBucketMeter.h:34
virtual int inet::TokenBucketMeter::numInitStages ( ) const
inlineoverrideprotectedvirtual
47 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::TokenBucketMeter::refreshDisplay ( ) const
overrideprotectedvirtual
69 {
70  char buf[50] = "";
71  if (numRcvd > 0)
72  sprintf(buf + strlen(buf), "rcvd: %d ", numRcvd);
73  if (numRed > 0)
74  sprintf(buf + strlen(buf), "red:%d ", numRed);
75  getDisplayString().setTagArg("t", 0, buf);
76 }
int numRed
Definition: TokenBucketMeter.h:41
int numRcvd
Definition: TokenBucketMeter.h:40

Member Data Documentation

long inet::TokenBucketMeter::CBS = 0
protected
double inet::TokenBucketMeter::CIR = NaN
protected
bool inet::TokenBucketMeter::colorAwareMode = false
protected
simtime_t inet::TokenBucketMeter::lastUpdateTime
protected
int inet::TokenBucketMeter::numRcvd = 0
protected
int inet::TokenBucketMeter::numRed = 0
protected
long inet::TokenBucketMeter::Tc = 0
protected

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