PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/common/error-model.h

https://bitbucket.org/nbaldo/prova
C Header | 276 lines | 83 code | 31 blank | 162 comment | 0 complexity | 71b5cbf4559299e1287c5cfffbf6de94 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3. * Copyright (c) 2007 University of Washington
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation;
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Author: Tom Henderson <tomhend@u.washington.edu>
  19. * This code has been ported from ns-2 (queue/errmodel.{cc,h}
  20. */
  21. #ifndef ERROR_MODEL_H
  22. #define ERROR_MODEL_H
  23. #include <list>
  24. #include "ns3/object.h"
  25. #include "ns3/random-variable.h"
  26. namespace ns3 {
  27. class Packet;
  28. /**
  29. * \ingroup common
  30. * \defgroup errormodel Error Model
  31. */
  32. /**
  33. * \ingroup errormodel
  34. * \brief General error model that can be used to corrupt packets
  35. *
  36. * This object is used to flag packets as being lost/errored or not.
  37. * It is part of the Object framework and can be aggregated to
  38. * other ns3 objects and handled by the Ptr class.
  39. *
  40. * The main method is IsCorrupt(Ptr<Packet> p) which returns true if
  41. * the packet is to be corrupted according to the underlying model.
  42. * Depending on the error model, the packet itself may have its packet
  43. * data buffer errored or not, or side information may be returned to
  44. * the client in the form of a packet tag. (Note: No such error models
  45. * that actually error the bits in a packet presently exist).
  46. * The object can have state (resettable by Reset()).
  47. * The object can also be enabled and disabled via two public member functions.
  48. *
  49. * Typical code (simplified) to use an ErrorModel may look something like
  50. * this:
  51. * \code
  52. * Ptr<ErrorModel> rem = CreateObject<RateErrorModel> ();
  53. * rem->SetRandomVariable (UniformVariable ());
  54. * rem->SetRate (0.001);
  55. * ...
  56. * Ptr<Packet> p;
  57. * if (rem->IsCorrupt (p))
  58. * {
  59. * dropTrace(p);
  60. * } else {
  61. * Forward (p);
  62. * }
  63. * \endcode
  64. *
  65. * Two practical error models, a ListErrorModel and a RateErrorModel,
  66. * are currently implemented.
  67. */
  68. class ErrorModel : public Object
  69. {
  70. public:
  71. static TypeId GetTypeId (void);
  72. ErrorModel ();
  73. virtual ~ErrorModel ();
  74. /**
  75. * Note: Depending on the error model, this function may or may not
  76. * alter the contents of the packet upon returning true.
  77. *
  78. * \returns true if the Packet is to be considered as errored/corrupted
  79. * \param pkt Packet to apply error model to
  80. */
  81. bool IsCorrupt (Ptr<Packet> pkt);
  82. /**
  83. * Reset any state associated with the error model
  84. */
  85. void Reset (void);
  86. /**
  87. * Enable the error model
  88. */
  89. void Enable (void);
  90. /**
  91. * Disable the error model
  92. */
  93. void Disable (void);
  94. /**
  95. * \return true if error model is enabled; false otherwise
  96. */
  97. bool IsEnabled (void) const;
  98. private:
  99. /*
  100. * These methods must be implemented by subclasses
  101. */
  102. virtual bool DoCorrupt (Ptr<Packet>) = 0;
  103. virtual void DoReset (void) = 0;
  104. bool m_enable;
  105. };
  106. enum ErrorUnit
  107. {
  108. EU_BIT,
  109. EU_BYTE,
  110. EU_PKT
  111. };
  112. /**
  113. * \brief Determine which packets are errored corresponding to an underlying
  114. * distribution, rate, and unit.
  115. *
  116. * This object is used to flag packets as being lost/errored or not.
  117. * The two parameters that govern the behavior are the rate (or
  118. * equivalently, the mean duration/spacing between errors), and the
  119. * unit (which may be per-bit, per-byte, and per-packet).
  120. * Users can optionally provide a RandomVariable object; the default
  121. * is to use a Uniform(0,1) distribution.
  122. * Reset() on this model will do nothing
  123. *
  124. * IsCorrupt() will not modify the packet data buffer
  125. */
  126. class RateErrorModel : public ErrorModel
  127. {
  128. public:
  129. static TypeId GetTypeId (void);
  130. RateErrorModel ();
  131. virtual ~RateErrorModel ();
  132. /**
  133. * \returns the ErrorUnit being used by the underlying model
  134. */
  135. enum ErrorUnit GetUnit (void) const;
  136. /**
  137. * \param error_unit the ErrorUnit to be used by the underlying model
  138. */
  139. void SetUnit (enum ErrorUnit error_unit);
  140. /**
  141. * \returns the error rate being applied by the model
  142. */
  143. double GetRate (void) const;
  144. /**
  145. * \param rate the error rate to be used by the model
  146. */
  147. void SetRate (double rate);
  148. /**
  149. * \param ranvar A random variable distribution to generate random variates
  150. */
  151. void SetRandomVariable (const RandomVariable &ranvar);
  152. private:
  153. virtual bool DoCorrupt (Ptr<Packet> p);
  154. virtual bool DoCorruptPkt (Ptr<Packet> p);
  155. virtual bool DoCorruptByte (Ptr<Packet> p);
  156. virtual bool DoCorruptBit (Ptr<Packet> p);
  157. virtual void DoReset (void);
  158. enum ErrorUnit m_unit;
  159. double m_rate;
  160. RandomVariable m_ranvar;
  161. };
  162. /**
  163. * \brief Provide a list of Packet uids to corrupt
  164. *
  165. * This object is used to flag packets as being lost/errored or not.
  166. * A note on performance: the list is assumed to be unordered, and
  167. * in general, Packet uids received may be unordered. Therefore,
  168. * each call to IsCorrupt() will result in a walk of the list with
  169. * the present underlying implementation.
  170. *
  171. * Note also that if one wants to target multiple packets from looking
  172. * at an (unerrored) trace file, the act of erroring a given packet may
  173. * cause subsequent packet uids to change. For instance, suppose one wants
  174. * to error packets 11 and 17 on a given device. It may be that erroring
  175. * packet 11 will cause the subsequent uid stream to change and 17 may no
  176. * longer correspond to the second packet that one wants to lose. Therefore,
  177. * be advised that it might take some trial and error to select the
  178. * right uids when multiple are provided.
  179. *
  180. * Reset() on this model will clear the list
  181. *
  182. * IsCorrupt() will not modify the packet data buffer
  183. */
  184. class ListErrorModel : public ErrorModel
  185. {
  186. public:
  187. static TypeId GetTypeId (void);
  188. ListErrorModel ();
  189. virtual ~ListErrorModel ();
  190. /**
  191. * \return a copy of the underlying list
  192. */
  193. std::list<uint32_t> GetList (void) const;
  194. /**
  195. * \param packetlist The list of packet uids to error.
  196. *
  197. * This method overwrites any previously provided list.
  198. */
  199. void SetList (const std::list<uint32_t> &packetlist);
  200. private:
  201. virtual bool DoCorrupt (Ptr<Packet> p);
  202. virtual void DoReset (void);
  203. typedef std::list<uint32_t> PacketList;
  204. typedef std::list<uint32_t>::const_iterator PacketListCI;
  205. PacketList m_packetList;
  206. };
  207. /**
  208. * \brief Provide a list of Packets to corrupt
  209. *
  210. * This model also processes a user-generated list of packets to
  211. * corrupt, except that the list corresponds to the sequence of
  212. * received packets as observed by this error model, and not the
  213. * Packet UID.
  214. *
  215. * Reset() on this model will clear the list
  216. *
  217. * IsCorrupt() will not modify the packet data buffer
  218. */
  219. class ReceiveListErrorModel : public ErrorModel
  220. {
  221. public:
  222. static TypeId GetTypeId (void);
  223. ReceiveListErrorModel ();
  224. virtual ~ReceiveListErrorModel ();
  225. /**
  226. * \return a copy of the underlying list
  227. */
  228. std::list<uint32_t> GetList (void) const;
  229. /**
  230. * \param packetlist The list of packets to error.
  231. *
  232. * This method overwrites any previously provided list.
  233. */
  234. void SetList (const std::list<uint32_t> &packetlist);
  235. private:
  236. virtual bool DoCorrupt (Ptr<Packet> p);
  237. virtual void DoReset (void);
  238. typedef std::list<uint32_t> PacketList;
  239. typedef std::list<uint32_t>::const_iterator PacketListCI;
  240. PacketList m_packetList;
  241. uint32_t m_timesInvoked;
  242. };
  243. } //namespace ns3
  244. #endif