Commit 53e17eab authored by Samir Medjiah's avatar Samir Medjiah
Browse files

- Code refactoring & enhancing

- Enahnced network routing
- Added : the support of target URI for oneM2M messages
- Fixed : CPU & RAM management (single threaded)
parent 4ce2671b
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -8,8 +8,9 @@ Define_Module(Sensor);

void Sensor::initialize()
{
    name = par("name").stdstringValue();
    protocol = Protocol::StrToInt(getParentModule()->par("protocol").stdstringValue());
    this->name = par("name").stdstringValue();
    this->baseURL = par("baseURL").stdstringValue();
    this->protocol = Protocol::StrToInt(getParentModule()->par("protocol").stdstringValue());

    json objParam = SensorDeserializer::parseInput(par("cinGenerator").stdstringValue());
    cinGenerator = SensorDeserializer::deserializeEventGenerator(objParam);
@@ -33,13 +34,13 @@ void Sensor::handleMessage(cMessage *msg)
    {
        OneM2MPrimitive* request = new OneM2MPrimitive("CREATE CIN", PacketKind::ONEM2M);
        request->setIsRequest(true);
        request->setOperation(Operation::_CREATE);
        request->setOperation(OperationType::_CREATE);
        char cinName[10];
        sprintf(cinName, "cin-%05d", this->cinID++);
        request->setMessageID(this->cinID);
        request->setResourceName(cinName);
        request->setResourceType(ResourceType::CIN);
        request->setUri(string("mn-cse/"+name+"/DATA").c_str());
        request->setUri(string(baseURL + "/" + name + "/DATA").c_str());

        request->setSource(this->name.c_str());
        request->setDestination(getParentModule()->par("remoteCSE").stringValue());
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ class Sensor : public cSimpleModule
{
  private:
     string name;
     string baseURL;
     int protocol;
     EventGenerator* cinGenerator;
     int cinID = 0;
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ simple Sensor
{
    parameters:
        string name;
        string baseURL;
        string cinGenerator = default("");
        
        @display("i=block/tx");
+71 −0
Original line number Diff line number Diff line
@@ -6,9 +6,14 @@

Define_Module(Internet);

const int LINK_OUTPUT = 1;
const int NOROUTE_LINK =2;

void Internet::initialize()
{
    this->gateIndex = new map<string, int>();
    this->portQueue = new map<int, cPacketQueue*>();
    this->portScheduler = new map<int, cMessage*>();

    int linkCount = gateSize("link$i");
    for (int i=0; i<linkCount; i++) {
@@ -16,12 +21,78 @@ void Internet::initialize()
        cModule* networkElement = prevGate->getOwnerModule();
        string subnet = networkElement->par("subnetAddress").stdstringValue();
        this->gateIndex->insert(pair<string, int>(subnet, i));
        this->portQueue->insert(pair<int, cPacketQueue*>(i, new cPacketQueue("PacketQueue")));
        cMessage* aTimer = new cMessage("timer", PacketKind::TIMER);
        aTimer->addPar("gateIndex").setLongValue(i);
        this->portScheduler->insert(pair<int, cMessage*>(i, aTimer));

        EV << subnet << " at gate idx = " << i << '\n';
        cout << subnet << " at gate idx = " << i << '\n';

    }
}

void Internet::handleMessage(cMessage *msg)
{
    if (msg->isName("timer")) {
        long gateIndex = msg->par("gateIndex").longValue();

        NetworkPacket* pk = (NetworkPacket*)portQueue->at(gateIndex)->pop();
        string gateName = pk->par("gateName").longValue() == LINK_OUTPUT ? "link$o" : "NoRoute$o";
        send(pk, gateName.c_str(), gateIndex);

        if (portQueue->at(gateIndex)->getLength()>0) {
            simtime_t targetDate = gate(gateName.c_str(), gateIndex)->getTransmissionChannel()->getTransmissionFinishTime();
            scheduleAt(targetDate, msg);
        }
    }
    else
    {
        NetworkPacket* netPacket = dynamic_cast<NetworkPacket*>(msg);
        string destIP = netPacket->getDestAddress();
        int idx = RoutingTools::getNetworkGateIndex(destIP, gateIndex);
        if (idx != -1){
            simtime_t targetDate = gate("link$o", idx)->getTransmissionChannel()->getTransmissionFinishTime();
            if (targetDate <= simTime()){
                send(netPacket, "link$o", idx);
            } else {
                netPacket->addPar("gateName").setLongValue(LINK_OUTPUT);
                netPacket->addPar("gateIndex").setLongValue(idx);

                portQueue->at(idx)->insert(netPacket);
                cMessage* timer = portScheduler->at(idx);
                if (!timer->isScheduled())
                    scheduleAt(targetDate, timer);
            }
        }
        else
            if (msg->arrivedOn("link$i")) {
                simtime_t targetDate = gate("NoRoute$o")->getTransmissionChannel()->getTransmissionFinishTime();
                if (targetDate <= simTime()){
                    send(netPacket, "NoRoute$o");
                } else {
                    netPacket->addPar("gateName").setLongValue(NOROUTE_LINK);
                    netPacket->addPar("gateIndex").setLongValue(0);
                    portQueue->at(0)->insert(netPacket);
                    cMessage* timer = portScheduler->at(0);
                    if (!timer->isScheduled())
                        scheduleAt(targetDate, timer);
                }
            }
    }
}
Internet::~Internet(){
    for (auto queue = this->portQueue->begin(); queue != this->portQueue->end(); ++queue){
        while(queue->second->getLength()>0) {
            cPacket* pkt = queue->second->pop();
            cancelAndDelete(pkt);
        }
        delete queue->second;
    }
    delete portQueue;

    for (auto scheduler = this->portScheduler->begin(); scheduler != this->portScheduler->end(); ++scheduler){
        cancelAndDelete(scheduler->second);
    }
    delete portScheduler;
}
+8 −0
Original line number Diff line number Diff line
@@ -7,17 +7,25 @@

#include <omnetpp.h>

#include "../../../lib/constants.hpp"
#include "../../../lib/routing.hpp"
#include "../../../messages/NetworkPacket_m.h"

using namespace omnetpp;
using namespace std;


class Internet : public cSimpleModule
{
  public:
    ~Internet();
  protected:
    virtual void initialize() override;
    virtual void handleMessage(cMessage *msg) override;

    map<string, int> * gateIndex;
    map<int, cPacketQueue*>* portQueue;
    map<int, cMessage*>* portScheduler;
};

#endif
Loading