| 1 |
/* |
| 2 |
* i-scream central monitoring system |
| 3 |
* http://www.i-scream.org.uk |
| 4 |
* 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 |
//---PACKAGE DECLARATION--- |
| 22 |
|
| 23 |
//---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 |
* @author $Author: tdb $ |
| 31 |
* @version $Id: JavaHost.java,v 1.9 2002/05/21 16:47:12 tdb Exp $ |
| 32 |
*/ |
| 33 |
class JavaHost { |
| 34 |
|
| 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 |
* of the Filter Manager. |
| 44 |
* |
| 45 |
*/ |
| 46 |
public JavaHost( String serverName, int port ){ |
| 47 |
// create a connection to the filter manager |
| 48 |
System.out.println("Creating connection with Filter Manager"); |
| 49 |
Config config = new Config(serverName, port); |
| 50 |
// the config class knows what values it wants to get from |
| 51 |
// the configuration system |
| 52 |
|
| 53 |
// create a SystemMonitor object and pass the config |
| 54 |
// as a param so it knows what to do! |
| 55 |
System.out.println("Creating System Monitor"); |
| 56 |
SystemMonitor sysMon = new SystemMonitor(config); |
| 57 |
|
| 58 |
try { |
| 59 |
udpcheckInterval = Long.parseLong(config.getProperty("UDPUpdateTime")) * 1000; |
| 60 |
System.out.println("Configured UDPUpdateTime"); |
| 61 |
} |
| 62 |
catch ( NumberFormatException e ){ |
| 63 |
System.out.println("The value for UDPUpdateTime is invalid, using a default"); |
| 64 |
// 1 mins |
| 65 |
udpcheckInterval = 1000 * 60; // 1 minute |
| 66 |
} |
| 67 |
|
| 68 |
try { |
| 69 |
tcpcheckInterval = Long.parseLong(config.getProperty("TCPUpdateTime")) * 1000; |
| 70 |
System.out.println("Configured TCPUpdateTime"); |
| 71 |
} |
| 72 |
catch ( NumberFormatException e ){ |
| 73 |
System.out.println("The value for UDPUpdateTime is invalid, using a default"); |
| 74 |
// 10 mins |
| 75 |
tcpcheckInterval = 10000 * 60; // 5 minutes |
| 76 |
} |
| 77 |
|
| 78 |
// the current time.. |
| 79 |
long currentTime = System.currentTimeMillis(); |
| 80 |
long nextUDP = currentTime + udpcheckInterval; |
| 81 |
long nextTCP = currentTime + tcpcheckInterval; |
| 82 |
|
| 83 |
while ( true ){ |
| 84 |
currentTime = System.currentTimeMillis(); |
| 85 |
// keep going for ever and ever ;) |
| 86 |
|
| 87 |
if ( nextTCP < nextUDP ){ |
| 88 |
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 |
System.out.print("+"); |
| 97 |
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 |
} |
| 107 |
else { |
| 108 |
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 |
|
| 119 |
} |
| 120 |
catch ( InterruptedException e ){ |
| 121 |
// do nothing |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
} // while |
| 128 |
|
| 129 |
|
| 130 |
} // public javahost |
| 131 |
|
| 132 |
|
| 133 |
//---PUBLIC METHODS--- |
| 134 |
|
| 135 |
//---PRIVATE METHODS--- |
| 136 |
|
| 137 |
//---ACCESSOR/MUTATOR METHODS--- |
| 138 |
|
| 139 |
//---ATTRIBUTES--- |
| 140 |
|
| 141 |
private long udpcheckInterval; |
| 142 |
private long tcpcheckInterval; |
| 143 |
private final long defaultUpdateTime = 60000; |
| 144 |
|
| 145 |
|
| 146 |
//---STATIC ATTRIBUTES--- |
| 147 |
|
| 148 |
} // class |