#include //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 int bufforSize; int lostPackages = 0; int packages = 0; int lostPackegesOneByOne = 0; simtime_t overflowTime; cDoubleHistogram hisOverflowTime; cLongHistogram hisOverflowTimeOneByOne; protected: virtual void initialize(); virtual void handleMessage(cMessage *msgin); }; Define_Module(Server); void Server::initialize() { departure = new cMessage("Departure"); bufforSize = par("buffor_size"); //hisOverflowTime.setRange(0, 2.5); } 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"); if(queue.length() == bufforSize - 1){ hisOverflowTime.collect(simTime() - overflowTime); if(lostPackegesOneByOne != 0){ hisOverflowTimeOneByOne.collect(lostPackegesOneByOne); } } 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 //job arrival { packages++; 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 { departure_time=simTime()+par("service_time"); scheduleAt(departure_time,departure); } if(queue.length() < bufforSize){ queue.insert(msgin); //insert the job at the end of the queue if(queue.length() == bufforSize){ overflowTime = simTime(); } else{ lostPackeges } }else{ lostPackages++; delete msgin; lostPackegesOneByOne++; } double L = (lostPackages * 1.0) / packages; ev << "L = " << L; } }