1 |
tdb |
1.8 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
tdb |
1.9 |
* http://www.i-scream.org.uk |
4 |
tdb |
1.8 |
* Copyright (C) 2000-2002 i-scream |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
ab11 |
1.2 |
//---PACKAGE DECLARATION--- |
22 |
ab11 |
1.1 |
|
23 |
ab11 |
1.2 |
//---IMPORTS--- |
24 |
|
|
|
25 |
|
|
/** |
26 |
|
|
* A small efficient JavaHost |
27 |
|
|
* Designed to be used as a working design to build the C++ hosts on. |
28 |
|
|
* Hopefully fully functional and thus can be used as part of a test rig. |
29 |
|
|
* |
30 |
tdb |
1.9 |
* @author $Author: tdb $ |
31 |
tdb |
1.10 |
* @version $Id: JavaHost.java,v 1.9 2002/05/21 16:47:12 tdb Exp $ |
32 |
ab11 |
1.2 |
*/ |
33 |
ab11 |
1.1 |
class JavaHost { |
34 |
ab11 |
1.2 |
|
35 |
|
|
//---FINAL ATTRIBUTES--- |
36 |
|
|
|
37 |
|
|
//---STATIC METHODS--- |
38 |
|
|
|
39 |
|
|
//---CONSTRUCTORS--- |
40 |
|
|
|
41 |
|
|
/** |
42 |
|
|
* Constructor for the class. Takes in the hostname and port number |
43 |
tdb |
1.4 |
* of the Filter Manager. |
44 |
ab11 |
1.2 |
* |
45 |
|
|
*/ |
46 |
|
|
public JavaHost( String serverName, int port ){ |
47 |
tdb |
1.4 |
// create a connection to the filter manager |
48 |
|
|
System.out.println("Creating connection with Filter Manager"); |
49 |
ab11 |
1.2 |
Config config = new Config(serverName, port); |
50 |
|
|
// the config class knows what values it wants to get from |
51 |
tdb |
1.4 |
// the configuration system |
52 |
ab11 |
1.2 |
|
53 |
tdb |
1.4 |
// create a SystemMonitor object and pass the config |
54 |
ab11 |
1.2 |
// as a param so it knows what to do! |
55 |
ab11 |
1.3 |
System.out.println("Creating System Monitor"); |
56 |
ab11 |
1.2 |
SystemMonitor sysMon = new SystemMonitor(config); |
57 |
|
|
|
58 |
ab11 |
1.5 |
try { |
59 |
ab11 |
1.7 |
udpcheckInterval = Long.parseLong(config.getProperty("UDPUpdateTime")) * 1000; |
60 |
|
|
System.out.println("Configured UDPUpdateTime"); |
61 |
ab11 |
1.5 |
} |
62 |
|
|
catch ( NumberFormatException e ){ |
63 |
ab11 |
1.7 |
System.out.println("The value for UDPUpdateTime is invalid, using a default"); |
64 |
|
|
// 1 mins |
65 |
|
|
udpcheckInterval = 1000 * 60; // 1 minute |
66 |
ab11 |
1.5 |
} |
67 |
|
|
|
68 |
|
|
try { |
69 |
ab11 |
1.7 |
tcpcheckInterval = Long.parseLong(config.getProperty("TCPUpdateTime")) * 1000; |
70 |
|
|
System.out.println("Configured TCPUpdateTime"); |
71 |
ab11 |
1.5 |
} |
72 |
|
|
catch ( NumberFormatException e ){ |
73 |
ab11 |
1.7 |
System.out.println("The value for UDPUpdateTime is invalid, using a default"); |
74 |
|
|
// 10 mins |
75 |
|
|
tcpcheckInterval = 10000 * 60; // 5 minutes |
76 |
ab11 |
1.5 |
} |
77 |
|
|
|
78 |
|
|
// the current time.. |
79 |
|
|
long currentTime = System.currentTimeMillis(); |
80 |
|
|
long nextUDP = currentTime + udpcheckInterval; |
81 |
|
|
long nextTCP = currentTime + tcpcheckInterval; |
82 |
|
|
|
83 |
ab11 |
1.2 |
while ( true ){ |
84 |
ab11 |
1.5 |
currentTime = System.currentTimeMillis(); |
85 |
ab11 |
1.2 |
// keep going for ever and ever ;) |
86 |
ab11 |
1.5 |
|
87 |
|
|
if ( nextTCP < nextUDP ){ |
88 |
ab11 |
1.7 |
try { |
89 |
|
|
long sleeptime = nextTCP - currentTime; |
90 |
|
|
if ( sleeptime > 0 ){ |
91 |
|
|
Thread.sleep(sleeptime); |
92 |
|
|
} |
93 |
|
|
nextTCP += tcpcheckInterval; |
94 |
|
|
// send a heartbeat |
95 |
|
|
//System.out.println("Sending Heartbeat"); |
96 |
tdb |
1.6 |
System.out.print("+"); |
97 |
ab11 |
1.7 |
config.sendHeartBeat(); |
98 |
|
|
if ( config.reloadConfig() ){ |
99 |
|
|
System.out.println("\nRestarting System"); |
100 |
|
|
break; |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
catch ( InterruptedException e ){ |
104 |
|
|
// do nothing |
105 |
|
|
} |
106 |
ab11 |
1.3 |
} |
107 |
ab11 |
1.5 |
else { |
108 |
ab11 |
1.7 |
try { |
109 |
|
|
long sleeptime = nextUDP - currentTime; |
110 |
|
|
if ( sleeptime > 0 ){ |
111 |
|
|
Thread.sleep(sleeptime); |
112 |
|
|
} |
113 |
|
|
nextUDP += udpcheckInterval; |
114 |
|
|
// send a heartbeat |
115 |
|
|
//System.out.println("Sending UDP Packet"); |
116 |
|
|
System.out.print("."); |
117 |
|
|
LowLevelNet.sendUDPPacket(config, sysMon.getInfo()); |
118 |
ab11 |
1.5 |
|
119 |
ab11 |
1.7 |
} |
120 |
|
|
catch ( InterruptedException e ){ |
121 |
|
|
// do nothing |
122 |
|
|
} |
123 |
ab11 |
1.5 |
} |
124 |
|
|
|
125 |
|
|
|
126 |
|
|
|
127 |
ab11 |
1.3 |
} // while |
128 |
ab11 |
1.2 |
|
129 |
ab11 |
1.3 |
|
130 |
ab11 |
1.2 |
} // public javahost |
131 |
|
|
|
132 |
|
|
|
133 |
|
|
//---PUBLIC METHODS--- |
134 |
|
|
|
135 |
|
|
//---PRIVATE METHODS--- |
136 |
|
|
|
137 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
138 |
|
|
|
139 |
|
|
//---ATTRIBUTES--- |
140 |
ab11 |
1.5 |
|
141 |
|
|
private long udpcheckInterval; |
142 |
|
|
private long tcpcheckInterval; |
143 |
|
|
private final long defaultUpdateTime = 60000; |
144 |
|
|
|
145 |
ab11 |
1.2 |
|
146 |
|
|
//---STATIC ATTRIBUTES--- |
147 |
|
|
|
148 |
tdb |
1.6 |
} // class |