/Users/lyon/j4p/src/net/rmi/rmiSynth/Host.java

1    package net.rmi.rmiSynth; 
2     
3     
4    import gui.run.RunJob; 
5     
6    import java.io.Serializable; 
7    import java.net.InetAddress; 
8    import java.net.UnknownHostException; 
9    import java.rmi.RemoteException; 
10    
11   /** 
12    * The <code>HostManager</code> is invoked 
13    * remotely by unicast remote protocol. New 
14    * workers that enter into the grid must know the 
15    * IP addBk.address of the HostManager 
16    */ 
17    
18   public class Host implements Serializable { 
19       private InetAddress localAddress = null; 
20    
21    
22       // The timeOfUpdate is used by the HostManager to 
23       // determine if the lease has gui.run out on the host 
24       private long timeOfUpdate = 0; 
25    
26    
27       private void registerWithHostManager() { 
28    
29           try { 
30               HostManagerInterface hmi = getProxy(); 
31               System.out.println( 
32                       "I have the proxy to the hostmanagerinterface"); 
33               registerWithHostManager(hmi); 
34               System.out.println( 
35                       "I have registerd with the host manager"); 
36           } catch (RemoteException e) { 
37               System.out.println( 
38                       "could not add host, process dies!"); 
39               System.exit(0); 
40           } 
41    
42       } 
43    
44       private void registerWithHostManager( 
45               HostManagerInterface hmi) 
46               throws RemoteException { 
47           hmi.add(this); 
48       } 
49    
50       private HostManagerInterface getProxy() 
51               throws RemoteException { 
52           HostManagerInterface hmi = 
53                   HostManager.getProxy(); 
54           return hmi; 
55       } 
56    
57    
58       private void runBenchMark() { 
59           for (int i = 0; i < 10000; i++) 
60               Math.sin(i); 
61       } 
62    
63       public double getBenchMark() { 
64           long startTime = System.currentTimeMillis(); 
65           runBenchMark(); 
66           long stopTime = System.currentTimeMillis(); 
67           double seconds = (stopTime - startTime) / 
68                            1000.0; 
69           return seconds; 
70       } 
71    
72       public String toString() { 
73           return localAddress.toString(); 
74       } 
75    
76       /** 
77        * return the local addBk.address. 
78        */ 
79       public String getIP() { 
80           return localAddress.getHostAddress(); 
81       } 
82    
83       public boolean equals(Host h) { 
84           System.out.println("h1=" + h.getIP()); 
85           System.out.println("h2=" + getIP()); 
86           return getIP().equals(h.getIP()); 
87       } 
88    
89       public Host() { 
90           try { 
91               System.out.println( 
92                       "new host instance..."); 
93               localAddress = getAddress(); 
94               System.out.println("addBk.address:" + 
95                                  localAddress); 
96           } catch (UnknownHostException e) { 
97               e.printStackTrace(); 
98           } 
99       } 
100   
101      public static void main(String args[]) { 
102          final Host h = new Host(); 
103          System.out.println( 
104                  "host ip=" + h.getIP()); 
105   
106          new RunJob(1) { 
107              public void run() { 
108                  h.registerWithHostManager(); 
109              } 
110          }; 
111   
112      } 
113   
114   
115      /** 
116       * print(Host.getAddress()) to see the local 
117       * IP. 
118       */ 
119      public static InetAddress getAddress() 
120              throws UnknownHostException { 
121          InetAddress ia = InetAddress.getLocalHost(); 
122          return ia; 
123      } 
124   
125      public long getTimeOfUpdate() { 
126          return timeOfUpdate; 
127      } 
128   
129      public void setTimeOfUpdate( 
130              long timeSinceUpdate) { 
131          this.timeOfUpdate = timeSinceUpdate; 
132      } 
133  } 
134