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 ; 8 import java.io.InputStream ; 9 import java.io.InputStreamReader ; 10 import java.util.Properties ; 11 12 19 public class FD_PING extends FD { 20 22 String cmd="ping"; 23 24 25 boolean verbose=true; 26 27 public String getName() { 28 return "FD_PING"; 29 } 30 31 32 public boolean setProperties(Properties props) { 33 String 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 (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 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 String host=ping_dest instanceof IpAddress? ((IpAddress)ping_dest).getIpAddress().getHostAddress() : ping_dest.toString(); 77 String 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) { num_tries=0; 86 } 87 else { 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 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 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 command, Log log) throws IOException , InterruptedException { 119 Process p=Runtime.getRuntime().exec(command); 120 InputStream in=p.getInputStream(), err=p.getErrorStream(); 121 try { 122 Reader in_reader, err_reader; 123 in_reader=new Reader (in, log); 124 err_reader=new Reader (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 { 139 InputStreamReader in; 140 Log log=null; 141 boolean trace=false; 142 143 Reader(InputStream in, Log log) { 144 this.in=new InputStreamReader (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 sb=new StringBuffer (); 154 while(true) { 155 try { 156 c=in.read(); 157 if(c == -1) 158 break; 159 sb.append((char)c); 160 } 161 catch(IOException e) { 162 break; 163 } 164 } 165 if(log.isTraceEnabled()) 166 log.trace(sb.toString()); 167 } 168 } 169 } 170 171 } 172 | Popular Tags |