Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Energy, onHostDestruction: ensured ptr existence
[simgrid.git] / contrib / psg / src / psgsim / PSGProcessController.java
1 package psgsim;
2
3 import java.util.LinkedHashMap;
4 import java.util.Map;
5 import org.simgrid.msg.Host;
6 import org.simgrid.msg.MsgException;
7
8 import peersim.core.Control;
9
10 /**
11  * This class executes all controls object scheduled in the
12  * {@link PSGPlatform#controlStepMap} collection.
13  * 
14  * @author Khaled Baati 10/12/2014
15  * @version version 1.1
16  */
17 public class PSGProcessController extends org.simgrid.msg.Process {
18         
19         private Map<Control, Double> controlStepMapTmp = new LinkedHashMap<Control, Double>();
20
21         public PSGProcessController(Host host, String name, String[] args) {
22                 super(host, name, null);
23         }
24
25         @Override
26         public void main(String[] args) throws MsgException {
27                 Double nextControlEvent;
28                 for (Map.Entry<Control, Double> entry : PSGPlatform.controlStepMap
29                                 .entrySet()) {
30                         controlStepMapTmp.put(entry.getKey(), entry.getValue());
31                 }
32                 while (PSGPlatform.getTime() <= PSGPlatform.getDuration()) {
33                         for (Map.Entry<Control, Double> entrytmp : controlStepMapTmp
34                                         .entrySet()) {
35                                 Control cle = entrytmp.getKey();
36                                 Double valeur = entrytmp.getValue();
37                                 if (PSGPlatform.getTime() % valeur == 0) {
38                                         cle.execute();
39                                         if (PSGPlatform.getTime() != 0)
40                                                 for (Map.Entry<Control, Double> entry : PSGPlatform.controlStepMap
41                                                                 .entrySet()) {
42                                                         if (cle == entry.getKey())
43                                                                 controlStepMapTmp.replace(cle, valeur, valeur
44                                                                                 + entry.getValue());
45
46                                                 }
47                                 }
48                         }
49                         nextControlEvent = next();
50                         if (nextControlEvent + PSGPlatform.getTime() >= PSGPlatform.getDuration()) {
51                                 break;
52                         } else {
53                                 waitFor(nextControlEvent);
54                         }
55                 }
56         }
57
58         private Double next() {
59                 Double min = controlStepMapTmp.values().iterator().next();
60                 for (Map.Entry<Control, Double> entry : controlStepMapTmp.entrySet()) {
61                         Double valeur = (entry.getValue() - PSGPlatform.getClock());
62                         if (min > valeur)
63                                 min = valeur;
64                 }
65
66                 return min;
67         }
68 }