Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
89dc10716afcf47f834a9dd31016354571c429b6
[jaceP2P.git] / src / jaceP2P / LocalHost.java
1 package jaceP2P ;
2
3 import java.net.InetAddress;
4
5 public class LocalHost {
6
7         // attributes
8         private static LocalHost Instance;
9         private static String name;
10         private String IP;
11         private int port;
12         private int socketPort = 1097;
13         private static JaceInterface ref = null;
14         private static JaceSuperNodeInterface refSuperNode = null;
15         @SuppressWarnings("unused")
16         private static String superNodeIP = null;
17         private boolean startedThreads = false;
18
19         private LocalHost() {
20                 try {
21                         InetAddress ia = InetAddress.getLocalHost();
22                         name = ia.getCanonicalHostName();
23                         IP = ia.getHostAddress();
24                 } catch (Exception e) {
25                         System.err.println("Jace Error: Unknown Host: " + e);
26                 }
27         }
28
29         public int getSocketPort() {
30                 return socketPort;
31         }
32
33         public synchronized static LocalHost Instance() {
34                 if (Instance == null) {
35                         Instance = new LocalHost();
36                 }
37                 return Instance;
38         }
39
40         public void kill() {
41                 Instance = null;
42         }
43
44         /*
45          * public void setSender(Thread t){ sender=t; } public Thread getSender(){
46          * return sender; }
47          */
48
49         public String resolve(String name) {
50                 String ip = null;
51                 try {
52                         ip = InetAddress.getByName(name).getHostAddress();
53                         // System.out.println("resolve : "+ip);
54                 } catch (java.net.UnknownHostException e) {
55                         System.err.println("Cannot find IP address of " + name + " :" + e);
56                 }
57                 return ip;
58         }
59
60         public synchronized void setPort(int portOfComm) {
61                 port = portOfComm;
62         }
63
64         public int getPort() {
65                 return port;
66         }
67
68         public synchronized String getName() {
69                 return name;
70         }
71
72         public synchronized String getIP() {
73                 return IP;
74         }
75
76         public synchronized void setStub(JaceInterface stub) {
77                 ref = stub;
78         }
79
80         public synchronized void setSuperNodeStub(JaceSuperNodeInterface stub) {
81                 refSuperNode = stub;
82         }
83
84         public JaceSuperNodeInterface getSuperNodeStub() {
85                 return refSuperNode;
86         }
87
88         public synchronized void setSuperNodeIP(String IP) {
89                 superNodeIP = IP;
90         }
91
92         public synchronized JaceInterface getStub() {
93                 return ref;
94         }
95
96         public synchronized void setStartedThreads(boolean b) {
97                 startedThreads = b;
98         }
99
100         public synchronized boolean getStartedThreads() {
101                 return startedThreads;
102         }
103 }
104
105 /** ! **/