// Path: common/src/main/java/org/apache/hms/common/util/DaemonWatcher.java
// public class DaemonWatcher extends PidFile {
//   private static DaemonWatcher instance = null;
//   
//   public synchronized static DaemonWatcher createInstance(String name, int port) {
//     if(instance == null) {
//       instance = new DaemonWatcher(name, port);
//       Runtime.getRuntime().addShutdownHook(instance);
//     }
//     return instance;
//   }
//   
//   public static DaemonWatcher getInstance() {
//     return instance;
//   }
//   
//   private DaemonWatcher(String name, int port) {
//     super(name, port);
//   }
//   
//   public static void bailout(int status) {
//     System.exit(status);
//   }
// }
// 
// Path: common/src/main/java/org/apache/hms/common/util/MulticastDNS.java
// public class MulticastDNS {
//   private static Log LOG = LogFactory.getLog(MulticastDNS.class);
//   public static JmDNS jmdns;
//   private InetAddress addr;
//   private String svcType = "_zookeeper._tcp.local.";
//   private String svcName;
//   private int svcPort = 2181;
//   private Hashtable<String, String> settings;
//  
//   public MulticastDNS() throws UnknownHostException {
//     super();
//     InetAddress addr = InetAddress.getLocalHost();
//     String hostname = addr.getHostName();
//     if(hostname.indexOf('.')>0) {
//       hostname = hostname.substring(0, hostname.indexOf('.'));
//     }
//     svcName = hostname;
//     settings = new Hashtable<String,String>();
//     settings.put("host", svcName);
//     settings.put("port", new Integer(svcPort).toString());
//   }
//   
//   public MulticastDNS(String svcType, int svcPort) throws UnknownHostException {
//     this();
//     this.svcType = svcType;
//     this.svcPort = svcPort;
//   }
//  
//   public MulticastDNS(InetAddress addr, String svcType, int svcPort) throws UnknownHostException {
//     this();
//     this.addr = addr;
//     this.svcType = svcType;
//     this.svcPort = svcPort;
//   }
// 
//   protected void handleRegisterCommand() throws IOException {
//     if(jmdns==null) {
//       if(addr!=null) {
//         jmdns = JmDNS.create(this.addr);      
//       } else {
//         jmdns = JmDNS.create();
//       }
//     }
//     ServiceInfo svcInfo = ServiceInfo.create(svcType, svcName, svcPort, 1, 1, settings);
//     try {
//       this.jmdns.registerService(svcInfo);
//       LOG.info("Registered service '" + svcName + "' as: " + svcInfo);
//     } catch (IOException e) {
//       LOG.error("Failed to register service '" + svcName + "'");
//     }
//   }
// 
//   protected void handleUnregisterCommand() {
//     this.jmdns.unregisterAllServices();
//     try {
//       this.jmdns.close();
//     } catch (IOException e) {
//       LOG.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
//  
// }


import java.io.IOException;
import java.net.UnknownHostException;
import org.apache.hms.common.util.DaemonWatcher;
import org.apache.hms.common.util.MulticastDNS;
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hms.beacon;


/**
 * 
 * HMS Beacon broadcast ZooKeeper server location by using MulticastDNS.
 * This utility runs on the same node as ZooKeeper server.
 *
 */
public class Beacon extends MulticastDNS {
  public Beacon() throws UnknownHostException {
    super();
  }
 
  public Beacon(String svcType, int svcPort) throws UnknownHostException {
    super(svcType, svcPort);  
  }
  
  /**
   * Register Zookeeper host location in MulticastDNS
   * @throws IOException
   */
  public void start() throws IOException {
    handleRegisterCommand();
  }
  
  /**
   * Remove Zookeeper host location from MulticastDNS
   * @throws IOException
   */
  public void stop() throws IOException {
    handleUnregisterCommand();
  }
 
