Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
kill another out of date script
[simgrid.git] / contrib / psg / src / peersim / config / FastConfig.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.config;
20
21 /**
22  * Reads configuration regarding relations between protocols.
23  * 
24  * Technically, this class is not necessary because protocols could
25  * access the configuration directly. However, it provides much faster
26  * access to "linkable" and "transport" information, enhancing runtime speed.
27  *
28  * This class is a static singleton and is initialized only when first accessed.
29  * During initialization it reads and caches the configuration info it handles.
30  */
31 public class FastConfig
32 {
33
34 // ======================= fields ===========================================
35 // ===========================================================================
36
37 /**
38  * Parameter name in configuration that attaches a linkable protocol to a
39  * protocol. The property can contain multiple protocol names, in one line,
40  * separated by non-word characters (e.g. whitespace or ",").
41  * @config
42  */
43 private static final String PAR_LINKABLE = "linkable";
44
45 /**
46  * Parameter name in configuration that attaches a transport layer protocol to a
47  * protocol.
48  * @config
49  */
50 private static final String PAR_TRANSPORT = "transport";
51
52 /**
53  * This array stores the protocol ids of the {@link peersim.core.Linkable}
54  * protocols that are linked to the protocol given by the array index.
55  */
56 protected static final int[][] links;
57
58 /**
59  * This array stores the protocol id of the {@link peersim.transport.Transport}
60  * protocol that is linked to the protocol given by the array index.
61  */
62 protected static final int[] transports;
63
64
65 // ======================= initialization ===================================
66 // ==========================================================================
67
68
69 /**
70  * This static initialization block reads the configuration for information that
71  * it understands. Currently it understands property {@value #PAR_LINKABLE}
72  * and {@value #PAR_TRANSPORT}.
73  * 
74  * Protocols' linkable and transport definitions are prefetched
75  * and stored in arrays, to enable fast access during simulation.
76  *
77  * Note that this class does not perform any type checks. The purpose of the
78  * class is purely to speed up access to linkable and transport information,
79  * by providing a fast alternative to reading directly from the
80  * <code>Configuration</code> class.
81  */
82 static {
83         String[] names = Configuration.getNames(Configuration.PAR_PROT);
84         links = new int[names.length][];
85         transports = new int[names.length];
86         for (int i = 0; i < names.length; ++i)
87         {
88                 if (Configuration.contains(names[i] + "." + PAR_LINKABLE))
89                 {
90                         // get string of linkables
91                         String str = Configuration.getString(names[i] + "." + PAR_LINKABLE);
92                         // split around non-word characters
93                         String[] linkNames = str.split("\\W+");
94                         links[i] = new int[linkNames.length];
95                         for (int j=0; j<linkNames.length; ++j)
96                                 links[i][j] = Configuration.lookupPid(linkNames[j]);
97                 }               
98                 else
99                         links[i] = new int[0]; // empty set
100
101                 if (Configuration.contains(names[i] + "." + PAR_TRANSPORT))
102                         transports[i] = 
103                         Configuration.getPid(names[i] + "." + PAR_TRANSPORT);
104                 else
105                         transports[i] = -1;
106         }
107 }
108
109 // ---------------------------------------------------------------------
110
111 /** to prevent construction */
112 private FastConfig() {}
113
114 // ======================= methods ==========================================
115 // ==========================================================================
116
117
118 /**
119  * Returns true if the given protocol has at least one linkable protocol
120  * associated with it, otherwise false.
121  */
122 public static boolean hasLinkable(int pid) { return numLinkables(pid) > 0; }
123
124 // ---------------------------------------------------------------------
125
126 /**
127  * Returns the number of linkable protocols associated with a given protocol.
128  */
129 public static int numLinkables(int pid) { return links[pid].length; }
130
131 // ---------------------------------------------------------------------
132
133 /**
134  * Returns the protocol id of the <code>linkIndex</code>-th linkable used by
135  * the protocol identified by pid. Throws an
136  * IllegalParameterException if there is no linkable associated with the given
137  * protocol: we assume here that this happens when the configuration is
138  * incorrect.
139  */
140 public static int getLinkable(int pid, int linkIndex)
141 {
142         if (linkIndex >= numLinkables(pid)) {
143                 String[] names = Configuration.getNames(Configuration.PAR_PROT);
144                 throw new IllegalParameterException(names[pid],
145                         "Protocol " + pid + " has no "+PAR_LINKABLE+
146                         " parameter with index" + linkIndex);
147         }
148         return links[pid][linkIndex];
149 }
150
151 //---------------------------------------------------------------------
152
153 /**
154  * Invokes <code>getLinkable(pid, 0)</code>.
155  */
156 public static int getLinkable(int pid)
157 {
158         return getLinkable(pid, 0);
159 }
160
161 // ---------------------------------------------------------------------
162
163 /**
164  * Returns true if the given protocol has a transport protocol associated with
165  * it, otherwise false.
166  */
167 public static boolean hasTransport(int pid)
168 {
169         return transports[pid] >= 0;
170 }
171
172 // ---------------------------------------------------------------------
173
174 /**
175  * Returns the id of the transport protocol used by the protocol identified
176  * by pid.
177  * Throws an IllegalParameterException if there is no transport associated
178  * with the given protocol: we assume here that his happens when the
179  * configuration is incorrect.
180  */
181 public static int getTransport(int pid)
182 {
183         if (transports[pid] < 0) {
184                 String[] names = Configuration.getNames(Configuration.PAR_PROT);
185                 throw new IllegalParameterException(names[pid],
186                 "Protocol " + pid + " has no "+PAR_TRANSPORT + " parameter");
187         }
188         return transports[pid];
189 }
190
191 }