Skip to content

Commit

Permalink
Connection status added to status bar
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmipt committed Nov 24, 2023
1 parent 9931e18 commit 1e19071
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 10 deletions.
1 change: 1 addition & 0 deletions notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
* Rework kdb connection logic
* No IPC protocol downgrade in case of first attempt failure
* Fix logic to detect server disconnect
* Connection status added to the status bar
* File drag and drop (no support for a folder drag and drop)
* Persist location of editor windows
* Keep config properties sorted on disk
Expand Down
6 changes: 6 additions & 0 deletions src/kx/ConnectionStateListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kx;

public interface ConnectionStateListener {

void connectionStateChange(boolean connected);
}
12 changes: 12 additions & 0 deletions src/kx/KConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class KConnection {
private Socket s;

private SocketReader socketReader;
private ConnectionStateListener connectionStateListener = null;

void io(Socket s) throws IOException {
s.setTcpNoDelay(true);
Expand Down Expand Up @@ -50,6 +51,9 @@ public void close() {
s.close();
} catch (IOException e) {}
}
if (connectionStateListener != null) {
connectionStateListener.connectionStateChange(false);
}
}


Expand Down Expand Up @@ -86,6 +90,10 @@ private void connect() throws IOException, K4AccessException {
socketReader.setName("Reader " + host + ":" + port);
socketReader.setDaemon(true);
socketReader.start();

if (connectionStateListener != null) {
connectionStateListener.connectionStateChange(true);
}
}

public KConnection(String h, int p, String userPassword, boolean useTLS) {
Expand All @@ -95,6 +103,10 @@ public KConnection(String h, int p, String userPassword, boolean useTLS) {
this.useTLS = useTLS;
}

public void setConnectionStateListener(ConnectionStateListener connectionStateListener) {
this.connectionStateListener = connectionStateListener;
}

private final static byte[] HEADER = new byte[] {0,1,0,0};

private void send(K.KBase query) throws IOException {
Expand Down
11 changes: 10 additions & 1 deletion src/studio/kdb/Session.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package studio.kdb;

import kx.ConnectionStateListener;
import kx.K4Exception;
import kx.KConnection;
import kx.ProgressCallback;
Expand All @@ -16,7 +17,7 @@
import java.util.List;
import java.util.Map;

public class Session {
public class Session implements ConnectionStateListener {
private KConnection kConn;
private long created;
private final Server server;
Expand Down Expand Up @@ -55,6 +56,13 @@ public void removeTab(EditorTab editor) {
}
}

@Override
public void connectionStateChange(boolean connected) {
for(EditorTab editor: editors) {
editor.setSessonConnection(connected);
}
}

private Session(Server server) {
this.server = server;
init();
Expand All @@ -65,6 +73,7 @@ private void init() {
kConn = createConnection(server);
if (kConn == null) throw new RuntimeException("Failure in the authentication plugin");
created = System.currentTimeMillis();
kConn.setConnectionStateListener(this);
}

static void mock(SessionCreator sessionCreator) {
Expand Down
4 changes: 4 additions & 0 deletions src/studio/ui/EditorPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ public void stopClock() {
editorStatusBar.stopClock();
}

public void setSessionConnected(boolean connected) {
editorStatusBar.setSessionConnected(connected);
}

@Override
public void mouseWheelMoved(MouseWheelEvent e) {
if ((e.getModifiers() & StudioWindow.menuShortcutKeyMask) == 0) return;
Expand Down
5 changes: 5 additions & 0 deletions src/studio/ui/EditorTab.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ public StudioWindow getStudioWindow() {
return studioWindow;
}

public void setSessonConnection(boolean connected) {
editorPane.setSessionConnected(connected);
}

public void setStudioWindow(StudioWindow studioWindow) {
this.studioWindow = studioWindow;
}
Expand Down Expand Up @@ -251,6 +255,7 @@ public void setServer(Server server) {
}
this.server = server;
session = Session.newSession(this);
setSessonConnection(!session.isClosed());

getTextArea().setBackground(server.getBackgroundColor());

Expand Down
30 changes: 21 additions & 9 deletions src/studio/ui/statusbar/EditorStatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@

public class EditorStatusBar extends StatusBar {

private final MinSizeLabel lblClock;
private final MinSizeLabel lblConnection;

private final Timer timer;
private long clock;
private boolean sessionConnected = false;

private final static String CONNECTED = "Connected";
private final static String DISCONNECTED = "Disconnected";

public EditorStatusBar() {
lblClock = new MinSizeLabel("");
lblClock.setHorizontalAlignment(JLabel.CENTER);
lblClock.setMinimumWidth("1:00:00");
addComponent(lblClock);
lblClock.setVisible(false);
lblConnection = new MinSizeLabel("");
lblConnection.setHorizontalAlignment(JLabel.CENTER);
lblConnection.setMinimumWidth("1:00:00", CONNECTED, DISCONNECTED);
addComponent(lblConnection);

timer = new Timer(500, this::timerClockAction);
refreshConnectedLabel();
}

public void startClock() {
Expand All @@ -29,7 +33,16 @@ public void startClock() {

public void stopClock() {
timer.stop();
lblClock.setVisible(false);
refreshConnectedLabel();
}

public void setSessionConnected(boolean connected) {
sessionConnected = connected;
refreshConnectedLabel();
}

private void refreshConnectedLabel() {
lblConnection.setText(sessionConnected ? CONNECTED : DISCONNECTED);
}

private void timerClockAction(ActionEvent event) {
Expand All @@ -42,8 +55,7 @@ private void timerClockAction(ActionEvent event) {
long min = time % 60;
long hour = time / 60;

lblClock.setText(String.format("%d:%02d:%02d",hour, min, sec));
lblClock.setVisible(true);
lblConnection.setText(String.format("%d:%02d:%02d",hour, min, sec));
}

}

0 comments on commit 1e19071

Please sign in to comment.