昨天学习了peersim的cycle模式,今天开始继续悟事件模式。
总的来说,我个人认为事件模式是周期模式的升级版,或者说,周期模式只是事件模式的一个子功能。
事件模式是基于时间和事件的(没打错),每次实验的开始会设定结束时间和若干要处理的事件,当时间结束,或者事件全部做完,实验就结束,而在结束之前,也是可以周期性的执行一些事件,所以说周期模式只是事件模式的一个子功能。
学习方法还是直接看代码,看不懂的地方直接去找对应的源文件,一层层从子类往上看。
贴上样例代码,功能和周期模式一样,首先是对网络中的每个节点赋初始值(这里使用线性分布),接着每个节点和相邻节点求平均值
样例代码:
/*
* Copyright (c) 2003 The BISON Project
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 2 as
* published by the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/ package example.edaggregation; import peersim.vector.SingleValueHolder;
import peersim.config.*;
import peersim.core.*;
import peersim.transport.Transport;
import peersim.cdsim.CDProtocol;
import peersim.edsim.EDProtocol; /**
* Event driven version of epidemic averaging.
*/
public class AverageED extends SingleValueHolder
implements CDProtocol, EDProtocol { //这里除了ED协议,还实现了CD协议,正是因为在实验中会周期性的执行一些事件,所以要实现CD协议 //--------------------------------------------------------------------------
// Initialization
//-------------------------------------------------------------------------- /**
* @param prefix string prefix for config properties
*/
public AverageED(String prefix) { super(prefix); } //--------------------------------------------------------------------------
// methods
//-------------------------------------------------------------------------- /**
* This is the standard method the define periodic activity.
* The frequency of execution of this method is defined by a
* {@link peersim.edsim.CDScheduler} component in the configuration.
*/
public void nextCycle( Node node, int pid ) //这里就是周期模式的核心代码部分,定义了周期性的行为
{
Linkable linkable =
(Linkable) node.getProtocol( FastConfig.getLinkable(pid) );
if (linkable.degree() > )
{
Node peern = linkable.getNeighbor(
CommonState.r.nextInt(linkable.degree())); //这部分的功能是得到本地节点的一个邻居节点,而不是所有相邻节点 // XXX quick and dirty handling of failures
// (message would be lost anyway, we save time)
if(!peern.isUp()) return; ((Transport)node.getProtocol(FastConfig.getTransport(pid))). //这里涉及到了传输层,功能是像邻居节点发送信息,把本地的值发过去
send(
node,
peern,
new AverageMessage(value,node),
pid);
}
} //-------------------------------------------------------------------------- /**
* This is the standard method to define to process incoming messages.
*/
public void processEvent( Node node, int pid, Object event ) { //这里是ED模式的核心代码部分,用来处理事件 AverageMessage aem = (AverageMessage)event; //从邻居节点那里接受到的信息 if( aem.sender!=null ) //如果邻居节点的发送者不为空,说明本节点并不是目的节点,所以继续帮他转发
((Transport)node.getProtocol(FastConfig.getTransport(pid))).
send(
node,
aem.sender,
new AverageMessage(value,null),
pid); value = (value + aem.value) / ; //如果为空,则说明本节点就是目的节点,所以把对方的值和本地的平均值替换本地值
} } //--------------------------------------------------------------------------
//-------------------------------------------------------------------------- /**
* The type of a message. It contains a value of type double and the
* sender node of type {@link peersim.core.Node}.
*/
class AverageMessage { //信息类,非常简单,只有一个value和sender final double value;
/** If not null,
this has to be answered, otherwise this is the answer. */
final Node sender;
public AverageMessage( double value, Node sender )
{
this.value = value;
this.sender = sender;
}
}
以上就是ed模式下的一个简单的样例,功能也很简单,对比下周期模式,我觉得他是这么工作的:在每个定义的周期内向邻居发送节点,当节点接受到发送来的信息后,则会处理(求平均值),其中内部部分是通过一个堆来管理这些事件(send()方法本身就是把事件添加到堆当中)
然而难点并不是这,而是配置文件部分。。。shit :(
贴代码
# network size #首先是类似宏定义的一样定义了几个值,方便一会使用,果然换了ED模式连这里都高级起来了
SIZE # parameters of periodic execution
CYCLES
CYCLE SIZE*10000 # parameters of message transfer
# delay values here are relative to cycle length, in percentage,
# eg means half the cycle length, twice the cycle length, etc.
MINDELAY
MAXDELAY
# drop is a probability, <=DROP<=
DROP random.seed
network.size SIZE
simulation.endtime CYCLE*CYCLES
simulation.logtime CYCLE ################### protocols =========================== #这部分比较麻烦,全是新的玩意 protocol.link peersim.core.IdleProtocol #网络还是选的之前的一样 protocol.avg example.edaggregation.AverageED
protocol.avg.linkable link
protocol.avg.step CYCLE #这里的step类似于周期的概念
protocol.avg.transport tr #这里的tr在下面有定义 protocol.urt UniformRandomTransport #这里是具体的传输层协议,功能是以随机的延迟(随机范围在下面定义)发送消息,发送的实质是向事件的堆中添加事件
protocol.urt.mindelay (CYCLE*MINDELAY)/
protocol.urt.maxdelay (CYCLE*MAXDELAY)/ protocol.tr UnreliableTransport #一个不稳定的传输层协议,以下面的drop的值随机丢包,实际传输还是用的上面urt
protocol.tr.transport urt
protocol.tr.drop DROP ################### initialization ====================== init.rndlink WireKOut #网络布局,节点以k出度相连
init.rndlink.k
init.rndlink.protocol link init.vals LinearDistribution #给每个节点的赋初值,线性分布,具体分布方法是定义step= (max.doubleValue()-min.doubleValue())/(Network.size()-1); setter.set(i,Math.round(i*step)+min.longValue()); 详细见源文件
init.vals.protocol avg
init.vals.max SIZE
init.vals.min init.sch CDScheduler #这里很关键,定义了一个周期调度管理器,会在每个周期的开始随机打乱节点的部分,每隔一个cycle值就打乱一次
init.sch.protocol avg
init.sch.randstart ################ control ============================== control. SingleValueObserver #这里的step指定了周期值,使得监测者也会在每个指定的周期调用一次,否则control则会想周期模式一样调度
control..protocol avg
control..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