Public Member Functions | |
Tic8 () | |
virtual | ~Tic8 () |
Protected Member Functions | |
virtual void | initialize () |
virtual void | handleMessage (cMessage *msg) |
Private Attributes | |
simtime_t | timeout |
cMessage * | timeoutEvent |
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.
Tic8::Tic8 | ( | ) |
00041 { 00042 timeoutEvent = NULL; 00043 }
Tic8::~Tic8 | ( | ) | [virtual] |
00046 { 00047 cancelAndDelete(timeoutEvent); 00048 }
void Tic8::handleMessage | ( | cMessage * | msg | ) | [protected, virtual] |
Reimplemented from cSimpleModule.
00064 { 00065 if (msg==timeoutEvent) 00066 { 00067 // If we receive the timeout event, that means the packet hasn't 00068 // arrived in time and we have to re-send it. 00069 EV << "Timeout expired, resending message and restarting timer\n"; 00070 cMessage *newMsg = new cMessage("tictocMsg"); 00071 send(newMsg, "out"); 00072 scheduleAt(simTime()+timeout, timeoutEvent); 00073 } 00074 else // message arrived 00075 { 00076 // Acknowledgement received -- delete the received message and cancel 00077 // the timeout event. 00078 EV << "Timer cancelled.\n"; 00079 cancelEvent(timeoutEvent); 00080 delete msg; 00081 00082 // Ready to send another one. 00083 cMessage *newMsg = new cMessage("tictocMsg"); 00084 send(newMsg, "out"); 00085 scheduleAt(simTime()+timeout, timeoutEvent); 00086 } 00087 }
void Tic8::initialize | ( | ) | [protected, virtual] |
Reimplemented from cComponent.
00051 { 00052 // Initialize variables. 00053 timeout = 1.0; 00054 timeoutEvent = new cMessage("timeoutEvent"); 00055 00056 // Generate and send initial message. 00057 EV << "Sending initial message\n"; 00058 cMessage *msg = new cMessage("tictocMsg"); 00059 send(msg, "out"); 00060 scheduleAt(simTime()+timeout, timeoutEvent); 00061 }
simtime_t Tic8::timeout [private] |
Referenced by handleMessage(), and initialize().
cMessage* Tic8::timeoutEvent [private] |
Referenced by handleMessage(), initialize(), Tic8(), and ~Tic8().