From db120133b9edf03f4226126a2820805bbb909573 Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Fri, 26 Feb 2016 09:31:08 +1100 Subject: [PATCH] Don't try to start bonjour without an IP. Although not reproduced, it looks very much like this would be related to, and should subsequently fix #556. --- .../localrepo/type/BonjourBroadcast.java | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/F-Droid/src/org/fdroid/fdroid/localrepo/type/BonjourBroadcast.java b/F-Droid/src/org/fdroid/fdroid/localrepo/type/BonjourBroadcast.java index 7497ca68b..11711ee49 100644 --- a/F-Droid/src/org/fdroid/fdroid/localrepo/type/BonjourBroadcast.java +++ b/F-Droid/src/org/fdroid/fdroid/localrepo/type/BonjourBroadcast.java @@ -1,6 +1,7 @@ package org.fdroid.fdroid.localrepo.type; import android.content.Context; +import android.support.annotation.Nullable; import android.util.Log; import org.fdroid.fdroid.FDroidApp; @@ -10,6 +11,7 @@ import org.fdroid.fdroid.localrepo.SwapService; import java.io.IOException; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.HashMap; import javax.jmdns.JmDNS; @@ -31,10 +33,15 @@ public class BonjourBroadcast extends SwapType { @Override public void start() { - Utils.debugLog(TAG, "Preparing to start Bonjour service."); sendBroadcast(SwapService.EXTRA_STARTING); + InetAddress address = getDeviceAddress(); + if (address == null) { + Log.e(TAG, "Starting Bonjour service, but couldn't ascertain IP address. Seems we are not connected to a network."); + return; + } + /* * a ServiceInfo can only be registered with a single instance * of JmDNS, and there is only ever a single LocalHTTPD port to @@ -43,6 +50,7 @@ public class BonjourBroadcast extends SwapType { if (pairService != null || jmdns != null) { clearCurrentMDNSService(); } + String repoName = Preferences.get().getLocalRepoName(); HashMap values = new HashMap<>(); values.put("path", "/fdroid/repo"); @@ -59,7 +67,7 @@ public class BonjourBroadcast extends SwapType { try { Utils.debugLog(TAG, "Starting bonjour service..."); pairService = ServiceInfo.create(type, repoName, FDroidApp.port, 0, 0, values); - jmdns = JmDNS.create(InetAddress.getByName(FDroidApp.ipAddressString)); + jmdns = JmDNS.create(address); jmdns.registerService(pairService); setConnected(true); Utils.debugLog(TAG, "... Bounjour service started."); @@ -90,4 +98,15 @@ public class BonjourBroadcast extends SwapType { return SwapService.BONJOUR_STATE_CHANGE; } + @Nullable + private InetAddress getDeviceAddress() { + if (FDroidApp.ipAddressString != null) { + try { + return InetAddress.getByName(FDroidApp.ipAddressString); + } catch (UnknownHostException e) { } + } + + return null; + } + }