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 ****************************************************************************/
21 import java.io.IOException;
22 import java.net.DatagramPacket;
23 import java.net.DatagramSocket;
26 * Client-side Failure Detector (FD) implementation.
27 * @param server_addr: IP address of the remote server
28 * @param port: the port that the FD service listens for incoming UDP packets
29 * @param timeout: how often the FD checks the status of client processes
30 * @param debug: debug mode on/off
33 public class DNSServer {
35 public static void main(String[] args) throws IOException {
36 if(args.length != 5) {
37 System.out.println("Missing input parameters");
38 System.out.println("Usage:");
39 System.out.print("\t- java FDClient [process ID] [server IP address] [server port] [timeout (sec)] [debug]\n");
42 // IP address and port of the remote server
44 String SERVER_ADDR = args[1];
45 int PORT = Integer.parseInt(args[2]);
46 long TIMEOUT = Long.parseLong(args[3]) * 1000; // convert the FD timeout to milliseconds
47 int debug = Integer.parseInt(args[4]);
55 new FDClient(PID, SERVER_ADDR, PORT, TIMEOUT, DEBUG);
57 // Listen for reply messages
58 @SuppressWarnings("resource")
59 DatagramSocket sock = new DatagramSocket(PORT);
60 byte[] buffer = new byte[256];
61 boolean gre_tunnel_enabled = false;
64 DatagramPacket packet = new DatagramPacket(buffer, buffer.length);
66 String[] content = new String(packet.getData()).trim().split(":"); // Remove leading and trailing spaces
67 String msg_type = content[0];
68 String ip = content[1];
69 // Process only PING UDP packets
70 if(msg_type.equals("PONG")) {
71 if(DEBUG) System.out.println("Reply message received from process " + ip);
72 if(!gre_tunnel_enabled) {
73 // Set GRE tunnel towards load balancer
74 String script = new String("bash /opt/FDclient/set_gre_tunnel.sh " + ip);
75 Runtime.getRuntime().exec(script);
76 if(DEBUG) System.out.println("GRE tunnel towards load balancer " + ip + " done");
77 gre_tunnel_enabled = true;
81 if(DEBUG) System.out.println("The received message is not a PONG. Received content: " + content);