2015-10-17 67 views
-1

我使用的是Omnet ++ 4.6,我創建了一個類來繼承AODVRouting。現在,我在我的新類中創建了一個覆蓋父類的handleMessage()的函數。編譯器指示該函數確實被覆蓋。我鍵入一個EV<<命令將該函數的開頭打印到事件日誌,但它不打印到事件日誌。問題是什麼??覆蓋功能不執行EV <<命令?

父類中的函數是虛擬的並受保護。這是我的繼承class.cc:

// 
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU Lesser General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
// 
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
// GNU Lesser General Public License for more details. 
// 
// You should have received a copy of the GNU Lesser General Public License 
// along with this program. If not, see http://www.gnu.org/licenses/. 
// 


#include "MalAODVRouter.h" 
#include "IPv4ControlInfo.h" 

Define_Module(MalAODVRouter); 
MalAODVRouter::MalAODVRouter() 
{ 
    AODVRouting::AODVRouting(); 
} 
void MalAODVRouter::initialize(int stage) 
{ 
    AODVRouting::initialize(stage); 
} 

void MalAODVRouter::handleMessage(cMessage *msg) 
{ 
    std::cout<<"Mal Host Activity"<<endl; 
    EV <<"Mal Host Activity \n"; 
    this->bubble("Mal Host Activity"); 


    //capturedMsgs++; 
    //if (capturedMsgs==1) // One out of every 10 packets (frequency of replay) 
    //{ 
     cMessage *ReplayMsg = msg->dup(); 
     std::cout<<"Done Duplicating MSG"<<endl; 
     EV<<"Done Duplicating MSG \n"; 

     /*UDPPacket *udpPacket = dynamic_cast<UDPPacket *>(msg); 
     AODVControlPacket *ctrlPacket = check_and_cast<AODVControlPacket *>(udpPacket->decapsulate()); 
     IPv4ControlInfo *udpProtocolCtrlInfo = dynamic_cast<IPv4ControlInfo *>(udpPacket->getControlInfo()); 
     ASSERT(udpProtocolCtrlInfo != NULL); 
     IPv4Address sourceAddr = udpProtocolCtrlInfo->getSrcAddr();   //get Source Address 
     IPv4Address destinationAddr = udpProtocolCtrlInfo->getDestAddr(); //get Destination Address 
     IPv4Address addr = getSelfIPAddress(); 
     if (addr != destinationAddr)  // if it is not destined for "Eve" 
     { 
      UDPPacket *ReplayUDPPacket = udpPacket; 
      AODVControlPacket *ReplayCtrlPacket = check_and_cast<AODVControlPacket *>(ReplayUDPPacket->decapsulate()); 
      IPv4ControlInfo *ReplayUDPProtocolCtrlInfo = dynamic_cast<IPv4ControlInfo *>(ReplayUDPPacket->getControlInfo()); 
      ASSERT(ReplayUDPProtocolCtrlInfo != NULL); 
      ReplayUDPProtocolCtrlInfo->setSrcAddr(sourceAddr);   //Forge Source 
      ReplayUDPProtocolCtrlInfo->setDestAddr(destinationAddr); //Keep Destination 

*/ 

      //we can add a delay before sending the copy of the message again (10 time units) 
     scheduleAt(simTime() + 1, ReplayMsg); 
     //sendDelayed(ReplayMsg, 0.1,"ipOut"); 
     ReplayedMsgs++; 
     std::cout<<"Launched Replay Packet!\n"; 
     EV<<"Launched Replay Packet!\n"; 
     this->bubble("Attack"); 

      //this->capturedMsgs=0; 
     // } 
    //} 
    AODVRouting::handleMessage(msg); 
    std::cout<<"Finished handling msg"<<endl; 
    EV<<"Finished handling msg"<<endl; 

} 

/*void MalAODVRouter::finish() 
{ 

    recordScalar("captured Msgs", capturedMsgs); 
    recordScalar("Replayed Msgs", ReplayedMsgs); 

    }*/ 


MalAODVRouter::~MalAODVRouter() 
{ 
    AODVRouting::~AODVRouting(); 
} 

,這是我的.h文件:

// 
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU Lesser General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
// 
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
// GNU Lesser General Public License for more details. 
// 
// You should have received a copy of the GNU Lesser General Public License 
// along with this program. If not, see http://www.gnu.org/licenses/. 
// 

#ifndef __COPS_MALAODVROUTER_H_ 
#define __COPS_MALAODVROUTER_H_ 
#include "AODVRouting.h" 
#include <omnetpp.h> 


/** 
* TODO - Generated class 
*/ 

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

public: 
    MalAODVRouter(); 
    //finish(); 
    ~MalAODVRouter(); 
    int capturedMsgs=0; 
    int ReplayedMsgs=0; 

}; 

#endif 

回答

1

爲了查看由EV生成自己的消息,你需要切換日誌查看器「模塊的輸出」模式。開始模擬,並在右下角窗口中按最右邊的圖標。

+0

我這樣做,這不是我意思。由於某種原因,我傾向於認爲我的重寫函數沒有被調用......只有父類函數被調用:( –

0
  • 確保類已註冊了宏>> Define_Module(....); (好像做)
  • 確保類鏈接>>你應該能夠通過與「-H 類」選項運行它所有註冊類在你的模擬得到的 的完整列表
  • 驗證在NED文件名是一樣的配置文件抄送
  • 嘗試項目的文件夾,在這個右鍵 屬性>> OMNET ++ >> >>鳥神星應用