Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Correction and modification of save mechanisms.
[hpcvm.git] / src / InGuest.java
1 import java.io.BufferedReader;
2 import java.io.BufferedWriter;
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.io.OutputStreamWriter;
7 import java.io.PrintWriter;
8 import java.net.InetAddress;
9 import java.net.Socket;
10 import java.net.UnknownHostException;
11
12
13 public class InGuest
14 {
15         
16         private static String name ;
17         private static String IP ;
18         private static String path = "/tmp/vm_host_ip" ;
19         private static FileReader fr ;
20         private static String host_ip ;
21         private static String host_port ;
22         
23         public static void main( String argv[] )
24         {
25                 System.out.println( "Running information program for HpcVm ..." ) ;
26                 
27                 /** Retrieving informations **/
28                 try {
29                         InetAddress ia = InetAddress.getLocalHost() ;
30                         name = ia.getCanonicalHostName() ;
31                         IP = ia.getHostAddress() ;
32                 } catch( Exception e ) {
33                         System.err.println( "Error: Unknown Host: " + e ) ;
34                 }
35                 
36                 /** Retrieving host informations **/
37                 try {
38                         fr = new FileReader( path ) ;
39                 } catch( FileNotFoundException e ) {
40                         e.printStackTrace() ;
41                 }
42                 BufferedReader bf = new BufferedReader( fr ) ;
43                 
44                 String line = null ;
45                 
46                 try {
47                         line = bf.readLine() ;
48                 } catch( IOException e ) {
49                         e.printStackTrace() ;
50                 }
51                 
52                 String[] infos = line.split( " " ) ;
53                 
54                 try {
55                         host_ip = infos[0] ;
56                         host_port = infos[1] ;
57                 } catch( Exception e ) {
58                         e.printStackTrace() ;
59                 }
60                 
61                 /* Close file */
62                 try {
63                         bf.close() ;
64                         fr.close() ;
65                 } catch( IOException e ) {
66                         e.printStackTrace() ;
67                 }
68                 
69                 
70                 /** Sending informations to host **/
71                 try {
72                         Socket socket = new Socket( host_ip, Integer.parseInt( host_port ) ) ;
73                         
74                         PrintWriter pw = new PrintWriter( new BufferedWriter( 
75                                         new OutputStreamWriter( socket.getOutputStream() ) ), true ) ;
76                         
77                         pw.print( "infos" ) ;
78                         
79                         /* Sending name */
80                         pw.print( name ) ;
81                         
82                         /* Sending IP */
83                         pw.print( IP ) ;
84                         
85                         
86                         /* Close streams */
87                         pw.close() ;
88                         socket.close() ;
89                         
90                         
91                 } catch( NumberFormatException e ) {
92                         e.printStackTrace();
93                 } catch( UnknownHostException e ) {
94                         e.printStackTrace() ;
95                 } catch( IOException e ) {
96                         e.printStackTrace() ;
97                 }
98                 
99                 System.out.println( "... Ok" ) ;
100         }
101 }