文章目录
- 实验介绍
- 实验内容:
- 步骤
- 具体步骤
- 1 构建网络图
- .ned
- 节点模型
- 网络模型
- 2 写路由算法
- .cc
- msg.h
- msg.cc
- 3 启动网络
- .ini
实验介绍
编程实现一个4维的立方体网络仿真,网络节点按照如下方式运行,
实验要求:
1.网络节点按照默认的顺序,如节点标识0,1,…,15从小到大的顺序依次产生一个数据包。
2.节点产生(或接收到)一个数据包后,随机选择一个相邻节点发送数据包,依此规则重复执行,直至产生数据包的节点接收到自己的数据包后,直接删除该数据包。
超级立方体网络指具有 d 个维度的网络具有2d个网络节点,网络节点按照0,1,2,…2d-1顺序进行编号。标识 i 的节点采用二进制方式可表示为d 位的二进制序列,网络任意两个节点二进制形式表示的d位标识符,对应位只有某一位不同时,表示节点是直接相邻接,否则,两个节点之间不存在直接相邻接。例如,对于一个3维的超级立方体网络,网络中存在8(8 = 23)个网络节点,如0(000),1(001) ,2(010) ,3(011) ,4(100) ,5(101) ,6(110) ,7(111)。网络拓扑结构按照如下方式连接,节点 0(000)与节点1(001) ,2(010) ,4(100) 直接相临接,因节点 0(000)与节点1,2,4分别在第1位,第2位,第3位不同(从左往右数),其他节点按此规律相邻接。
实验内容:
1.生成4维超立方体网络模型图及网络运行截图;
2.画出网络流程框图;
3.写出节点随机选择相邻节点的路由算法伪代码
4.统计每个节点产生的数据包一个轮回产生的跳数,同时,统计每个节点发送、接收的数据包的个数。
步骤
- 构建4维的立方体网络网络模型图,编写.ned代码
- 写出节点随机选择相邻节点的路由算法,编写.cc代码
具体步骤
1 构建网络图
先上图,生成的模型图如下(参考hypercube )
.ned
自己 new 一个 project(注意是empty project),再project中新建一个.ned文件。
工程创建参考:link
节点模型
1
2
3
4
5
6
7
8
9simple Node { parameters: int dim = default(4);//维数,默认为4,可自行修改生成各种维数的立方体 @display("i=block/circle;is=vs"); gates: inout gate[dim];//根据维数构建输出输入门 }
网络模型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17network Hypercube { parameters: int dim = default(4); types: channel Channel extends ned.DelayChannel { delay = 100ms; } submodules: node[2^dim]:Node; connections: for i=0..2^dim-1, for j=0..dim-1 {. node[i].gate[j] <--> Channel<-->node[i#(1<<j)].gate[j] if (i < i#(1<<j));//关键代码 //此句代码将根据维数生成按照如下方式连接的网络拓扑结构:例如三维:节点 0(000)与节点1(001) ,2(010) ,4(100) 直接相临接,因节点 0(000)与节点1,2,4分别在第1位,第2位,第3位不同(从左往右数),其他节点按此规律相邻接。 } }
2 写路由算法
.cc
在project中,新建一个.cc文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88#include <omnetpp.h> #include "msg.h" #include <stdio.h> using namespace omnetpp; class Node: public cSimpleModule { private: int hop; int sendCounter;//存储发送数目 int recCounter;//存储接受数目 int dim;//维数 int pointCounter;//根据维数生成的节点数目 protected: virtual TicTocMsg *generateMessage(); virtual void initialize() override; virtual void handleMessage(cMessage *msg) override; void SendRandomMessage(TicTocMsg *msg);//随机选择从该节点的 一个节点发送消息出去 }; Define_Module(Node); void Node::initialize() { hop=0; sendCounter=0; recCounter=0; pointCounter=1; WATCH(hop); WATCH(sendCounter); WATCH(recCounter); //根据维数生成相应数量的点 dim = par("dim"); for(int i=0;i<dim;i++) pointCounter*=2; EV<<"pointCounter"<<pointCounter<<endl; for(int i=0;i<pointCounter;i++) { if (getIndex() == i) { EV<<"node "<<i<<" :start!"<<endl; TicTocMsg *msg = generateMessage();//产生消息 SendRandomMessage(msg);//随机选择一个门发送出去,随机转发算法 } } } void Node::handleMessage(cMessage *msg) { recCounter++;//处理消息,即接受到消息,接受消息数目+1 TicTocMsg *ttmsg = check_and_cast<TicTocMsg *>(msg);//转化类型 //消息到达目的节点,打印调数,删除旧消息,生成新消息,随机发送消息 if (ttmsg->getY() == getIndex()) { // Message arrived. EV << "Message " << ttmsg << " arrived after " << ttmsg->getHopCount() << " hops.n"; hop = ttmsg->getHopCount();//统计消息到达时的hop bubble("ARRIVED");//在网络视图中 产生 对话框, delete ttmsg; } //消息没到达目的节点,随机转发 else { SendRandomMessage(ttmsg);//随机转发 } } void Node::SendRandomMessage(TicTocMsg *msg) { // Increment hop count. //发送出去,跳数+1,发送数目+1 msg->setHopCount(msg->getHopCount()+1); sendCounter++; // Same routing as before: random gate. int n = gateSize("gate"); EV<<"gataSize:"<<n<<endl; int k = intuniform(0, n-1); EV << "Forwarding message " << msg << " on gate[" << k << "]n"; send(msg, "gate$o", k);//$0表示从out门发送出去 } //生成消息, TicTocMsg* Node::generateMessage() { int src = getIndex(); // our module index int dest = src;//据题意:目的节点就是源节点 char msgname[20]; sprintf(msgname, "tic-%d", src); // Create message object and set source and destination field. TicTocMsg *msg = new TicTocMsg(msgname); msg->setX(src); msg->setY(dest); return msg; }
msg.h
自定义消息头文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#ifndef MSG_H_ #define MSG_H_ #include <omnetpp.h> class TicTocMsg; class TicTocMsg : public ::omnetpp::cMessage { protected: int x = 0;//存储源地址,这里命名有点low int y = 0;//存储目的地址,命名不规范 int hopCount = 0;//存储跳数 private: void copy(const TicTocMsg& other); protected: // protected and unimplemented operator==(), to prevent accidental usage bool operator==(const TicTocMsg&); public: TicTocMsg(const char *name=nullptr, short kind=0); TicTocMsg(const TicTocMsg& other); virtual ~TicTocMsg(); // field getter/setter methods virtual int getX() const; virtual void setX(int source); virtual int getY() const; virtual void setY(int destination); virtual int getHopCount() const; virtual void setHopCount(int hopCount); }; #endif /* MSG_H_ */
msg.cc
自定义消息实现文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47#include "msg.h" #include <iostream> #include <sstream> #include <memory> namespace omnetpp { } void TicTocMsg::copy(const TicTocMsg& other) { this->x = other.x; this->y = other.y; this->hopCount = other.hopCount; } TicTocMsg::TicTocMsg(const char *name, short kind) : ::omnetpp::cMessage(name, kind) { } TicTocMsg::TicTocMsg(const TicTocMsg& other) : ::omnetpp::cMessage(other) { copy(other); } TicTocMsg::~TicTocMsg() { } int TicTocMsg::getX() const { return this->x; } void TicTocMsg::setX(int source) { this->x = source; } int TicTocMsg::getY() const { return this->y; } void TicTocMsg::setY(int destination) { this->y = destination; } int TicTocMsg::getHopCount() const { return this->hopCount; } void TicTocMsg::setHopCount(int hopCount) { this->hopCount = hopCount; }
3 启动网络
.ini
网络配置
1
2
3[General] network = Hypercube
欢迎读者们提出你们宝贵的建议。
最后
以上就是坚定麦片最近收集整理的关于omnet++ 4维的立方体网络仿真实验介绍的全部内容,更多相关omnet++内容请搜索靠谱客的其他文章。
发表评论 取消回复