Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction of some bugs and performance enhancement.
[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 = null ;
78                 ref = stub;
79         }
80
81         public synchronized void setSuperNodeStub(JaceSuperNodeInterface stub) {
82                 refSuperNode = null ;
83                 refSuperNode = stub;
84         }
85
86         public JaceSuperNodeInterface getSuperNodeStub() {
87                 return refSuperNode;
88         }
89
90         public synchronized void setSuperNodeIP(String IP) {
91                 superNodeIP = null ;
92                 superNodeIP = IP;
93         }
94
95         public synchronized JaceInterface getStub() {
96                 return ref;
97         }
98
99         public synchronized void setStartedThreads(boolean b) {
100                 startedThreads = b;
101         }
102
103         public synchronized boolean getStartedThreads() {
104                 return startedThreads;
105         }
106 }
107
108 /** ! **/