Create WebSocketSession base class and make variable server session a derivative of it. #847 #730

This commit is contained in:
Penn, John M 047828115 2019-08-19 15:15:33 -05:00
parent c633c4cddd
commit c1cbb2ffaa
4 changed files with 30 additions and 32 deletions

View File

@ -9,33 +9,32 @@ LIBRARY DEPENDENCIES:
#include <vector>
#include <string>
#include <mongoose.h>
#include "WebSocketSession.hh"
#include "WSSessionVariable.hh"
class WSsession {
class WSsession : public WebSocketSession {
public:
WSsession(struct mg_connection *nc);
void stageData(); /* -- virtual Base */
void sendMessage(); /* -- virtual Base */
int handleMessage (std::string); /* -- virtual Base */
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 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() {}
int sendErrorMessage(const char* fmt, ... );
//struct mg_connection* connection; /* -- Base */
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 */

View File

@ -13,7 +13,7 @@ LIBRARY DEPENDENCIES:
#include <pthread.h>
#include <string>
#include <map>
#include "../include/WSSession.hh"
#include "../include/WebSocketSession.hh"
typedef void (*httpMethodHandler)(struct mg_connection *, struct http_message *);
@ -28,7 +28,7 @@ class HTTP_Server {
std::map< std::string, httpMethodHandler> httpMethodHandlerMap; /* ** */
pthread_mutex_t APIMapLock; /* ** */
std::map<mg_connection*, WSsession*> sessionMap; /* ** */
std::map<mg_connection*, WebSocketSession*> sessionMap; /* ** */
pthread_mutex_t sessionMapLock; /* ** */
struct mg_serve_http_opts http_server_options; /* ** mongoose*/
struct mg_bind_opts bind_opts; /* ** mongoose*/
@ -42,7 +42,7 @@ class HTTP_Server {
void sendSessionMessages(struct mg_connection *nc);
void handleClientMessage(struct mg_connection *nc, std::string msg);
void addSession(struct mg_connection *nc, WSsession* session);
void addSession(struct mg_connection *nc, WebSocketSession* session);
void deleteSession(struct mg_connection *nc);
void install_API_GET_handler(std::string APIname, httpMethodHandler handler);
void handle_API_GET_request(struct mg_connection *nc, http_message *hm, std::string handlerName);

View File

@ -10,8 +10,7 @@ LIBRARY DEPENDENCIES:
#include "../include/WSSession.hh"
#include "../include/simpleJSON.hh"
WSsession::WSsession( struct mg_connection *nc ) {
connection = nc;
WSsession::WSsession( struct mg_connection *nc ) : WebSocketSession(nc) {
intervalTimeTics = exec_get_time_tic_value(); // Default time interval is one second.
nextTime = LLONG_MAX;
cyclicSendEnabled = false;
@ -37,7 +36,7 @@ REF2* WSsession::make_error_ref(const char* in_name) {
}
#define MAX_MSG_SIZE 4096
int WSsession::emitError(const char* fmt, ... ) {
int WSsession::sendErrorMessage(const char* fmt, ... ) {
char errText[MAX_MSG_SIZE];
char msgText[MAX_MSG_SIZE];
va_list args;
@ -60,21 +59,21 @@ void WSsession::addVariable(char* vname){
REF2 * new_ref ;
new_ref = ref_attributes(vname);
if ( new_ref == NULL ) {
emitError("Variable Server could not find variable %s.\n", vname);
sendErrorMessage("Variable Server could not find variable %s.\n", vname);
new_ref = make_error_ref(vname);
} else if ( new_ref->attr ) {
if ( new_ref->attr->type == TRICK_STRUCTURED ) {
emitError("Variable Server: var_add cant add \"%s\" because its a composite variable.\n", vname);
sendErrorMessage("Variable Server: var_add cant add \"%s\" because its a composite variable.\n", vname);
free(new_ref);
new_ref = make_error_ref(vname);
} else if ( new_ref->attr->type == TRICK_STL ) {
emitError("Variable Server: var_add cant add \"%s\" because its an STL variable.\n", vname);
sendErrorMessage("Variable Server: var_add cant add \"%s\" because its an STL variable.\n", vname);
free(new_ref);
new_ref = make_error_ref(vname);
}
} else {
emitError("Variable Server: BAD MOJO - Missing ATTRIBUTES.");
sendErrorMessage("Variable Server: BAD MOJO - Missing ATTRIBUTES.");
free(new_ref);
new_ref = make_error_ref(vname);
}
@ -174,7 +173,7 @@ int WSsession::handleMessage(std::string client_msg) {
} else if ( strcmp(cmd, "var_exit") == 0 ) {
//TODO
} else {
emitError("Unknown Command: \"%s\".\n", cmd);
sendErrorMessage("Unknown Command: \"%s\".\n", cmd);
status = 1;
}
return status;

View File

@ -53,7 +53,7 @@ static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) {
std::string uri(hm->uri.p, hm->uri.len);
std::cout << "WEBSOCKET[" << (void*)nc << "] OPENED. URI=\"" << uri << "\"." << std::endl;
// Create a session object to store information about this web-socket connection.
WSsession* session = new WSsession(nc);
WebSocketSession* session = new WSsession(nc);
hs->addSession(nc, session);
} break;
@ -141,35 +141,35 @@ void HTTP_Server::handle_API_GET_request(struct mg_connection *nc, http_message
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;
std::map<mg_connection*, WebSocketSession*>::iterator iter;
iter = sessionMap.find(nc);
if (iter != sessionMap.end()) {
WSsession* session = iter->second;
WebSocketSession* session = iter->second;
session->sendMessage();
}
}
void HTTP_Server::deleteSession(struct mg_connection *nc) {
std::map<mg_connection*, WSsession*>::iterator iter;
std::map<mg_connection*, WebSocketSession*>::iterator iter;
iter = sessionMap.find(nc);
if (iter != sessionMap.end()) {
WSsession* session = iter->second;
WebSocketSession* session = iter->second;
delete session;
sessionMap.erase(iter);
}
}
void HTTP_Server::handleClientMessage(struct mg_connection *nc, std::string msg) {
std::map<mg_connection*, WSsession*>::iterator iter;
std::map<mg_connection*, WebSocketSession*>::iterator iter;
iter = sessionMap.find(nc);
if (iter != sessionMap.end()) {
WSsession* session = iter->second;
WebSocketSession* session = iter->second;
session->handleMessage(msg);
}
}
void HTTP_Server::addSession(struct mg_connection *nc, WSsession* session) {
void HTTP_Server::addSession(struct mg_connection *nc, WebSocketSession* session) {
pthread_mutex_lock(&sessionMapLock);
sessionMap.insert( std::pair<mg_connection*, WSsession*>(nc, session) );
sessionMap.insert( std::pair<mg_connection*, WebSocketSession*>(nc, session) );
pthread_mutex_unlock(&sessionMapLock);
}
@ -218,10 +218,10 @@ int HTTP_Server::http_top_of_frame() {
if (listener != NULL) {
// Have all of the sessions stage their data. We do this here, in a
// top_of_frame job, so that all of the data is time-homogeneous.
std::map<mg_connection*, WSsession*>::iterator iter;
std::map<mg_connection*, WebSocketSession*>::iterator iter;
pthread_mutex_lock(&sessionMapLock);
for (iter = sessionMap.begin(); iter != sessionMap.end(); iter++ ) {
WSsession* session = iter->second;
WebSocketSession* session = iter->second;
session->stageData();
}
pthread_mutex_unlock(&sessionMapLock);