Search in sources :

Example 1 with ServiceInfo

use of javax.jmdns.ServiceInfo in project JAirPort by froks.

the class JAirPort method main.

public static void main(String[] args) throws Exception {
    Runtime.getRuntime().addShutdownHook(new ShutdownThread());
    String hwAddr = Utils.byteAddrToString(Configuration.getHardwareAddress());
    String host = Configuration.getHostName();
    String name = hwAddr + "@JAirPort on " + host;
    int port = Configuration.getPort();
    // Announce Raop Service
    ServiceInfo info = ServiceInfo.create(name + "._raop._tcp.local", name, port, "tp=UDP sm=false sv=false ek=1 et=0,1 cn=0,1 ch=2 ss=16 sr=44100 pw=false vn=3 txtvers=1");
    dns = JmmDNS.Factory.getInstance();
    ((JmmDNSImpl) dns).inetAddressAdded(new NetworkTopologyEventImpl(JmDNS.create(InetAddress.getByName("localhost")), InetAddress.getByName("localhost")));
    // If this isn't done the Announcement sometimes doesn't go out on the local interface
    Thread.sleep(1000);
    dns.registerService(info);
    System.out.println("Service registered");
    // Start RtspServer
    RtspServer server = new RtspServer(port);
    serverThread = new Thread(server, "RtspServer");
    serverThread.setDaemon(false);
    serverThread.start();
    serverThread.join();
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) NetworkTopologyEventImpl(javax.jmdns.impl.NetworkTopologyEventImpl) JmmDNSImpl(javax.jmdns.impl.JmmDNSImpl)

Example 2 with ServiceInfo

use of javax.jmdns.ServiceInfo in project JAirPort by froks.

the class JmDNSImpl method getServiceInfoFromCache.

ServiceInfoImpl getServiceInfoFromCache(String type, String name, String subtype, boolean persistent) {
    // Check if the answer is in the cache.
    ServiceInfoImpl info = new ServiceInfoImpl(type, name, subtype, 0, 0, 0, persistent, (byte[]) null);
    DNSEntry pointerEntry = this.getCache().getDNSEntry(new DNSRecord.Pointer(type, DNSRecordClass.CLASS_ANY, false, 0, info.getQualifiedName()));
    if (pointerEntry instanceof DNSRecord) {
        ServiceInfoImpl cachedInfo = (ServiceInfoImpl) ((DNSRecord) pointerEntry).getServiceInfo(persistent);
        if (cachedInfo != null) {
            // To get a complete info record we need to retrieve the service, address and the text bytes.
            Map<Fields, String> map = cachedInfo.getQualifiedNameMap();
            byte[] srvBytes = null;
            String server = "";
            DNSEntry serviceEntry = this.getCache().getDNSEntry(info.getQualifiedName(), DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_ANY);
            if (serviceEntry instanceof DNSRecord) {
                ServiceInfo cachedServiceEntryInfo = ((DNSRecord) serviceEntry).getServiceInfo(persistent);
                if (cachedServiceEntryInfo != null) {
                    cachedInfo = new ServiceInfoImpl(map, cachedServiceEntryInfo.getPort(), cachedServiceEntryInfo.getWeight(), cachedServiceEntryInfo.getPriority(), persistent, (byte[]) null);
                    srvBytes = cachedServiceEntryInfo.getTextBytes();
                    server = cachedServiceEntryInfo.getServer();
                }
            }
            DNSEntry addressEntry = this.getCache().getDNSEntry(server, DNSRecordType.TYPE_A, DNSRecordClass.CLASS_ANY);
            if (addressEntry instanceof DNSRecord) {
                ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent);
                if (cachedAddressInfo != null) {
                    for (Inet4Address address : cachedAddressInfo.getInet4Addresses()) {
                        cachedInfo.addAddress(address);
                    }
                    cachedInfo._setText(cachedAddressInfo.getTextBytes());
                }
            }
            addressEntry = this.getCache().getDNSEntry(server, DNSRecordType.TYPE_AAAA, DNSRecordClass.CLASS_ANY);
            if (addressEntry instanceof DNSRecord) {
                ServiceInfo cachedAddressInfo = ((DNSRecord) addressEntry).getServiceInfo(persistent);
                if (cachedAddressInfo != null) {
                    for (Inet6Address address : cachedAddressInfo.getInet6Addresses()) {
                        cachedInfo.addAddress(address);
                    }
                    cachedInfo._setText(cachedAddressInfo.getTextBytes());
                }
            }
            DNSEntry textEntry = this.getCache().getDNSEntry(cachedInfo.getQualifiedName(), DNSRecordType.TYPE_TXT, DNSRecordClass.CLASS_ANY);
            if (textEntry instanceof DNSRecord) {
                ServiceInfo cachedTextInfo = ((DNSRecord) textEntry).getServiceInfo(persistent);
                if (cachedTextInfo != null) {
                    cachedInfo._setText(cachedTextInfo.getTextBytes());
                }
            }
            if (cachedInfo.getTextBytes().length == 0) {
                cachedInfo._setText(srvBytes);
            }
            if (cachedInfo.hasData()) {
                info = cachedInfo;
            }
        }
    }
    return info;
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) Inet4Address(java.net.Inet4Address) Fields(javax.jmdns.ServiceInfo.Fields) Inet6Address(java.net.Inet6Address)

