Data and communication

profilerplpatel22
Node_w_Packets_v01.cc

// ========================================================================= // Filename: Node_w_Packets.cc // // Purpose: // - GECE 586 Project 1 sample // - Implementation file for a simple node module, Node_w_Packets, that will // received a packet, make a copy of it for a later retransmission if // necessary, and transmit the packet to its destination node. // // Revision History // - File created: Oct. 23, 2019, W. Lee // // ========================================================================= #include <string.h> #include <omnetpp.h> // To convert a packet name to a packet number--------------------------------- #include <stdio.h> // Use this for Method A below //#include <sstream> // Use this for Method B below // ---------------------------------------------------------------------------- using namespace omnetpp; class Node_w_Packets: public cSimpleModule { private: int Pkt_Num; // Packet number extracted from the message const char * Pkt_Name; // Packet name (Note: msg->getName() returns const char *) protected: // The following redefined virtual function holds the algorithm. virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; virtual void sendCopyOf(cMessage *msg); // see txc9.cc }; // The module class needs to be registered with OMNeT++ Define_Module(Node_w_Packets); void Node_w_Packets::initialize() { // Do nothing for now. } void Node_w_Packets::handleMessage(cMessage *msg) { // The handleMessage() method is called whenever a message arrives at // the module. A copy of the message is sent out to the destination. EV << "Received " << msg->getName() << endl; sendCopyOf(msg); EV << " ==> Sending it out '" << msg->getName() << "' \n"; // Convert Pkt_Name (string) to Pkt_Num (integer) ========================= Pkt_Name = msg->getName(); // Method A: -------------------------------------------------------------- sscanf(msg->getName(), "%d", &Pkt_Num); // Method B: -------------------------------------------------------------- //std::stringstream geek(Pkt_Name); // object from the class stringstream //geek >> Pkt_Num; // stream the object value to the integer Pkt_Num // ------------------------------------------------------------------------ EV << " ==> Packet name (i.e., \"" << Pkt_Name << "\")'s Pkt_Num is " << Pkt_Num << "\n"; // ======================================================================== } void Node_w_Packets::sendCopyOf(cMessage *msg) { // Duplicate message and send the copy. cMessage *copy = (cMessage *)msg->dup(); send(copy, "out"); }