Convert address fields to strings to better manage DNS names

This commit is contained in:
Cameron Gutman 2017-09-09 13:39:54 -07:00
parent 60cd951774
commit 46a998c113
6 changed files with 38 additions and 54 deletions

View File

@ -62,8 +62,8 @@ public class ComputerDatabaseManager {
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(COMPUTER_NAME_COLUMN_NAME, details.name); values.put(COMPUTER_NAME_COLUMN_NAME, details.name);
values.put(COMPUTER_UUID_COLUMN_NAME, details.uuid.toString()); values.put(COMPUTER_UUID_COLUMN_NAME, details.uuid.toString());
values.put(LOCAL_IP_COLUMN_NAME, ADDRESS_PREFIX+details.localAddress.getHostAddress()); values.put(LOCAL_IP_COLUMN_NAME, ADDRESS_PREFIX+details.localAddress);
values.put(REMOTE_IP_COLUMN_NAME, ADDRESS_PREFIX+details.remoteAddress.getHostAddress()); values.put(REMOTE_IP_COLUMN_NAME, ADDRESS_PREFIX+details.remoteAddress);
values.put(MAC_COLUMN_NAME, details.macAddress); values.put(MAC_COLUMN_NAME, details.macAddress);
return -1 != computerDb.insertWithOnConflict(COMPUTER_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE); return -1 != computerDb.insertWithOnConflict(COMPUTER_TABLE_NAME, null, values, SQLiteDatabase.CONFLICT_REPLACE);
} }
@ -86,17 +86,13 @@ public class ComputerDatabaseManager {
// too. To disambiguate, we'll need to prefix them with a string // too. To disambiguate, we'll need to prefix them with a string
// greater than the allowable IP address length. // greater than the allowable IP address length.
try { try {
details.localAddress = InetAddress.getByAddress(c.getBlob(2)); details.localAddress = InetAddress.getByAddress(c.getBlob(2)).getHostAddress();
LimeLog.warning("DB: Legacy local address for "+details.name); LimeLog.warning("DB: Legacy local address for "+details.name);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
// This is probably a hostname/address with the prefix string // This is probably a hostname/address with the prefix string
String stringData = c.getString(2); String stringData = c.getString(2);
if (stringData.startsWith(ADDRESS_PREFIX)) { if (stringData.startsWith(ADDRESS_PREFIX)) {
try { details.localAddress = c.getString(2).substring(ADDRESS_PREFIX.length());
details.localAddress = InetAddress.getByName(c.getString(2).substring(ADDRESS_PREFIX.length()));
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
} }
else { else {
LimeLog.severe("DB: Corrupted local address for "+details.name); LimeLog.severe("DB: Corrupted local address for "+details.name);
@ -104,17 +100,13 @@ public class ComputerDatabaseManager {
} }
try { try {
details.remoteAddress = InetAddress.getByAddress(c.getBlob(3)); details.remoteAddress = InetAddress.getByAddress(c.getBlob(3)).getHostAddress();
LimeLog.warning("DB: Legacy remote address for "+details.name); LimeLog.warning("DB: Legacy remote address for "+details.name);
} catch (UnknownHostException e) { } catch (UnknownHostException e) {
// This is probably a hostname/address with the prefix string // This is probably a hostname/address with the prefix string
String stringData = c.getString(3); String stringData = c.getString(3);
if (stringData.startsWith(ADDRESS_PREFIX)) { if (stringData.startsWith(ADDRESS_PREFIX)) {
try { details.remoteAddress = c.getString(3).substring(ADDRESS_PREFIX.length());
details.remoteAddress = InetAddress.getByName(c.getString(3).substring(ADDRESS_PREFIX.length()));
} catch (UnknownHostException e1) {
e1.printStackTrace();
}
} }
else { else {
LimeLog.severe("DB: Corrupted local address for "+details.name); LimeLog.severe("DB: Corrupted local address for "+details.name);

View File

@ -155,7 +155,7 @@ public class ComputerManagerService extends Service {
} }
} }
}; };
t.setName("Polling thread for " + tuple.computer.localAddress.getHostAddress()); t.setName("Polling thread for " + tuple.computer.localAddress);
return t; return t;
} }
@ -211,7 +211,7 @@ public class ComputerManagerService extends Service {
} }
} }
public boolean addComputerBlocking(InetAddress addr) { public boolean addComputerBlocking(String addr) {
return ComputerManagerService.this.addComputerBlocking(addr); return ComputerManagerService.this.addComputerBlocking(addr);
} }
@ -290,7 +290,7 @@ public class ComputerManagerService extends Service {
@Override @Override
public void notifyComputerAdded(MdnsComputer computer) { public void notifyComputerAdded(MdnsComputer computer) {
// Kick off a serverinfo poll on this machine // Kick off a serverinfo poll on this machine
addComputerBlocking(computer.getAddress()); addComputerBlocking(computer.getAddress().getHostAddress());
} }
@Override @Override
@ -339,7 +339,7 @@ public class ComputerManagerService extends Service {
} }
} }
public boolean addComputerBlocking(InetAddress addr) { public boolean addComputerBlocking(String addr) {
// Setup a placeholder // Setup a placeholder
ComputerDetails fakeDetails = new ComputerDetails(); ComputerDetails fakeDetails = new ComputerDetails();
fakeDetails.localAddress = addr; fakeDetails.localAddress = addr;
@ -405,14 +405,14 @@ public class ComputerManagerService extends Service {
} }
} }
private ComputerDetails tryPollIp(ComputerDetails details, InetAddress ipAddr) { private ComputerDetails tryPollIp(ComputerDetails details, String address) {
// Fast poll this address first to determine if we can connect at the TCP layer // Fast poll this address first to determine if we can connect at the TCP layer
if (!fastPollIp(ipAddr)) { if (!fastPollIp(address)) {
return null; return null;
} }
try { try {
NvHTTP http = new NvHTTP(ipAddr, idManager.getUniqueId(), NvHTTP http = new NvHTTP(address, idManager.getUniqueId(),
null, PlatformBinding.getCryptoProvider(ComputerManagerService.this)); null, PlatformBinding.getCryptoProvider(ComputerManagerService.this));
ComputerDetails newDetails = http.getComputerDetails(); ComputerDetails newDetails = http.getComputerDetails();
@ -433,10 +433,10 @@ public class ComputerManagerService extends Service {
// Just try to establish a TCP connection to speculatively detect a running // Just try to establish a TCP connection to speculatively detect a running
// GFE server // GFE server
private boolean fastPollIp(InetAddress addr) { private boolean fastPollIp(String address) {
Socket s = new Socket(); Socket s = new Socket();
try { try {
s.connect(new InetSocketAddress(addr, NvHTTP.HTTPS_PORT), FAST_POLL_TIMEOUT); s.connect(new InetSocketAddress(address, NvHTTP.HTTPS_PORT), FAST_POLL_TIMEOUT);
s.close(); s.close();
return true; return true;
} catch (IOException e) { } catch (IOException e) {
@ -444,11 +444,11 @@ public class ComputerManagerService extends Service {
} }
} }
private void startFastPollThread(final InetAddress addr, final boolean[] info) { private void startFastPollThread(final String address, final boolean[] info) {
Thread t = new Thread() { Thread t = new Thread() {
@Override @Override
public void run() { public void run() {
boolean pollRes = fastPollIp(addr); boolean pollRes = fastPollIp(address);
synchronized (info) { synchronized (info) {
info[0] = true; // Done info[0] = true; // Done
@ -458,16 +458,16 @@ public class ComputerManagerService extends Service {
} }
} }
}; };
t.setName("Fast Poll - "+addr.getHostAddress()); t.setName("Fast Poll - "+address);
t.start(); t.start();
} }
private ComputerDetails.Reachability fastPollPc(final InetAddress local, final InetAddress remote) throws InterruptedException { private ComputerDetails.Reachability fastPollPc(final String localAddress, final String remoteAddress) throws InterruptedException {
final boolean[] remoteInfo = new boolean[2]; final boolean[] remoteInfo = new boolean[2];
final boolean[] localInfo = new boolean[2]; final boolean[] localInfo = new boolean[2];
startFastPollThread(local, localInfo); startFastPollThread(localAddress, localInfo);
startFastPollThread(remote, remoteInfo); startFastPollThread(remoteAddress, remoteInfo);
// Check local first // Check local first
synchronized (localInfo) { synchronized (localInfo) {
@ -524,7 +524,7 @@ public class ComputerManagerService extends Service {
polledDetails = tryPollIp(details, details.remoteAddress); polledDetails = tryPollIp(details, details.remoteAddress);
} }
InetAddress reachableAddr = null; String reachableAddr = null;
if (polledDetails == null && !details.localAddress.equals(details.remoteAddress)) { if (polledDetails == null && !details.localAddress.equals(details.remoteAddress)) {
// Failed, so let's try the fallback // Failed, so let's try the fallback
if (!localFirst) { if (!localFirst) {
@ -708,12 +708,12 @@ public class ComputerManagerService extends Service {
continue; continue;
} }
NvHTTP http = new NvHTTP(ServerHelper.getCurrentAddressFromComputer(computer), idManager.getUniqueId(),
null, PlatformBinding.getCryptoProvider(ComputerManagerService.this));
PollingTuple tuple = getPollingTuple(computer); PollingTuple tuple = getPollingTuple(computer);
try { try {
NvHTTP http = new NvHTTP(ServerHelper.getCurrentAddressFromComputer(computer), idManager.getUniqueId(),
null, PlatformBinding.getCryptoProvider(ComputerManagerService.this));
String appList; String appList;
if (tuple != null) { if (tuple != null) {
// If we're polling this machine too, grab the network lock // If we're polling this machine too, grab the network lock
@ -808,10 +808,10 @@ class PollingTuple {
} }
class ReachabilityTuple { class ReachabilityTuple {
public final InetAddress reachableAddress; public final String reachableAddress;
public final ComputerDetails computer; public final ComputerDetails computer;
public ReachabilityTuple(ComputerDetails computer, InetAddress reachableAddress) { public ReachabilityTuple(ComputerDetails computer, String reachableAddress) {
this.computer = computer; this.computer = computer;
this.reachableAddress = reachableAddress; this.reachableAddress = reachableAddress;
} }

View File

@ -22,10 +22,9 @@ public class NetworkAssetLoader {
} }
public InputStream getBitmapStream(CachedAppAssetLoader.LoaderTuple tuple) { public InputStream getBitmapStream(CachedAppAssetLoader.LoaderTuple tuple) {
NvHTTP http = new NvHTTP(ServerHelper.getCurrentAddressFromComputer(tuple.computer), uniqueId, null, PlatformBinding.getCryptoProvider(context));
InputStream in = null; InputStream in = null;
try { try {
NvHTTP http = new NvHTTP(ServerHelper.getCurrentAddressFromComputer(tuple.computer), uniqueId, null, PlatformBinding.getCryptoProvider(context));
in = http.getBoxArt(tuple.app); in = http.getBoxArt(tuple.app);
} catch (IOException ignored) {} } catch (IOException ignored) {}

View File

@ -50,19 +50,13 @@ public class AddComputerManually extends Activity {
SpinnerDialog dialog = SpinnerDialog.displayDialog(this, getResources().getString(R.string.title_add_pc), SpinnerDialog dialog = SpinnerDialog.displayDialog(this, getResources().getString(R.string.title_add_pc),
getResources().getString(R.string.msg_add_pc), false); getResources().getString(R.string.msg_add_pc), false);
try { if (!managerBinder.addComputerBlocking(host)){
InetAddress addr = InetAddress.getByName(host);
if (!managerBinder.addComputerBlocking(addr)){
msg = getResources().getString(R.string.addpc_fail); msg = getResources().getString(R.string.addpc_fail);
} }
else { else {
msg = getResources().getString(R.string.addpc_success); msg = getResources().getString(R.string.addpc_success);
finish = true; finish = true;
} }
} catch (UnknownHostException e) {
msg = getResources().getString(R.string.addpc_unknown_host);
}
dialog.dismiss(); dialog.dismiss();

View File

@ -18,7 +18,7 @@ import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
public class ServerHelper { public class ServerHelper {
public static InetAddress getCurrentAddressFromComputer(ComputerDetails computer) { public static String getCurrentAddressFromComputer(ComputerDetails computer) {
return computer.reachability == ComputerDetails.Reachability.LOCAL ? return computer.reachability == ComputerDetails.Reachability.LOCAL ?
computer.localAddress : computer.remoteAddress; computer.localAddress : computer.remoteAddress;
} }
@ -26,8 +26,7 @@ public class ServerHelper {
public static Intent createStartIntent(Activity parent, NvApp app, ComputerDetails computer, public static Intent createStartIntent(Activity parent, NvApp app, ComputerDetails computer,
ComputerManagerService.ComputerManagerBinder managerBinder) { ComputerManagerService.ComputerManagerBinder managerBinder) {
Intent intent = new Intent(parent, Game.class); Intent intent = new Intent(parent, Game.class);
intent.putExtra(Game.EXTRA_HOST, intent.putExtra(Game.EXTRA_HOST, getCurrentAddressFromComputer(computer));
getCurrentAddressFromComputer(computer).getHostAddress());
intent.putExtra(Game.EXTRA_APP_NAME, app.getAppName()); intent.putExtra(Game.EXTRA_APP_NAME, app.getAppName());
intent.putExtra(Game.EXTRA_APP_ID, app.getAppId()); intent.putExtra(Game.EXTRA_APP_ID, app.getAppId());
intent.putExtra(Game.EXTRA_UNIQUEID, managerBinder.getUniqueId()); intent.putExtra(Game.EXTRA_UNIQUEID, managerBinder.getUniqueId());
@ -44,7 +43,7 @@ public class ServerHelper {
} }
public static void doQuit(final Activity parent, public static void doQuit(final Activity parent,
final InetAddress address, final String address,
final NvApp app, final NvApp app,
final ComputerManagerService.ComputerManagerBinder managerBinder, final ComputerManagerService.ComputerManagerBinder managerBinder,
final Runnable onComplete) { final Runnable onComplete) {

@ -1 +1 @@
Subproject commit bea3830a63b4a8c537dc2f22c8b91e24c0545155 Subproject commit 2c4319196f62bac1d12d498914b0f4b6f1086e99