Skip to content

Instantly share code, notes, and snippets.

@elkorn
Created April 11, 2013 09:27
Show Gist options
  • Save elkorn/5361980 to your computer and use it in GitHub Desktop.
Save elkorn/5361980 to your computer and use it in GitHub Desktop.
#include <omnetpp.h>
class Server : public cSimpleModule
{
private:
cQueue queue; //the queue; first job in the queue is being serviced
cMessage *departure; //message-reminder of the end of service (job departure)
simtime_t departure_time; //time of the next departure
cOutVector *proces;
cMessage *pomiar;
cMessage *pomiarCzasu;
simtime_t T;
cLongHistogram *rozklad_kolejki;
cLongHistogram *rozklad_kolejki_naplywajace;
cDoubleHistogram *rozklad_wirtualnego_oczekiwania;
protected:
virtual void initialize();
virtual void handleMessage(cMessage *msgin);
};
Define_Module(Server);
void Server::initialize()
{
departure = new cMessage("Departure");
pomiarCzasu = new cMessage("Pomiar wirtualnego czasu oczekiwania");
proces = new cOutVector("PROCES KOLEJKI");
pomiar = new cMessage("POMIAR");
T = 1;
scheduleAt(T, pomiar);
scheduleAt(T, pomiarCzasu);
rozklad_kolejki = new cLongHistogram("ROZKLAD KOLEJKI");
rozklad_kolejki_naplywajace = new cLongHistogram("ROZKLAD KOLEJKI DLA NAPLYWAJACYCH ZADAN");
rozklad_wirtualnego_oczekiwania = new cDoubleHistogram("ROZKLAD WIRTUALNEGO CZASU OCZEKIWANIA");
rozklad_wirtualnego_oczekiwania->setRange(0, 10);
rozklad_wirtualnego_oczekiwania->setNumCells(100);
}
void Server::handleMessage(cMessage *msgin)
{
if(msgin == pomiar)
{
// wykonywanie pomiaru
rozklad_kolejki->collect(queue.length());
if(!queue.empty())
{
rozklad_wirtualnego_oczekiwania->collect(simTime() - ((cMessage *)queue.front())->getArrivalTime());
}
scheduleAt(simTime() + T, pomiar);
}
else
{
if (msgin==departure) //job departure
{
cMessage *msg = (cMessage *)queue.pop(); //remove job from the head of the queue
proces->record(queue.length());
send(msg,"out");
if (!queue.empty()) //schedule next departure event
{
departure_time=simTime()+par("service_time");
scheduleAt(departure_time,departure);
}
}
else //job arrival
{
rozklad_kolejki_naplywajace->collect(queue.length());
if (queue.empty())
{
departure_time=simTime()+par("service_time");
scheduleAt(departure_time,departure);
}
queue.insert(msgin); //put job at the end of the queue
proces->record(queue.length());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment