Omnet ++:如何发送到inet中的多个主机

时间:2017-01-19 12:00:01

标签: omnet++ inet

如果我希望主机发送给多个主机但不是全部主机,该怎么办。

**.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"

我正在尝试通过编辑app模块来解决这个问题

代码是

//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;
}

我所做的更改如下

//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;
}

结果是如果bool multiaddress与前一种情况一样假,它可以正常工作,但如果它是真的但没有错误也不起作用。好像它被一个空地址覆盖了。

&#34;我很抱歉长码,但我害怕错过可能与我正在做的事情有关的功能而且我没注意到&#34;

1 个答案:

答案 0 :(得分:1)

您在destAddresses中使用逗号作为分隔符,而空格(即空格,制表符,CR,LF)是cStringTokenizer中的默认分隔符。 因此,您应该通过更改以下行来告知来自cStringTokenizer的解析器,逗号是分隔符:

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

成:

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

v的声明已被省略,因为您可能打算在您的类中使用v(在C ++中,任何局部变量都将覆盖类中具有相同名称的变量)。