Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
peersimgrid release 1.0
[simgrid.git] / contrib / psg / src / psgsim / NodeHost.java
1 package psgsim;
2
3 import org.simgrid.msg.Host;
4 import peersim.core.Network;
5 import peersim.core.Node;
6
7 import java.util.Comparator;
8 import java.util.Map;
9 import java.util.TreeMap;
10
11 /**
12  * 
13  * NodeHost class used to make the mapping Node-Host.
14  * 
15  * @author Khaled Baati 26/10/2014
16  * @version version 1.1
17  */
18 public class NodeHost {
19
20         /**
21          * A collection of map contained the couple (host,node)
22          */
23         public static TreeMap<Node, Host> mapHostNode = new TreeMap<Node, Host>(
24                         new Comparator<Node>() {
25                                 public int compare(Node n1, Node n2) {
26                                         return String.valueOf(n1.getID()).compareTo(
27                                                         String.valueOf(n2.getID()));
28                                 }
29                         });
30
31         /**
32          * The main method to make the mapping Node to Host in the
33          * {@link #mapHostNode} field
34          */
35         public static void start() {
36                 Host host = null;
37                 for (Integer i = 0; i < PSGSimulator.size; i++) {
38                         host = PSGPlatform.hostList[i];
39                         mapHostNode.put(Network.get(i), host);
40                 }
41         }
42
43         /**
44          * This static method gets a Node instance associated with a host of your
45          * platform.
46          * 
47          * @param host
48          *            The host associated in your platform.
49          * @return The node associated.
50          */
51         public static Node getNode(Host host) {
52                 for (Map.Entry<Node, Host> element : mapHostNode.entrySet()) {
53                         if (element.getValue() == host)
54                                 return element.getKey();
55                 }
56                 return null;
57         }
58
59         /**
60          * This static method gets a host instance associated with the node of your
61          * platform.
62          * 
63          * @param node
64          *            The node associated in your platform.
65          * @return The host associated, else return null (host doesn't exist).
66          */
67         public static Host getHost(Node node) {
68                 for (Map.Entry<Node, Host> element : mapHostNode.entrySet()) {
69                         if (element.getKey() == node)
70                                 return element.getValue();
71                 }
72                 return null;
73         }
74 }