Skip to content

Commit

Permalink
Clean up service resolving code. (#764)
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Baumann <[email protected]>
  • Loading branch information
maniac103 authored and mueller-ma committed Mar 13, 2018
1 parent 8734a0e commit 4231785
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
import org.openhab.habdroid.model.OpenHABSitemap;
import org.openhab.habdroid.ui.activity.ContentController;
import org.openhab.habdroid.util.AsyncServiceResolver;
import org.openhab.habdroid.util.AsyncServiceResolverListener;
import org.openhab.habdroid.util.Constants;
import org.openhab.habdroid.util.MyHttpClient;
import org.openhab.habdroid.util.MyWebImage;
Expand Down Expand Up @@ -129,7 +128,7 @@
import static org.openhab.habdroid.util.Util.removeProtocolFromUrl;

public class OpenHABMainActivity extends AppCompatActivity implements
MemorizingResponder, AsyncServiceResolverListener, ConnectionFactory.UpdateListener {
MemorizingResponder, AsyncServiceResolver.Listener, ConnectionFactory.UpdateListener {

private abstract class DefaultHttpResponseHandler implements MyHttpClient.ResponseHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,48 +30,44 @@

public class AsyncServiceResolver extends Thread implements ServiceListener {
private final static String TAG = AsyncServiceResolver.class.getSimpleName();
private Context mCtx;

public interface Listener {
void onServiceResolved(ServiceInfo serviceInfo);
void onServiceResolveFailed();
}

// Multicast lock for mDNS
private MulticastLock mMulticastLock;
// mDNS service
private JmDNS mJmdns;
private String mServiceType;
private ServiceInfo mResolvedServiceInfo;
private Thread mSleepingThread;
private boolean mIsResolved = false;
private AsyncServiceResolverListener mListener;
private final static int DEFAULT_DISCOVERY_TIMEOUT = 3000;
private Listener mListener;
private Handler mHandler;

public AsyncServiceResolver(Context context, String serviceType) {
super();
mCtx = context;
mServiceType = serviceType;
if (context instanceof AsyncServiceResolverListener)
mListener = (AsyncServiceResolverListener)context;
}
private final static int DEFAULT_DISCOVERY_TIMEOUT = 3000;

public AsyncServiceResolver(Context context, AsyncServiceResolverListener listener, String serviceType) {
public AsyncServiceResolver(Context context, Listener listener, String serviceType) {
super();
mCtx = context;
mServiceType = serviceType;
mListener = listener;
mHandler = new Handler(Looper.getMainLooper());

WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mMulticastLock = wifiManager.createMulticastLock("HABDroidMulticastLock");
mMulticastLock.setReferenceCounted(true);
}

@Override
public void run() {
WifiManager wifi =
(android.net.wifi.WifiManager)
mCtx.getApplicationContext().getSystemService(android.content.Context.WIFI_SERVICE);
mMulticastLock = wifi.createMulticastLock("HABDroidMulticastLock");
mMulticastLock.setReferenceCounted(true);
try {
mMulticastLock.acquire();
} catch (SecurityException e) {
Log.i(TAG, "Security exception during multicast lock");
Log.i(TAG, "Could not acquire multicast lock", e);
}
mSleepingThread = Thread.currentThread();

Log.i(TAG, "Discovering service " + mServiceType);
try {
//Log.i(TAG, "Local IP:" + getLocalIpv4Address().getHostAddress().toString());
/* TODO: This is a dirty fix of some crazy ipv6 incompatibility
This workaround makes JMDNS work on local ipv4 address an thus
discover openHAB on ipv4 address. This should be fixed to fully
Expand All @@ -81,51 +77,43 @@ public void run() {
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}

try {
// Sleep for specified timeout
Thread.sleep(DEFAULT_DISCOVERY_TIMEOUT);
if (!mIsResolved) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mListener.onServiceResolveFailed();
}
});
if (mResolvedServiceInfo == null) {
mHandler.post(() -> mListener.onServiceResolveFailed());
shutdown();
}
} catch (InterruptedException ignored) {}
}

@Override
public void serviceAdded(ServiceEvent event) {
Log.d(TAG, "Service Added " + event.getName());
Log.d(TAG, "Service added " + event.getName());
mJmdns.requestServiceInfo(event.getType(), event.getName(), 1);
}

@Override
public void serviceRemoved(ServiceEvent event) {
}

@Override
public void serviceResolved(ServiceEvent event) {
mResolvedServiceInfo = event.getInfo();
mIsResolved = true;
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
mListener.onServiceResolved(mResolvedServiceInfo);
}
});
mHandler.post(() -> mListener.onServiceResolved(mResolvedServiceInfo));
shutdown();
mSleepingThread.interrupt();
interrupt();
}

private void shutdown() {
if (mMulticastLock != null)
mMulticastLock.release();
mMulticastLock.release();
if (mJmdns != null) {
mJmdns.removeServiceListener(mServiceType, this);
try {
mJmdns.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage());
// ignore
}
}
}
Expand Down

This file was deleted.

0 comments on commit 4231785

Please sign in to comment.