001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tc.process;
006:
007: import java.io.BufferedReader;
008: import java.io.InputStreamReader;
009: import java.io.PrintWriter;
010: import java.net.Socket;
011: import java.net.SocketTimeoutException;
012: import java.text.DateFormat;
013: import java.text.SimpleDateFormat;
014: import java.util.Date;
015:
016: public class HeartBeatClient extends Thread {
017: private static final int HEARTBEAT_TIMEOUT = HeartBeatServer.PULSE_INTERVAL * 2;
018: private static DateFormat DATEFORMAT = new SimpleDateFormat(
019: "HH:mm:ss.SSS");
020:
021: private Socket socket;
022: private boolean isAppServer = false;
023: private String clientName;
024: private int missedPulse = 0;
025:
026: public HeartBeatClient(int listenPort, String clientName,
027: boolean isAppServer) {
028: this .isAppServer = isAppServer;
029: this .clientName = clientName;
030: try {
031: socket = new Socket("localhost", listenPort);
032: socket.setSoTimeout(HEARTBEAT_TIMEOUT);
033: socket.setTcpNoDelay(true);
034: } catch (Exception e) {
035: throw new RuntimeException(e);
036: }
037: }
038:
039: public static void log(String message) {
040: System.out.println(DATEFORMAT.format(new Date())
041: + " - HeartBeatClient: " + message);
042: }
043:
044: public void run() {
045: try {
046: BufferedReader in = new BufferedReader(
047: new InputStreamReader(socket.getInputStream()));
048: PrintWriter out = new PrintWriter(socket.getOutputStream(),
049: true);
050:
051: // introduce myself to the server
052: // sending clientName
053: out.println(clientName + ":" + socket.getLocalPort());
054:
055: while (true) {
056: try {
057: // will time out if it didn't get any pulse from server
058: String signal = in.readLine();
059: if (signal == null) {
060: throw new Exception("Null signal");
061: } else if (HeartBeatServer.PULSE.equals(signal)) {
062: log("Received pulse from heartbeat server, port "
063: + socket.getLocalPort());
064: out.println(signal);
065: missedPulse = 0;
066: } else if (HeartBeatServer.KILL.equals(signal)) {
067: log("Received KILL from heartbeat server. Killing self.");
068: System.exit(1);
069: } else if (HeartBeatServer.IS_APP_SERVER_ALIVE
070: .equals(signal)) {
071: log("Received IS_APP_SERVER_ALIVE from heartbeat server. ");
072: if (isAppServer) {
073: out.println(HeartBeatServer.IM_ALIVE);
074: log(" responded: IM_ALIVE");
075: } else {
076: out.println("NOT_AN_APP_SERVER");
077: log(" responded: NOT_AN_APP_SERVER");
078: }
079: } else {
080: throw new Exception("Unknown signal");
081: }
082: } catch (SocketTimeoutException toe) {
083: log("No pulse received for "
084: + (HeartBeatServer.PULSE_INTERVAL / 1000)
085: + " seconds");
086: log("Missed pulse count: " + missedPulse++);
087: if (missedPulse >= 5) {
088: throw new Exception(
089: "Missing 3 pulse from HeartBeatServer");
090: }
091: }
092: }
093: } catch (Throwable e) {
094: log("Caught exception in heartbeat client. Killing self.");
095: e.printStackTrace();
096: } finally {
097: try {
098: socket.close();
099: } catch (Exception ignored) {
100: }
101: System.exit(1);
102: }
103: }
104:
105: }
|