P2psim 源代码分析二

P2psim 源代码分析二

Kejieleung

 

一、NodeTopologyEventGenerator

(1)     Node类:作为整个节点的模拟,包括所有相关的操作,最重要的是事件发送与接收,同时也有流量统计、状态统计等。Node是最重要的一个基类,之后由P2Potocol类继承,具体协议再继承P2Potocol类。

 

main函数里Node::Parse处理protocol_file的参数读入,下面是./example/protocol.txt下的一个实例:

 

Kademlia k=20 alpha=3 stabilize_timer=32000 refresh_rate=32000 initstate =1

 

         这里指定协议为Kademlia(就是emule使用的P2P协议),结点个数为20,随机alpha值为3(这是Kademlia的具体参数),调整周期为32000ms,刷新周期为32000ms,设定initstate初始化

由于Node类涉及很我方面的操作,以后会重点分析这个大类,现在先分析其它两个类。

 

(2)     Topology类:比较简单,基类Topology提供从文件topology_file读取定义的结点拓扑结构的函数,以及提供计算延时的接口(虚函数)

 

  1. class Topology {
  2. public:
  3.   // will create the appropriate topology object
  4.   static Topology* parse(char *);
  5.   virtual void parse(ifstream&) = 0;    //kejie:处理topology_file(注意是虚函数,会在工厂类里提供具体的读入操作)
  6.   virtual Time latency(IPAddress, IPAddress, bool = false) = 0; //kejie:Euclidean::latency(IPAddress ip1x, IPAddress ip2x, bool reply)
  7.   virtual string get_node_name(IPAddress);
  8.   virtual Time median_lat() { return _med_lat; } 
  9.   virtual bool valid_latency (IPAddress ip1x, IPAddress ip2x) { return true;};
  10.   virtual ~Topology();
  11.   unsigned lossrate() { return _lossrate; }
  12.   unsigned noise_variance() { return _noise; }
  13.   unsigned num() { return _num; }
  14. protected:
  15.   Topology();
  16.   Time _med_lat;
  17.   unsigned _lossrate;
  18.   unsigned _noise;
  19.   unsigned int _num;
  20. };

最重要的是两个parse函数:

static Topology* parse(char *);

virtual void parse(ifstream&) = 0;

 

前者是在main函数里直接调用读入topology_file,处理的信息如下(./example/topology.txt)

 

topology Euclidean 7

failure_model ConstantFailureModel

 

1 0,0

2 5,5

3 8,8

4 12,6

5 13,8

6 0,0

7 20,34

 

设定的拓扑结构为 Euclidean,个数为7,失效模式为ConstantFailureModel,之后就是具体的拓扑结点位置参数,Euclidean型的拓扑是根据两点之间的距离来计算延时。

 

根据读入的结构后,会传递到Network初始化,最后再调用后者,完成具体类型拓扑的参数读入(topology_file指定:有Euclidean型,E2EGraph等,定义和实现在./topology文件夹下):

  1.   // create the network.
  2.  //kejie:static Network* Instance(Topology*, FailureModel*);
  3.   Network::Instance(top, failure_model);
  4.   // leave the rest of the file to the specific topology
  5.   //kejie: 读入结点信息, in 还是选打打开的 topology_file 文件指针,
  6.   //但是这里的 parse (虚函数)是在工厂类里提供具体的读入操作
  7.   top->parse(in);

 

 

(3)     EventGenerator类:先看看类层次结构吧

从类继承关系EventGenerator作为中层接口层存在,具体操作是由下面具体的事件类型来完成。注意这里使用了几种模式的设计,还是很值得学习的。

 

./ eventgenerators/churneventgenerator.h里有event文件是格式

In your events file, include the line:

 

generator ChurnEventGenerator [arg1=val1 [arg2=val2 [...]]]

 

Where args can currently be the following:

name            default         description

---------------------------------------------------------------------------

wkn             1               the ip of the wellknown node (bootstrapper)

proto                           the name of the protocol (i.e. "Chord")

lifemean        100000          average time (ms) a node is alive at a time

deathmean       lifemean        average time (ms) a node is dead at a time

lookupmean      10000           average time (ms) between lookups per node

exittime        200000          length of the experiment (in ms)

ipkeys          false           generate lookups where keys are node IPs

datakeys        false           generate lookups where keys are data items     

Join, crash, and lookup events will be exponentially distributed about the

means given above.

 

./example/event.txtParse读入的处理实例:

 

generator ChurnEventGenerator proto=Kademlia ipkeys=1 exittime=7200000 track_conncomp_timer=2000

 

这时指定的事件发生器为ChurnEventGenerator,使用协议Kademlia,设定以ip作为 key,退出时间为7200000mstrack_conncomp_timer跟踪时间2000ms

EventGenerator类的Parse处理时序图:

 

./ eventgenerators下有各类事件发生器的实现,下篇将以churneventgenerator为例分析事件的发生以事件队列观察者模式EventQueueObserver

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值