Skip to content

Commit

Permalink
Implement RESTART command
Browse files Browse the repository at this point in the history
  • Loading branch information
rlktradewright committed Dec 30, 2022
1 parent 46cb82f commit 90693cf
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 12 deletions.
26 changes: 26 additions & 0 deletions src/ibcalpha/ibc/CommandDispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package ibcalpha.ibc;

import java.awt.event.KeyEvent;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;
import javax.swing.JFrame;

Expand Down Expand Up @@ -47,6 +49,8 @@ class CommandDispatcher
handleReconnectDataCommand();
} else if (cmd.equalsIgnoreCase("RECONNECTACCOUNT")) {
handleReconnectAccountCommand();
} else if (cmd.equalsIgnoreCase("RESTART")) {
handleRestartCommand();
} else {
handleInvalidCommand(cmd);
}
Expand Down Expand Up @@ -102,5 +106,27 @@ private void handleReconnectAccountCommand() {
private void handleStopCommand() {
(new StopTask(mChannel, mIsGateway)).run(); // run on the current thread
}

private void handleRestartCommand() {
if (MainWindowManager.mainWindowManager().isGateway()) {
LocalTime now = LocalTime.now();
LocalTime restartTime;
if (now.getSecond() < 20) {
restartTime = now.withMinute(now.getMinute() + 1);
} else {
restartTime = now.withMinute(now.getMinute() + 2);
}
Utils.logToConsole("Setting auto-restart time to " + restartTime.format(DateTimeFormatter.ofPattern("KK:mm a")));
(new ConfigurationTask(new ConfigureAutoLogoffOrRestartTimeTask(
"Auto restart",
restartTime)
)
).executeAsync();
} else {
Utils.invokeMenuItem(MainWindowManager.mainWindowManager().getMainWindow(), new String[] {"File", "Restart..."});
}
mChannel.writeAck("Restart in progress");
mChannel.close();
}

}
2 changes: 2 additions & 0 deletions src/ibcalpha/ibc/ConfigDialogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,7 @@ public static ConfigDialogManager configDialogManager() {
public abstract void setConfigDialog(JDialog window);

public abstract void setSplashScreenClosed();

public abstract void setNonBrokerageAccountDialogClosed();

}
5 changes: 5 additions & 0 deletions src/ibcalpha/ibc/ConfigureAutoLogoffOrRestartTimeTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class ConfigureAutoLogoffOrRestartTimeTask implements ConfigurationAction {
this.autoActionTime = autoActionTime;
}

ConfigureAutoLogoffOrRestartTimeTask(String autoActionName, LocalTime autoActionTime) {
this.autoActionName=autoActionName;
this.autoActionTime = autoActionTime.format(DateTimeFormatter.ofPattern("KK:mm a"));
}