Example 3 with ServiceInfo

use of javax.jmdns.ServiceInfo in project JAirPort by froks.

the class ServiceResolver method addAnswers.

/*
     * (non-Javadoc)
     * @see javax.jmdns.impl.tasks.Resolver#addAnswers(javax.jmdns.impl.DNSOutgoing)
     */
@Override
protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException {
    DNSOutgoing newOut = out;
    long now = System.currentTimeMillis();
    for (ServiceInfo info : this.getDns().getServices().values()) {
        newOut = this.addAnswer(newOut, new DNSRecord.Pointer(info.getType(), DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, info.getQualifiedName()), now);
    // newOut = this.addAnswer(newOut, new DNSRecord.Service(info.getQualifiedName(), DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, info.getPriority(), info.getWeight(), info.getPort(),
    // this.getDns().getLocalHost().getName()), now);
    }
    return newOut;
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) DNSOutgoing(javax.jmdns.impl.DNSOutgoing)

Example 4 with ServiceInfo

use of javax.jmdns.ServiceInfo in project JMRI by JMRI.

the class ZeroConfService method publish.

/**
     * 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);
            });
        }
    }
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) Inet4Address(java.net.Inet4Address) JmDNS(javax.jmdns.JmDNS) Inet6Address(java.net.Inet6Address) IOException(java.io.IOException)

Example 5 with ServiceInfo

use of javax.jmdns.ServiceInfo in project EngineDriver by JMRI.

the class JmmDNSImpl method listBySubtype.

/*
     * (non-Javadoc)
     * @see javax.jmdns.JmmDNS#listBySubtype(java.lang.String, long)
     */
@Override
public Map<String, ServiceInfo[]> listBySubtype(final String type, final long timeout) {
    Map<String, List<ServiceInfo>> map = new HashMap<String, List<ServiceInfo>>(5);
    for (ServiceInfo info : this.list(type, timeout)) {
        String subtype = info.getSubtype();
        if (!map.containsKey(subtype)) {
            map.put(subtype, new ArrayList<ServiceInfo>(10));
        }
        map.get(subtype).add(info);
    }
    Map<String, ServiceInfo[]> result = new HashMap<String, ServiceInfo[]>(map.size());
    for (String subtype : map.keySet()) {
        List<ServiceInfo> infoForSubType = map.get(subtype);
        result.put(subtype, infoForSubType.toArray(new ServiceInfo[infoForSubType.size()]));
    }
    return result;
}
Also used : ServiceInfo(javax.jmdns.ServiceInfo) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List)

Aggregations

ServiceInfo (javax.jmdns.ServiceInfo)43 ArrayList (java.util.ArrayList)15 IOException (java.io.IOException)14 JmDNS (javax.jmdns.JmDNS)11 List (java.util.List)10 LinkedList (java.util.LinkedList)7 HashMap (java.util.HashMap)6 ExecutorService (java.util.concurrent.ExecutorService)6 SocketException (java.net.SocketException)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 DNSOutgoing (javax.jmdns.impl.DNSOutgoing)4 Inet4Address (java.net.Inet4Address)3 Inet6Address (java.net.Inet6Address)3 InetAddress (java.net.InetAddress)3 URI (java.net.URI)3 ExecutionException (java.util.concurrent.ExecutionException)3 HashSet (java.util.HashSet)2 Callable (java.util.concurrent.Callable)2 Future (java.util.concurrent.Future)2 ServiceEvent (javax.jmdns.ServiceEvent)2