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

如何在OMNeT++ 6.0.1(INET 4.4.1)中实现无线广播信标及回复

OMNeT++ 信标广播与回复功能实现求助

我是OMNeT新手,基于AODV架构创建了继承自AdhocHost的LocalisationNode模块,使用OMNeT 6.0.1和INET Framework 4.4.1。想要实现以下功能:

  • 仿真启动时,节点自动发送广播信标
  • 收到其他节点的信标后,回复"received"消息

以下是我已编写的代码:

LocalisationNode.ned 代码

package inet.node.localisation;
import inet.node.inet.AdhocHost;
import inet.routing.localisation.Localisation;

module LocalisationNode extends AdhocHost
{
    submodules:
        localisation: Localisation {
            @display("p=825,226");
        }
    connections:
        localisation.socketOut --> at.in++;
        localisation.socketIn <-- at.out++;
}

Localisation.h 代码

#ifndef __INET_LOCALISATION_H
#define __INET_LOCALISATION_H

namespace omnetpp{
namespace inet {

class Localisation : public cSimpleModule{
protected:
  virtual void initialize(int stage) override;
  virtual void handleMessage(cMessage *msg) override;

public:
  Localisation();
  virtual ~Localisation();
};

} /* namespace inet */
}
#endif /* INET_MYPROTOCOL_MYPROTOCOL_H_ */

Localisation.cc 代码

#include "inet/routing/localisation/Localisation.h"

namespace omnetpp{
namespace inet {

Define_Module(Localisation);
void Localisation::initialize(int stage)
{
 
}

void Localisation::handleMessage(cMessage *msg)
{
 
}

Localisation::Localisation() {
    // TODO Auto-generated constructor stub

}

Localisation::~Localisation() {
    // TODO Auto-generated destructor stub
}

} }/* namespace inet */

Localisation.ned 代码

package inet.routing.localisation;
import inet.applications.contract.IApp;
module Localisation like IApp
{
    @class("Localisation");
    @display("i=block/network2");
    gates:
    input socketIn;
    output socketOut;
}

实现方案

要完成信标广播和回复功能,需要借助INET的UdpSocket处理UDP通信,以下是修改后的代码:

修改后的 Localisation.h

#ifndef __INET_LOCALISATION_H
#define __INET_LOCALISATION_H

#include "inet/transportlayer/contract/udp/UdpSocket.h"

namespace omnetpp{
namespace inet {

class Localisation : public cSimpleModule, public UdpSocket::ICallback
{
protected:
    UdpSocket socket;
    int beaconPort = 1234; // 自定义信标端口

    virtual void initialize(int stage) override;
    virtual void handleMessage(cMessage *msg) override;

    // UdpSocket回调函数
    virtual void socketDataArrived(UdpSocket *socket, Packet *packet) override;
    virtual void socketErrorArrived(UdpSocket *socket, Indication *indication) override;
    virtual void socketClosed(UdpSocket *socket) override;

public:
    Localisation();
    virtual ~Localisation();
};

} /* namespace inet */
}
#endif /* INET_LOCALISATION_H_ */

修改后的 Localisation.cc

#include "inet/routing/localisation/Localisation.h"
#include "inet/common/packet/Packet.h"
#include "inet/common/ByteArray.h"
#include "inet/networklayer/common/L3AddressResolver.h"

namespace omnetpp{
namespace inet {

Define_Module(Localisation);

Localisation::Localisation() {
}

Localisation::~Localisation() {
}

void Localisation::initialize(int stage)
{
    cSimpleModule::initialize(stage);
    if (stage == INITSTAGE_APPLICATION_LAYER) {
        // 初始化UDP socket
        socket.setOutputGate(gate("socketOut"));
        socket.bind(beaconPort);
        socket.setCallback(this);

        // 发送广播信标
        const char* beaconMsg = "Beacon from node ";
        std::string fullMsg = beaconMsg + std::string(getParentModule()->getName());
        auto packet = new Packet("BeaconPacket");
        auto byteArray = makeShared<ByteArray>();
        byteArray->setBytes(fullMsg.c_str(), fullMsg.length());
        packet->insertAtBack(byteArray);

        // 获取广播地址
        L3Address broadcastAddr = L3AddressResolver().resolve("255.255.255.255");
        socket.sendTo(packet, broadcastAddr, beaconPort);
        EV_INFO << "已发送广播信标: " << fullMsg << endl;
    }
}

void Localisation::handleMessage(cMessage *msg)
{
    socket.processMessage(msg);
}

void Localisation::socketDataArrived(UdpSocket *socket, Packet *packet)
{
    // 解析收到的信标消息
    auto byteArray = packet->peekAtBack<ByteArray>();
    std::string receivedMsg(byteArray->getBytes(), byteArray->getLength());
    EV_INFO << "收到信标: " << receivedMsg << endl;

    // 回复"received"消息
    auto replyPacket = new Packet("ReplyPacket");
    auto replyData = makeShared<ByteArray>();
    std::string replyMsg = "received from " + std::string(getParentModule()->getName());
    replyData->setBytes(replyMsg.c_str(), replyMsg.length());
    replyPacket->insertAtBack(replyData);

    // 获取发送方地址和端口
    L3Address srcAddr = packet->getTag<L3AddressInd>()->getSrcAddress();
    int srcPort = packet->getTag<TransportProtocolInd>()->getSrcPort();
    socket->sendTo(replyPacket, srcAddr, srcPort);
    EV_INFO << "已回复消息: " << replyMsg << endl;

    delete packet;
}

void Localisation::socketErrorArrived(UdpSocket *socket, Indication *indication)
{
    EV_WARN << "Socket error: " << indication->getName() << endl;
    delete indication;
}

void Localisation::socketClosed(UdpSocket *socket)
{
    EV_INFO << "Socket closed" << endl;
}

} }/* namespace inet */

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 19:23:15