Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from IvBaranov/both_ip_versions
Browse files Browse the repository at this point in the history
Both ip versions
  • Loading branch information
aurae committed Sep 14, 2015
2 parents 9bb0901 + 361e16c commit d0dfc96
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 14 deletions.
10 changes: 8 additions & 2 deletions example/src/main/java/rxbonjour/example/BonjourVH.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public class BonjourVH extends RvBaseHolder<BonjourService> {

@Bind(R.id.tv_name) TextView tvName;
@Bind(R.id.tv_type) TextView tvType;
@Bind(R.id.tv_host_port) TextView tvHostPort;
@Bind(R.id.tv_host_port_v4) TextView tvHostPortV4;
@Bind(R.id.tv_host_port_v6) TextView tvHostPortV6;
@Bind(R.id.tv_txtrecords) TextView tvTxtRecords;

/**
Expand All @@ -35,7 +36,12 @@ protected BonjourVH(LayoutInflater inflater, ViewGroup parent) {
@Override protected void onBindItem(BonjourService item) {
tvName.setText(item.getName());
tvType.setText(item.getType());
tvHostPort.setText(item.getHost() + ":" + item.getPort());
if (item.getmHostv4() != null) {
tvHostPortV4.setText(item.getmHostv4() + ":" + item.getPort());
}
if (item.getmHostv6() != null) {
tvHostPortV6.setText(item.getmHostv6() + ":" + item.getPort());
}

// Display TXT records, if any could be resolved
int txtRecordCount = item.getTxtRecordCount();
Expand Down
8 changes: 7 additions & 1 deletion example/src/main/res/layout/item_bonjourservice.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
android:textSize="14sp"/>

<TextView
android:id="@+id/tv_host_port"
android:id="@+id/tv_host_port_v4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"/>

<TextView
android:id="@+id/tv_host_port_v6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"/>
Expand Down
16 changes: 15 additions & 1 deletion lib/src/main/java/rxbonjour/internal/JBBonjourDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
import android.os.Bundle;

import java.lang.ref.WeakReference;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
Expand Down Expand Up @@ -71,8 +75,18 @@ public JBBonjourDiscovery() {
txtRecords = new Bundle(0);
}

InetAddress host = serviceInfo.getHost();
Inet4Address hostv4 = null;
Inet6Address hostv6 = null;
if (host instanceof Inet4Address) {
hostv4 = (Inet4Address) host;
} else if (host instanceof Inet6Address) {
hostv6 = (Inet6Address) host;
}


// Create and return an event wrapping the BonjourService
BonjourService service = new BonjourService(serviceInfo.getServiceName(), serviceInfo.getServiceType(), serviceInfo.getHost(), serviceInfo.getPort(), txtRecords);
BonjourService service = new BonjourService(serviceInfo.getServiceName(), serviceInfo.getServiceType(), hostv4, hostv6, serviceInfo.getPort(), txtRecords);
return new BonjourEvent(type, service);
}

Expand Down
16 changes: 12 additions & 4 deletions lib/src/main/java/rxbonjour/internal/SupportBonjourDiscovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.Enumeration;

import java.util.HashMap;
import java.util.Map;
import javax.jmdns.JmDNS;
import javax.jmdns.ServiceEvent;
import javax.jmdns.ServiceInfo;
Expand Down Expand Up @@ -64,11 +68,15 @@ private BonjourEvent newBonjourEvent(BonjourEvent.Type type, ServiceEvent event)
// Access the event's ServiceInfo and obtain a suitable IP address
ServiceInfo info = event.getInfo();
InetAddress[] addresses = info.getInetAddresses();
InetAddress address = null;
Inet4Address inet4Address = null;
Inet6Address inet6Address = null;
for (InetAddress a : addresses) {
if (a != null) {
address = a;
break;
if (a instanceof Inet4Address) {
inet4Address = (Inet4Address) a;
} else if (a instanceof Inet6Address) {
inet6Address = (Inet6Address) a;
}
}
}

Expand All @@ -81,7 +89,7 @@ private BonjourEvent newBonjourEvent(BonjourEvent.Type type, ServiceEvent event)
}

// Create the service object and wrap it in an event
BonjourService service = new BonjourService(event.getName(), event.getType(), address, info.getPort(), txtRecords);
BonjourService service = new BonjourService(event.getName(), event.getType(), inet4Address, inet6Address, info.getPort(), txtRecords);
return new BonjourEvent(type, service);
}

Expand Down
32 changes: 26 additions & 6 deletions lib/src/main/java/rxbonjour/model/BonjourService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import android.os.Bundle;

import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.util.Map;

/**
* @author marcel
Expand All @@ -11,14 +14,16 @@ public class BonjourService {

private String mName;
private String mType;
private InetAddress mHost;
private Inet4Address mHostv4;
private Inet6Address mHostv6;
private int mPort;
private Bundle mTxtRecords;

public BonjourService(String name, String type, InetAddress host, int port, Bundle txtRecords) {
public BonjourService(String name, String type, Inet4Address hostv4, Inet6Address hostv6, int port, Bundle txtRecords) {
mName = name;
mType = type;
mHost = host;
mHostv4 = hostv4;
mHostv6 = hostv6;
mPort = port;
mTxtRecords = txtRecords;
}
Expand All @@ -31,8 +36,21 @@ public String getType() {
return mType;
}

@Deprecated
public InetAddress getHost() {
return mHost;
if (mHostv4 != null) {
return mHostv4;
} else {
return mHostv6;
}
}

public Inet4Address getmHostv4() {
return mHostv4;
}

public Inet6Address getmHostv6() {
return mHostv6;
}

public int getPort() {
Expand All @@ -56,7 +74,8 @@ public String getTxtRecord(String key, String defaultValue) {
return "BonjourService{" +
"mName='" + mName + '\'' +
", mType='" + mType + '\'' +
", mHost=" + mHost +
", mHostv4=" + mHostv4 +
", mHostv6=" + mHostv6 +
", mPort=" + mPort +
'}';
}
Expand All @@ -73,7 +92,8 @@ public String getTxtRecord(String key, String defaultValue) {
@Override public int hashCode() {
int result = mName != null ? mName.hashCode() : 0;
result = 31 * result + (mType != null ? mType.hashCode() : 0);
result = 31 * result + (mHost != null ? mHost.hashCode() : 0);
result = 31 * result + (mHostv4 != null ? mHostv4.hashCode() : 0);
result = 31 * result + (mHostv6 != null ? mHostv6.hashCode() : 0);
result = 31 * result + mPort;
return result;
}
Expand Down

0 comments on commit d0dfc96

Please sign in to comment.