Main Page | Namespace List | Class Hierarchy | Class List | File List | Class Members

netelem.h

00001 
00008 #ifndef NETELEM_H
00009 #define NETELEM_H
00010 
00011 #include <queue>
00012 #include <map>
00013 #include <vector>
00014 #include <list>
00015 #include <stdio.h>
00016 
00017 using namespace std;
00018 
00019 #include "distrib.h"
00020 #include "lstat.h"
00021 #include "ev.h"
00022 
00023 class Packet;
00024 class NetElement;
00025 class PacketSource;
00026 class PacketSink;
00027 class Server;
00028 class SrcSink;
00029 class Fragmenter;
00030 
00031 
00042 class NetElement 
00043 {
00044 public:
00048   unsigned int              GetId(void) const;
00049 
00055   NetElement*      GetTo(unsigned int k) const;
00056 
00060   inline unsigned int GetSizeTo(void) const {
00061     return tovector_.size();
00062   };
00063 
00077   virtual void     Send     (timeunits now, Packet* p);
00078 
00086   virtual void     Recv     (timeunits now, Packet* p) = 0;  
00087 
00094   virtual void     Reset     (bool dump=false) = 0;  
00103   NetElement*      ConnectTo(NetElement* to);  
00104 
00119   virtual void     FragmentDrop (Packet* p);
00120 
00121   virtual ~NetElement(void);    
00127   static void traceOn(char* filename =0);
00133   bool IsTraceing(void) const {return namtraceflag;};
00138   FILE* traceFile(void) const {return fo;}
00149   void packetTrace(timeunits t, char event, Packet* p);
00158   static void Transit (timeunits t);
00159 
00164   static void Batch (timeunits tinitial, timeunits lapse, int batches);
00170   static void resetAll(bool dump=false);
00171 
00172 private:
00173   //Trace-related variables
00174   static bool namtraceflag;
00175   static FILE* fo;
00176 
00177   static unsigned int numElements_;
00178   static list<NetElement*> resetList_;
00179 
00180   unsigned int     id_;
00181   vector <NetElement*> tovector_;  
00182 
00183 protected:
00184 
00185   NetElement (void);
00186   virtual void     namConnectTrace (void);
00187 };
00188 
00193 class Packet {
00194 private:
00195   static           unsigned long int totalPackets_;
00196   unsigned long int id_;          
00197   NetElement*      source_;       
00198   NetElement*      destination_;  
00199   unsigned int     bits_;         
00200   timeunits        timestamp_;    
00201   unsigned long    datagramid_;  
00202   unsigned int     totalbits_;   
00203   unsigned int     currenthop_;  
00204   
00205 public:
00216   Packet (NetElement* src, NetElement* dst, 
00217           unsigned int bits, timeunits stamp, 
00218           unsigned long datagramid = 0, 
00219           unsigned int totalbits = 0, 
00220           unsigned int k = 0);
00221   virtual ~Packet(void);
00225   inline timeunits GetStamp(void) const {return timestamp_;};
00229   unsigned long int         GetPacketId(void) const;
00233   unsigned int              GetBits (void)    const;
00237   unsigned int     GetTotalBits (void) const;
00241   unsigned long    GetDatagramId (void) const;
00245   NetElement*      GetSrc(void)       const;
00249   NetElement*      GetDst(void)       const;
00254   unsigned int     GetPos (void) const;
00258   void             Jump(void);
00263   void             SetTotalBits (unsigned int b);
00268   void             SetBytes (unsigned int B);
00273   void             SetBits (unsigned int b);
00277   void             SetDatagramId (unsigned long id);
00282   void             Stamp(timeunits stamp); 
00283 };
00284 
00291 class Timer : public Event {
00292 public:
00297   typedef void (NetElement::* TimerHandler)(timeunits, Packet*);
00298 
00308   Timer(timeunits t, unsigned int id, 
00309         NetElement* to, TimerHandler th, 
00310         Packet* p);
00311   ~Timer(void);
00312 private:
00313   void              Handle(void);
00314   NetElement*       to_;
00315   TimerHandler      OnExpiry_; //to be called when this timer expires
00316   Packet*           packet_;   //packet this timer refers to
00317 };
00322 class PacketSource : public NetElement {
00323 private:
00324   Distrib*          interarrival_; //Burst Inter-arrival time, in timeunits
00325   Distrib*          size_;         //Packet size, in bits
00326   LStat             S_interArrival; //Statistics
00327   vector <NetElement*> route_;
00328   LStat S_packetDelay_;
00329   //  virtual void      namConnectTrace (void);
00336   virtual void      Send  (timeunits now,  Packet *p);
00340   void              Recv  (timeunits now,  Packet *p);
00341 
00342 protected:
00343   unsigned long int numPackets_;
00344 
00345 public:
00354   PacketSource(Distrib* interarrival, Distrib* service);
00355   virtual ~PacketSource(void);
00360   virtual void      Start (timeunits now);
00364   void              Reset (bool dump=false);
00370   void              AccumDelay (timeunits t);
00378   PacketSource*     AddHop (NetElement* ne);
00385   inline NetElement* GetNextHop (unsigned int index) const {
00386     if(index<route_.size()) {
00387       return route_[index];
00388     }
00389     else {
00390       return 0;
00391     }
00392   }
00393 };
00397 class PacketSink : public NetElement {
00398 private:
00402   void              Send  (timeunits now,  Packet *p);
00403 public:
00404   PacketSink(void);  
00405   virtual ~PacketSink(void);
00409   virtual void      Recv  (timeunits now,  Packet *p);
00413   void              Reset (bool dump=false){};
00414   void              namConnectTrace(void);
00415 };
00421 class SrcSink : public PacketSink {
00422 private:
00423   map <unsigned int, unsigned long> srcmap;//source, count
00424   map <unsigned int, unsigned long> srcfragmentsmap;//source, count
00425   map <unsigned long, unsigned int> srcfragmentscount;//datagram,bits
00426   map <unsigned long, unsigned int> srcdroppedscount;//datagram,bits
00427 
00428 public:
00429   SrcSink (void);
00430   virtual ~SrcSink (void);
00437   virtual void Recv (timeunits now, Packet *p);
00445   virtual void FragmentDrop (Packet* p);
00449   void              Reset (bool dump=false);
00450 };
00451 
00455 typedef enum queuediscipline {
00456   fifo, 
00457   lifo, 
00458   sjf   
00459 };
00463 class CmpPacket {
00464 public:
00470   CmpPacket (queuediscipline discipline = fifo) : discipline_(discipline) {
00471     // Empty
00472   }
00473 
00481   inline bool operator() (Packet*& p1, Packet*& p2) const {
00482     // The return funtion is defined by the discipline used in the constructor
00483     switch (discipline_){
00484     case sjf: return p1->GetBits() > p2->GetBits();
00485     case lifo: return p1->GetStamp() < p2->GetStamp();
00486     case fifo:
00487     default: return p1->GetStamp() > p2->GetStamp();
00488     }
00489   }
00490 
00491 private:
00492   queuediscipline discipline_; // Keeps the comparison discipline
00493 };
00497 class Server : public NetElement {
00498 public:
00510   Server  (char* name,           //Identifies the server
00511            double rBin,          //Server capacity in bits/timeunit
00512            int qsize=-1,         //Queue size, -1 means infinite
00513            timeunits delay = 0,  //Additional constant delay 
00514            //(e.g. propagation delay)
00515            queuediscipline discipline = fifo,  //Queueing discipline
00516            unsigned int mtu = 0, // Maximun Tranfer Unit 
00517            double pebit = 0); // Bit error probability
00523   virtual ~Server(void);
00529   void          Send (timeunits now, Packet *p);
00537   void          Recv (timeunits now, Packet *p);
00538 
00542   void          Reset (bool dump=false);
00543 
00544 private:
00545 
00546   char*        name_;
00547   double       rBin_; //In bits per timeunit
00548   int maxQ_;   //Max Queue Size, in bits, signed.
00549   timeunits   delay_; //In timeunits
00550   unsigned int mtu_; // In bits
00551   int inSystemPackets_; /* 0  when empty system
00552                          * 1  when queue empty and 
00553                          *     packet being served and
00554                          * >1 when packet being served and
00555                          *     others waiting
00556                          * signed, to compare to maxQ_ w/o warnings
00557                          */
00558 
00559   unsigned long int droppedPackets_;
00560   double pebit_; 
00561   
00562   Unif01 txnError_;
00563 
00564   int inSystemBits_; //output token bucket
00565 
00566   //Packet queue. Packets are sorted according to their timestamp_
00567   priority_queue <Packet*, vector<Packet*>, CmpPacket>* qPacket_; 
00568  
00569   
00570   LStat         S_serviceTime; //Service Time
00571   LStat         S_obsNumPackets;//Observed num of pkts in the server
00572   TStat         T_numPackets;   //Expected num of pkts in the server
00573   TStat         T_load;        //Server load
00574 
00575   void          DeQueue (timeunits now, Packet *p);
00576   void          namConnectTrace (void);
00577 };
00578 
00583 class Fragmenter : public NetElement {
00584 private:
00585   unsigned int mtu_; // Maximum Transfer Unit.
00586   void       namConnectTrace (void);
00587   void       Send  (timeunits now,  Packet *p);
00588   void       Recv  (timeunits now,  Packet *p);
00589 public:
00599   Fragmenter (unsigned int mtu = 0);
00603   void              Reset (bool dump=false){};
00604 
00605   virtual ~Fragmenter(void);
00606 };
00607 
00608 
00609 
00615 class SourceState {
00616 private:
00617   Distrib* duration_;       // Duration, in timeunits
00618   Distrib* interarrival_;   // Trx. rate in this state, in bits/timeunit
00619   int      samplesize_;     // In bits
00620   int      sampleCount_;
00621 public:
00634   SourceState(Distrib* duration, Distrib* interarrival, int samplesize);
00638   virtual ~SourceState(void);
00642   int GetSampleSize(void) const;
00646   timeunits GetDuration(void) const;
00650   virtual timeunits GetInterarrival(void) const;
00654   inline void AddToPacketCount (int n) { sampleCount_ += n;}
00655 };
00656 
00657 
00664 class BiStateSource : public PacketSource {
00665 private:
00666   int state_;
00667   timeunits maxDelay_;
00668   SourceState *s_[2];
00669   
00670   timeunits pendingTimeStamp_;
00671   int pendingBits_;
00672   int evid_;
00673 
00674   void              Reset (bool dump=false){};
00675   void              Send(timeunits now,  Packet *p);
00676 public:
00688   BiStateSource (SourceState* on, SourceState* off, timeunits maxdelay = 0);
00689 
00693   virtual ~BiStateSource(void);
00698   void Start(timeunits now);
00699 };
00700 
00701 
00706 class Enveloper : public NetElement {
00707 private:
00708   unsigned int bitOverhead_;
00709   void       Send  (timeunits now,  Packet *p);
00710   void       Recv  (timeunits now,  Packet *p);
00711   void              Reset (bool dump=false){};
00712 public:
00719   Enveloper (unsigned int bOh);
00720   ~Enveloper (void){};
00721 };
00722 
00723 
00727 class VoIPEnveloper : public Enveloper {
00728 private:
00729   void              Reset (bool dump=false){};
00730 public:
00734   VoIPEnveloper (void) : Enveloper(320){};
00735   ~VoIPEnveloper (void){};
00736 };
00737 
00738 
00739 
00743 class VoiceTraceFileSource : public PacketSource {
00744 private:
00745   static const int maxEvents_;
00746   int fd_;
00747   bool loop_;
00748   int offset_;
00749   int nfpp_;
00750   timeunits tuPerSample_;
00751   char activeCode_;
00752   int  activeBitSize_;
00753   char sidCode_;
00754   int  sidBitSize_;
00755   int  position_;
00756   void Reset(bool dump=false){};
00757   void Recv(timeunits now, Packet *p){};
00758   void Send(timeunits now,  Packet *p);
00759 public:
00772   VoiceTraceFileSource (const char* infile, 
00773                         bool loop,
00774                         int offset, 
00775                         int nfpp, 
00776                         timeunits tuPerSample,
00777                         char activeCode, int activeBitSize, 
00778                         char sidCode, int sidBitSize);
00779     
00784   void Start(timeunits now);
00785   virtual ~VoiceTraceFileSource (void);
00786 };
00787 
00788 #endif
00789 

Generated on Fri Jan 19 15:35:17 2007 by  doxygen 1.4.2