Initial OpenECOMP Demo commit
[demo.git] / vnfs / vLB / DNSClient / src / main / java / FDClient.java
1
2 /*************************************************************************//**
3  *
4  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
5  *
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
10  *
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.
16  *
17  ****************************************************************************/
18
19 package main.java;
20
21 import java.io.*;
22 import java.net.*;
23
24 /*
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
30  */
31
32 public class FDClient implements Runnable {
33         private String PID;
34         private String SERVER_ADDR;
35         private int PORT;
36         private long TIMEOUT;
37         private boolean DEBUG;
38         
39         public FDClient(String pid, String server_addr, int port, long timeout, boolean debug) throws IOException {
40                 PID = pid;
41                 SERVER_ADDR = server_addr;
42                 PORT = port;
43                 TIMEOUT = timeout;
44                 DEBUG = debug;
45                 (new Thread(this)).start();
46         }
47
48         @SuppressWarnings("resource")
49         @Override
50         public void run() {
51                 // Define a DatagramSocket object to send packets to the server
52                 try {
53                         DatagramSocket sock = new DatagramSocket();
54                         InetAddress IPAddress = InetAddress.getByName(SERVER_ADDR);
55
56                         // Allocate buffer for the PING message
57                         String content = "PING:" + PID;
58                         byte[] buffer = content.getBytes();
59                 
60                         // Sent a PING message every TIMEOUT milliseconds
61                         while(true) {
62                                 DatagramPacket packet = new DatagramPacket(buffer, buffer.length, IPAddress, PORT);
63                                 sock.send(packet);
64
65                                 if(DEBUG) System.out.println("Sent PING message to server " + SERVER_ADDR + ":" + PORT);
66                                 try {
67                                         Thread.sleep(TIMEOUT);
68                                 }
69                                 catch(InterruptedException e) {}
70                         } 
71                 } catch(IOException e) {}
72         }
73 }