KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > protocols > FD_PING


1 package org.jgroups.protocols;
2
3 import org.apache.commons.logging.Log;
4 import org.jgroups.stack.IpAddress;
5 import org.jgroups.util.Util;
6
7 import java.io.IOException JavaDoc;
8 import java.io.InputStream JavaDoc;
9 import java.io.InputStreamReader JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 /**
13  * Protocol which uses an executable (e.g. /sbin/ping, or a script) to check whether a given host is up or not,
14  * taking 1 argument; the host name of the host to be pinged. Property 'cmd' determines the program to be executed
15  * (use a fully qualified name if the program is not on the path).
16  * @author Bela Ban
17  * @version $Id: FD_PING.java,v 1.4 2007/04/27 07:59:19 belaban Exp $
18  */

19 public class FD_PING extends FD {
20     /** Command (script or executable) to ping a host: a return value of 0 means success, anything else is a failure.
21      * The only argument passed to cmd is the host's address (symbolic name or dotted-decimal IP address) */

22     String JavaDoc cmd="ping";
23
24     /** Write the stdout of the command to the log */
25     boolean verbose=true;
26
27     public String JavaDoc getName() {
28         return "FD_PING";
29     }
30
31
32     public boolean setProperties(Properties JavaDoc props) {
33         String JavaDoc str;
34         str=props.getProperty("cmd");
35         if(str != null) {
36             cmd=str;
37             props.remove("cmd");
38         }
39
40         str=props.getProperty("verbose");
41         if(str != null) {
42             verbose=new Boolean JavaDoc(str).booleanValue();
43             props.remove("verbose");
44         }
45
46         super.setProperties(props);
47
48         if(props.size() > 0) {
49             log.error("the following properties are not recognized: " + props);
50             return false;
51         }
52         return true;
53     }
54
55     protected Monitor createMonitor() {
56         return new PingMonitor();
57     }
58
59
60     /**
61      * Executes the ping command. Each time the command fails, we increment num_tries. If num_tries > max_tries, we
62      * emit a SUSPECT message. If ping_dest changes, or we do receive traffic from ping_dest, we reset num_tries to 0.
63      */

64     protected class PingMonitor extends Monitor {
65
66         public void run() {
67             if(ping_dest == null) {
68                 if(log.isWarnEnabled())
69                     log.warn("ping_dest is null: members=" + members + ", pingable_mbrs=" +
70                             pingable_mbrs + ", local_addr=" + local_addr);
71                 return;
72             }
73
74
75             // 1. execute ping command
76
String JavaDoc host=ping_dest instanceof IpAddress? ((IpAddress)ping_dest).getIpAddress().getHostAddress() : ping_dest.toString();
77             String JavaDoc command=cmd + " " + host;
78             if(log.isDebugEnabled())
79                 log.debug("executing \"" + command + "\" (own address=" + local_addr + ')');
80             try {
81                 Log tmp_log=verbose? log : null;
82                 int rc=Pinger.execute(command, tmp_log);
83                 num_heartbeats++;
84                 if(rc == 0) { // success
85
num_tries=0;
86                 }
87                 else { // failure
88
num_tries++;
89                     if(log.isDebugEnabled())
90                         log.debug("could not ping " + ping_dest + " (tries=" + num_tries + ')');
91                 }
92
93                 if(num_tries >= max_tries) {
94                     if(log.isDebugEnabled())
95                         log.debug("[" + local_addr + "]: could not ping " + ping_dest + " for " + (num_tries +1) +
96                                 " times (" + ((num_tries+1) * timeout) + " milliseconds), suspecting it");
97                     // broadcast a SUSPECT message to all members - loop until
98
// unsuspect or view change is received
99
bcast_task.addSuspectedMember(ping_dest);
100                     num_tries=0;
101                     if(stats) {
102                         num_suspect_events++;
103                         suspect_history.add(ping_dest);
104                     }
105                 }
106             }
107             catch(Exception JavaDoc ex) {
108                 if(log.isErrorEnabled())
109                     log.error("failed executing command " + command, ex);
110             }
111         }
112     }
113
114
115
116     protected static class Pinger {
117
118         static int execute(String JavaDoc command, Log log) throws IOException JavaDoc, InterruptedException JavaDoc {
119             Process JavaDoc p=Runtime.getRuntime().exec(command);
120             InputStream JavaDoc in=p.getInputStream(), err=p.getErrorStream();
121             try {
122                 Reader JavaDoc in_reader, err_reader;
123                 in_reader=new Reader JavaDoc(in, log);
124                 err_reader=new Reader JavaDoc(err, log);
125                 in_reader.start();
126                 err_reader.start();
127                 in_reader.join();
128                 err_reader.join();
129                 return p.exitValue();
130             }
131             finally {
132                 Util.close(in);
133                 Util.close(err);
134             }
135         }
136
137
138         static class Reader extends Thread JavaDoc {
139             InputStreamReader JavaDoc in;
140             Log log=null;
141             boolean trace=false;
142
143             Reader(InputStream JavaDoc in, Log log) {
144                 this.in=new InputStreamReader JavaDoc(in);
145                 this.log=log;
146                 if(log != null) {
147                     trace=log.isTraceEnabled();
148                 }
149             }
150
151             public void run() {
152                 int c;
153                 StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
154                 while(true) {
155                     try {
156                         c=in.read();
157                         if(c == -1)
158                             break;
159                         sb.append((char)c);
160                     }
161                     catch(IOException JavaDoc e) {
162                         break;
163                     }
164                 }
165                 if(log.isTraceEnabled())
166                     log.trace(sb.toString());
167             }
168         }
169     }
170
171 }
172
Popular Tags