diff --git a/trick_sims/Cannon/models/mongoose_httpd/include/WSSession.hh b/trick_sims/Cannon/models/mongoose_httpd/include/WSSession.hh index 34e8cf6e..47b49604 100644 --- a/trick_sims/Cannon/models/mongoose_httpd/include/WSSession.hh +++ b/trick_sims/Cannon/models/mongoose_httpd/include/WSSession.hh @@ -13,30 +13,34 @@ LIBRARY DEPENDENCIES: class WSsession { public: - WSsession( struct mg_connection *nc); - void setTimeInterval(unsigned int milliseconds); - void addVariable(char* vname); - void stageValuesSynchronously(); - void stageValues(); - void sendValues(); - void pause(); - void unpause(); - void clear(); - void exit(); - int handle_msg (std::string); + WSsession(struct mg_connection *nc); + + void stageData(); /* -- virtual Base */ + void sendMessage(); /* -- virtual Base */ + int handleMessage (std::string); /* -- virtual Base */ + + void setTimeInterval(unsigned int milliseconds); /* -- VariableServerSession specific */ + void addVariable(char* vname); /* -- VariableServerSession specific */ + void stageVariableValues(); /* -- VariableServerSession specific */ + void pause(); /* -- VariableServerSession specific */ + void unpause(); /* -- VariableServerSession specific */ + void clear(); /* -- VariableServerSession specific */ + void exit(); /* -- VariableServerSession specific */ + int emitError(const char* fmt, ... ); static int bad_ref_int ; private: WSsession() {} - REF2* make_error_ref(const char* in_name); - struct mg_connection* connection; - std::vector sessionVariables; - bool cyclicSendEnabled; - double stageTime; - bool valuesStaged; - long long nextTime; - long long intervalTimeTics; + + struct mg_connection* connection; /* -- Base */ + + REF2* make_error_ref(const char* in_name); /* -- VariableServerSession specific */ + double stageTime; /* -- VariableServerSession specific */ + std::vector sessionVariables; /* -- VariableServerSession specific */ + bool cyclicSendEnabled; /* -- VariableServerSession specific */ + long long nextTime; /* -- VariableServerSession specific */ + long long intervalTimeTics; /* -- VariableServerSession specific */ }; #endif diff --git a/trick_sims/Cannon/models/mongoose_httpd/include/http_server.h b/trick_sims/Cannon/models/mongoose_httpd/include/http_server.h index 68e5f5af..290d23f6 100644 --- a/trick_sims/Cannon/models/mongoose_httpd/include/http_server.h +++ b/trick_sims/Cannon/models/mongoose_httpd/include/http_server.h @@ -40,7 +40,7 @@ class HTTP_Server { int http_top_of_frame(); int http_shutdown(); - void sendSessionValues(struct mg_connection *nc); + void sendSessionMessages(struct mg_connection *nc); void handleClientMessage(struct mg_connection *nc, std::string msg); void addSession(struct mg_connection *nc, WSsession* session); void deleteSession(struct mg_connection *nc); diff --git a/trick_sims/Cannon/models/mongoose_httpd/src/WSSession.cpp b/trick_sims/Cannon/models/mongoose_httpd/src/WSSession.cpp index fa67f3a6..974dccbf 100644 --- a/trick_sims/Cannon/models/mongoose_httpd/src/WSSession.cpp +++ b/trick_sims/Cannon/models/mongoose_httpd/src/WSSession.cpp @@ -12,7 +12,6 @@ LIBRARY DEPENDENCIES: WSsession::WSsession( struct mg_connection *nc ) { connection = nc; - valuesStaged = false; intervalTimeTics = exec_get_time_tic_value(); // Default time interval is one second. nextTime = LLONG_MAX; cyclicSendEnabled = false; @@ -88,42 +87,38 @@ void WSsession::addVariable(char* vname){ } } -void WSsession::stageValues() { +void WSsession::stageVariableValues() { stageTime = (double)exec_get_time_tics() / exec_get_time_tic_value(); std::vector::iterator it; for (it = sessionVariables.begin(); it != sessionVariables.end(); it++ ) { (*it)->stageValue(); } - valuesStaged = true; } -void WSsession::stageValuesSynchronously() { +void WSsession::stageData() { long long simulation_time_tics = exec_get_time_tics(); if ( cyclicSendEnabled && ( simulation_time_tics >= nextTime )) { - stageValues(); + stageVariableValues(); } nextTime = (simulation_time_tics - (simulation_time_tics % intervalTimeTics) + intervalTimeTics); } -void WSsession::sendValues() { - if ( valuesStaged ) { - std::vector::iterator it; - std::stringstream ss; +void WSsession::sendMessage() { + std::vector::iterator it; + std::stringstream ss; - ss << "{ \"msg_type\" : \"values\",\n"; - ss << " \"time\" : " << std::setprecision(16) << stageTime << ",\n"; - ss << " \"values\" : [\n"; + ss << "{ \"msg_type\" : \"values\",\n"; + ss << " \"time\" : " << std::setprecision(16) << stageTime << ",\n"; + ss << " \"values\" : [\n"; - for (it = sessionVariables.begin(); it != sessionVariables.end(); it++ ) { - if (it != sessionVariables.begin()) ss << ",\n"; - (*it)->writeValue(ss); - } - ss << "]}" << std::endl; - std::string tmp = ss.str(); - const char * message = tmp.c_str(); - mg_send_websocket_frame(connection, WEBSOCKET_OP_TEXT, message, strlen(message)); - valuesStaged = false; - } + for (it = sessionVariables.begin(); it != sessionVariables.end(); it++ ) { + if (it != sessionVariables.begin()) ss << ",\n"; + (*it)->writeValue(ss); + } + ss << "]}" << std::endl; + std::string tmp = ss.str(); + const char * message = tmp.c_str(); + mg_send_websocket_frame(connection, WEBSOCKET_OP_TEXT, message, strlen(message)); } void WSsession::pause() { cyclicSendEnabled = false; } @@ -141,7 +136,7 @@ void WSsession::clear() { void WSsession::exit() {} -int WSsession::handle_msg (std::string client_msg) { +int WSsession::handleMessage(std::string client_msg) { int status = 0; std::vector members = parseJSON(client_msg.c_str()); @@ -172,8 +167,8 @@ int WSsession::handle_msg (std::string client_msg) { } else if ( strcmp(cmd, "var_unpause") == 0 ) { unpause(); } else if ( strcmp(cmd, "var_send") == 0 ) { - stageValues(); - sendValues(); + stageVariableValues(); + sendMessage(); } else if ( strcmp(cmd, "var_clear") == 0 ) { clear(); } else if ( strcmp(cmd, "var_exit") == 0 ) { diff --git a/trick_sims/Cannon/models/mongoose_httpd/src/http_server.cpp b/trick_sims/Cannon/models/mongoose_httpd/src/http_server.cpp index 12815b55..9bf1fd2e 100644 --- a/trick_sims/Cannon/models/mongoose_httpd/src/http_server.cpp +++ b/trick_sims/Cannon/models/mongoose_httpd/src/http_server.cpp @@ -76,7 +76,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { // called periodically by the threaded function connectionAttendant() [below]. // Send websocket messages to the client (web browser). if (nc->flags & MG_F_IS_WEBSOCKET) { - hs->sendSessionValues(nc); + hs->sendSessionMessages(nc); } } break; case MG_EV_HTTP_REQUEST: { @@ -138,14 +138,14 @@ void HTTP_Server::handle_API_GET_request(struct mg_connection *nc, http_message } } -void HTTP_Server::sendSessionValues(struct mg_connection *nc) { +void HTTP_Server::sendSessionMessages(struct mg_connection *nc) { // Find the session that goes with the given websocket connection, // and tell it to send its values to the client (web browser). std::map::iterator iter; iter = sessionMap.find(nc); if (iter != sessionMap.end()) { WSsession* session = iter->second; - session->sendValues(); + session->sendMessage(); } } @@ -163,7 +163,7 @@ void HTTP_Server::handleClientMessage(struct mg_connection *nc, std::string msg) iter = sessionMap.find(nc); if (iter != sessionMap.end()) { WSsession* session = iter->second; - session->handle_msg(msg); + session->handleMessage(msg); } } @@ -222,7 +222,7 @@ int HTTP_Server::http_top_of_frame() { pthread_mutex_lock(&sessionMapLock); for (iter = sessionMap.begin(); iter != sessionMap.end(); iter++ ) { WSsession* session = iter->second; - session->stageValuesSynchronously(); + session->stageData(); } pthread_mutex_unlock(&sessionMapLock); // Signal the server thread to construct and send the values-message to the client.