All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bidib.wizard.gateway.service.ServiceRegistrationService Maven / Gradle / Ivy

There is a newer version: 2.0.30
Show newest version
package org.bidib.wizard.gateway.service;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Arrays;

import javax.annotation.PreDestroy;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;

public class ServiceRegistrationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceRegistrationService.class);

    private JmDNS bonjourService;

    private int listenPortNetBidib;

    public ServiceRegistrationService(int listenPortNetBidib) {

        this.listenPortNetBidib = listenPortNetBidib;

        LOGGER.info("Create new instance of ServiceRegistrationService, listenPortNetBidib: {}", listenPortNetBidib);
    }

    @EventListener
    public void handleContextStart(ApplicationStartedEvent ase) {
        LOGGER.info("The application is started. Register the bonjour service for the bidib-gateway.");

        try {

            String bonjourServiceType = "_http._tcp.local.";
            // String bonjourServiceType = "_netbidib._tcp.local.";

            String hostname = InetAddress.getLocalHost().getHostName();
            String hostAddress = InetAddress.getLocalHost().getHostAddress();
            LOGGER.info("Current hostname: {}, hostAddress: {}", hostname, hostAddress);

            InetAddress[] addAddresses = InetAddress.getAllByName(hostname);
            LOGGER.info("Get all addresses: {}", Arrays.asList(addAddresses));

            for (InetAddress addr : addAddresses) {
                LOGGER.info("Current inetAddress: {}", addr);
                // Create a JmDNS instance
                // jmdns = JmDNS.create(InetAddress.getLocalHost(), "bidib-gateway");
                bonjourService = JmDNS.create(addr, "bidib-gateway");

                // Register a service
                ServiceInfo serviceInfo =
                    ServiceInfo.create(bonjourServiceType, "netBiDiB", listenPortNetBidib, "path=index.html");
                // ServiceInfo.create("_netbidib._tcp.local.", "netBiDiB", listenPortNetBidib, "netBiDiB Service");
                bonjourService.registerService(serviceInfo);
            }

        }
        catch (IOException ex) {
            LOGGER.warn("Register service failed.", ex);
        }

    }

    @PreDestroy
    public void unregisterAllServices() {
        LOGGER.info("Unregister all services.");
        try {

            // Unregister all services
            bonjourService.unregisterAllServices();

        }
        catch (Exception ex) {
            LOGGER.warn("Unregister services failed.", ex);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy