KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > tribe > faultdetection > UDPPingThread


1 /**
2  * Tribe: Group communication library.
3  * Copyright (C) 2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: tribe@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Nicolas Modrzyk.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.tribe.faultdetection;
26
27 import java.io.IOException JavaDoc;
28 import java.net.DatagramPacket JavaDoc;
29 import java.net.DatagramSocket JavaDoc;
30 import java.net.SocketException JavaDoc;
31
32 import org.objectweb.tribe.common.IpAddress;
33 import org.objectweb.tribe.messages.DatagramMessage;
34
35 /**
36  * This class defines a UDPPingThread. This defines a Ping thread that can be use and reuse to ping a target IpAddress
37  *
38  * @author <a HREF="mailto:Nicolas.Modrzyk@inrialpes.fr">Nicolas Modrzyk</a>
39  * @version 1.0
40  */

41 public class UDPPingThread extends Thread JavaDoc
42 {
43
44   IpAddress target;
45   IpAddress src;
46   int timeout;
47   boolean alive;
48   DatagramSocket JavaDoc socket;
49   DatagramMessage message;
50   DatagramPacket JavaDoc packet;
51   
52   /**
53    * Creates a new <code>UDPPingThread</code> object
54    * @throws SocketException
55    *
56    *
57    */

58   public UDPPingThread(IpAddress target,IpAddress src,int timeout) throws SocketException JavaDoc
59   {
60     super("UDP Ping Thread");
61     this.target = target;
62     this.src = src;
63     this.timeout = timeout;
64     socket = new DatagramSocket JavaDoc(src.getPort());
65     socket.setSoTimeout(timeout);
66     message = new DatagramMessage(src,target);
67     message.setContent(new String JavaDoc("Iamaping").getBytes());
68     packet = message.getDatagramPacket();
69   }
70
71   /**
72    * @see java.lang.Runnable#run()
73    */

74   public void run()
75   {
76     try
77     {
78       alive = true;
79       socket.send(packet);
80       // Wait only for the time specified by timeout
81
socket.receive(packet);
82     }
83     catch (IOException JavaDoc e)
84     {
85       alive = false;
86     }
87   }
88
89   /**
90    * Returns the alive value.
91    *
92    * @return Returns the alive.
93    */

94   public boolean isTargetAlive()
95   {
96     return alive;
97   }
98 }
99
Popular Tags