Skip to content

Commit

Permalink
Attempt fixing issues with jmdns #107
Browse files Browse the repository at this point in the history
  • Loading branch information
khk624 authored and khk624 committed Jun 16, 2014
1 parent 60ffc89 commit 1f5cd42
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
19 changes: 19 additions & 0 deletions src/com/connectsdk/core/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

package com.connectsdk.core;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
Expand All @@ -28,6 +30,9 @@

import org.apache.http.conn.util.InetAddressUtils;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Handler;
import android.os.Looper;

Expand Down Expand Up @@ -123,4 +128,18 @@ public static boolean isIPv4Address(String ipAddress) {
public static boolean isIPv6Address(String ipAddress) {
return InetAddressUtils.isIPv6Address(ipAddress);
}

public static InetAddress getIpAddress(Context context) throws UnknownHostException {
WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();

if (ip == 0) {
return null;
}
else {
byte[] ipAddress = convertIpAddress(ip);
return InetAddress.getByAddress(ipAddress);
}
}
}
5 changes: 3 additions & 2 deletions src/com/connectsdk/discovery/DiscoveryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@
import com.connectsdk.device.ConnectableDeviceListener;
import com.connectsdk.device.ConnectableDeviceStore;
import com.connectsdk.device.DefaultConnectableDeviceStore;
import com.connectsdk.discovery.provider.ZeroconfDiscoveryProvider;
import com.connectsdk.discovery.provider.CastDiscoveryProvider;
import com.connectsdk.discovery.provider.SSDPDiscoveryProvider;
import com.connectsdk.discovery.provider.ZeroconfDiscoveryProvider;
import com.connectsdk.service.AirPlayService;
import com.connectsdk.service.CastService;
import com.connectsdk.service.DIALService;
Expand Down Expand Up @@ -199,6 +199,7 @@ public DiscoveryManager(Context context, ConnectableDeviceStore connectableDevic

WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
multicastLock = wifiMgr.createMulticastLock("Connect SDK");
multicastLock.setReferenceCounted(true);

capabilityFilters = new ArrayList<CapabilityFilter>();
pairingLevel = PairingLevel.OFF;
Expand Down Expand Up @@ -364,7 +365,7 @@ public boolean deviceIsCompatible(ConnectableDevice device) {
* + NetcastTVService
* + RokuService
* + WebOSTVService
* - AirPlayDiscoveryProvider
* - ZeroconfDiscoveryProvider
* + AirPlayService
*/
public void registerDefaultDeviceTypes() {
Expand Down
13 changes: 3 additions & 10 deletions src/com/connectsdk/discovery/provider/SSDPDiscoveryProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,10 @@ private void openSocket() {
if (mSSDPSocket != null && mSSDPSocket.isConnected())
return;

WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();

int ip = wifiInfo.getIpAddress();
if (ip == 0)
return;

byte[] ipAddress = Util.convertIpAddress(ip);

try {
InetAddress source = InetAddress.getByAddress(ipAddress);
InetAddress source = Util.getIpAddress(context);
if (source == null)
return;

mSSDPSocket = new SSDPSocket(source);
} catch (UnknownHostException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.connectsdk.discovery.provider;

import java.io.IOException;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
Expand All @@ -31,6 +32,7 @@
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceListener;
import javax.jmdns.ServiceTypeListener;

import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -45,6 +47,8 @@
import com.connectsdk.service.config.ServiceDescription;

public class ZeroconfDiscoveryProvider implements DiscoveryProvider {
private static final String HOSTNAME = "connectsdk";

This comment has been minimized.

Copy link
@iheart2code

iheart2code Jun 16, 2014

Contributor

@khk624 Why are you generating a static value for the hostname? Isn't this supposed to come from the device somewhere?

This comment has been minimized.

Copy link
@iheart2code

iheart2code Jun 16, 2014

Contributor

Nevermind, I was able to figure it out. I would change the variable name to something else though, as HOSTNAME is a bit misleading. JMDNS_INSTANCE_NAME might be better, although it's a bit long. We might also just pall null to the create method at line 140.

http://jmdns.sourceforge.net/apidocs/javax/jmdns/JmDNS.html#create(java.net.InetAddress, java.lang.String)


JmDNS jmdns;

private final static int RESCAN_INTERVAL = 10000;
Expand Down Expand Up @@ -116,20 +120,24 @@ public void serviceAdded(ServiceEvent event) {
private CopyOnWriteArrayList<DiscoveryProviderListener> serviceListeners;

public ZeroconfDiscoveryProvider(Context context) {
initJmDNS();
initJmDNS(context);

services = new ConcurrentHashMap<String, ServiceDescription>(8, 0.75f, 2);
serviceListeners = new CopyOnWriteArrayList<DiscoveryProviderListener>();
serviceFilters = new ArrayList<JSONObject>();
}

private void initJmDNS() {
private void initJmDNS(final Context context) {
Util.runInBackground(new Runnable() {

@Override
public void run() {
try {
jmdns = JmDNS.create();
InetAddress source = Util.getIpAddress(context);
if (source == null)
return;

jmdns = JmDNS.create(source, HOSTNAME);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down

0 comments on commit 1f5cd42

Please sign in to comment.