2 * Copyright (c) 2003 The BISON Project
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.
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.
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.
19 package example.edaggregation;
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;
29 * Event driven version of epidemic averaging.
31 public class AverageED extends SingleValueHolder implements CDProtocol,
34 // --------------------------------------------------------------------------
36 // --------------------------------------------------------------------------
40 * string prefix for config properties
42 public AverageED(String prefix) {
46 // --------------------------------------------------------------------------
48 // --------------------------------------------------------------------------
51 * This is the standard method the define periodic activity. The frequency
52 * of execution of this method is defined by a
53 * {@link peersim.edsim.CDScheduler} component in the configuration.
55 public void nextCycle(Node node, int pid) {
56 Linkable linkable = (Linkable) node.getProtocol(FastConfig
58 if (linkable.degree() > 0) {
59 int degree=linkable.degree();
60 int i=CommonState.r.nextInt(degree);
61 Node peern = linkable.getNeighbor(i);
62 System.out.println("Pid of the protocol: "+pid);
63 System.out.println("Time="+CommonState.getTime()+" degree="+degree+" i="+i+" peernID="+peern.getID()+" peernIndex="+peern.getIndex());
66 AverageMessage ob=new AverageMessage(value, node);
67 System.out.println("NextCycle\t"+"\t Time: " + CommonState.getTime()+ "\t src: " + node.getID() + "\t dst: " + peern.getID()+"\t msg:"+ob.value);
68 ((Transport) node.getProtocol(FastConfig.getTransport(pid))).send(
69 node, peern, ob, pid);
73 // --------------------------------------------------------------------------
76 * This is the standard method to define to process incoming messages.
78 public void processEvent(Node node, int pid, Object event) {
80 AverageMessage aem = (AverageMessage) event;
82 AverageMessage ob=null;
83 if (aem.sender != null){
84 System.out.println("ProcessEventR\t"+"\t Time: " + CommonState.getTime() + "\t src: " + aem.sender.getID() + "\t dst: " + node.getID()+"\t msg:"+aem.value);
85 ob=new AverageMessage(value, null);
86 System.out.println("ProcessEventS\t"+"\t Time: " + CommonState.getTime()+ "\t src: " + node.getID() + "\t dst: " + aem.sender.getID()+"\t msg:"+ob.value);
87 ((Transport) node.getProtocol(FastConfig.getTransport(pid))).send(
88 node, aem.sender, ob, pid);
90 System.out.println("ProcessEventR\t"+"\t Time: " +CommonState.getTime() + "\t src: " + "NULL" + "\t dst: " + node.getID()+"\t msg:"+aem.value);
92 value = (value + aem.value) / 2;
97 // --------------------------------------------------------------------------
98 // --------------------------------------------------------------------------
101 * The type of a message. It contains a value of type double and the sender node
102 * of type {@link peersim.core.Node}.
104 class AverageMessage {
108 * If not null, this has to be answered, otherwise this is the answer.
112 public AverageMessage(double value, Node sender) {
114 this.sender = sender;