KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgroups.protocols;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.jgroups.util.Util;
6
7 /**
8  * Sends num_ping_request GET_MBRS_REQ messages, distributed over timeout ms
9  * @author Bela Ban
10  * @version $Id: PingSender.java,v 1.3.2.1 2005/05/19 06:21:30 belaban Exp $
11  */

12 public class PingSender implements Runnable JavaDoc {
13     Thread JavaDoc 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
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
29     public synchronized void start() {
30         if(t == null || !t.isAlive()) {
31             t=new Thread JavaDoc(this, "PingSender");
32             t.setDaemon(true);
33             t.start();
34         }
35     }
36
37     public synchronized void stop() {
38         if(t != null) {
39             Thread JavaDoc tmp=t;
40             t=null;
41             try {tmp.interrupt();} catch(SecurityException JavaDoc ex) {}
42         }
43     }
44
45
46     public synchronized boolean isRunning() {
47         return t != null && t.isAlive();
48     }
49
50
51
52     public void run() {
53         for(int i=0; i < num_requests; i++) {
54             if(t == null || !t.equals(Thread.currentThread()))
55                 break;
56             if(log.isTraceEnabled())
57                 log.trace("sending GET_MBRS_REQ");
58             discovery_prot.sendGetMembersRequest();
59             Util.sleep((long)interval);
60         }
61     }
62 }
63
Popular Tags