Facebook
From Meleciusz, 1 Month ago, written in C++.
Embed
Download Paste or View Raw
Hits: 150
  1. #include <omnetpp.h>
  2. //using namespace omnetpp; //for Omnet++ ver. 5
  3.  
  4. class Server : public cSimpleModule
  5. {  
  6.   private:
  7.     cQueue queue;             //the queue of jobs; it is assumed that the first job in the queue is the one being serviced
  8.  cMessage *departure;        //special message; it reminds about the end of service and the need for job departure
  9.  simtime_t departure_time;   //time of the next departure
  10.  int bufforSize;
  11.  int lostPackages = 0;
  12.  int packages = 0;
  13.  int lostPackegesOneByOne = 0;
  14.  
  15.  simtime_t overflowTime;
  16.  cDoubleHistogram hisOverflowTime;
  17.  cLongHistogram hisOverflowTimeOneByOne;
  18.  
  19.  
  20.   protected:
  21.     virtual void initialize();
  22.     virtual void handleMessage(cMessage *msgin);
  23.  
  24. };
  25.  
  26. Define_Module(Server);
  27.  
  28.  
  29. void Server::initialize()
  30. {
  31.  departure = new cMessage("Departure");
  32.  bufforSize = par("buffor_size");
  33.  //hisOverflowTime.setRange(0, 2.5);
  34. }
  35.  
  36.  
  37. void Server::handleMessage(cMessage *msgin)  //two types of messages may arrive: a job from the source, or the special message initiating job departure
  38. {  
  39.     if (msgin==departure)   //job departure
  40.  {
  41.   cMessage *msg = (cMessage *)queue.pop();    //remove the finished job from the head of the queue
  42.   send(msg,"out");
  43.  
  44.  
  45.   if(queue.length() == bufforSize - 1){
  46.  
  47.       hisOverflowTime.collect(simTime() - overflowTime);
  48.  
  49.       if(lostPackegesOneByOne != 0){
  50.           hisOverflowTimeOneByOne.collect(lostPackegesOneByOne);
  51.       }
  52.   }
  53.  
  54.  
  55.  
  56.   if (!queue.isEmpty())                         //if the queue is not empty, initiate the next service, i.e. schedule the next departure event in the future
  57.   {
  58.    departure_time=simTime()+par("service_time");
  59.          scheduleAt(departure_time,departure);
  60.   }
  61.  }
  62.  else                    //job arrival
  63.  {
  64.      packages++;
  65.  
  66.   if (queue.isEmpty())  //if the queue is empty, the job that has just arrived has to be served immediately, i.e. the departure event of this job has to be scheduled in the future
  67.   {
  68.    departure_time=simTime()+par("service_time");
  69.             scheduleAt(departure_time,departure);
  70.   }
  71.   if(queue.length() < bufforSize){
  72.  
  73.       queue.insert(msgin); //insert the job at the end of the queue
  74.  
  75.       if(queue.length() == bufforSize){
  76.           overflowTime = simTime();
  77.       }
  78.       else{
  79.            lostPackeges
  80.       }
  81.   }else{
  82.       lostPackages++;
  83.       delete msgin;
  84.       lostPackegesOneByOne++;
  85.   }
  86.  
  87.   double L = (lostPackages * 1.0) / packages;
  88.   ev << "L = " << L;
  89.  }
  90. }
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.