Search in sources :

Example 1 with Fields

use of javax.jmdns.ServiceInfo.Fields 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 2 with Fields

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

the class JmDNSImpl method registerServiceType.

/**
 * {@inheritDoc}
 */
@Override
public boolean registerServiceType(String type) {
    boolean typeAdded = false;
    Map<Fields, String> map = ServiceInfoImpl.decodeQualifiedNameMapForType(type);
    String domain = map.get(Fields.Domain);
    String protocol = map.get(Fields.Protocol);
    String application = map.get(Fields.Application);
    String subtype = map.get(Fields.Subtype);
    final String name = (application.length() > 0 ? "_" + application + "." : "") + (protocol.length() > 0 ? "_" + protocol + "." : "") + domain + ".";
    final String loname = name.toLowerCase();
    if (logger.isLoggable(Level.FINE)) {
        logger.fine(this.getName() + ".registering service type: " + type + " as: " + name + (subtype.length() > 0 ? " subtype: " + subtype : ""));
    }
    if (!_serviceTypes.containsKey(loname) && !application.toLowerCase().equals("dns-sd") && !domain.toLowerCase().endsWith("in-addr.arpa") && !domain.toLowerCase().endsWith("ip6.arpa")) {
        typeAdded = _serviceTypes.putIfAbsent(loname, new ServiceTypeEntry(name)) == null;
        if (typeAdded) {
            final ServiceTypeListenerStatus[] list = _typeListeners.toArray(new ServiceTypeListenerStatus[_typeListeners.size()]);
            final ServiceEvent event = new ServiceEventImpl(this, name, "", null);
            for (final ServiceTypeListenerStatus status : list) {
                _executor.submit(new Runnable() {

                    /**
                     * {@inheritDoc}
                     */
                    @Override
                    public void run() {
                        status.serviceTypeAdded(event);
                    }
                });
            }
        }
    }
    if (subtype.length() > 0) {
        ServiceTypeEntry subtypes = _serviceTypes.get(loname);
        if ((subtypes != null) && (!subtypes.contains(subtype))) {
            synchronized (subtypes) {
                if (!subtypes.contains(subtype)) {
                    typeAdded = true;
                    subtypes.add(subtype);
                    final ServiceTypeListenerStatus[] list = _typeListeners.toArray(new ServiceTypeListenerStatus[_typeListeners.size()]);
                    final ServiceEvent event = new ServiceEventImpl(this, "_" + subtype + "._sub." + name, "", null);
                    for (final ServiceTypeListenerStatus status : list) {
                        _executor.submit(new Runnable() {

                            /**
                             * {@inheritDoc}
                             */
                            @Override
                            public void run() {
                                status.subTypeForServiceTypeAdded(event);
                            }
                        });
                    }
                }
            }
        }
    }
    return typeAdded;
}
Also used : Fields(javax.jmdns.ServiceInfo.Fields) ServiceEvent(javax.jmdns.ServiceEvent) ServiceTypeListenerStatus(javax.jmdns.impl.ListenerStatus.ServiceTypeListenerStatus)

Example 3 with Fields

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

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();
                }
            }
            for (DNSEntry addressEntry : this.getCache().getDNSEntryList(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());
                    }
                }
            }
            for (DNSEntry addressEntry : this.getCache().getDNSEntryList(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 4 with Fields

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

the class JmDNSImpl method registerServiceType.

/**
 * {@inheritDoc}
 */
@Override
public boolean registerServiceType(String type) {
    boolean typeAdded = false;
    Map<Fields, String> map = ServiceInfoImpl.decodeQualifiedNameMapForType(type);
    String domain = map.get(Fields.Domain);
    String protocol = map.get(Fields.Protocol);
    String application = map.get(Fields.Application);
    String subtype = map.get(Fields.Subtype);
    final String name = (application.length() > 0 ? "_" + application + "." : "") + (protocol.length() > 0 ? "_" + protocol + "." : "") + domain + ".";
    final String loname = name.toLowerCase();
    if (logger.isLoggable(Level.FINE)) {
        logger.fine(this.getName() + ".registering service type: " + type + " as: " + name + (subtype.length() > 0 ? " subtype: " + subtype : ""));
    }
    if (!_serviceTypes.containsKey(loname) && !application.toLowerCase().equals("dns-sd") && !domain.toLowerCase().endsWith("in-addr.arpa") && !domain.toLowerCase().endsWith("ip6.arpa")) {
        typeAdded = _serviceTypes.putIfAbsent(loname, new ServiceTypeEntry(name)) == null;
        if (typeAdded) {
            final ServiceTypeListenerStatus[] list = _typeListeners.toArray(new ServiceTypeListenerStatus[_typeListeners.size()]);
            final ServiceEvent event = new ServiceEventImpl(this, name, "", null);
            for (final ServiceTypeListenerStatus status : list) {
                _executor.submit(new Runnable() {

                    /**
                     * {@inheritDoc}
                     */
                    @Override
                    public void run() {
                        status.serviceTypeAdded(event);
                    }
                });
            }
        }
    }
    if (subtype.length() > 0) {
        ServiceTypeEntry subtypes = _serviceTypes.get(loname);
        if ((subtypes != null) && (!subtypes.contains(subtype))) {
            synchronized (subtypes) {
                if (!subtypes.contains(subtype)) {
                    typeAdded = true;
                    subtypes.add(subtype);
                    final ServiceTypeListenerStatus[] list = _typeListeners.toArray(new ServiceTypeListenerStatus[_typeListeners.size()]);
                    final ServiceEvent event = new ServiceEventImpl(this, "_" + subtype + "._sub." + name, "", null);
                    for (final ServiceTypeListenerStatus status : list) {
                        _executor.submit(new Runnable() {

                            /**
                             * {@inheritDoc}
                             */
                            @Override
                            public void run() {
                                status.subTypeForServiceTypeAdded(event);
                            }
                        });
                    }
                }
            }
        }
    }
    return typeAdded;
}
Also used : Fields(javax.jmdns.ServiceInfo.Fields) ServiceEvent(javax.jmdns.ServiceEvent) ServiceTypeListenerStatus(javax.jmdns.impl.ListenerStatus.ServiceTypeListenerStatus)

Aggregations

Fields (javax.jmdns.ServiceInfo.Fields)4 Inet4Address (java.net.Inet4Address)2 Inet6Address (java.net.Inet6Address)2 ServiceEvent (javax.jmdns.ServiceEvent)2 ServiceInfo (javax.jmdns.ServiceInfo)2 ServiceTypeListenerStatus (javax.jmdns.impl.ListenerStatus.ServiceTypeListenerStatus)2