Mour_Ka
Mour_Ka

Reputation: 258

Omnet++ : How to sent to multiple hosts in inet

What if I want a host to send to several hosts but not all.

**.Host1.app1.destAddress = "6e:27:f5:71:ab:11"  
**.Host1.app2.destAddress = "6e:27:f5:71:ac:12"  
**.Host1.app3.destAddress = "6e:27:f5:71:ad:13"

I am trying out with a work around by editing the app module as per below

The code was

//omnetpp.ini    
**.Host1.app.destAddress = "6e:27:f5:71:ab:11"

void EtherTrafGen::handleMessage(cMessage *msg)
{
if (!isNodeUp())
        throw cRuntimeError("Application is not running");
    if (msg->isSelfMessage())
    {
       if (msg->getKind() == START)
         {
            destMACAddress = resolveDestMACAddress();
            if (destMACAddress.isUnspecified())
            return;
          }
        sendBurstPackets();
        scheduleNextPacket(simTime());
        }
    }
    else
        receivePacket(check_and_cast<cPacket *>(msg));
    }
}

MACAddress EtherTrafGen::resolveDestMACAddress()
{
    MACAddress destMACAddress;
    destAddress = par("destAddress");
    if (destAddress[0]) {
        if (!destMACAddress.tryParse(destAddress)) {
            cModule *destStation = getModuleByPath(destAddress);
            if (!destStation)
                throw cRuntimeError("...");
            cModule *destMAC = destStation->getSubmodule("mac");
            if (!destMAC)
                throw cRuntimeError("...", destAddress);
            destMACAddress.setAddress(destMAC->par("address"));
        }
    }
    return destMACAddress;
}

The changes I have done as follows

//omnetpp.ini
**.Host1.app.destAddresses = "6e:27:f5:71:ab:11,6e:27:f5:71:ac:12,6e:27:f5:71:ad:13"

class INET_API EtherTrafGen : public cSimpleModule, public ILifecycle
{
  public:
    const char *destAddress;
    bool multipacket;
    std::vector<std::string> v;
    unsigned int x;
}

void EtherTrafGen::handleMessage(cMessage *msg)
{
    multipacket = true;
    if (!isNodeUp())
        throw cRuntimeError("Application is not running");
    if (msg->isSelfMessage())
    {
        const char *destAddresses = par("destAddresses");
        if(multipacket)
        {
            std::vector<std::string> v = cStringTokenizer(destAddresses).asVector();
            for (x = 0; x <= v.size(); x++)
            {
                if (msg->getKind() == START)
                        {
                            destMACAddress = resolveDestMACAddress();
                            // if no dest address given, nothing to do
                            if (destMACAddress.isUnspecified())
                                return;
                        }
                        sendBurstPackets();
                        scheduleNextPacket(simTime());
            }
        }

        else if(!(multipacket))
        {
            if (msg->getKind() == START)
                    {
                        destMACAddress = resolveDestMACAddress();
                        // if no dest address given, nothing to do
                        if (destMACAddress.isUnspecified())
                            return;
                    }
                    sendBurstPackets();
                    scheduleNextPacket(simTime());
        }
    }
    else
        receivePacket(check_and_cast<cPacket *>(msg));
}

MACAddress EtherTrafGen::resolveDestMACAddress()
{
    MACAddress destMACAddress;

    if (multipacket)
    {destAddress = v[x].c_str();}
    else if (!multipacket)
    { destAddress = par("destAddress");}

    if (destAddress[0]) {
        if (!destMACAddress.tryParse(destAddress)) {
            cModule *destStation = getModuleByPath(destAddress);
            if (!destStation)
                throw cRuntimeError("...");

            cModule *destMAC = destStation->getSubmodule("mac");
            if (!destMAC)
                throw cRuntimeError("...", destAddress);
            destMACAddress.setAddress(destMAC->par("address"));
        }
    }
    return destMACAddress;
}

The result is that it works fine if the bool multiaddress is false as the previous case but doesn't work if it is true but no error as well. As if it is overridden with an empty address.

"I am so sorry for the long code, but I am afraid to miss function that might be related to what I am doing and I am not noticing"

Upvotes: 0

Views: 723

Answers (1)

Jerzy D.
Jerzy D.

Reputation: 7002

You are using a comma as a delimiter in destAddresses, while a whitespace (i.e. space, tab, CR, LF) is the default delimiter in cStringTokenizer. Therefore you should inform parser from cStringTokenizer that the comma is the separator, by changing the line:

std::vector<std::string> v = cStringTokenizer(destAddresses).asVector();

into:

v = cStringTokenizer(destAddresses, ",").asVector();

The declaration of v has been omitted because you probably intend using v from your class (in C++ any local variable will cover a variable with the same name from class).

Upvotes: 1

Related Questions