More refactoring. #847 #730

This commit is contained in:
Penn, John M 047828115 2019-08-16 20:16:27 -05:00
parent fee9b89a07
commit c633c4cddd
4 changed files with 49 additions and 50 deletions

View File

@ -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<WSsessionVariable*> 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<WSsessionVariable*> sessionVariables; /* -- VariableServerSession specific */
bool cyclicSendEnabled; /* -- VariableServerSession specific */
long long nextTime; /* -- VariableServerSession specific */
long long intervalTimeTics; /* -- VariableServerSession specific */
};
#endif

View File

@ -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);

View File

@ -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<WSsessionVariable*>::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<WSsessionVariable*>::iterator it;
std::stringstream ss;
void WSsession::sendMessage() {
std::vector<WSsessionVariable*>::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<Member*> 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 ) {

View File

@ -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<mg_connection*, WSsession*>::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.