Skip to content

Commit

Permalink
fix: auto refreshing of domain tokens and clean up ws locks
Browse files Browse the repository at this point in the history
  • Loading branch information
lart2150 committed Nov 30, 2024
1 parent b1181dc commit f48bd7c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
13 changes: 1 addition & 12 deletions src/com/tivo/kmttg/gui/dialog/configMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 20 additions & 5 deletions src/com/tivo/kmttg/main/config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
33 changes: 22 additions & 11 deletions src/com/tivo/kmttg/rpc/TiVoRPCWS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Integer, String> responseMap = new HashMap<Integer, String>();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -134,6 +136,7 @@ public void run(){
}
wc.tsn = tsn;
wc.ready = true;
log.print("WS connection established");
break;
}
}
Expand All @@ -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");
}
}
};
Expand Down

0 comments on commit f48bd7c

Please sign in to comment.