Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / peersim / cdsim / NextCycle.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
24 /**
25 * It generalizes its superclass so that the list of protocols to run can
26 * be specified. The superclass ({@link FullNextCycle}) always runs all the
27 * {@link CDProtocol}s.
28 */
29 public class NextCycle extends FullNextCycle {
30
31
32 // ============== fields ===============================================
33 // =====================================================================
34
35
36 /**
37 * Gives the list of protocols (whitespace separated) that need to be
38 * iterated over.
39 * @config
40 */
41 private static final String PAR_PROTS = "protocol";
42
43 private final int[] pids;
44
45
46 // =============== initialization ======================================
47 // =====================================================================
48
49 /**
50 * reads configuration parameters and the {@link Scheduler}s.
51 */
52 public NextCycle(String prefix) {
53         
54         super(prefix);
55         
56         String prots = Configuration.getString(prefix+"."+PAR_PROTS);
57         String[] protnames = prots.split("\\s");
58         pids = new int[protnames.length];
59         for(int i=0; i<protnames.length; ++i)
60         {
61                 pids[i] = Configuration.lookupPid(protnames[i]);
62         }
63 }
64
65 // =============== methods =============================================
66 // =====================================================================
67
68 /** 
69  * Execute the configured protocols on all nodes.
70  * It works exactly as {@link FullNextCycle#execute}, only just the configured
71  * protocols are iterated over.
72  */
73 public boolean execute() {
74
75         final int cycle=CDState.getCycle();
76         if( shuffle ) rperm.reset( Network.size() );
77         for(int j=0; j<Network.size(); ++j)
78         {
79                 Node node = null;
80                 if( getpair_rand )
81                         node = Network.get(CDState.r.nextInt(Network.size()));
82                 else if( shuffle )
83                         node = Network.get(rperm.next());
84                 else
85                         node = Network.get(j);
86                 if( !node.isUp() ) continue; 
87                 CDState.setNode(node);
88                 CDState.setCycleT(j);
89                 for(int pid: pids)
90                 {
91                         // Check if the protocol should be executed, given the
92                         // associated scheduler.
93                         if (!protSchedules[pid].active(cycle))
94                                 continue;
95                                 
96                         CDState.setPid(pid);
97                         Protocol protocol = node.getProtocol(pid);
98                         if( protocol instanceof CDProtocol )
99                         {
100                                 ((CDProtocol)protocol).nextCycle(node, pid);
101                                 if( !node.isUp() ) break;
102                         }
103                 }
104         }
105
106         return false;
107 }
108
109 }
110
111