//// This file is part of an OMNeT++/OMNEST simulation example.//// Copyright (C) 2003-2015 Andras Varga//// This file is distributed WITHOUT ANY WARRANTY. See the file// `license' for details on this and other legal matters.//#include <cstdio>#include <cstring>#include <omnetpp.h>using namespace omnetpp;/** * Let us take a step back, and remove random delaying from the code. * We'll leave in, however, losing the packet with a small probability. * And, we'll we do something very common in telecommunication networks: * if the packet doesn't arrive within a certain period, we'll assume it * was lost and create another one. The timeout will be handled using * (what else?) a self-message. */class Tic8 : public cSimpleModule{ private: simtime_t timeout; // timeout cMessage *timeoutEvent = nullptr; // holds pointer to the timeout self-message public: virtual ~Tic8(); protected: virtual void initialize() override; virtual void handleMessage(cMessage *msg) override;};Define_Module(Tic8);Tic8::~Tic8(){ cancelAndDelete(timeoutEvent);}void Tic8::initialize(){ // Initialize variables. timeout = 1.0; timeoutEvent = new cMessage("timeoutEvent"); // Generate and send initial message. EV << "Sending initial message\n"; cMessage *msg = new cMessage("tictocMsg"); send(msg, "out"); scheduleAt(simTime()+timeout, timeoutEvent);}void Tic8::handleMessage(cMessage *msg){ if (msg == timeoutEvent) { // If we receive the timeout event, that means the packet hasn't // arrived in time and we have to re-send it. EV << "Timeout expired, resending message and restarting timer\n"; cMessage *newMsg = new cMessage("tictocMsg"); send(newMsg, "out"); scheduleAt(simTime()+timeout, timeoutEvent); } else { // message arrived // Acknowledgement received -- delete the received message and cancel // the timeout event. EV << "Timer cancelled.\n"; cancelEvent(timeoutEvent); delete msg; // Ready to send another one. cMessage *newMsg = new cMessage("tictocMsg"); send(newMsg, "out"); scheduleAt(simTime()+timeout, timeoutEvent); }}/** * Sends back an acknowledgement -- or not. */class Toc8 : public cSimpleModule{ protected: virtual void handleMessage(cMessage *msg) override;};Define_Module(Toc8);void Toc8::handleMessage(cMessage *msg){ if (uniform(0, 1) < 0.1) { // Probabilidad del 10% de perder el mensaje EV << "\"Losing\" message.\n"; bubble("message lost"); /// Muestra una burbuja de notificación en la GUI delete msg; } else { // Procesa el mensaje normalmente si no se pierde EV << "Sending back same message as acknowledgement.\n"; send(msg, "out"); }}