1 |
ab11 |
1.2 |
//---PACKAGE DECLARATION--- |
2 |
ab11 |
1.1 |
|
3 |
ab11 |
1.2 |
//---IMPORTS--- |
4 |
|
|
|
5 |
|
|
/** |
6 |
|
|
* A small efficient JavaHost |
7 |
|
|
* Designed to be used as a working design to build the C++ hosts on. |
8 |
|
|
* Hopefully fully functional and thus can be used as part of a test rig. |
9 |
|
|
* |
10 |
tdb |
1.6 |
* @author $Author: ab11 $ |
11 |
|
|
* @version $Id: JavaHost.java,v 1.5 2000/12/08 15:44:56 ab11 Exp $ |
12 |
ab11 |
1.2 |
*/ |
13 |
ab11 |
1.1 |
class JavaHost { |
14 |
ab11 |
1.2 |
|
15 |
|
|
//---FINAL ATTRIBUTES--- |
16 |
|
|
|
17 |
|
|
//---STATIC METHODS--- |
18 |
|
|
|
19 |
|
|
//---CONSTRUCTORS--- |
20 |
|
|
|
21 |
|
|
/** |
22 |
|
|
* Constructor for the class. Takes in the hostname and port number |
23 |
tdb |
1.4 |
* of the Filter Manager. |
24 |
ab11 |
1.2 |
* |
25 |
|
|
*/ |
26 |
|
|
public JavaHost( String serverName, int port ){ |
27 |
tdb |
1.4 |
// create a connection to the filter manager |
28 |
|
|
System.out.println("Creating connection with Filter Manager"); |
29 |
ab11 |
1.2 |
Config config = new Config(serverName, port); |
30 |
|
|
// the config class knows what values it wants to get from |
31 |
tdb |
1.4 |
// the configuration system |
32 |
ab11 |
1.2 |
|
33 |
tdb |
1.4 |
// create a SystemMonitor object and pass the config |
34 |
ab11 |
1.2 |
// as a param so it knows what to do! |
35 |
ab11 |
1.3 |
System.out.println("Creating System Monitor"); |
36 |
ab11 |
1.2 |
SystemMonitor sysMon = new SystemMonitor(config); |
37 |
|
|
|
38 |
ab11 |
1.5 |
try { |
39 |
|
|
udpcheckInterval = Long.parseLong(config.getProperty("UDPUpdateTime")) * 1000; |
40 |
|
|
System.out.println("Configured UDPUpdateTime"); |
41 |
|
|
} |
42 |
|
|
catch ( NumberFormatException e ){ |
43 |
|
|
System.out.println("The value for UDPUpdateTime is invalid, using a default"); |
44 |
|
|
// 1 mins |
45 |
|
|
udpcheckInterval = 1000 * 60; // 1 minute |
46 |
|
|
} |
47 |
|
|
|
48 |
|
|
try { |
49 |
|
|
tcpcheckInterval = Long.parseLong(config.getProperty("TCPUpdateTime")) * 1000; |
50 |
|
|
System.out.println("Configured TCPUpdateTime"); |
51 |
|
|
} |
52 |
|
|
catch ( NumberFormatException e ){ |
53 |
|
|
System.out.println("The value for UDPUpdateTime is invalid, using a default"); |
54 |
|
|
// 10 mins |
55 |
|
|
tcpcheckInterval = 10000 * 60; // 5 minutes |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
// the current time.. |
59 |
|
|
long currentTime = System.currentTimeMillis(); |
60 |
|
|
long nextUDP = currentTime + udpcheckInterval; |
61 |
|
|
long nextTCP = currentTime + tcpcheckInterval; |
62 |
|
|
|
63 |
ab11 |
1.2 |
while ( true ){ |
64 |
ab11 |
1.5 |
currentTime = System.currentTimeMillis(); |
65 |
ab11 |
1.2 |
// keep going for ever and ever ;) |
66 |
ab11 |
1.5 |
|
67 |
|
|
if ( nextTCP < nextUDP ){ |
68 |
|
|
try { |
69 |
|
|
long sleeptime = nextTCP - currentTime; |
70 |
|
|
if ( sleeptime > 0 ){ |
71 |
|
|
Thread.sleep(sleeptime); |
72 |
|
|
} |
73 |
|
|
nextTCP += tcpcheckInterval; |
74 |
|
|
// send a heartbeat |
75 |
tdb |
1.6 |
//System.out.println("Sending Heartbeat"); |
76 |
|
|
System.out.print("+"); |
77 |
ab11 |
1.5 |
config.sendHeartBeat(); |
78 |
|
|
if ( config.reloadConfig() ){ |
79 |
tdb |
1.6 |
System.out.println("\nRestarting System"); |
80 |
ab11 |
1.5 |
break; |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
catch ( InterruptedException e ){ |
84 |
|
|
// do nothing |
85 |
|
|
} |
86 |
ab11 |
1.3 |
} |
87 |
ab11 |
1.5 |
else { |
88 |
|
|
try { |
89 |
|
|
long sleeptime = nextUDP - currentTime; |
90 |
|
|
if ( sleeptime > 0 ){ |
91 |
|
|
Thread.sleep(sleeptime); |
92 |
|
|
} |
93 |
|
|
nextUDP += udpcheckInterval; |
94 |
|
|
// send a heartbeat |
95 |
tdb |
1.6 |
//System.out.println("Sending UDP Packet"); |
96 |
|
|
System.out.print("."); |
97 |
ab11 |
1.5 |
LowLevelNet.sendUDPPacket(config, sysMon.getInfo()); |
98 |
|
|
|
99 |
|
|
} |
100 |
|
|
catch ( InterruptedException e ){ |
101 |
|
|
// do nothing |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
|
106 |
|
|
|
107 |
ab11 |
1.3 |
} // while |
108 |
ab11 |
1.2 |
|
109 |
ab11 |
1.3 |
|
110 |
ab11 |
1.2 |
} // public javahost |
111 |
|
|
|
112 |
|
|
|
113 |
|
|
//---PUBLIC METHODS--- |
114 |
|
|
|
115 |
|
|
//---PRIVATE METHODS--- |
116 |
|
|
|
117 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
118 |
|
|
|
119 |
|
|
//---ATTRIBUTES--- |
120 |
ab11 |
1.5 |
|
121 |
|
|
private long udpcheckInterval; |
122 |
|
|
private long tcpcheckInterval; |
123 |
|
|
private final long defaultUpdateTime = 60000; |
124 |
|
|
|
125 |
ab11 |
1.2 |
|
126 |
|
|
//---STATIC ATTRIBUTES--- |
127 |
|
|
|
128 |
tdb |
1.6 |
} // class |