You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何扩展VeinsInetSampleApplication实现多节点并发收发报文的可扩展方案

VeinsInetSampleApplication 多节点并发发送实现方案

之前操作未生效的原因

原VeinsInetSampleApplication代码中,不仅发送报文的逻辑包裹在node[0]的判断条件中,发送定时器的初始化逻辑也在同一个条件分支内,仅注释第46行的报文发送if条件,其余节点根本不会启动发送定时器,自然不会产生发送行为。

可扩展实现步骤

该方案支持任意数量发送端向任意数量接收端并发发送多份报文,无需修改代码仅调整配置即可适配不同场景:

1. 扩展头文件定义计数变量

在VeinsInetSampleApplication.h的私有成员中新增报文计数变量,用于记录向不同目标发送的报文数量:

private:
    std::map<std::string, int> sentCount;

2. 扩展NED文件定义可配置参数

在VeinsInetSampleApplication.ned中新增如下参数,实现收发角色、目标地址、发送参数的灵活配置:

bool isSender = default(false); // 标记当前节点是否为发送端
bool isReceiver = default(false); // 标记当前节点是否为接收端
string destAddresses = default(""); // 多个目标节点地址用空格分隔
double sendInterval = default(1s); // 报文发送时间间隔
int packetSize = default(1000B); // 单报文大小
int numPacketsPerDest = default(10); // 向每个目标发送的总报文数

3. 修改初始化逻辑

替换原有仅node[0]启动定时器的硬编码逻辑,所有标记为发送端的节点都自动启动发送定时器:

void VeinsInetSampleApplication::initialize(int stage)
{
    VeinsInetApplicationBase::initialize(stage);
    if (stage == INITSTAGE_LOCAL) {
        // 保留原有其他初始化逻辑
        sentCount.clear();
        if (par("isSender").boolValue()) {
            scheduleAt(simTime() + par("startTime").doubleValue(), sendTimer);
        }
    }
}

4. 实现多目标并发发送逻辑

定时器触发时解析所有目标地址,向每个目标独立生成并发送报文,支持按设定间隔持续发送:

void VeinsInetSampleApplication::handleSelfMessage(cMessage *msg)
{
    if (msg == sendTimer) {
        // 解析目标地址列表
        std::string destStr = par("destAddresses").stringValue();
        cStringTokenizer tokenizer(destStr.c_str(), " ");
        std::vector<std::string> destAddrs = tokenizer.asVector();
        
        // 遍历所有目标发送报文
        for (const auto& addr : destAddrs) {
            if (sentCount[addr] < par("numPacketsPerDest").intValue()) {
                auto packet = createPacket();
                packet->setByteLength(par("packetSize").intValue());
                sendPacket(packet, addr.c_str());
                sentCount[addr]++;
            }
        }
        
        // 还有未发送完成的报文则重启定时器
        bool hasMore = false;
        for (const auto& addr : destAddrs) {
            if (sentCount[addr] < par("numPacketsPerDest").intValue()) {
                hasMore = true;
                break;
            }
        }
        if (hasMore) {
            scheduleAt(simTime() + par("sendInterval").doubleValue(), sendTimer);
        }
    }
}

5. 仿真配置示例

要实现node[0]、node[1]并发向node[2]发送报文,仅需在omnetpp.ini中添加如下配置即可,无需修改代码:

*.node[*].app[0].typename = "VeinsInetSampleApplication"
*.node[0].app[0].isSender = true
*.node[0].app[0].destAddresses = "node[2]"
*.node[0].app[0].sendInterval = 0.5s
*.node[1].app[0].isSender = true
*.node[1].app[0].destAddresses = "node[2]"
*.node[1].app[0].sendInterval = 0.5s
*.node[2].app[0].isReceiver = true

如果需要支持多接收端场景,比如node[0]同时向node[2]和node[3]发送报文,仅需修改对应配置:
*.node[0].app[0].destAddresses = "node[2] node[3]"


内容的提问来源于stack exchange,提问作者Mohanned Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 20:06:10