/*
 * Copyright 2013-2014 Odysseus Software GmbH
 *
 * Licensed 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.musicmount.util;

import java.io.Closeable;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;

/**
 * MusicMount Bonjour service
 */
public class BonjourService implements Closeable {
	private static final String SERVICE_TYPE = "_musicmount._tcp.local.";
	private static final String SERVICE_NAME = "MusicMount";
	
	private final JmDNS jmDNS;
	private final Thread shutdownHook;

	public BonjourService(boolean stopOnShutdown) throws IOException {
		jmDNS = JmDNS.create();
		if (stopOnShutdown) {
			Runtime.getRuntime().addShutdownHook(shutdownHook = new Thread() {
				@Override
				public void run() {
					try {
						BonjourService.this.stop();
					} catch (IOException e) {
						// ignore
					}
				}
			});
		} else {
			shutdownHook = null;
		}
	}
	
	public String getHostName() {
		return jmDNS.getHostName();
	}

	private void start(int port, Map<String, String> properties) throws IOException {
		jmDNS.registerService(ServiceInfo.create(SERVICE_TYPE, SERVICE_NAME, port, 0, 0, properties));
	}

	/**
	 * Register service.
	 * @param name name property
	 * @param url url property
	 * @param user user property
	 * @throws IOException
	 */
	public void start(String name, URL url, String user) throws IOException {
		Map<String, String> properties = new HashMap<>();
		if (name != null) {
			properties.put("name", name);
		}
		if (url != null) {
			properties.put("url", url.toString());
		}
		if (user != null) {
			properties.put("user", user);
		}
		start(url.getPort() > 0 ? url.getPort() : url.getDefaultPort(), properties);
	}

	/**
	 * Register service.
	 * @param name name property
	 * @param port service port
	 * @param path path property
	 * @param user user property
	 * @throws IOException
	 */
	public void start(String name, int port, String path, String user) throws IOException {
		Map<String, String> properties = new HashMap<>();
		if (name != null) {
			properties.put("name", name);
		}
		if (path != null) {
			properties.put("path", path);
		}
		if (user != null) {
			properties.put("user", user);
		}
		start(port, properties);
	}
	
	/**
	 * Unregister service.
	 * @throws IOException
	 */
	public void stop() throws IOException {
		jmDNS.unregisterAllServices();
	}

	@Override
	public void close() throws IOException {
		try {
			jmDNS.close();
		} finally {
			if (shutdownHook != null) {
				Runtime.getRuntime().removeShutdownHook(shutdownHook);
			}
		}
	}
}

@@ -756,16 +756,48 @@
 til.Map;
+%0Aimport java.util.logging.Level;
 %0A%0Aimport
@@ -938,16 +938,158 @@
 eable %7B%0A
+%09static %7B%0A%09%09LoggingUtil.configure(%22javax.jmdns%22, Level.SEVERE); // get rid of %22WARNING: There was an OPT answer. Not currently handled.%22%0A%09%7D%0A%09%0A
 %09private

set log level of "javax.jmdns" to SEVERE