Skip to content

Commit

Permalink
Deal with case where autorestart file contains invalid credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
rlktradewright committed Aug 12, 2024
1 parent f9e177d commit b015a83
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 19 deletions.
59 changes: 40 additions & 19 deletions src/ibcalpha/ibc/AbstractLoginHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.awt.Window;
import java.awt.event.WindowEvent;
import java.io.File;
import java.util.concurrent.TimeUnit;
import javax.swing.JButton;
import javax.swing.JFrame;
Expand Down Expand Up @@ -55,8 +56,40 @@ public final void handleWindow(Window window, int eventID) {
LoginManager.loginManager().setLoginFrame((JFrame) window);
switch (LoginManager.loginManager().getLoginState()){
case LOGGED_OUT:
// don't initiate login if we're auto-restarting
if (! SessionManager.isRestart()) initiateLogin(window);
if (SessionManager.isRestart()) {
// IBC thinks we're auto-restarting because of the existence
// of the autorestart file. If the autorestart file contains
// valid credentials, TWS does everything automatically and
// there is nothing for IBC to do: in this case, the userid,
// and password fields are disabled (and sometimes aren't
// present at all).
//
// However, if the autorestart file contains invalid or
// expired credentials, TWS just displays the normal login
// dialog and waits for userid and password to be supplied
// (ie a normal full login). In this case, the user id and
// password fields are enabled and empty.

if (isUserIdDisabledOrAbsent(window) && isPasswordDisabledOrAbsent(window)) {
// nothing to do so get out
break;
}

// the autorestart file is invalid. TWS doesn't remove
// it, so we need to delete it before proceeding with a
// normal login

Utils.logToConsole("Autorestart file contains invalid credentials: performing full login");
Utils.logToConsole("Deleting Autorestart file");
File autorestartFile = new File(System.getProperty("jtsConfigDir") +
File.separator +
System.getProperty("restart") +
File.separator +
"autorestart");
autorestartFile.delete();
};

initiateLogin(window);
}
}

Expand Down Expand Up @@ -142,6 +175,10 @@ private void checkChangeToSecondFactorAuthenticationDialog(Window window) {
protected abstract boolean preLogin(final Window window, int eventID) throws IbcException;

protected abstract boolean setFields(Window window, int eventID) throws IbcException;

protected abstract boolean isUserIdDisabledOrAbsent(Window window);

protected abstract boolean isPasswordDisabledOrAbsent(Window window);

private JButton findLoginButton(final Window window) {
JButton b = SwingUtils.findButton(window, "Login");
Expand Down Expand Up @@ -179,24 +216,8 @@ protected final boolean setTradingMode(final Window window) {
// and do a cold restart

Utils.logToConsole("Login dialog has been invslidated - initiate cold restart");
GuiDeferredExecutor.instance().execute(new StopTask(null, true, "Login Error dialog encountered"));
MyCachedThreadPool.getInstance().execute(new StopTask(null, true, "Login Error dialog encountered"));
return false;

// JComboBox<?> tradingModeCombo;
// if (Settings.settings().getBoolean("FIX", false)) {
// tradingModeCombo = SwingUtils.findComboBox(window, 1);
// } else {
// tradingModeCombo = SwingUtils.findComboBox(window, 0);
// }
//
// if (tradingModeCombo != null ) {
// Utils.logToConsole("Setting Trading mode = " + tradingMode);
// if (tradingMode.equalsIgnoreCase(TradingModeManager.TRADING_MODE_LIVE)) {
// tradingModeCombo.setSelectedItem("Live Trading");
// } else {
// tradingModeCombo.setSelectedItem("Paper Trading");
// }
// }
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/ibcalpha/ibc/GatewayLoginFrameHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.awt.Window;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JToggleButton;

final class GatewayLoginFrameHandler extends AbstractLoginHandler {
Expand Down Expand Up @@ -137,4 +138,20 @@ private void switchToIBAPI(Window window) throws IbcException {
}
}

@Override
protected boolean isUserIdDisabledOrAbsent(Window window) {
int index = SessionManager.isFIX() ? 2 : 0;
JTextField userID = SwingUtils.findTextField(window, index);
if (userID == null) return true;
return ! userID.isEnabled();
}

@Override
protected boolean isPasswordDisabledOrAbsent(Window window) {
int index = SessionManager.isFIX() ? 3 : 1;
JTextField password = SwingUtils.findTextField(window, index);
if (password == null) return true;
return ! password.isEnabled();
}

}
14 changes: 14 additions & 0 deletions src/ibcalpha/ibc/LoginFrameHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,19 @@ protected final boolean setFields(Window window, int eventID) throws IbcExceptio
return true;
}

@Override
protected boolean isUserIdDisabledOrAbsent(Window window) {
JTextField userID = SwingUtils.findTextField(window, 0);
if (userID == null) return true;
return ! userID.isEnabled();
}

@Override
protected boolean isPasswordDisabledOrAbsent(Window window) {
JTextField password = SwingUtils.findTextField(window, 1);
if (password == null) return true;
return ! password.isEnabled();
}

}

0 comments on commit b015a83

Please sign in to comment.