  public static void main(String[] args) throws IOException {
// Path: common/src/main/java/org/apache/hms/common/util/DaemonWatcher.java
// public class DaemonWatcher extends PidFile {
//   private static DaemonWatcher instance = null;
//   
//   public synchronized static DaemonWatcher createInstance(String name, int port) {
//     if(instance == null) {
//       instance = new DaemonWatcher(name, port);
//       Runtime.getRuntime().addShutdownHook(instance);
//     }
//     return instance;
//   }
//   
//   public static DaemonWatcher getInstance() {
//     return instance;
//   }
//   
//   private DaemonWatcher(String name, int port) {
//     super(name, port);
//   }
//   
//   public static void bailout(int status) {
//     System.exit(status);
//   }
// }
// 
// Path: common/src/main/java/org/apache/hms/common/util/MulticastDNS.java
// public class MulticastDNS {
//   private static Log LOG = LogFactory.getLog(MulticastDNS.class);
//   public static JmDNS jmdns;
//   private InetAddress addr;
//   private String svcType = "_zookeeper._tcp.local.";
//   private String svcName;
//   private int svcPort = 2181;
//   private Hashtable<String, String> settings;
//  
//   public MulticastDNS() throws UnknownHostException {
//     super();
//     InetAddress addr = InetAddress.getLocalHost();
//     String hostname = addr.getHostName();
//     if(hostname.indexOf('.')>0) {
//       hostname = hostname.substring(0, hostname.indexOf('.'));
//     }
//     svcName = hostname;
//     settings = new Hashtable<String,String>();
//     settings.put("host", svcName);
//     settings.put("port", new Integer(svcPort).toString());
//   }
//   
//   public MulticastDNS(String svcType, int svcPort) throws UnknownHostException {
//     this();
//     this.svcType = svcType;
//     this.svcPort = svcPort;
//   }
//  
//   public MulticastDNS(InetAddress addr, String svcType, int svcPort) throws UnknownHostException {
//     this();
//     this.addr = addr;
//     this.svcType = svcType;
//     this.svcPort = svcPort;
//   }
// 
//   protected void handleRegisterCommand() throws IOException {
//     if(jmdns==null) {
//       if(addr!=null) {
//         jmdns = JmDNS.create(this.addr);      
//       } else {
//         jmdns = JmDNS.create();
//       }
//     }
//     ServiceInfo svcInfo = ServiceInfo.create(svcType, svcName, svcPort, 1, 1, settings);
//     try {
//       this.jmdns.registerService(svcInfo);
//       LOG.info("Registered service '" + svcName + "' as: " + svcInfo);
//     } catch (IOException e) {
//       LOG.error("Failed to register service '" + svcName + "'");
//     }
//   }
// 
//   protected void handleUnregisterCommand() {
//     this.jmdns.unregisterAllServices();
//     try {
//       this.jmdns.close();
//     } catch (IOException e) {
//       LOG.error(ExceptionUtil.getStackTrace(e));
//     }
//   }
//  
// }


// Path: beacon/src/main/java/org/apache/hms/beacon/Beacon.java
import java.io.IOException;
import java.net.UnknownHostException;
import org.apache.hms.common.util.DaemonWatcher;
import org.apache.hms.common.util.MulticastDNS;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hms.beacon;


/**
 * 
 * HMS Beacon broadcast ZooKeeper server location by using MulticastDNS.
 * This utility runs on the same node as ZooKeeper server.
 *
 */
public class Beacon extends MulticastDNS {
  public Beacon() throws UnknownHostException {
    super();
  }
 
  public Beacon(String svcType, int svcPort) throws UnknownHostException {
    super(svcType, svcPort);  
  }
  
  /**
   * Register Zookeeper host location in MulticastDNS
   * @throws IOException
   */
  public void start() throws IOException {
    handleRegisterCommand();
  }
  
  /**
   * Remove Zookeeper host location from MulticastDNS
   * @throws IOException
   */
  public void stop() throws IOException {
    handleUnregisterCommand();
  }
 
  public static void main(String[] args) throws IOException {
    DaemonWatcher.createInstance(System.getProperty("PID"), 9101);