2015-02-26 15:02:31 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <iostream>
|
|
|
|
#ifdef __linux
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#endif
|
2015-06-24 20:58:17 +00:00
|
|
|
#include <netdb.h>
|
|
|
|
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2015-06-01 15:28:29 +00:00
|
|
|
#include "trick/VariableServer.hh"
|
|
|
|
#include "trick/variable_server_sync_types.h"
|
|
|
|
#include "trick/input_processor_proto.h"
|
|
|
|
#include "trick/tc_proto.h"
|
|
|
|
#include "trick/message_proto.h"
|
|
|
|
#include "trick/message_type.h"
|
|
|
|
#include "trick/realtimesync_proto.h"
|
2015-06-02 13:29:34 +00:00
|
|
|
#include "trick/ExecutiveException.hh"
|
2015-06-01 15:28:29 +00:00
|
|
|
#include "trick/exec_proto.h"
|
2015-02-26 15:02:31 +00:00
|
|
|
|
|
|
|
void exit_var_thread(void *in_vst) ;
|
|
|
|
|
|
|
|
void * Trick::VariableServerThread::thread_body() {
|
|
|
|
|
2023-02-22 17:35:30 +00:00
|
|
|
// Check for short running sims
|
|
|
|
test_shutdown(NULL, NULL);
|
|
|
|
|
2015-02-26 15:02:31 +00:00
|
|
|
// We need to make the thread to VariableServerThread map before we accept the connection.
|
|
|
|
// Otherwise we have a race where this thread is unknown to the variable server and the
|
|
|
|
// client gets confirmation that the connection is ready for communication.
|
|
|
|
vs->add_vst( pthread_self() , this ) ;
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
// Accept client connection
|
2023-02-22 17:35:30 +00:00
|
|
|
int status = connection->start();
|
|
|
|
|
|
|
|
if (status != 0) {
|
2022-08-23 18:47:56 +00:00
|
|
|
// TODO: Use a real error handler
|
|
|
|
vs->delete_vst(pthread_self());
|
|
|
|
|
|
|
|
// Tell main thread that we failed to initialize
|
|
|
|
pthread_mutex_lock(&connection_status_mutex);
|
|
|
|
connection_status = CONNECTION_FAIL;
|
|
|
|
pthread_cond_signal(&connection_status_cv);
|
|
|
|
pthread_mutex_unlock(&connection_status_mutex);
|
|
|
|
|
2023-02-22 17:35:30 +00:00
|
|
|
cleanup();
|
2022-08-23 18:47:56 +00:00
|
|
|
pthread_exit(NULL);
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
2022-08-23 18:47:56 +00:00
|
|
|
|
|
|
|
// Create session
|
2023-02-22 17:35:30 +00:00
|
|
|
session = new VariableServerSession(connection);
|
2022-08-23 18:47:56 +00:00
|
|
|
vs->add_session( pthread_self(), session );
|
|
|
|
|
|
|
|
// Tell main that we are ready
|
|
|
|
pthread_mutex_lock(&connection_status_mutex);
|
|
|
|
connection_status = CONNECTION_SUCCESS;
|
|
|
|
pthread_cond_signal(&connection_status_cv);
|
|
|
|
pthread_mutex_unlock(&connection_status_mutex);
|
|
|
|
|
2015-02-26 15:02:31 +00:00
|
|
|
// if log is set on for variable server (e.g., in input file), turn log on for each client
|
|
|
|
if (vs->get_log()) {
|
2022-08-23 18:47:56 +00:00
|
|
|
session->set_log_on();
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
while (1) {
|
2023-02-22 17:35:30 +00:00
|
|
|
// Shutdown here if it's time
|
|
|
|
test_shutdown(exit_var_thread, (void *) this);
|
|
|
|
|
2016-04-19 18:50:30 +00:00
|
|
|
// Pause here if we are in a restart condition
|
|
|
|
pthread_mutex_lock(&restart_pause) ;
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
// Look for a message from the client
|
|
|
|
// Parse and execute if one is availible
|
|
|
|
session->handleMessage();
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
// Check to see if exit is necessary
|
|
|
|
if (session->exit_cmd == true) {
|
2023-02-22 17:35:30 +00:00
|
|
|
pthread_mutex_unlock(&restart_pause) ;
|
2015-02-26 15:02:31 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
// Copy data out of sim if async mode
|
|
|
|
if ( session->get_copy_mode() == VS_COPY_ASYNC ) {
|
|
|
|
session->copy_sim_data() ;
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
2022-08-23 18:47:56 +00:00
|
|
|
|
|
|
|
bool should_write_async = (session->get_write_mode() == VS_WRITE_ASYNC) ||
|
|
|
|
( session->get_copy_mode() == VS_COPY_ASYNC && (session->get_write_mode() == VS_WRITE_WHEN_COPIED)) ||
|
|
|
|
(! is_real_time());
|
|
|
|
|
|
|
|
// Write data out to connection if async mode and not paused
|
|
|
|
if ( should_write_async && !session->get_pause()) {
|
|
|
|
int ret = session->write_data() ;
|
|
|
|
if ( ret < 0 ) {
|
2023-02-22 17:35:30 +00:00
|
|
|
pthread_mutex_unlock(&restart_pause) ;
|
2022-08-23 18:47:56 +00:00
|
|
|
break ;
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 18:50:30 +00:00
|
|
|
pthread_mutex_unlock(&restart_pause) ;
|
2015-02-26 15:02:31 +00:00
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
usleep((unsigned int) (session->get_update_rate() * 1000000));
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
2015-06-02 13:29:34 +00:00
|
|
|
} catch (Trick::ExecutiveException & ex ) {
|
2015-02-26 15:02:31 +00:00
|
|
|
message_publish(MSG_ERROR, "\nVARIABLE SERVER COMMANDED exec_terminate\n ROUTINE: %s\n DIAGNOSTIC: %s\n" ,
|
|
|
|
ex.file.c_str(), ex.message.c_str()) ;
|
|
|
|
exit(ex.ret_code) ;
|
|
|
|
} catch (const std::exception &ex) {
|
|
|
|
message_publish(MSG_ERROR, "\nVARIABLE SERVER caught std::exception\n DIAGNOSTIC: %s\n" ,
|
|
|
|
ex.what()) ;
|
|
|
|
exit(-1) ;
|
|
|
|
#ifdef __linux
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#if __GNUC__ >= 4 && __GNUC_MINOR__ >= 2
|
|
|
|
// for post gcc 4.1.2 or whatever glibc version is used in RHEL6 and above.
|
|
|
|
} catch (abi::__forced_unwind&) {
|
|
|
|
//pthread_exit and pthread_cancel will cause an abi::__forced_unwind to be thrown. Rethrow it.
|
|
|
|
throw;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
} catch (...) {
|
|
|
|
#ifdef __linux
|
|
|
|
#ifdef __GNUC__
|
2022-08-18 15:47:07 +00:00
|
|
|
#if (__GNUC__ == 4 && __GNUC_MINOR__ == 1) || __GNUC__ == 12
|
|
|
|
// for gcc 4.1.2 or whatever glib version in RHEL 5 that does not work with the abi::__forced_unwind
|
|
|
|
// Also seems to have a problem with gcc 12
|
2015-02-26 15:02:31 +00:00
|
|
|
throw;
|
|
|
|
#else
|
|
|
|
message_publish(MSG_ERROR, "\nVARIABLE SERVER caught unknown exception\n" ) ;
|
|
|
|
exit(-1) ;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (debug >= 3) {
|
2023-02-22 17:35:30 +00:00
|
|
|
message_publish(MSG_DEBUG, "%p tag=<%s> var_server receive loop exiting\n", connection, connection->getClientTag().c_str());
|
2015-02-26 15:02:31 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 17:35:30 +00:00
|
|
|
thread_shutdown(exit_var_thread, this);
|
|
|
|
|
2015-02-26 15:02:31 +00:00
|
|
|
return NULL ;
|
|
|
|
|
|
|
|
}
|
|
|
|
|