2 * Copyright (c) 2003-2005 The BISON Project
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.
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.
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.
19 package peersim.config;
22 * Reads configuration regarding relations between protocols.
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.
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.
31 public class FastConfig
34 // ======================= fields ===========================================
35 // ===========================================================================
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 ",").
43 private static final String PAR_LINKABLE = "linkable";
46 * Parameter name in configuration that attaches a transport layer protocol to a
50 private static final String PAR_TRANSPORT = "transport";
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.
56 protected static final int[][] links;
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.
62 protected static final int[] transports;
65 // ======================= initialization ===================================
66 // ==========================================================================
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}.
74 * Protocols' linkable and transport definitions are prefetched
75 * and stored in arrays, to enable fast access during simulation.
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.
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)
88 if (Configuration.contains(names[i] + "." + PAR_LINKABLE))
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]);
99 links[i] = new int[0]; // empty set
101 if (Configuration.contains(names[i] + "." + PAR_TRANSPORT))
103 Configuration.getPid(names[i] + "." + PAR_TRANSPORT);
109 // ---------------------------------------------------------------------
111 /** to prevent construction */
112 private FastConfig() {}
114 // ======================= methods ==========================================
115 // ==========================================================================
119 * Returns true if the given protocol has at least one linkable protocol
120 * associated with it, otherwise false.
122 public static boolean hasLinkable(int pid) { return numLinkables(pid) > 0; }
124 // ---------------------------------------------------------------------
127 * Returns the number of linkable protocols associated with a given protocol.
129 public static int numLinkables(int pid) { return links[pid].length; }
131 // ---------------------------------------------------------------------
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
140 public static int getLinkable(int pid, int linkIndex)
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);
148 return links[pid][linkIndex];
151 //---------------------------------------------------------------------
154 * Invokes <code>getLinkable(pid, 0)</code>.
156 public static int getLinkable(int pid)
158 return getLinkable(pid, 0);
161 // ---------------------------------------------------------------------
164 * Returns true if the given protocol has a transport protocol associated with
165 * it, otherwise false.
167 public static boolean hasTransport(int pid)
169 return transports[pid] >= 0;
172 // ---------------------------------------------------------------------
175 * Returns the id of the transport protocol used by the protocol identified
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.
181 public static int getTransport(int pid)
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");
188 return transports[pid];