Data and communication

profilerplpatel22
mySource_v01.cc

// ========================================================================= // Filename: mySource.cc // // Purpose: // - GECE 586 Project 1 sample // - Implementation file for a simple node module, mySource, that will // generate packets with a sequence number and send it to its associated // node, i.e., Node A. // // Revision History // - File created: Oct. 23, 2019, W. Lee // // ========================================================================= #include <omnetpp.h> using namespace omnetpp; class mySource : public cSimpleModule { private: int seq; // message sequence number int counter; // number of messages to simulate cMessage *message; // message that has to be re-sent; see txc9.cc public: mySource(); virtual ~mySource(); protected: virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; virtual cMessage *generateNewMessage(); // see txc9.cc }; Define_Module(mySource); mySource::mySource() { message = nullptr; } mySource::~mySource() { delete message; } void mySource::initialize() { // Initialize variables seq = 1000; // Let's start with 1000 to use this also for packet name (rather than 0) counter = 100; // To examine the variable under Tkenv. After doing a few steps in the // simulation, double-click `gen_pkt', select the Contents tab in the // dialog that pops up,and you'll find "counter" in the list. WATCH(counter); // Generate and send the initial message. message = generateNewMessage(); send(message, "out"); // No need to keep a copy at the source; Node A will keep copies. EV << "counter = " << counter << endl; // to check (debug) EV << "The initial message " << message->getName() << " sent!" << endl; } void mySource::handleMessage(cMessage *msg) { // Decrement counter and check value. counter--; EV << "counter = " << counter << endl; // to check (debug) if (counter == 0) { // If counter is zero, Delete the message. // The simulation will stop at this point with the message // "no more events". EV << msg->getName() << "'s counter reached zero. \n"; delete msg; } else { msg = generateNewMessage(); send(msg, "out"); EV << " " << msg->getName() << " sent! \n"; } } cMessage *mySource::generateNewMessage() { // Generate a message with a different name every time. char msgname[20]; sprintf(msgname, "%d", ++seq); // Use seq. number for packet name. cMessage *msg = new cMessage(msgname); // Generate a message return msg; }