2 /*************************************************************************//**
4 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ****************************************************************************/
25 * Client-side Failure Detector (FD) implementation.
26 * @param server_addr: IP address of the remote server
27 * @param port: the port that the FD service listens for incoming UDP packets
28 * @param timeout: how often the FD checks the status of client processes
29 * @param debug: debug mode on/off
32 public class FDClient implements Runnable {
34 private String SERVER_ADDR;
37 private boolean DEBUG;
39 public FDClient(String pid, String server_addr, int port, long timeout, boolean debug) throws IOException {
41 SERVER_ADDR = server_addr;
45 (new Thread(this)).start();
48 @SuppressWarnings("resource")
51 // Define a DatagramSocket object to send packets to the server
53 DatagramSocket sock = new DatagramSocket();
54 InetAddress IPAddress = InetAddress.getByName(SERVER_ADDR);
56 // Allocate buffer for the PING message
57 String content = "PING:" + PID;
58 byte[] buffer = content.getBytes();
60 // Sent a PING message every TIMEOUT milliseconds
62 DatagramPacket packet = new DatagramPacket(buffer, buffer.length, IPAddress, PORT);
65 if(DEBUG) System.out.println("Sent PING message to server " + SERVER_ADDR + ":" + PORT);
67 Thread.sleep(TIMEOUT);
69 catch(InterruptedException e) {}
71 } catch(IOException e) {}