Skip to content

Commit b8904bb

Browse files
author
Karim Mreisi
committed
fix: support host names with _
Use a JSON to properly encapsule different computer addresses and their port. Fix usage of '_' in computer host names / domain names.
1 parent 1aa9639 commit b8904bb

File tree

2 files changed

+48
-35
lines changed

2 files changed

+48
-35
lines changed

app/src/main/java/com/limelight/computers/ComputerDatabaseManager.java

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import java.util.List;
1010
import java.util.Locale;
1111

12+
import com.limelight.LimeLog;
1213
import com.limelight.nvstream.http.ComputerDetails;
14+
import com.limelight.nvstream.http.LimelightCryptoProvider;
1315
import com.limelight.nvstream.http.NvHTTP;
1416

1517
import android.content.ContentValues;
@@ -18,6 +20,10 @@
1820
import android.database.sqlite.SQLiteDatabase;
1921
import android.database.sqlite.SQLiteException;
2022

23+
import org.json.JSONArray;
24+
import org.json.JSONException;
25+
import org.json.JSONObject;
26+
2127
public class ComputerDatabaseManager {
2228
private static final String COMPUTER_DB_NAME = "computers3.db";
2329
private static final String COMPUTER_TABLE_NAME = "Computers";
@@ -27,9 +33,6 @@ public class ComputerDatabaseManager {
2733
private static final String MAC_ADDRESS_COLUMN_NAME = "MacAddress";
2834
private static final String SERVER_CERT_COLUMN_NAME = "ServerCert";
2935

30-
private static final char ADDRESS_DELIMITER = ';';
31-
private static final char PORT_DELIMITER = '_';
32-
3336
private SQLiteDatabase computerDb;
3437

3538
public ComputerDatabaseManager(Context c) {
@@ -75,13 +78,17 @@ public boolean updateComputer(ComputerDetails details) {
7578
values.put(COMPUTER_UUID_COLUMN_NAME, details.uuid);
7679
values.put(COMPUTER_NAME_COLUMN_NAME, details.name);
7780

78-
StringBuilder addresses = new StringBuilder();
79-
addresses.append(details.localAddress != null ? splitTupleToAddress(details.localAddress) : "");
80-
addresses.append(ADDRESS_DELIMITER).append(details.remoteAddress != null ? splitTupleToAddress(details.remoteAddress) : "");
81-
addresses.append(ADDRESS_DELIMITER).append(details.manualAddress != null ? splitTupleToAddress(details.manualAddress) : "");
82-
addresses.append(ADDRESS_DELIMITER).append(details.ipv6Address != null ? splitTupleToAddress(details.ipv6Address) : "");
81+
try {
82+
JSONObject addresses = new JSONObject();
83+
addresses.put("local", ComputerDetails.AddressTuple.toJson((details.localAddress)));
84+
addresses.put("remote", ComputerDetails.AddressTuple.toJson((details.remoteAddress)));
85+
addresses.put("manual", ComputerDetails.AddressTuple.toJson((details.manualAddress)));
86+
addresses.put("ipv6", ComputerDetails.AddressTuple.toJson((details.ipv6Address)));
87+
values.put(ADDRESSES_COLUMN_NAME, addresses.toString());
88+
} catch (JSONException e) {
89+
LimeLog.warning("JSON error, failed to write computer address information, " + e.getMessage());
90+
}
8391

84-
values.put(ADDRESSES_COLUMN_NAME, addresses.toString());
8592
values.put(MAC_ADDRESS_COLUMN_NAME, details.macAddress);
8693
try {
8794
if (details.serverCert != null) {
@@ -105,36 +112,20 @@ private static String readNonEmptyString(String input) {
105112
return input;
106113
}
107114

108-
private static ComputerDetails.AddressTuple splitAddressToTuple(String input) {
109-
if (input == null) {
110-
return null;
111-
}
112-
113-
String[] parts = input.split(""+PORT_DELIMITER, -1);
114-
if (parts.length == 1) {
115-
return new ComputerDetails.AddressTuple(parts[0], NvHTTP.DEFAULT_HTTP_PORT);
116-
}
117-
else {
118-
return new ComputerDetails.AddressTuple(parts[0], Integer.parseInt(parts[1]));
119-
}
120-
}
121-
122-
private static String splitTupleToAddress(ComputerDetails.AddressTuple tuple) {
123-
return tuple.address+PORT_DELIMITER+tuple.port;
124-
}
125-
126115
private ComputerDetails getComputerFromCursor(Cursor c) {
127116
ComputerDetails details = new ComputerDetails();
128117

129118
details.uuid = c.getString(0);
130119
details.name = c.getString(1);
131-
132-
String[] addresses = c.getString(2).split(""+ADDRESS_DELIMITER, -1);
133-
134-
details.localAddress = splitAddressToTuple(readNonEmptyString(addresses[0]));
135-
details.remoteAddress = splitAddressToTuple(readNonEmptyString(addresses[1]));
136-
details.manualAddress = splitAddressToTuple(readNonEmptyString(addresses[2]));
137-
details.ipv6Address = splitAddressToTuple(readNonEmptyString(addresses[3]));
120+
try {
121+
JSONObject addresses = new JSONObject(c.getString(2));
122+
details.localAddress = ComputerDetails.AddressTuple.fromJson(addresses.getJSONObject("local"));
123+
details.remoteAddress = ComputerDetails.AddressTuple.fromJson(addresses.getJSONObject("remote"));
124+
details.manualAddress = ComputerDetails.AddressTuple.fromJson(addresses.getJSONObject("manual"));
125+
details.ipv6Address = ComputerDetails.AddressTuple.fromJson(addresses.getJSONObject("ipv6"));
126+
} catch (JSONException e) {
127+
LimeLog.warning("JSON error, failed to read computer address information, " + e.getMessage());
128+
}
138129

139130
// External port is persisted in the remote address field
140131
if (details.remoteAddress != null) {

app/src/main/java/com/limelight/nvstream/http/ComputerDetails.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.limelight.nvstream.http;
22

3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
36
import java.security.cert.X509Certificate;
47

58

@@ -31,7 +34,7 @@ public AddressTuple(String address, int port) {
3134

3235
@Override
3336
public int hashCode() {
34-
return address.hashCode();
37+
return new Object[] {address, port}.hashCode();
3538
}
3639

3740
@Override
@@ -54,6 +57,25 @@ public String toString() {
5457
return address + ":" + port;
5558
}
5659
}
60+
61+
public static JSONObject toJson(AddressTuple tuple) throws JSONException {
62+
if (tuple == null) {
63+
return null;
64+
}
65+
JSONObject json = new JSONObject();
66+
json.put("address", tuple.address);
67+
json.put("port", tuple.port);
68+
69+
return json;
70+
}
71+
72+
public static AddressTuple fromJson(JSONObject json) throws JSONException {
73+
if (json == null) {
74+
return null;
75+
}
76+
77+
return new AddressTuple(json.getString("address"), json.getInt("port"));
78+
}
5779
}
5880

5981
// Persistent attributes

0 commit comments

Comments
 (0)