From f48bd7c5dd78eee7d973863c904e71a47afa388e Mon Sep 17 00:00:00 2001 From: Brian Engert Date: Sat, 30 Nov 2024 17:33:41 -0600 Subject: [PATCH] fix: auto refreshing of domain tokens and clean up ws locks --- src/com/tivo/kmttg/gui/dialog/configMain.java | 13 +------- src/com/tivo/kmttg/main/config.java | 25 +++++++++++--- src/com/tivo/kmttg/rpc/TiVoRPCWS.java | 33 ++++++++++++------- 3 files changed, 43 insertions(+), 28 deletions(-) diff --git a/src/com/tivo/kmttg/gui/dialog/configMain.java b/src/com/tivo/kmttg/gui/dialog/configMain.java index af65b699..40915c37 100755 --- a/src/com/tivo/kmttg/gui/dialog/configMain.java +++ b/src/com/tivo/kmttg/gui/dialog/configMain.java @@ -442,18 +442,7 @@ private static void domainTokenCB() { Thread t = new Thread() { public void run() { - GetDomainToken getDT = new GetDomainToken(); - try { - getDT.getToken(); - } catch (Exception e) { - log.error("Failed to get domain token. Check your tivo.com username or password."); - return; - } - if (config.isDomainTokenExpired()) { - log.error("Failed to get domain token. Check your tivo.com username or password."); - return; - } - log.warn("Successfully got token"); + config.refreshDomainToken(); } }; t.start(); diff --git a/src/com/tivo/kmttg/main/config.java b/src/com/tivo/kmttg/main/config.java index 65a28246..62117b4f 100644 --- a/src/com/tivo/kmttg/main/config.java +++ b/src/com/tivo/kmttg/main/config.java @@ -35,8 +35,8 @@ import java.util.Set; import java.util.Stack; +import com.tivo.kmttg.rpc.GetDomainToken; import com.tivo.kmttg.rpc.Remote; -import com.tivo.kmttg.rpc.TiVoRPCWS; import com.tivo.kmttg.util.*; import com.tivo.kmttg.gui.gui; import com.tivo.kmttg.httpserver.kmttgServer; @@ -659,14 +659,29 @@ public static void setTivoPassword(String password) { } public static String getDomainToken() { - if (tivo_domain_token.length() == 0 && !isDomainTokenExpired()) - return null; + if (tivo_domain_token.length() == 0 || isDomainTokenExpired()) + return refreshDomainToken(); return tivo_domain_token; } + public static String refreshDomainToken() { + GetDomainToken getDT = new GetDomainToken(); + String token = ""; + try { + token = getDT.getToken().getValue(); + } catch (Exception e) { + log.error("Failed to get domain token. Check your tivo.com username or password."); + return null; + } + if (config.isDomainTokenExpired()) { + log.error("Failed to get domain token. Check your tivo.com username or password."); + return null; + } + log.warn("Successfully got token"); + return token; + } + public static boolean isDomainTokenExpired() { - System.out.println("DomainToken: " + tivo_domain_token_expires); - System.out.println("now: " + (new Date()).getTime()); return (new Date()).getTime() > tivo_domain_token_expires; } diff --git a/src/com/tivo/kmttg/rpc/TiVoRPCWS.java b/src/com/tivo/kmttg/rpc/TiVoRPCWS.java index 2fabb0ab..dc88d383 100644 --- a/src/com/tivo/kmttg/rpc/TiVoRPCWS.java +++ b/src/com/tivo/kmttg/rpc/TiVoRPCWS.java @@ -5,6 +5,7 @@ import java.nio.ByteBuffer; import java.util.Collections; import java.util.HashMap; +import java.util.concurrent.locks.ReentrantLock; import org.java_websocket.client.WebSocketClient; import org.java_websocket.drafts.Draft; @@ -29,6 +30,7 @@ public class TiVoRPCWS extends WebSocketClient { protected final String IP; protected final int port; protected Boolean ready = false; + private final ReentrantLock lock = new ReentrantLock(); private String tsn; private HashMap responseMap = new HashMap(); @@ -75,33 +77,33 @@ public boolean waitForReady() throws InterruptedException { if (this.ready) { return true; } - synchronized(this.ready){ - while (!this.ready) { - if (!this.isOpen()) { - return false; - } - this.ready.wait(); - } - return this.isOpen(); + this.lock.lock(); + synchronized (this.lock ) { + if (!this.isOpen()) { + return false; + } + this.lock.wait(); + this.lock.unlock(); } + return this.isOpen(); } @Override public void onOpen(ServerHandshake handshakedata) { - //send("Hello, it is me. Mario :)"); log.print("WS connection started"); TiVoRPCWS wc = this; Thread thread = new Thread(){ public void run(){ - Boolean readyLock = wc.ready; + String token = config.getDomainToken(); + ReentrantLock readyLock = wc.lock; try { JSONObject credential = new JSONObject(); JSONObject h = new JSONObject(); JSONObject domainToken = new JSONObject(); domainToken.put("domain", "tivo"); domainToken.put("type", "domainToken"); - domainToken.put("token", config.getDomainToken()); + domainToken.put("token", token); credential.put("type", "domainTokenCredential"); credential.put("domainToken", domainToken); @@ -134,6 +136,7 @@ public void run(){ } wc.tsn = tsn; wc.ready = true; + log.print("WS connection established"); break; } } @@ -157,13 +160,21 @@ public void run(){ } } } + } else if (result.has("type") && result.getString("type").equals("error")) { + String err = ""; + if (result.has("text")) { + err = ": " + result.getString("text"); + } + log.error("Error establishing WS connection" + err); } } } catch (Exception e) { + e.printStackTrace(); error("rpc Auth error - " + e.getMessage()); } synchronized(readyLock){ readyLock.notifyAll(); + log.print("notifyAll"); } } };