Subscribe

request serviceInfo in different thread

  1. 2009-03-10 13:00:19 PDT
    Hey all, random question. I added a service listener and was having a load of trouble getting it to actually resolve a service. It would add services fine, but there seemed to be some blocking/threading/timing issues that made it randomly succeed/fail at resolving the service (the callback would either get called or not get called).

    I started requesting the info in a new thread, and this seems to have totally fixed the problem, but I would really like to know what actually happened. In the examples, and in the browser, they do not seem to have to do this workaround.

    This is all running on a local machine, with one client and one host on the same machine with different names

    Thanks,
    hamy

    ____________________________________

    private class ServiceRequestor implements Runnable {
    private ServiceEvent event_;
    public ServiceRequestor(ServiceEvent e) { event_ = e; }
    public void run() { jmdns_.requestServiceInfo(event_.getType(), event_.getName()); }
    }

    private class HostMonitorListener implements ServiceListener {
    // TODO bad practice that I am accessing private parent members...
    public void serviceAdded(ServiceEvent e) {
    log("found service " + e.getName());

    if (e.getName().equals(HostImpl.SERVICENAME)) {
    SwingUtilities.invokeLater(new ServiceRequestor(e));
    }
    }

    public void serviceRemoved(ServiceEvent e) {
    log("service " + e.getName() + " removed");
    }

    public void serviceResolved(ServiceEvent e) {
    log("service finally resolved");
    if (e.getName().equals(HostImpl.SERVICENAME)) {

    ServiceInfo info = e.getInfo();
    if (info == null)
    return;
    String hostURI = "rmi://" + info.getHostAddress() + ":"
    + info.getPort() + "/" + info.getName();
    log("Found uri of " + hostURI);
    Host h = (Host) RemotingUtils.lookupRMIService(hostURI,
    Host.class);
    hostList_.add(h);
    }
    }
  2. 2010-01-21 15:14:10 PST
    I can confirm this. I believe this must be a bug. I did a wireshark trace and the dns sever was returning the information, but the serviceResolved method would not get called consistently unless requestServiceInfo was placed inside of a thread. Here is a slimmed down version of what I did to get consistent resolving. import java.io.IOException; import java.net.InetAddress; import java.util.Enumeration; import javax.jmdns.JmDNS; import javax.jmdns.ServiceEvent; import javax.jmdns.ServiceInfo; import javax.jmdns.ServiceListener; import javax.jmdns.ServiceTypeListener; import javax.jmdns.impl.JmDNSImpl; public class Listener implements ServiceListener, ServiceTypeListener { Listener() { try { jmdns = JmDNS.create(); jmdns.addServiceTypeListener(this); jmdns.registerServiceType("_daap._tcp.local."); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static JmDNS jmdns = null; @Override public void serviceAdded(ServiceEvent event) { String name = event.getName(); String type = event.getType(); System.out.println("Service of type " + type + " added: " + name); (new resolveThread(event)).start(); } @Override public void serviceRemoved(ServiceEvent event) { String name = event.getName(); System.out.println("Service removed: " + name); } @Override public void serviceResolved(ServiceEvent event) { String name = event.getName(); String type = event.getType(); ServiceInfo info = event.getInfo(); if (info == null) { System.out.println("Service not found!"); } else { StringBuffer buf = new StringBuffer(); buf.append(name); buf.append('.'); buf.append(type); buf.append('\n'); buf.append(info.getServer()); buf.append(':'); buf.append(info.getPort()); buf.append('\n'); buf.append(info.getAddress()); buf.append(':'); buf.append(info.getPort()); buf.append('\n'); for (Enumeration names = info.getPropertyNames(); names .hasMoreElements();) { String prop = (String) names.nextElement(); buf.append(prop); buf.append('='); buf.append(info.getPropertyString(prop)); buf.append('\n'); } System.out.println(buf); address = info.getAddress(); } } @Override public void serviceTypeAdded(ServiceEvent event) { jmdns.removeServiceListener(type, this); jmdns.addServiceListener(type, this); } public InetAddress getAddress() { return address; } private InetAddress address; private class resolveThread extends Thread { private ServiceEvent event_; public resolveThread(ServiceEvent e) { event_ = e; } @Override public void run() { jmdns.requestServiceInfo(event_.getType(), event_.getName()); } } }
  3. 2010-01-21 15:54:55 PST
    Could please confirm which version of the code you are using? Thanks Pierre
Jump To:
< Previous | 1 | Next >

Add a Reply

This forum does not allow anonymous participation.

Log in to add a reply. Not registered? Create an account to participate and receive email updates when replies are posted to this topic.