📄 rip.cc~
字号:
/*********************************************************************************************************************(c) 2008 ESL of CCNU-------------------------------------------------------------------------------File name: rip.ccVersion: 0.1Author: Bisu[Shield]Date: 2008-04-16 17:21:30Discription: This is the SOURCE CODE of RIP routing protocol The orignal version!**********************************************************************************************************************/#include<omnetpp.h>#include<stdio.h>#include<string.h>#define RIP_UPDATE_TIME 30#define RIP_GLOBAL_CLEAR_TIME 600#include"rip_m.h"#define RIP_ROUTE_TABLE_SIZE 100typedef struct RIP_Cell_msg { int destAdd; int nextHop; int hopCount;}RIP_Cell; class RIP : public cSimpleModule{ private: cMessage *timeoutEvent;//check table event cMessage *message;//message to be sent when timeout cMessage *clearEvent;//clear everyone's route table cMessage *message_1;//message to be send when clear int num; int routeCount; RIP_Cell RIP_Table[RIP_ROUTE_TABLE_SIZE]; public: RIP(); virtual ~RIP(); void updateRouteTable(update *msg); void sendRouteTable(); void showRouteTable(); void clearRouteTable(); protected: virtual void initialize(); virtual void handleMessage(cMessage *msg);};//hook RIP to OMNeT++3.3Define_Module(RIP);RIP::RIP(){ timeoutEvent=message=NULL; clearEvent=message_1=NULL;}RIP::~RIP(){ cancelAndDelete(timeoutEvent); delete message; cancelAndDelete(clearEvent); delete message_1;}void RIP::initialize(){
ev<<"My id is "<<id()<<endl; ev<<"My name is "<<name()<<endl; num = id(); RIP_Table[0].destAdd = num; RIP_Table[0].nextHop = 0; RIP_Table[0].hopCount = 0; for(int i=1;i<RIP_ROUTE_TABLE_SIZE;i++) { RIP_Table[i].destAdd = 0; RIP_Table[i].nextHop = 0; RIP_Table[i].hopCount = 0; } routeCount = 1; timeoutEvent = new cMessage("timeoutEvent"); double y = uniform(0,1); double x = y+RIP_UPDATE_TIME; scheduleAt(simTime()+x, timeoutEvent); clearEvent = new cMessage("clearEvent"); scheduleAt(simTime()+RIP_GLOBAL_CLEAR_TIME,clearEvent);}void RIP::handleMessage(cMessage *msg){
if(msg == timeoutEvent) { bubble("I send my route table to my neighbor!"); delete msg; timeoutEvent = new cMessage("timeoutEvent"); double y = uniform(0,1); double x = y+RIP_UPDATE_TIME; scheduleAt(simTime()+x, timeoutEvent); sendRouteTable(); } else if(msg == clearEvent) { bubble("Clear my route table NOW!"); delete msg; clearEvent = new cMessage("clearEvent"); scheduleAt(simTime()+RIP_GLOBAL_CLEAR_TIME,clearEvent); clearRouteTable(); } //handing the update messages else { update *ttmsg = check_and_cast<update *>(msg); updateRouteTable(ttmsg); delete ttmsg; bubble("Updating my route table NOW!"); showRouteTable(); }}void RIP::clearRouteTable(){ RIP_Table[0].destAdd = num; RIP_Table[0].nextHop = 0; RIP_Table[0].hopCount = 0; for(int i=1;i<RIP_ROUTE_TABLE_SIZE;i++) { RIP_Table[i].destAdd = 0; RIP_Table[i].nextHop = 0; RIP_Table[i].hopCount = 0; } routeCount = 1; }void RIP::updateRouteTable(update *msg){ int n = msg->getCount(); for(int j=0;j<n;j++) { int thisAdd = msg->getThisAdd(j); int dest = msg->getDestAdd(j); //int next = msg->getNextHop(j); int count = msg->getHopCount(j) + 1; if(count <= 16)//RIP max hop count { int flag = 0; for(int i=0;i<routeCount;i++) { if(dest == RIP_Table[i].destAdd || dest == 0) { flag = 1; break; } } if(flag == 0)
{ RIP_Table[routeCount].destAdd = dest; RIP_Table[routeCount].nextHop = thisAdd; RIP_Table[routeCount].hopCount = count; routeCount = routeCount + 1; } else
{ for(int i=0;i<routeCount;i++) { if(RIP_Table[i].destAdd == dest && RIP_Table[i].hopCount >= count) { RIP_Table[i].destAdd = dest; RIP_Table[i].nextHop = thisAdd; RIP_Table[i].hopCount = count; } } } } }}void RIP::sendRouteTable(){ int k = gate("out")->size(); for(int j=0;j<k;j++) { update *msg = new update("update"); msg->setCount(routeCount); for(int i=0;i<routeCount;i++) { msg->setThisAdd(i,num); msg->setDestAdd(i,RIP_Table[i].destAdd); msg->setNextHop(i,RIP_Table[i].nextHop); msg->setHopCount(i,RIP_Table[i].hopCount); } send(msg,"out",j); }}void RIP::showRouteTable(){ for(int i=0;i<routeCount;i++) { ev<<"From-"<<num<<"-to-"<<RIP_Table[i].destAdd<<"-nextHop-"<<RIP_Table[i].nextHop<<"-hopCount-"<<RIP_Table[i].hopCount<<endl; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -