Logo AND Algorithmique Numérique Distribuée

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