Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / contrib / psg / src / peersim / cdsim / DaemonProtocol.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 java.util.Arrays;
22 import peersim.config.Configuration;
23 import peersim.core.Node;
24 import peersim.core.Control;
25
26 /**
27 * A protocol that is not really a protocol, but a trick to carry out all
28 * kinds of tasks during the simulation. Many users will probably not need it,
29 * but it is a nice way to e.g. run controls at any time, not only between cycles.
30 */
31 public class DaemonProtocol implements CDProtocol {
32
33
34 // ========================= fields =================================
35 // ==================================================================
36
37
38 /**
39 * This is the prefix for network dynamism managers.
40 * @config
41 */
42 private static final String PAR_CTRL = "control";
43
44 /**
45 * The controls will be run according to this frequency.
46 * It is interpreted within a cycle, in terms of cycle time
47 * ({@link CDState#getCycleT}). The first cycletime is 0.
48 * Defaults to 1.
49 * @config
50 */
51 private static final String PAR_STEP = "cstep";
52
53 // --------------------------------------------------------------------
54
55 private static Control[] controls=null;
56
57 private static int step;
58
59 // ========================= initialization =========================
60 // ==================================================================
61
62
63 public DaemonProtocol(String s)
64 {  
65         step = Configuration.getInt(s+"."+PAR_STEP,1);
66         
67         String[] names = Configuration.getNames(s+"."+PAR_CTRL);
68         controls = new Control[names.length];
69         for(int i=0; i<names.length; ++i)
70         {
71                 controls[i]=(Control)Configuration.getInstance(names[i]);
72         }
73         System.err.println(s+": loaded controls "+Arrays.asList(names));
74 }
75
76 // ------------------------------------------------------------------
77
78 public Object clone() {
79
80         DaemonProtocol ip = null;
81         try { ip=(DaemonProtocol)super.clone(); }
82         catch( CloneNotSupportedException e ) {} // never happens
83         return ip;
84 }
85
86
87 // ========================= methods =================================
88 // ===================================================================
89
90         
91 /**
92 * Runs the configured controls if {@link CDState#getCycleT} %
93 * {@value #PAR_STEP}=0.
94 */
95 public void nextCycle( Node node, int protocolID ) {
96
97         if( CDState.getCycleT() % step != 0 ) return;
98         for(int j=0; j<controls.length; ++j) controls[j].execute();
99 }
100
101 }
102