Omnet++:如何发送到 inet 中的多个主机
Omnet++ : How to sent to multiple hosts in 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"
我正在尝试按照以下方式编辑应用程序模块
密码是
//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 多地址为 false,它工作正常,但如果它为 true,则不起作用,但也没有错误。好像它被一个空地址覆盖了。
"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"
您在 destAddresses
中使用逗号作为分隔符,而 [=13] 中的默认分隔符是白色 space(即 space、制表符、CR、LF) =].
因此,您应该从 cStringTokenizer
通知解析器逗号是分隔符,方法是更改行:
std::vector<std::string> v = cStringTokenizer(destAddresses).asVector();
进入:
v = cStringTokenizer(destAddresses, ",").asVector();
v
的声明已被省略,因为您可能打算使用 class 中的 v
(在 C++ 中,任何局部变量都将覆盖 class).
如果我想让一个主机发送给多个主机而不是所有主机怎么办?
**.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 多地址为 false,它工作正常,但如果它为 true,则不起作用,但也没有错误。好像它被一个空地址覆盖了。
"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"
您在 destAddresses
中使用逗号作为分隔符,而 [=13] 中的默认分隔符是白色 space(即 space、制表符、CR、LF) =].
因此,您应该从 cStringTokenizer
通知解析器逗号是分隔符,方法是更改行:
std::vector<std::string> v = cStringTokenizer(destAddresses).asVector();
进入:
v = cStringTokenizer(destAddresses, ",").asVector();
v
的声明已被省略,因为您可能打算使用 class 中的 v
(在 C++ 中,任何局部变量都将覆盖 class).