Skip to content

Commit

Permalink
Adding more defensive checks / exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Feb 27, 2023
1 parent 73e1a33 commit de0e3c8
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
package com.chip.casting;

import android.net.nsd.NsdServiceInfo;
import android.util.Log;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class DiscoveredNodeData {
private static final String TAG = DiscoveredNodeData.class.getSimpleName();

private static final int MAX_IP_ADDRESSES = 5;
private static final int MAX_ROTATING_ID_LEN = 50;
private static final String KEY_DEVICE_NAME = "DN";
Expand Down Expand Up @@ -66,21 +69,29 @@ public DiscoveredNodeData(NsdServiceInfo serviceInfo) {
String vp = new String(attributes.get(KEY_VENDOR_PRODUCT), StandardCharsets.UTF_8);
if (vp != null) {
String[] vpArray = vp.split("\\+");
if (vpArray.length > 0) {
this.vendorId = Long.parseLong(vpArray[0]);
if (vpArray.length == 2) {
this.productId = Long.parseLong(vpArray[1]);
try {
if (vpArray.length > 0) {
this.vendorId = Long.parseLong(vpArray[0]);
if (vpArray.length == 2) {
this.productId = Long.parseLong(vpArray[1]);
}
}
} catch (NumberFormatException e) {
Log.e(TAG, "Could not parse TXT record for VP: " + e.getMessage());
}
} else {
Log.e(TAG, "TXT Record for VP was null");
}
}
}

if (serviceInfo.getHost() != null) {
this.hostName = serviceInfo.getHost().getHostName();
this.ipAddresses = Arrays.asList(serviceInfo.getHost());
} else {
Log.e(TAG, "Host name was null");
}
this.port = serviceInfo.getPort();
this.ipAddresses = Arrays.asList(serviceInfo.getHost());
this.numIPs = 1;
}

Expand Down

0 comments on commit de0e3c8

Please sign in to comment.