【发布时间】:2017-01-19 12:00:00
【问题描述】:
如果我想让一个主机发送给多个主机但不是全部。
**.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"
我正在尝试通过如下编辑应用程序模块来解决问题
代码是
//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 为 false 则它工作正常,但如果它为 true 但也没有错误,则它不起作用。就好像它被一个空地址覆盖一样。
“我很抱歉代码太长,但我害怕错过可能与我正在做的事情有关的功能,我没有注意到”
【问题讨论】: