Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / example / edaggregation / AverageED.java
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 implements CDProtocol,
32                 EDProtocol {
33
34         // --------------------------------------------------------------------------
35         // Initialization
36         // --------------------------------------------------------------------------
37
38         /**
39          * @param prefix
40          *            string prefix for config properties
41          */
42         public AverageED(String prefix) {
43                 super(prefix);
44         }
45
46         // --------------------------------------------------------------------------
47         // methods
48         // --------------------------------------------------------------------------
49
50         /**
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.
54          */
55         public void nextCycle(Node node, int pid) {
56                 Linkable linkable = (Linkable) node.getProtocol(FastConfig
57                                 .getLinkable(pid));
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());
64                         if (!peern.isUp())
65                                 return;
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);
70                 }
71         }
72
73         // --------------------------------------------------------------------------
74
75         /**
76          * This is the standard method to define to process incoming messages.
77          */
78         public void processEvent(Node node, int pid, Object event) {
79
80                 AverageMessage aem = (AverageMessage) event;
81
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);
89         } else {
90                 System.out.println("ProcessEventR\t"+"\t Time: " +CommonState.getTime() + "\t src: " + "NULL" + "\t dst: " + node.getID()+"\t msg:"+aem.value);
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 sender node
102  * of type {@link peersim.core.Node}.
103  */
104 class AverageMessage {
105
106         final double value;
107         /**
108          * If not null, this has to be answered, otherwise this is the answer.
109          */
110         final Node sender;
111
112         public AverageMessage(double value, Node sender) {
113                 this.value = value;
114                 this.sender = sender;
115         }
116 }