android - How To Make JmDNS Work On A Large Network -
i'm working on adding discovery our app using (http://jmdns.sourceforge.net/). it's working correctly on small home network. fails on large network @ office. seem recall reading if app blocks more 5 seconds get's reset. , appears what's happening. first, how can sure that's problem?
of course, main question how can can make jmdns work on large network. , more general question do when need more 5 seconds?
small snippet of code (it's in asynctask):
inetaddress bindingaddress = inetaddress.getbyname(ipofthisdevice); jmdns = jmdns.create(bindingaddress); servicelist = arrays.aslist(jmdns.list("_myappname._tcp.local.",5000)); // 5 second timeout
here code used in android app seems work @ office?
/* zeroconf browser - http://melloware.com/ copyright (c) 2010 melloware inc rights reserved. */ package com.melloware.zeroconf; import java.io.ioexception; import java.net.inetaddress; import java.net.unknownhostexception; import java.util.arraylist; import java.util.collections; import java.util.enumeration; import java.util.iterator; import java.util.treemap; import javax.jmdns.jmdns; import javax.jmdns.serviceevent; import javax.jmdns.serviceinfo; import javax.jmdns.servicelistener; import javax.jmdns.servicetypelistener; import android.app.expandablelistactivity; import android.content.context; import android.net.wifi.wifiinfo; import android.net.wifi.wifimanager; import android.net.wifi.wifimanager.multicastlock; import android.os.bundle; import android.text.html; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.imageview; import android.widget.textview; /** * service tab contains expandable list of dns services , details of each service type * gathered. * <p> * copyright (c) 2010 melloware, inc. <http://www.melloware.com> * @author emil a. lefkof iii <info@melloware.com> * @version 1.0 */ public class serviceactivity extends expandablelistactivity implements servicelistener, servicetypelistener { /** * tag used logging */ private static final string tag = serviceactivity.class.getname(); /** * value used identify in zeroconf */ private static final string hostname = "melloware"; /** * sorted array of top level items service types */ public static final arraylist<servicetype> groups = new arraylist<servicetype>(); /** * sorted list of details each service type */ public static final treemap<string, arraylist<serviceinfo>> details = new treemap<string, arraylist<serviceinfo>>(); /** * instance of bonjour/rendezvous/zeroconf handler */ public static jmdns jmdns = null; /** * allows application receive wifi multicast packets. */ private static multicastlock multicastlock = null; /** * backing adapter listview of services */ private static dnsexpandablelistadapter madapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); madapter = new dnsexpandablelistadapter(); setlistadapter(madapter); } /* * (non-javadoc) * @see android.app.activity#onstart() */ @override protected void onstart() { log.i(tag, "starting serviceactivity..."); super.onstart(); try { log.i(tag, "starting mutlicast lock..."); wifimanager wifi = (wifimanager) this.getsystemservice(context.wifi_service); // device ip address final inetaddress deviceipaddress = getdeviceipaddress(wifi); multicastlock = wifi.createmulticastlock(getclass().getname()); multicastlock.setreferencecounted(true); multicastlock.acquire(); log.i(tag, "starting zeroconf probe...."); jmdns = jmdns.create(deviceipaddress, hostname); jmdns.addservicetypelistener(this); } catch (ioexception ex) { log.e(tag, ex.getmessage(), ex); } log.i(tag, "started zeroconf probe...."); } /* * (non-javadoc) * @see android.app.activitygroup#onstop() */ @override protected void onstop() { log.i(tag, "stopping serviceactivity..."); super.onstop(); stopscan(); details.clear(); groups.clear(); if (!isfinishing()) { madapter.notifydatasetchanged(); } } /** * stops scanning , cleans locks. */ private static void stopscan() { try { if (jmdns != null) { log.i(tag, "stopping zeroconf probe...."); jmdns.unregisterallservices(); jmdns.close(); jmdns = null; } if (multicastlock != null) { log.i(tag, "releasing mutlicast lock..."); multicastlock.release(); multicastlock = null; } } catch (exception ex) { log.e(tag, ex.getmessage(), ex); } } /** * gets current android device ip address or return 10.0.0.2 localhost on android. * <p> * @return inetaddress of android device */ private inetaddress getdeviceipaddress(wifimanager wifi) { inetaddress result = null; try { // default android localhost result = inetaddress.getbyname("10.0.0.2"); // figure out our wifi address, otherwise bail wifiinfo wifiinfo = wifi.getconnectioninfo(); int intaddr = wifiinfo.getipaddress(); byte[] byteaddr = new byte[] { (byte) (intaddr & 0xff), (byte) (intaddr >> 8 & 0xff), (byte) (intaddr >> 16 & 0xff), (byte) (intaddr >> 24 & 0xff) }; result = inetaddress.getbyaddress(byteaddr); } catch (unknownhostexception ex) { log.w(tag, string.format("getdeviceipaddress error: %s", ex.getmessage())); } return result; } /** * delegate method mdns when service added. */ public void serviceadded(serviceevent event) { log.i(tag, string.format("zeroconf serviceadded(event=\n%s\n)", event.tostring())); arraylist<serviceinfo> list = details.get(event.gettype()); if (list != null) { serviceinfo info = event.getinfo(); if (!list.contains(info)) { list.add(info); } } } /** * delegate method mdns when service removed. */ public void serviceremoved(serviceevent event) { log.w(tag, string.format("zeroconf serviceremoved(event=\n%s\n)", event.tostring())); } /** * delegate method mdns when service resolved. */ public void serviceresolved(serviceevent event) { log.i(tag, string.format("zeroconf serviceresolved(event=\n%s\n)", event.tostring())); arraylist<serviceinfo> list = details.get(event.gettype()); if (list != null) { serviceinfo info = event.getinfo(); if (!list.contains(info)) { list.add(info); } } } /** * delegate method mdns when new service type discovered. */ public void servicetypeadded(final serviceevent event) { log.i(tag, string.format("zeroconf servicetypeadded(event=\n%s\n)", event.tostring())); jmdns.addservicelistener(event.gettype(), this); runonuithread(new runnable() { public void run() { final servicetype type = new servicetype(); type.setname(event.gettype()); groups.add(type); collections.sort(groups); details.put(event.gettype(), new arraylist<serviceinfo>()); madapter.notifydatasetchanged(); } }); } /** * delegate method mdns when subtype discovered. */ public void subtypeforservicetypeadded(serviceevent event) { log.i(tag, string.format("zeroconf subtypeforservicetypeadded(event=\n%s\n)", event.tostring())); } /** * when scan complete show message if no services found. * <p> * @param context applicationcontext */ public static void scanfinished(context context) { if (groups.size() == 0) { final servicetype type = new servicetype(); type.setname(context.getresources().getstring(r.string.msg_noservices)); groups.add(type); madapter.notifydatasetchanged(); stopscan(); } } /** * expandablelistadapter displays service types groups , when each service type expanded displays * list of discovered services service type. */ public class dnsexpandablelistadapter extends baseexpandablelistadapter { layoutinflater inflater = layoutinflater.from(getapplicationcontext()); public object getchild(int groupposition, int childposition) { try { iterator<arraylist<serviceinfo>> = details.values().iterator(); int = 0; while (it.hasnext()) { arraylist<serviceinfo> type = it.next(); if (i == groupposition) { serviceinfo service = type.get(childposition); serviceinfo resolvedservice = jmdns.getserviceinfo(service.gettype(), service.getname()); if (resolvedservice != null) { service = resolvedservice; } stringbuffer buf = new stringbuffer(); buf.append("<b>"); buf.append(service.getname()); buf.append("</b><br/>"); buf.append(service.gettypewithsubtype()); buf.append("<br/>"); buf.append(service.getserver()); buf.append(':'); buf.append(service.getport()); buf.append("<br/>"); buf.append(service.getinetaddresses()[0]); buf.append("<br/>"); (enumeration<string> names = service.getpropertynames(); names.hasmoreelements();) { buf.append("<br/>"); string prop = names.nextelement(); buf.append("<b>"); buf.append(prop); buf.append("</b>"); buf.append(" = "); buf.append("<i>"); buf.append(service.getpropertystring(prop)); buf.append("</i>"); } return buf.tostring(); } i++; } } catch (exception e) { log.w("exception", e); } return "not available"; } public long getchildid(int groupposition, int childposition) { return childposition; } public int getchildrencount(int groupposition) { iterator<arraylist<serviceinfo>> = details.values().iterator(); int = 0; while (it.hasnext()) { arraylist<serviceinfo> type = it.next(); if (i == groupposition) { return type.size(); } i++; } return 1; } public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { convertview = this.inflater.inflate(r.layout.child_row, parent, false); ((textview) convertview.findviewbyid(r.id.childvalue)).settext(html.fromhtml(getchild(groupposition, childposition).tostring())); return convertview; } public object getgroup(int groupposition) { return groups.get(groupposition); } public int getgroupcount() { return groups.size(); } public long getgroupid(int groupposition) { return groupposition; } public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { convertview = this.inflater.inflate(r.layout.group_row, parent, false); servicetype type = (servicetype) getgroup(groupposition); imageview imageview = (imageview) convertview.findviewbyid(r.id.serviceicon); imageview.setimageresource(type.getimageicon()); ((textview) convertview.findviewbyid(r.id.service)).settext(type.tostring()); return convertview; } public boolean ischildselectable(int groupposition, int childposition) { return true; } public boolean hasstableids() { return true; } } }
Comments
Post a Comment