Exemplo n.º 1
0
 @Override
 public void inetAddressAdded(NetworkTopologyEvent nte) {
   if (nte.getInetAddress() instanceof Inet6Address
       && !ProfileUtils.getPreferences(
               ProfileManager.getDefault().getActiveProfile(), ZeroConfService.class, false)
           .getBoolean(ZeroConfService.IPv6, true)) {
     log.debug("Ignoring IPv6 address {}", nte.getInetAddress().getHostAddress());
     return;
   }
   if (nte.getInetAddress() instanceof Inet4Address
       && !ProfileUtils.getPreferences(
               ProfileManager.getDefault().getActiveProfile(), ZeroConfService.class, false)
           .getBoolean(ZeroConfService.IPv4, true)) {
     log.debug("Ignoring IPv4 address {}", nte.getInetAddress().getHostAddress());
     return;
   }
   if (!ZeroConfService.netServices.containsKey(nte.getInetAddress())) {
     log.debug("Adding address {}", nte.getInetAddress().getHostAddress());
     ZeroConfService.netServices.put(nte.getInetAddress(), nte.getDNS());
     ZeroConfService.allServices()
         .stream()
         .forEach(
             (service) -> {
               try {
                 if (!service.serviceInfos.containsKey(nte.getDNS().getInetAddress())) {
                   log.debug(
                       "Publishing zeroConf service for '{}' on {}",
                       service.key(),
                       nte.getInetAddress().getHostAddress());
                   nte.getDNS().registerService(service.addServiceInfo(nte.getDNS()));
                   service
                       .listeners
                       .stream()
                       .forEach(
                           (listener) -> {
                             listener.servicePublished(
                                 new ZeroConfServiceEvent(service, nte.getDNS()));
                           });
                 }
               } catch (IOException ex) {
                 log.error(ex.getLocalizedMessage(), ex);
               }
             });
   } else {
     log.debug("Address {} already known.", nte.getInetAddress().getHostAddress());
   }
 }
Exemplo n.º 2
0
 /** Start advertising the service. */
 public void publish() {
   if (!isPublished()) {
     ZeroConfService.services.put(this.key(), this);
     this.listeners
         .stream()
         .forEach(
             (listener) -> {
               listener.serviceQueued(new ZeroConfServiceEvent(this, null));
             });
     boolean useIPv4 =
         ProfileUtils.getPreferences(
                 ProfileManager.getDefault().getActiveProfile(), ZeroConfService.class, false)
             .getBoolean(ZeroConfService.IPv4, true);
     boolean useIPv6 =
         ProfileUtils.getPreferences(
                 ProfileManager.getDefault().getActiveProfile(), ZeroConfService.class, false)
             .getBoolean(ZeroConfService.IPv6, true);
     for (JmDNS netService : ZeroConfService.netServices().values()) {
       ZeroConfServiceEvent event;
       ServiceInfo info;
       try {
         if (netService.getInetAddress() instanceof Inet6Address && !useIPv6) {
           // Skip if address is IPv6 and should not be advertised on
           log.debug("Ignoring IPv6 address {}", netService.getInetAddress().getHostAddress());
           continue;
         }
         if (netService.getInetAddress() instanceof Inet4Address && !useIPv4) {
           // Skip if address is IPv4 and should not be advertised on
           log.debug("Ignoring IPv4 address {}", netService.getInetAddress().getHostAddress());
           continue;
         }
         try {
           log.debug(
               "Publishing ZeroConfService for '{}' on {}",
               key(),
               netService.getInetAddress().getHostAddress());
         } catch (IOException ex) {
           log.debug(
               "Publishing ZeroConfService for '{}' with IOException {}",
               key(),
               ex.getLocalizedMessage(),
               ex);
         }
         // JmDNS requires a 1-to-1 mapping of serviceInfo to InetAddress
         if (!this.serviceInfos.containsKey(netService.getInetAddress())) {
           try {
             info = this.serviceInfo();
             netService.registerService(info);
             log.debug(
                 "Register service '{}' on {} successful.",
                 this.key(),
                 netService.getInetAddress().getHostAddress());
           } catch (IllegalStateException ex) {
             // thrown if the reference serviceInfo object is in use
             try {
               log.debug(
                   "Initial attempt to register '{}' on {} failed.",
                   this.key(),
                   netService.getInetAddress().getHostAddress());
               info = this.addServiceInfo(netService);
               log.debug(
                   "Retrying register '{}' on {}.",
                   this.key(),
                   netService.getInetAddress().getHostAddress());
               netService.registerService(info);
             } catch (IllegalStateException ex1) {
               // thrown if service gets registered on interface by
               // the networkListener before this loop on interfaces
               // completes, so we only ensure a later notification
               // is not posted continuing to next interface in list
               log.debug(
                   "'{}' is already registered on {}.",
                   this.key(),
                   netService.getInetAddress().getHostAddress());
               continue;
             }
           }
         } else {
           log.debug(
               "skipping '{}' on {}, already in serviceInfos.",
               this.key(),
               netService.getInetAddress().getHostAddress());
         }
         event = new ZeroConfServiceEvent(this, netService);
       } catch (IOException ex) {
         log.error("Unable to publish service for '{}': {}", key(), ex.getMessage());
         continue;
       }
       this.listeners
           .stream()
           .forEach(
               (listener) -> {
                 listener.servicePublished(event);
               });
     }
   }
 }