// // This file is part of an OMNeT++/OMNEST simulation test. // // Copyright (C) 1992-2015 Andras Varga // // This file is distributed WITHOUT ANY WARRANTY. See the file // `license' for details on this and other legal matters. // #include #include #include "stresssource.h" #include "stress_m.h" Define_Module(StressSource); std::vector sources; StressSource::StressSource() { timer = new cMessage("Source timer"); sources.push_back(this); } StressSource::~StressSource() { EV << "Cancelling and deleting self message: " << timer << "\n"; cancelAndDelete(timer); } void StressSource::initialize() { EV << "Sending self message for the first time: " << timer << "\n"; scheduleAt(par("serviceTime").doubleValue(), timer); } void StressSource::handleMessage(cMessage *msg) { if (msg == timer) // either send our own message or the message will be generated by the module actually sending it out msg = uniform(0, 1) < 0.5 ? generateMessage() : nullptr; else { EV << "Cancelling self message due to received direct message: " << timer << "\n"; cancelEvent(timer); } // randomly call a source (including ourself) sources.at(intrand(sources.size()))->sendOut(msg); // make sure our timer is always active if (!timer->isScheduled()) { EV << "Reusing self message: " << timer << "\n"; scheduleAt(simTime() + par("serviceTime"), timer); } } void StressSource::sendOut(cMessage *msg) { bool otherModule = this != getSimulation()->getContextModule(); Enter_Method("sendOut"); // send our own message if did not get one if (!msg) { msg = generateMessage(); msg->setName("Source's own"); } else msg->setName("Other source's"); cGate *outGate = gate("out", intrand(gateSize("out"))); if (outGate->getTransmissionChannel()->isBusy()) { EV << "Output channel is busy, dropping message: " << msg << "\n"; delete msg; } else { take(msg); send(msg, outGate); } if (otherModule) { // cancel context module's timer EV << "Cancelling self message due to method call: " << timer << "\n"; cancelEvent(timer); } // make sure context module's timer is always active EV << "Reusing self message: " << timer << "\n"; scheduleAt(simTime() + par("serviceTime"), timer); } cMessage *StressSource::generateMessage() { bubble("Generating new message"); StressPacket *packet = new StressPacket(); packet->setBitLength((long)exponential(par("messageLength").doubleValue())); return packet; }