Skip to content

Commit

Permalink
Implement heartbeat on timer to keep visualizer alive (#1463)
Browse files Browse the repository at this point in the history
* implement heartbeat on timer to keep visualizer alive

* heartbeat every second

* Update VisWebSocket.java

Remove debugging line

* Update CHANGELOG.md

Fix issue 523, use version 4.5 in changelog since there's no 4.4.1 public distro
  • Loading branch information
aymanhab authored Nov 14, 2023
1 parent 57b50bd commit f54dbff
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ GitHub issues or pull requests that
are related to the items below. If there is no issue or pull
request related to the change, then we may provide the commit.

v4.4.1
v4.5
======
- Fix issue [#1378](https://github.com/opensim-org/opensim-gui/issues/1378): In Static Optimization Tool, dialog box has wrong title for "Directory" in output section.
- Fix issue [#1395](https://github.com/opensim-org/opensim-gui/issues/1395): Where visual selection and properties windows are not in sync.
- Fix issue [#1357](https://github.com/opensim-org/opensim-gui/issues/1357): Toolbar displaced and non visible after resizing window
- Fix issue [#1416](https://github.com/opensim-org/opensim-gui/issues/1416): Remove visual offset added to experimental data in "Preview Experimnetal Data".
- Fix issue [#1426](https://github.com/opensim-org/opensim-gui/issues/1426): Make graphical selection of pathpoints and markers more robust among overlapping objects.
- Fix issue [#523](https://github.com/opensim-org/opensim-gui/issues/523): Keep visualizer alive through arbitrary long idle time.

v4.4
====
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
package org.eclipse.jetty;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
Expand All @@ -43,6 +47,8 @@
@WebSocket
public class MyWebSocketHandler {

private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

@OnWebSocketClose
public void onClose(int statusCode, String reason) {
System.out.println("Close: statusCode=" + statusCode + ", reason=" + reason);
Expand All @@ -55,6 +61,16 @@ public void onError(Throwable t) {

@OnWebSocketConnect
public void onConnect(Session session) {
executorService.scheduleAtFixedRate(() -> {
try {
String data = "Ping";
ByteBuffer payload = ByteBuffer.wrap(data.getBytes());
session.getRemote().sendPing(payload);
} catch (IOException e) {
e.printStackTrace();
}
},
5, 5, TimeUnit.MINUTES);
System.out.println("Connect: " + session.getRemoteAddress().getAddress());
try {
session.getRemote().sendString("Hello Webbrowser");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@
package org.eclipse.jetty;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.Collections;
import java.util.HashSet;
import java.util.Observable;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jetty.websocket.api.Session;
Expand All @@ -52,6 +56,7 @@
@WebSocket(maxTextMessageSize = 64 * 4096, maxIdleTime=10000000)
public class VisWebSocket extends Observable { // Socket to handle incoming traffic from Browser
private static final Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>());
private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);

public VisWebSocket(){
// Register socket with WebSocketDB so it can be called back
Expand All @@ -66,7 +71,17 @@ public void onOpen (Session peer) {
this.setChanged();
this.notifyObservers();
}

executorService.scheduleAtFixedRate(() -> {
try {
String data = "Ping";
ByteBuffer payload = ByteBuffer.wrap(data.getBytes());
peer.getRemote().sendPing(payload);
//System.out.println("Sending ping to peer.");
} catch (IOException e) {
e.printStackTrace();
}
},
1, 1, TimeUnit.MINUTES);

}

Expand Down

0 comments on commit f54dbff

Please sign in to comment.