Skip to content

Commit

Permalink
Add new message types, request and handle server health check
Browse files Browse the repository at this point in the history
  • Loading branch information
ascibisz committed Nov 28, 2023
1 parent 075236e commit 07c2b8f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/simularium/RemoteSimulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class RemoteSimulator implements ISimulator {
protected lastRequestedFile: string;
public handleError: (error: FrontEndError) => void | (() => void);
protected useOctopus: boolean;
protected serverHealthy: boolean;

public constructor(
webSocketClient: WebsocketClient,
Expand All @@ -43,6 +44,7 @@ export class RemoteSimulator implements ISimulator {
(() => {
/* do nothing */
});
this.serverHealthy = false;

this.logger = jsLogger.get("netconnection");
this.logger.setLevel(jsLogger.DEBUG);
Expand All @@ -55,6 +57,7 @@ export class RemoteSimulator implements ISimulator {
this.onTrajectoryDataArrive = () => {
/* do nothing */
};
this.requestServerHealthCheck();
}

public setTrajectoryFileInfoHandler(
Expand Down Expand Up @@ -134,6 +137,10 @@ export class RemoteSimulator implements ISimulator {
// TODO: implement callback
}

public onHealthCheckResponse(): void {
this.serverHealthy = true;
}

private registerBinaryMessageHandlers(): void {
this.webSocketClient.addBinaryMessageHandler(
NetMessageEnum.ID_VIS_DATA_ARRIVE,
Expand Down Expand Up @@ -171,6 +178,11 @@ export class RemoteSimulator implements ISimulator {
NetMessageEnum.ID_MODEL_DEFINITION,
(_msg) => this.onModelDefinitionArrive()
);

this.webSocketClient.addJsonMessageHandler(
NetMessageEnum.ID_SERVER_HEALTHY_RESPONSE,
(_msg) => this.onHealthCheckResponse()
);
}

/**
Expand All @@ -190,6 +202,10 @@ export class RemoteSimulator implements ISimulator {
return this.webSocketClient.connectToRemoteServer();
}

public isServerHealthy(): boolean {
return this.serverHealthy;
}

/**
* Websocket Update Parameters
*/
Expand Down Expand Up @@ -412,4 +428,19 @@ export class RemoteSimulator implements ISimulator {
"Convert trajectory output to simularium file format"
);
}

public requestServerHealthCheck(): Promise<void> {
return this.connectToRemoteServer()
.then(() => {
this.webSocketClient.sendWebSocketRequest(
{
msgType: NetMessageEnum.ID_CHECK_HEALTH_REQUEST,
},
"Request server health check"
);
})
.catch((e) => {
throw new FrontEndError(e.message, ErrorLevel.ERROR);
});
}
}
3 changes: 3 additions & 0 deletions src/simularium/WebsocketClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export const enum NetMessageEnum {
ID_AVAILABLE_METRICS_RESPONSE = 18,
ID_PLOT_DATA_REQUEST = 19,
ID_PLOT_DATA_RESPONSE = 20,
ID_ERROR_MSG = 21,
ID_CHECK_HEALTH_REQUEST = 22,
ID_SERVER_HEALTHY_RESPONSE = 23,
// insert new values here before LENGTH
LENGTH,
}
Expand Down

0 comments on commit 07c2b8f

Please sign in to comment.