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();
}
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;
}
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;
}
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);
});
}
}
}
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;
}
Aggregations