Skip to content

Commit

Permalink
fixing Android crash in Bonjour.addEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
MattTuttle committed Dec 29, 2013
1 parent 0de0a06 commit ff4c0df
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
20 changes: 14 additions & 6 deletions hxnet/zeroconf/Bonjour.hx
Expand Up @@ -33,9 +33,9 @@ typedef BonjourCallback = BonjourService->Void;
class Bonjour
{

public var domain:String;
public var type:String;
public var name:String;
public var domain(default, null):String;
public var type(default, null):String;
public var name(default, null):String;
public var port:Int = 0;

public static inline var WILL_PUBLISH:String = "willPublish";
Expand Down Expand Up @@ -155,7 +155,7 @@ class NsdService
var a = new Array<Dynamic>();
a.push(__jobject);
a.push(port);
__jobject = __registerService(a);
__registerService(a);
}

private var __discoverServices:Dynamic;
Expand All @@ -165,7 +165,7 @@ class NsdService
__discoverServices = openfl.utils.JNI.createMemberMethod("hxnet.NsdService", "discoverServices", "()V", true);
var a = new Array<Dynamic>();
a.push(__jobject);
__jobject = __discoverServices(a);
__discoverServices(a);
}

private var __stopService:Dynamic;
Expand All @@ -175,7 +175,7 @@ class NsdService
__stopService = openfl.utils.JNI.createMemberMethod("hxnet.NsdService", "stopService", "()V", true);
var a = new Array<Dynamic>();
a.push(__jobject);
__jobject = __stopService(a);
__stopService(a);
}

public var __jobject:Dynamic;
Expand All @@ -185,6 +185,10 @@ class NsdService
class Bonjour
{

public var domain(default, null):String;
public var type(default, null):String;
public var name(default, null):String;

public static inline var WILL_PUBLISH:String = "willPublish";
public static inline var DID_PUBLISH:String = "didPublish";
public static inline var DID_NOT_PUBLISH:String = "didNotPublish";
Expand All @@ -195,9 +199,13 @@ class Bonjour

public function new(domain:String, type:String, name:String)
{
this.domain = domain;
this.type = type;
this.name = name;
// TODO: don't depend on lime
var getContext = openfl.utils.JNI.createStaticMethod("org.haxe.lime.GameActivity", "getContext", "()Landroid/content/Context;", true);
service = new NsdService(getContext(), type, name);
_listeners = new Map<String, Array<BonjourCallback>>();
}

public function publish(port:Int)
Expand Down
22 changes: 22 additions & 0 deletions template/android/NsdService.java
Expand Up @@ -41,6 +41,7 @@ public void registerService(int port)

public void discoverServices()
{
initializeResolveListener();
initializeDiscoveryListener();
mNsdManager.discoverServices(mServiceType, NsdManager.PROTOCOL_DNS_SD, mDiscoveryListener);
}
Expand Down Expand Up @@ -107,6 +108,27 @@ public void onStopDiscoveryFailed(String serviceType, int errorCode) {
};
}

public void initializeResolveListener() {
mResolveListener = new NsdManager.ResolveListener() {

@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
Log.e(TAG, "Resolve failed" + errorCode);
}

@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
Log.e(TAG, "Resolve Succeeded. " + serviceInfo);

if (serviceInfo.getServiceName().equals(mServiceName)) {
Log.d(TAG, "Same IP.");
return;
}
mServiceInfo = serviceInfo;
}
};
}

public void initializeRegistrationListener() {
mRegistrationListener = new NsdManager.RegistrationListener() {

Expand Down

0 comments on commit ff4c0df

Please sign in to comment.