Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #1 from mquinson/master
[simgrid.git] / contrib / psg / src / peersim / cdsim / FullNextCycle.java
1 /*
2  * Copyright (c) 2003-2005 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 peersim.cdsim;
20
21 import peersim.config.*;
22 import peersim.core.*;
23 import peersim.util.RandPermutation;
24
25 /**
26 * Control to run a cycle of the cycle driven simulation.
27 * This does not need to be explicitly configured (although you can do it for
28 * hacking purposes).
29 */
30 public class FullNextCycle implements Control {
31
32
33 // ============== fields ===============================================
34 // =====================================================================
35
36
37 /**
38 * The type of the getPair function. This parameter is of historic interest and
39 * was needed in a publication we wrote. You don't need to care about this.
40 * But if you wanna know: if set to "rand", then in a cycle the simulator
41 * does not simply iterate through the nodes, but instead picks a random one
42 * N times, where N is the network size.
43 * @config
44 */
45 private static final String PAR_GETPAIR = "getpair";
46
47 /**
48 * Shuffle iteration order if set. Not set by default. If set, then nodes are
49 * iterated in a random order. However, in the network the nodes actually
50 * stay in the order they originally were. The price for leaving the
51 * network untouched is memory: we need to store the permutation we use
52 * to iterate the network.
53 * @config
54 */
55 private static final String PAR_SHUFFLE = "shuffle";
56
57 // --------------------------------------------------------------------
58
59 protected final boolean getpair_rand;
60
61 protected final boolean shuffle;
62
63 /** Holds the protocol schedulers of this simulation */
64 protected Scheduler[] protSchedules = null;
65
66 /** The random permutation to use if config par {@value #PAR_SHUFFLE} is set. */
67 protected RandPermutation rperm = new RandPermutation( CDState.r );
68
69 // =============== initialization ======================================
70 // =====================================================================
71
72 /**
73 * Reads config parameters and {@link Scheduler}s.
74 */
75 public FullNextCycle(String prefix) {
76         
77         getpair_rand = Configuration.contains(prefix+"."+PAR_GETPAIR);
78         shuffle = Configuration.contains(prefix+"."+PAR_SHUFFLE);
79
80         // load protocol schedulers
81         String[] names = Configuration.getNames(Node.PAR_PROT);
82         protSchedules = new Scheduler[names.length];
83         for(int i=0; i<names.length; ++i)
84         {
85                 protSchedules[i] = new Scheduler(names[i]);
86         }
87 }
88
89 // =============== methods =============================================
90 // =====================================================================
91
92 /** 
93  * Execute all the {@link CDProtocol}s on all nodes that are up.
94  * If the node goes down as a result of the execution of a protocol, then
95  * the rest of the protocols on that node are not executed and we move on
96  * to the next node.
97  * It sets the {@link CDState} appropriately.
98  * @return always false
99  */
100 public boolean execute() {
101
102         final int cycle=CDState.getCycle();
103         if( shuffle ) rperm.reset( Network.size() );
104         for(int j=0; j<Network.size(); ++j)
105         {
106                 Node node = null;
107                 if( getpair_rand )
108                         node = Network.get(CDState.r.nextInt(Network.size()));
109                 else if( shuffle )
110                         node = Network.get(rperm.next());
111                 else
112                         node = Network.get(j);
113                 if( !node.isUp() ) continue; 
114                 CDState.setNode(node);
115                 CDState.setCycleT(j);
116                 final int len = node.protocolSize();
117                 for(int k=0; k<len; ++k)
118                 {
119                         // Check if the protocol should be executed, given the
120                         // associated scheduler.
121                         if (!protSchedules[k].active(cycle))
122                                 continue;
123                                 
124                         CDState.setPid(k);
125                         Protocol protocol = node.getProtocol(k);
126                         if( protocol instanceof CDProtocol )
127                         {
128                                 ((CDProtocol)protocol).nextCycle(node, k);
129                                 if( !node.isUp() ) break;
130                         }
131                 }
132         }
133
134         return false;
135 }
136
137 }