Skip to content

Commit

Permalink
fixed web server freeze issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Rockwell committed May 29, 2024
1 parent d72312c commit 7883453
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions trick_source/web/CivetServer/include/VariableServerSession.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ PURPOSE: (Represent the state of a variable server websocket connection.)
#endif

#include "trick/WebSocketSession.hh"
#include "trick/Executive.hh"
#include "VariableServerVariable.hh"

class VariableServerSession : public WebSocketSession {
Expand All @@ -38,13 +39,15 @@ class VariableServerSession : public WebSocketSession {
int sendSieMessage(void);
int sendUnitsMessage(const char* vname);
REF2* make_error_ref(const char* in_name);
void updateNextTime(long long simTimeTics);
double stageTime;
bool dataStaged;

std::vector<VariableServerVariable*> sessionVariables;
bool cyclicSendEnabled;
long long nextTime;
long long intervalTimeTics;
SIM_MODE mode;
};

WebSocketSession* makeVariableServerSession( struct mg_connection *nc );
Expand Down
24 changes: 22 additions & 2 deletions trick_source/web/CivetServer/src/VariableServerSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,26 @@ LIBRARY DEPENDENCIES:
#include "trick/exec_proto.h"
#include "VariableServerSession.hh"
#include "simpleJSON.hh"
#include "trick/message_proto.h"
#include "trick/message_type.h"

// CONSTRUCTOR
VariableServerSession::VariableServerSession( struct mg_connection *nc ) : WebSocketSession(nc) {
intervalTimeTics = exec_get_time_tic_value(); // Default time interval is one second.
nextTime = 0;
cyclicSendEnabled = false;
mode = Initialization;
}

// DESTRUCTOR
VariableServerSession::~VariableServerSession() {
clear();
}

void VariableServerSession::updateNextTime(long long simTimeTics) {
nextTime = (simTimeTics - (simTimeTics % intervalTimeTics) + intervalTimeTics);
}

/* Base class virtual function: marshallData
When HTTP_Server::time_homogeneous is set, WebSocketSession::marshallData() is
called from the main sim thread in a "top_of_frame" job, to ensure that all of
Expand All @@ -41,11 +48,23 @@ VariableServerSession::~VariableServerSession() {
(The specified period between messages).
*/
void VariableServerSession::marshallData() {
long long simulation_time_tics = exec_get_time_tics() + exec_get_freeze_time_tics();
long long simulation_time_tics = exec_get_time_tics();
SIM_MODE new_mode = the_exec->get_mode();

if(new_mode == Freeze) {
simulation_time_tics += exec_get_freeze_time_tics();
}

if(new_mode != mode) {
mode = new_mode;
updateNextTime(simulation_time_tics);
}

if ( cyclicSendEnabled && ( simulation_time_tics >= nextTime )) {
stageValues();
nextTime = (simulation_time_tics - (simulation_time_tics % intervalTimeTics) + intervalTimeTics);
updateNextTime(simulation_time_tics);
}

}

/* Base class virtual function: sendMessage
Expand Down Expand Up @@ -108,6 +127,7 @@ int VariableServerSession::handleMessage(const std::string& client_msg) {
unpause();
} else if (cmd == "var_send") {
// var_send responses are not guarenteed to be time-consistent.
message_publish(MSG_INFO, "Data is Staged via Messaging\n");
stageValues();
sendMessage();
} else if (cmd == "var_clear") {
Expand Down

0 comments on commit 7883453

Please sign in to comment.