Facebook
From Damian, 1 Month ago, written in Plain Text.
This paste is a reply to serwer from Damian - view diff
Embed
Download Paste or View Raw
Hits: 131
  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.  
  11.  double L;
  12.  int n;
  13.  int acc,lost;
  14.  
  15.   protected:
  16.     virtual void initialize();
  17.     virtual void handleMessage(cMessage *msgin);
  18.  
  19. };
  20.  
  21. Define_Module(Server);
  22.  
  23.  
  24. void Server::initialize()
  25. {
  26.  departure = new cMessage("Departure");
  27.  acc=lost=0;
  28.  L=0;
  29.  WATCH(L);
  30.  n=par("n");
  31. }
  32.  
  33.  
  34. void Server::handleMessage(cMessage *msgin)  //two types of messages may arrive: a job from the source, or the special message initiating job departure
  35. {  
  36.     if (msgin==departure)   //job departure
  37.  {
  38.   cMessage *msg = (cMessage *)queue.pop();    //remove the finished job from the head of the queue
  39.  
  40.   send(msg,"out");                            //depart the finished job
  41.   if (!queue.isEmpty())                         //if the queue is not empty, initiate the next service, i.e. schedule the next departure event in the future
  42.   {
  43.    departure_time=simTime()+par("service_time");
  44.          scheduleAt(departure_time,departure);
  45.   }
  46.  }
  47.  else if(queue.length()<n)                  //job arrival
  48.  {
  49.      acc++;
  50.   if (queue.isEmpty())
  51.   {
  52.    departure_time=simTime()+par("service_time");
  53.             scheduleAt(departure_time,departure);
  54.   }
  55.   queue.insert(msgin); //insert the job at the end of the queue
  56.  }
  57.  else
  58.  {
  59.      lost++;
  60.      delete msgin;
  61.  }
  62.     L=(double)lost/(acc+lost);
  63. }
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.