@Override
public void initialise(JDialog configDialog) {
this.configDialog = configDialog;
Expand Down
7 changes: 5 additions & 2 deletions src/ibcalpha/ibc/DefaultConfigDialogManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,12 @@ public void setSplashScreenClosed() {
if (configDialogTask != null) configDialogTask.setSplashScreenClosed();
}


@Override
public void setNonBrokerageAccountDialogClosed() {
if (configDialogTask != null) configDialogTask.setNonBrokerageAccountDialogClosed();
}

private boolean apiConfigChangeConfirmationExpected;

@Override
public boolean getApiConfigChangeConfirmationExpected() {
return apiConfigChangeConfirmationExpected;
Expand Down
31 changes: 26 additions & 5 deletions src/ibcalpha/ibc/GetConfigDialogTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package ibcalpha.ibc;

import static java.lang.Thread.sleep;
import java.util.concurrent.Callable;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
Expand All @@ -27,7 +28,7 @@

class GetConfigDialogTask implements Callable<JDialog>{
private volatile JDialog mConfigDialog;
private volatile boolean mGatewayInitialised;
private static volatile boolean _GatewayInitialised;
private final Lock lock = new ReentrantLock();
private final Condition gotConfigDialog = lock.newCondition();
private final Condition gatewayInitialised = lock.newCondition();
Expand All @@ -46,7 +47,7 @@ public JDialog call() throws IbcException, InterruptedException {
* For the gateway, the main form is loaded right at the start, and long before
* the menu items become responsive: any attempt to access the Configure > Settings
* menu item (even after it has been enabled) results in an exception being logged
* by TWS.
* by Gateway.
*
* It's not obvious how long we need to wait before the menu becomes responsive. However the splash
* frame that appears in front of the gateway main window during initialisation disappears when everything
Expand All @@ -58,7 +59,8 @@ public JDialog call() throws IbcException, InterruptedException {

lock.lock();
try {
while (!mGatewayInitialised) {
while (!_GatewayInitialised) {
sleep(10);
gatewayInitialised.await();
}
} finally {
Expand Down Expand Up @@ -96,12 +98,31 @@ void setConfigDialog(JDialog configDialog) {
}
}

private volatile boolean splashScreenClosed;
void setSplashScreenClosed() {
if (!isGateway) return;
lock.lock();
try {
mGatewayInitialised = true;
gatewayInitialised.signal();
splashScreenClosed = true;
if (nonBrokerageAccountDialogClosed) {
_GatewayInitialised = true;
gatewayInitialised.signal();
}
} finally {
lock.unlock();
}
}

private volatile boolean nonBrokerageAccountDialogClosed;
void setNonBrokerageAccountDialogClosed() {
if (!isGateway) return;
lock.lock();
try {
nonBrokerageAccountDialogClosed = true;
if (splashScreenClosed) {
_GatewayInitialised = true;
gatewayInitialised.signal();
}
} finally {
lock.unlock();
}
Expand Down
8 changes: 4 additions & 4 deletions src/ibcalpha/ibc/IbcTws.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ private static List<WindowHandler> createWindowHandlers() {
windowHandlers.add(new LoginErrorDialogHandler());
windowHandlers.add(new CryptoOrderConfirmationDialogHandler());
windowHandlers.add(new AutoRestartConfirmationDialog());
windowHandlers.add(new RestartConfirmationDialogHandler());

return windowHandlers;
}
Expand Down Expand Up @@ -501,12 +501,12 @@ private static void startTwsOrGateway(boolean isGateway) {

String autoLogoffTime = Settings.settings().getString("AutoLogoffTime", "");
String autoRestartTime = Settings.settings().getString("AutoRestartTime", "");
if (!autoRestartTime.equals("")) {
if (autoRestartTime.length() != 0) {
(new ConfigurationTask(new ConfigureAutoLogoffOrRestartTimeTask("Auto restart", autoRestartTime))).executeAsync();
if (!autoLogoffTime.equals("")) {
if (autoLogoffTime.length() != 0) {
Utils.logToConsole("AutoLogoffTime is ignored because AutoRestartTime is also set");
}
} else if (!autoLogoffTime.equals("")) {
} else if (autoLogoffTime.length() != 0) {
(new ConfigurationTask(new ConfigureAutoLogoffOrRestartTimeTask("Auto logoff", autoLogoffTime))).executeAsync();
}

Expand Down
7 changes: 7 additions & 0 deletions src/ibcalpha/ibc/NonBrokerageAccountDialogHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ public boolean filterEvent(Window window, int eventId) {
switch (eventId) {
case WindowEvent.WINDOW_OPENED:
return true;
case WindowEvent.WINDOW_CLOSED:
return true;
default:
return false;
}
}

@Override
public void handleWindow(Window window, int eventID) {
if (eventID == WindowEvent.WINDOW_CLOSED) {
ConfigDialogManager.configDialogManager().setNonBrokerageAccountDialogClosed();
return;
}

if (! Settings.settings().getBoolean("AcceptNonBrokerageAccountWarning", true)) return;

GuiDeferredExecutor.instance().execute(() -> MainWindowManager.mainWindowManager().iconizeIfRequired());
Expand Down
46 changes: 46 additions & 0 deletions src/ibcalpha/ibc/RestartConfirmationDialogHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is part of IBC.
// Copyright (C) 2004 Steven M. Kearns ([email protected] )
// Copyright (C) 2004 - 2022 Richard L King ([email protected])
// For conditions of distribution and use, see copyright notice in COPYING.txt

// IBC is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// IBC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with IBC. If not, see <http://www.gnu.org/licenses/>.

package ibcalpha.ibc;

import java.awt.Window;
import java.awt.event.WindowEvent;
import javax.swing.JDialog;

public class RestartConfirmationDialogHandler implements WindowHandler {
public boolean filterEvent(Window window, int eventId) {
switch (eventId) {
case WindowEvent.WINDOW_OPENED:
return true;
default:
return false;
}
}

public void handleWindow(Window window, int eventID) {
if (!SwingUtils.clickButton(window, "Yes")) {
Utils.logError("could not ignore shutdown confirmation dialog because we could not find one of the controls.");
}
}

public boolean recogniseWindow(Window window) {
if (! (window instanceof JDialog)) return false;

return (SwingUtils.findLabel(window, "Are you sure you would like to restart the application") != null);
}
}
2 changes: 1 addition & 1 deletion src/ibcalpha/ibc/StopTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private void stop() {
Utils.invokeMenuItem(MainWindowManager.mainWindowManager().getMainWindow(), closeMenuPath);
}

} catch (Exception e) {
} catch (IllegalStateException e) {
Utils.exitWithException(ErrorCodes.ERROR_CODE_UNHANDLED_EXCEPTION, e);
}
}
Expand Down

0 comments on commit 90693cf

Please sign in to comment.