01: package org.jgroups.protocols;
02:
03: import org.apache.commons.logging.Log;
04: import org.apache.commons.logging.LogFactory;
05: import org.jgroups.util.Util;
06:
07: /**
08: * Sends num_ping_request GET_MBRS_REQ messages, distributed over timeout ms
09: * @author Bela Ban
10: * @version $Id: PingSender.java,v 1.5.10.1 2007/04/27 08:03:51 belaban Exp $
11: */
12: public class PingSender implements Runnable {
13: Thread t = null;
14: long timeout = 3000;
15: double interval;
16: int num_requests = 1;
17: Discovery discovery_prot;
18: protected final Log log = LogFactory.getLog(this .getClass());
19: protected boolean trace = log.isTraceEnabled();
20:
21: public PingSender(long timeout, int num_requests, Discovery d) {
22: this .timeout = timeout;
23: this .num_requests = num_requests;
24: this .discovery_prot = d;
25: interval = timeout / (double) num_requests;
26: }
27:
28: public synchronized void start() {
29: if (t == null || !t.isAlive()) {
30: t = new Thread(this , "PingSender");
31: t.setDaemon(true);
32: t.start();
33: }
34: }
35:
36: public synchronized void stop() {
37: if (t != null) {
38: Thread tmp = t;
39: t = null;
40: try {
41: tmp.interrupt();
42: } catch (SecurityException ex) {
43: }
44: }
45: }
46:
47: public synchronized boolean isRunning() {
48: return t != null && t.isAlive();
49: }
50:
51: public void run() {
52: for (int i = 0; i < num_requests; i++) {
53: if (t == null || !t.equals(Thread.currentThread()))
54: break;
55: if (log.isTraceEnabled())
56: log.trace("sending GET_MBRS_REQ");
57: discovery_prot.sendGetMembersRequest();
58: Util.sleep((long) interval);
59: }
60: }
61: }
|