Source Code Cross Referenced for FD_PING.java in  » Net » JGroups-2.4.1-sp3 » org » jgroups » protocols » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » JGroups 2.4.1 sp3 » org.jgroups.protocols 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.jgroups.protocols;
002:
003:        import org.apache.commons.logging.Log;
004:        import org.jgroups.stack.IpAddress;
005:        import org.jgroups.util.Util;
006:
007:        import java.io.IOException;
008:        import java.io.InputStream;
009:        import java.io.InputStreamReader;
010:        import java.util.Properties;
011:
012:        /**
013:         * Protocol which uses an executable (e.g. /sbin/ping, or a script) to check whether a given host is up or not,
014:         * taking 1 argument; the host name of the host to be pinged. Property 'cmd' determines the program to be executed
015:         * (use a fully qualified name if the program is not on the path).
016:         * @author Bela Ban
017:         * @version $Id: FD_PING.java,v 1.3.2.1 2007/04/27 08:03:51 belaban Exp $
018:         */
019:        public class FD_PING extends FD {
020:            /** Command (script or executable) to ping a host: a return value of 0 means success, anything else is a failure.
021:             * The only argument passed to cmd is the host's address (symbolic name or dotted-decimal IP address) */
022:            String cmd = "ping";
023:
024:            /** Write the stdout of the command to the log */
025:            boolean verbose = true;
026:
027:            public String getName() {
028:                return "FD_PING";
029:            }
030:
031:            public boolean setProperties(Properties props) {
032:                String str;
033:                str = props.getProperty("cmd");
034:                if (str != null) {
035:                    cmd = str;
036:                    props.remove("cmd");
037:                }
038:
039:                str = props.getProperty("verbose");
040:                if (str != null) {
041:                    verbose = new Boolean(str).booleanValue();
042:                    props.remove("verbose");
043:                }
044:
045:                super .setProperties(props);
046:
047:                if (props.size() > 0) {
048:                    log.error("the following properties are not recognized: "
049:                            + props);
050:                    return false;
051:                }
052:                return true;
053:            }
054:
055:            protected Monitor createMonitor() {
056:                return new PingMonitor();
057:            }
058:
059:            /**
060:             * Executes the ping command. Each time the command fails, we increment num_tries. If num_tries > max_tries, we
061:             * emit a SUSPECT message. If ping_dest changes, or we do receive traffic from ping_dest, we reset num_tries to 0.
062:             */
063:            protected class PingMonitor extends Monitor {
064:
065:                public void run() {
066:                    if (ping_dest == null) {
067:                        if (log.isWarnEnabled())
068:                            log.warn("ping_dest is null: members=" + members
069:                                    + ", pingable_mbrs=" + pingable_mbrs
070:                                    + ", local_addr=" + local_addr);
071:                        return;
072:                    }
073:
074:                    // 1. execute ping command
075:                    String host = ping_dest instanceof  IpAddress ? ((IpAddress) ping_dest)
076:                            .getIpAddress().getHostAddress()
077:                            : ping_dest.toString();
078:                    String command = cmd + " " + host;
079:                    if (log.isDebugEnabled())
080:                        log.debug("executing \"" + command + "\" (own address="
081:                                + local_addr + ')');
082:                    try {
083:                        Log tmp_log = verbose ? log : null;
084:                        int rc = Pinger.execute(command, tmp_log);
085:                        num_heartbeats++;
086:                        if (rc == 0) { // success
087:                            num_tries = 0;
088:                        } else { // failure
089:                            num_tries++;
090:                            if (log.isDebugEnabled())
091:                                log.debug("could not ping " + ping_dest
092:                                        + " (tries=" + num_tries + ')');
093:                        }
094:
095:                        if (num_tries >= max_tries) {
096:                            if (log.isDebugEnabled())
097:                                log.debug("[" + local_addr
098:                                        + "]: could not ping " + ping_dest
099:                                        + " for " + (num_tries + 1)
100:                                        + " times ("
101:                                        + ((num_tries + 1) * timeout)
102:                                        + " milliseconds), suspecting it");
103:                            // broadcast a SUSPECT message to all members - loop until
104:                            // unsuspect or view change is received
105:                            bcast_task.addSuspectedMember(ping_dest);
106:                            num_tries = 0;
107:                            if (stats) {
108:                                num_suspect_events++;
109:                                suspect_history.add(ping_dest);
110:                            }
111:                        }
112:                    } catch (Exception ex) {
113:                        if (log.isErrorEnabled())
114:                            log
115:                                    .error("failed executing command "
116:                                            + command, ex);
117:                    }
118:                }
119:            }
120:
121:            protected static class Pinger {
122:
123:                static int execute(String command, Log log) throws IOException,
124:                        InterruptedException {
125:                    Process p = Runtime.getRuntime().exec(command);
126:                    InputStream in = p.getInputStream(), err = p
127:                            .getErrorStream();
128:                    try {
129:                        Reader in_reader, err_reader;
130:                        in_reader = new Reader(in, log);
131:                        err_reader = new Reader(err, log);
132:                        in_reader.start();
133:                        err_reader.start();
134:                        in_reader.join();
135:                        err_reader.join();
136:                        return p.exitValue();
137:                    } finally {
138:                        Util.close(in);
139:                        Util.close(err);
140:                    }
141:                }
142:
143:                static class Reader extends Thread {
144:                    InputStreamReader in;
145:                    Log log = null;
146:                    boolean trace = false;
147:
148:                    Reader(InputStream in, Log log) {
149:                        this .in = new InputStreamReader(in);
150:                        this .log = log;
151:                        if (log != null) {
152:                            trace = log.isTraceEnabled();
153:                        }
154:                    }
155:
156:                    public void run() {
157:                        int c;
158:                        StringBuffer sb = new StringBuffer();
159:                        while (true) {
160:                            try {
161:                                c = in.read();
162:                                if (c == -1)
163:                                    break;
164:                                sb.append((char) c);
165:                            } catch (IOException e) {
166:                                break;
167:                            }
168:                        }
169:                        if (log.isTraceEnabled())
170:                            log.trace(sb.toString());
171:                    }
172:                }
173:            }
174:
175:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.