#include <omnetpp.h> //using namespace omnetpp; //for Omnet++ ver. 5 class Server : public cSimpleModule { private: cQueue queue; //the queue of jobs; it is assumed that the first job in the queue is the one being serviced cMessage *departure; //special message; it reminds about the end of service and the need for job departure simtime_t departure_time; //time of the next departure double L; int n; int acc,lost; protected: virtual void initialize(); virtual void handleMessage(cMessage *msgin); }; Define_Module(Server); void Server::initialize() { departure = new cMessage("Departure"); acc=lost=0; L=0; WATCH(L); n=par("n"); } void Server::handleMessage(cMessage *msgin) //two types of messages may arrive: a job from the source, or the special message initiating job departure { if (msgin==departure) //job departure { cMessage *msg = (cMessage *)queue.pop(); //remove the finished job from the head of the queue send(msg,"out"); //depart the finished job if (!queue.isEmpty()) //if the queue is not empty, initiate the next service, i.e. schedule the next departure event in the future { departure_time=simTime()+par("service_time"); scheduleAt(departure_time,departure); } } else if(queue.length()<n) //job arrival { acc++; if (queue.isEmpty()) { departure_time=simTime()+par("service_time"); scheduleAt(departure_time,departure); } queue.insert(msgin); //insert the job at the end of the queue } else { lost++; delete msgin; } L=(double)lost/(acc+lost); }