昨天学习了peersim的cycle模式,今天开始继续悟事件模式。
总的来说,我个人认为事件模式是周期模式的升级版,或者说,周期模式只是事件模式的一个子功能。
事件模式是基于时间和事件的(没打错),每次实验的开始会设定结束时间和若干要处理的事件,当时间结束,或者事件全部做完,实验就结束,而在结束之前,也是可以周期性的执行一些事件,所以说周期模式只是事件模式的一个子功能。
学习方法还是直接看代码,看不懂的地方直接去找对应的源文件,一层层从子类往上看。
贴上样例代码,功能和周期模式一样,首先是对网络中的每个节点赋初始值(这里使用线性分布),接着每个节点和相邻节点求平均值
样例代码:
1 /* 2 * Copyright (c) 2003 The BISON Project 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License version 2 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 16 * 17 */ 18 19 package example.edaggregation; 20 21 import peersim.vector.SingleValueHolder; 22 import peersim.config.*; 23 import peersim.core.*; 24 import peersim.transport.Transport; 25 import peersim.cdsim.CDProtocol; 26 import peersim.edsim.EDProtocol; 27 28 /** 29 * Event driven version of epidemic averaging. 30 */ 31 public class AverageED extends SingleValueHolder 32 implements CDProtocol, EDProtocol { //这里除了ED协议,还实现了CD协议,正是因为在实验中会周期性的执行一些事件,所以要实现CD协议 33 34 //-------------------------------------------------------------------------- 35 // Initialization 36 //-------------------------------------------------------------------------- 37 38 /** 39 * @param prefix string prefix for config properties 40 */ 41 public AverageED(String prefix) { super(prefix); } 42 43 44 //-------------------------------------------------------------------------- 45 // methods 46 //-------------------------------------------------------------------------- 47 48 /** 49 * This is the standard method the define periodic activity. 50 * The frequency of execution of this method is defined by a 51 * {@link peersim.edsim.CDScheduler} component in the configuration. 52 */ 53 public void nextCycle( Node node, int pid ) //这里就是周期模式的核心代码部分,定义了周期性的行为 54 { 55 Linkable linkable = 56 (Linkable) node.getProtocol( FastConfig.getLinkable(pid) ); 57 if (linkable.degree() > 0) 58 { 59 Node peern = linkable.getNeighbor( 60 CommonState.r.nextInt(linkable.degree())); //这部分的功能是得到本地节点的一个邻居节点,而不是所有相邻节点 61 62 // XXX quick and dirty handling of failures 63 // (message would be lost anyway, we save time) 64 if(!peern.isUp()) return; 65 66 ((Transport)node.getProtocol(FastConfig.getTransport(pid))). //这里涉及到了传输层,功能是像邻居节点发送信息,把本地的值发过去 67 send( 68 node, 69 peern, 70 new AverageMessage(value,node), 71 pid); 72 } 73 } 74 75 //-------------------------------------------------------------------------- 76 77 /** 78 * This is the standard method to define to process incoming messages. 79 */ 80 public void processEvent( Node node, int pid, Object event ) { //这里是ED模式的核心代码部分,用来处理事件 81 82 AverageMessage aem = (AverageMessage)event; //从邻居节点那里接受到的信息 83 84 if( aem.sender!=null ) //如果邻居节点的发送者不为空,说明本节点并不是目的节点,所以继续帮他转发 85 ((Transport)node.getProtocol(FastConfig.getTransport(pid))). 86 send( 87 node, 88 aem.sender, 89 new AverageMessage(value,null), 90 pid); 91 92 value = (value + aem.value) / 2; //如果为空,则说明本节点就是目的节点,所以把对方的值和本地的平均值替换本地值 93 } 94 95 } 96 97 //-------------------------------------------------------------------------- 98 //-------------------------------------------------------------------------- 99 100 /** 101 * The type of a message. It contains a value of type double and the 102 * sender node of type {@link peersim.core.Node}. 103 */ 104 class AverageMessage { //信息类,非常简单,只有一个value和sender 105 106 final double value; 107 /** If not null, 108 this has to be answered, otherwise this is the answer. */ 109 final Node sender; 110 public AverageMessage( double value, Node sender ) 111 { 112 this.value = value; 113 this.sender = sender; 114 } 115 }
以上就是ed模式下的一个简单的样例,功能也很简单,对比下周期模式,我觉得他是这么工作的:在每个定义的周期内向邻居发送节点,当节点接受到发送来的信息后,则会处理(求平均值),其中内部部分是通过一个堆来管理这些事件(send()方法本身就是把事件添加到堆当中)
然而难点并不是这,而是配置文件部分。。。shit :(
贴代码
1 # network size #首先是类似宏定义的一样定义了几个值,方便一会使用,果然换了ED模式连这里都高级起来了 2 SIZE 1000 3 4 # parameters of periodic execution 5 CYCLES 100 6 CYCLE SIZE*10000 7 8 # parameters of message transfer 9 # delay values here are relative to cycle length, in percentage, 10 # eg 50 means half the cycle length, 200 twice the cycle length, etc. 11 MINDELAY 0 12 MAXDELAY 0 13 # drop is a probability, 0<=DROP<=1 14 DROP 0 15 16 random.seed 1234567890 17 network.size SIZE 18 simulation.endtime CYCLE*CYCLES 19 simulation.logtime CYCLE 20 21 ################### protocols =========================== #这部分比较麻烦,全是新的玩意 22 23 protocol.link peersim.core.IdleProtocol #网络还是选的之前的一样 24 25 protocol.avg example.edaggregation.AverageED 26 protocol.avg.linkable link 27 protocol.avg.step CYCLE #这里的step类似于周期的概念 28 protocol.avg.transport tr #这里的tr在下面有定义 29 30 protocol.urt UniformRandomTransport #这里是具体的传输层协议,功能是以随机的延迟(随机范围在下面定义)发送消息,发送的实质是向事件的堆中添加事件 31 protocol.urt.mindelay (CYCLE*MINDELAY)/100 32 protocol.urt.maxdelay (CYCLE*MAXDELAY)/100 33 34 protocol.tr UnreliableTransport #一个不稳定的传输层协议,以下面的drop的值随机丢包,实际传输还是用的上面urt 35 protocol.tr.transport urt 36 protocol.tr.drop DROP 37 38 ################### initialization ====================== 39 40 init.rndlink WireKOut #网络布局,节点以k出度相连 41 init.rndlink.k 20 42 init.rndlink.protocol link 43 44 init.vals LinearDistribution #给每个节点的赋初值,线性分布,具体分布方法是定义step= (max.doubleValue()-min.doubleValue())/(Network.size()-1); setter.set(i,Math.round(i*step)+min.longValue()); 详细见源文件
45 init.vals.protocol avg 46 init.vals.max SIZE 47 init.vals.min 1 48 49 init.sch CDScheduler #这里很关键,定义了一个周期调度管理器,会在每个周期的开始随机打乱节点的部分,每隔一个cycle值就打乱一次 50 init.sch.protocol avg 51 init.sch.randstart 52 53 ################ control ============================== 54 55 control.0 SingleValueObserver #这里的step指定了周期值,使得监测者也会在每个指定的周期调用一次,否则control则会想周期模式一样调度 56 control.0.protocol avg 57 control.0.step CYCLE
最后贴上实验结果:
EDSimulator: resetting
Network: no node defined, using GeneralNode
EDSimulator: running initializers
- Running initializer init.rndlink: class peersim.dynamics.WireKOut
- Running initializer init.sch: class peersim.edsim.CDScheduler
- Running initializer init.vals: class peersim.vector.LinearDistribution
EDSimulator: loaded controls [control.0]
Current time: 0
control.0: 1.0 1000.0 1000 500.5 83416.66666666667 1 1
Current time: 10000000
control.0: 37.5 919.0 1000 500.5 25724.159091250687 1 1
Current time: 20000000
control.0: 206.7109375 767.890625 1000 500.5 8096.807036889389 1 1
Current time: 30000000
control.0: 352.373046875 695.453125 1000 500.5 2578.022573176135 1 1
Current time: 40000000
control.0: 412.430419921875 625.474609375 1000 500.5 801.1082179446831 1 1
Current time: 50000000
control.0: 436.43787479400635 570.459858417511 1000 500.5 243.53994072762902 1 1
Current time: 60000000
control.0: 470.7608990445733 527.0359845032217 1000 500.49999999999994 74.13788674564383 1 2
Current time: 70000000
control.0: 483.6040476858616 518.0301055684686 1000 500.49999999999903 23.428974301677556 1 1
Current time: 80000000
control.0: 490.5196089811798 512.0301471857779 1000 500.4999999999993 7.285566419597019 1 1
Current time: 90000000
control.0: 494.97216907397836 506.0375954180854 1000 500.4999999999999 2.1798299307442246 1 1
Current time: 100000000
control.0: 497.18190345272336 503.5837144460532 1000 500.5000000000001 0.6073148838336206 1 1
Current time: 110000000
control.0: 498.54320551492475 502.3533156558903 1000 500.5 0.1786794435445898 1 2
Current time: 120000000
control.0: 499.4023441821402 501.4962048486104 1000 500.49999999999966 0.055257607540637785 1 1
Current time: 130000000
control.0: 500.0032071191514 501.09832936709677 1000 500.4999999999995 0.017914865984002482 1 1
Current time: 140000000
control.0: 500.2115701087355 500.83018846682955 1000 500.5000000000003 0.005681541785868155 1 1
Current time: 150000000
control.0: 500.34130672550015 500.6817038675913 1000 500.49999999999983 0.0018027434356275712 1 1
Current time: 160000000
control.0: 500.4099993828274 500.593872603867 1000 500.50000000000006 5.362825330674977E-4 1 1
Current time: 170000000
control.0: 500.45257293353984 500.54348273631905 1000 500.50000000000057 1.540724446790086E-4 1 1
Current time: 180000000
control.0: 500.4730336913324 500.5308245606462 1000 500.49999999999983 4.870521656989873E-5 2 1
Current time: 190000000
control.0: 500.48375224793585 500.5157654498322 1000 500.4999999999995 1.6009524787167412E-5 1 1
Current time: 200000000
control.0: 500.48865909872677 500.50651976294216 1000 500.50000000000017 5.077729859315717E-6 1 1
Current time: 210000000
control.0: 500.4951192430326 500.5047676794802 1000 500.5000000000002 1.6155684864646324E-6 1 1
Current time: 220000000
control.0: 500.4974085478983 500.502935896735 1000 500.50000000000045 5.105843169016166E-7 1 1
Current time: 230000000
control.0: 500.4987222960319 500.5016402188968 1000 500.5000000000002 1.6512563666543207E-7 1 1
Current time: 240000000
control.0: 500.49903235648463 500.50092451432386 1000 500.49999999999994 5.299286046125987E-8 1 1
Current time: 250000000
control.0: 500.49958979342233 500.50059026547086 1000 500.5000000000001 1.5440470612681545E-8 1 1
Current time: 260000000
control.0: 500.4996881991682 500.500274943262 1000 500.5000000000001 4.8652048911656945E-9 1 1
Current time: 270000000
control.0: 500.49977403242923 500.5001382754437 1000 500.4999999999995 2.0101744759906163E-9 1 1
Current time: 280000000
control.0: 500.49987495360983 500.50007640960587 1000 500.5 1.7479778052092316E-10 1 1
Current time: 290000000
control.0: 500.49994445650935 500.5000501190701 1000 500.5000000000004 0.0 1 1
Current time: 300000000
control.0: 500.4999661034419 500.50002345424616 1000 500.49999999999994 3.2046259762169247E-10 1 1
Current time: 310000000
control.0: 500.4999712575856 500.50001450012803 1000 500.5000000000001 0.0 1 1
Current time: 320000000
control.0: 500.49998996075124 500.50001003198986 1000 500.5000000000003 0.0 1 1
Current time: 330000000
control.0: 500.4999953242251 500.5000043994592 1000 500.50000000000045 0.0 1 1
Current time: 340000000
control.0: 500.4999966553132 500.5000031137919 1000 500.49999999999994 5.826592684030772E-11 2 1
Current time: 350000000
control.0: 500.49999822203824 500.50000161478096 1000 500.49999999999994 2.6219667078138473E-10 1 1
Current time: 360000000
control.0: 500.4999990494662 500.5000007140138 1000 500.4999999999997 4.0786148788215407E-10 1 1
Current time: 370000000
control.0: 500.49999953087456 500.5000003780667 1000 500.50000000000057 0.0 1 1
Current time: 380000000
control.0: 500.4999997027671 500.5000002330102 1000 500.50000000000006 0.0 1 1
Current time: 390000000
control.0: 500.49999980445176 500.5000001249846 1000 500.5000000000003 0.0 1 1
Current time: 400000000
control.0: 500.4999998909826 500.5000000637969 1000 500.50000000000017 0.0 2 1
Current time: 410000000
control.0: 500.4999999491945 500.50000003001117 1000 500.50000000000006 2.330637073612309E-10 1 1
Current time: 420000000
control.0: 500.49999997587577 500.500000020472 1000 500.5000000000005 0.0 1 1
Current time: 430000000
control.0: 500.4999999853711 500.5000000123341 1000 500.49999999999966 1.456648171007693E-10 1 1
Current time: 440000000
control.0: 500.4999999932832 500.500000006375 1000 500.5 0.0 1 1
Current time: 450000000
control.0: 500.4999999963538 500.50000000440656 1000 500.5000000000004 0.0 1 1
Current time: 460000000
control.0: 500.4999999981322 500.50000000189016 1000 500.4999999999998 4.952603781426156E-10 1 1
Current time: 470000000
control.0: 500.49999999882857 500.50000000100374 1000 500.5000000000001 2.913296342015386E-11 1 1
Current time: 480000000
control.0: 500.4999999993252 500.5000000005659 1000 500.49999999999983 8.739889026046158E-11 1 1
Current time: 490000000
control.0: 500.49999999949034 500.500000000275 1000 500.50000000000006 5.826592684030772E-11 1 1
Current time: 500000000
control.0: 500.49999999982185 500.500000000164 1000 500.50000000000006 2.6219667078138473E-10 1 1
Current time: 510000000
control.0: 500.4999999998772 500.5000000001006 1000 500.49999999999983 3.7872852446200017E-10 1 1
Current time: 520000000
control.0: 500.4999999999419 500.50000000005065 1000 500.4999999999994 7.574570489240003E-10 1 1
Current time: 530000000
control.0: 500.4999999999752 500.5000000000325 1000 500.5000000000001 0.0 1 2
Current time: 540000000
control.0: 500.49999999998687 500.5000000000167 1000 500.50000000000006 0.0 1 1
Current time: 550000000
control.0: 500.4999999999914 500.50000000000847 1000 500.5 5.826592684030772E-11 1 1
Current time: 560000000
control.0: 500.4999999999958 500.5000000000048 1000 500.5 0.0 1 2
Current time: 570000000
control.0: 500.49999999999693 500.50000000000296 1000 500.5 0.0 1 1
Current time: 580000000
control.0: 500.4999999999983 500.50000000000153 1000 500.5 0.0 1 1
Current time: 590000000
control.0: 500.49999999999886 500.5000000000009 1000 500.5 0.0 1 1
Current time: 600000000
control.0: 500.4999999999992 500.50000000000045 1000 500.5 0.0 1 3
Current time: 610000000
control.0: 500.49999999999966 500.5000000000003 1000 500.5 0.0 1 2
Current time: 620000000
control.0: 500.49999999999983 500.50000000000017 1000 500.5 0.0 4 1
Current time: 630000000
control.0: 500.4999999999999 500.5000000000001 1000 500.5 0.0 9 2
Current time: 640000000
control.0: 500.49999999999994 500.5 1000 500.5 0.0 10 990
Current time: 650000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 660000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 670000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 680000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 690000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 700000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 710000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 720000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 730000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 740000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 750000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 760000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 770000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 780000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 790000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 800000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 810000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 820000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 830000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 840000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 850000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 860000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 870000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 880000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 890000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 900000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 910000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 920000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 930000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 940000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 950000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 960000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 970000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 980000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
Current time: 990000000
control.0: 500.5 500.5 1000 500.5 0.0 1000 1000
EDSimulator: queue is empty, quitting at time 999980413