Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / contrib / psg / src / psgsim / PSGSimulator.java
1 package psgsim;
2
3 import org.simgrid.msg.Host;
4 import org.simgrid.msg.HostNotFoundException;
5 import org.simgrid.msg.Msg;
6 import org.simgrid.msg.NativeException;
7
8 import peersim.core.CommonState;
9 import peersim.core.Network;
10 import peersim.core.Node;
11
12 /**
13  * This is the main entry point to Simgrid simulator. This class loads the
14  * different parameters and initializes the simulation.
15  * 
16  * @author Khaled Baati 14/10/2014
17  * @version version 1.1
18  */
19 public class PSGSimulator {
20         public static int size;
21
22         static {
23                 Network.reset();
24                 size = Network.size();
25         }
26
27         // ========================== methods ==================================
28         // =====================================================================
29
30         /**
31          * Adds a new event to be scheduled, specifying the number of time units of
32          * delay (in seconds), the node and the protocol identifier to which the
33          * event will be delivered. A {@link psgsim.PSGProcessLauncher} process will
34          * be created according to this event.
35          * 
36          * @param delay
37          *            The number of time units (seconds in simgrid) before the event
38          *            is scheduled.
39          * @param event
40          *            The object associated to this event
41          * @param src
42          *            The node associated to the event.
43          * @param pid
44          *            The identifier of the protocol to which the event will be
45          *            delivered
46          */
47         public static void add(long delay, Object event, Node src, int pid) {
48                 Host host = NodeHost.getHost(src);
49                 double startTime = PSGPlatform.psToSgTime(delay) + Msg.getClock(); 
50                 if (startTime < PSGPlatform.psToSgTime(PSGPlatform.getDuration()) ) {
51                         try {
52                                 /**
53                                  * random instruction associated to Heap.add(...) method in
54                                  * peersim.edsim
55                                  **/
56                                 CommonState.r.nextInt(1 << 8);
57                                 new PSGProcessLauncher(host, host.getName(), startTime, event,
58                                                 pid).start();
59                         } catch (HostNotFoundException e) {
60                                 System.err.println("Host not found");
61                         }
62                 }
63
64         }
65
66         // ========================== main method ==================================
67         // =====================================================================
68         public static void main() throws NativeException, HostNotFoundException {
69
70                 String platformfile = PSGPlatform.platformFile();
71                 System.err.println(platformfile + " loaded");
72                 String[] arguments = { platformfile, "deployment.xml" };
73                 Msg.init(arguments);
74
75                 /** construct the platform */
76                 Msg.createEnvironment(arguments[0]);
77
78                 PSGPlatform.protocols();
79
80                 /** deploy the application **/
81                 Msg.deployApplication(arguments[1]);
82
83                 /** construct the host-node mapping **/
84                 NodeHost.start();
85
86                 /** Process Controller **/
87                 PSGPlatform.control();
88                 if (!PSGPlatform.controlStepMap.isEmpty())
89                         new PSGProcessController(PSGPlatform.hostList[0],
90                                         PSGPlatform.hostList[0].getName(), null).start();
91
92                 /** Load and execute the initializers classes in the configuration file **/
93                 PSGPlatform.init();
94
95                 PSGPlatform.delete("deployment.xml");
96                 /** execute the simulation. **/
97                 Msg.run();
98         }
99 }