4 import java.util.Comparator;
5 import java.util.LinkedHashMap;
7 import java.util.TreeMap;
10 import org.jdom2.output.*;
11 import org.simgrid.msg.Host;
12 import org.simgrid.msg.Msg;
14 import peersim.config.Configuration;
15 import peersim.core.Control;
16 import peersim.core.Protocol;
19 * A class store different configuration information for simulation. It creates
20 * the deployment file according to this informations.
22 * @author Khaled Baati 26/10/2014
23 * @version version 1.1
26 public class PSGPlatform {
32 /** unit of measure. **/
38 /** the default unit of measure **/
39 static final String sec = "sec";
41 /** All protocols defined in the configuration file. **/
42 static Protocol[] protocolsName;
44 /** A numeric identifier associated for each protocol. **/
48 static Host[] hostList;
50 /** A collection map represents the Control and its associated step. **/
51 static Map<Control, Double> controlStepMap = new LinkedHashMap<Control, Double>();
53 /** A collection map represents the protocol and its associated pid. **/
54 static TreeMap<Protocol, Integer> protocolsPidsMap = new TreeMap<Protocol, Integer>(
55 new Comparator<Protocol>() {
56 public int compare(Protocol p1, Protocol p2) {
57 return p1.toString().compareTo(p2.toString());
61 /** A collection map represents all CDProtocol and its associated step **/
62 static TreeMap<Protocol, Double> cdProtocolsStepMap = new TreeMap<Protocol, Double>(
63 new Comparator<Protocol>() {
64 public int compare(Protocol p1, Protocol p2) {
65 return p1.toString().compareTo(p2.toString());
68 /** the default platform file **/
69 static final String platformFile = "platforms/psg.xml";
71 /** the deployment file **/
72 static final String deploymentFile = "deployment.xml";
74 static Element racine;
75 static Document document;
76 static boolean interfED = false;
77 static boolean interfCD = false;
79 /** Prepare the deployment file **/
81 DocType dtype = new DocType("platform",
82 "http://simgrid.gforge.inria.fr/simgrid.dtd");
83 racine = new Element("platform");
84 document = new Document(racine, dtype);
85 Attribute version = new Attribute("version", "3");
86 racine.setAttribute(version);
89 // ========================== methods ==================================
90 // =====================================================================
93 * Convert PS unit time to Simgrid unit time
96 * the value to convert
97 * @return time converted
99 public static double psToSgTime(long valeur) {
100 timeUnit unit = unit();
103 return ((double) valeur) / 1000000;
105 return ((double) valeur) / 1000;
107 return (double) valeur;
113 * Convert Simgrid unit time to PS unit time
116 * the value to convert
117 * @return time converted
119 public static long sgToPsTime(double valeur) {
120 timeUnit unit = unit();
123 return (long) valeur * 1000000;
125 return (long) valeur * 1000;
127 return (long) valeur;
134 * @return the duration of simulation.
136 public static long getDuration() {
137 return Configuration.getLong("simulation.duration");
142 * @return PeerSim Time
144 public static long getTime() {
145 return sgToPsTime(Msg.getClock());
150 * @return the Simgrid Clock
152 public static double getClock() {
153 return Msg.getClock();
157 * Load and run initializers.
159 public static void init() {
160 Object[] inits = Configuration.getInstanceArray("init");
161 String names[] = Configuration.getNames("init");
162 for (int i = 0; i < inits.length; ++i) {
163 System.err.println("- Running initializer " + names[i] + ": "
164 + inits[i].getClass().toString());
165 ((Control) inits[i]).execute();
170 * Load all controls and stores them in {@link #controlStepMap} collection
171 * to be scheduled, and executed in {@link psgsim.PSGProcessController}.
173 public static void control() {
175 String[] names = Configuration.getNames("control");
177 for (int i = 0; i < names.length; ++i) {
178 control = (Control) Configuration.getInstance(names[i]);
179 Long stepControl = Configuration.getLong(names[i] + "." + "step");
180 controlStepMap.put(control, psToSgTime(stepControl));
185 * Lookup all protocols in the configuration file
187 public static void protocols() {
188 String[] names = Configuration.getNames("protocol");
190 protocolsName = new Protocol[names.length];
191 pid = new int[names.length];
192 boolean save = false;
193 for (int i = 0; i < names.length; i++) {
194 protocolsName[i] = (Protocol) Configuration.getInstance(names[i]);
195 if (i == names.length - 1)
197 userProtocol(protocolsName[i], names[i], save);
199 protocolsPidsMap.put(protocolsName[i], pid[i]);
205 * Lookup CDProtocol and EDProtocol among all protocols
212 * parameter equal true when parsing all protocols
214 public static void userProtocol(Protocol prot, String names, boolean save) {
215 Class[] interfaces = prot.getClass().getInterfaces();
216 for (int j = 0; j < interfaces.length; j++) {
217 if (interfaces[j].getName().endsWith("EDProtocol")) {
220 if (interfaces[j].getName().endsWith("CDProtocol")) {
221 String protName = names.substring("protocol".length() + 1);
222 long step = Configuration.getLong("protocol" + "." + protName
224 cdProtocolsStepMap.put(prot, psToSgTime(step));
235 private static void edProt() {
237 hostList = Host.all();
238 for (int i = 0; i < PSGSimulator.size; i++) {
239 hostVal = hostList[i];
240 Element process = new Element("process");
241 racine.addContent(process);
242 Attribute host = new Attribute("host", hostVal.getName());
243 Attribute function = new Attribute("function",
244 "psgsim.PSGProcessEvent");
245 process.setAttribute(host);
246 process.setAttribute(function);
248 save(deploymentFile);
256 private static void cdProt() {
257 for (int i = 0; i < PSGSimulator.size; i++) {
258 Element process = new Element("process");
259 racine.addContent(process);
260 Attribute host = new Attribute("host", String.valueOf(i));
261 Attribute function = new Attribute("function",
262 "psgsim.PSGProcessCycle");
263 process.setAttribute(host);
264 process.setAttribute(function);
267 save("deployment.xml");
272 * Reads given configuration property: "platform". If not found, returns the
275 * @return the platform file
277 public static String platformFile() {
278 String defFile = platformFile;
279 String file = Configuration.getString("platform", defFile);
284 * Reads given configuration property: "unit". If not found, returns the
285 * default value (ms).
287 * @return the unit of measure
289 public static timeUnit unit() {
290 String defUnit = sec;
291 String unit = Configuration.getString("unit", defUnit);
292 timeUnit t = timeUnit.valueOf(unit);
297 * Create the deployment file
300 * the name of the deployment file
302 public static void save(String file) {
304 // On utilise ici un affichage classique avec getPrettyFormat()
305 XMLOutputter out = new XMLOutputter(Format.getPrettyFormat());
306 out.output(document, new FileOutputStream(file));
307 } catch (java.io.IOException e) {
312 * Delete the deployment file
315 * the path of the deployment file
317 public static void delete(String path) {
318 File file = new File(path);
321 } catch (Exception e) {
322 System.err.println("deployment file not found");
325 System.err.println(path + "file deleted");