2022-08-23 18:47:56 +00:00
|
|
|
#include "trick/VariableServerSession.hh"
|
|
|
|
#include "trick/TrickConstant.hh"
|
|
|
|
#include "trick/exec_proto.h"
|
|
|
|
#include "trick/message_proto.h"
|
|
|
|
#include "trick/input_processor_proto.h"
|
|
|
|
#include "trick/realtimesync_proto.h"
|
|
|
|
|
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
Trick::VariableServerSession::VariableServerSession() {
|
|
|
|
_debug = 0;
|
|
|
|
_enabled = true ;
|
|
|
|
_log = false ;
|
|
|
|
_copy_mode = VS_COPY_ASYNC ;
|
|
|
|
_write_mode = VS_WRITE_ASYNC ;
|
|
|
|
_frame_multiple = 1 ;
|
|
|
|
_frame_offset = 0 ;
|
|
|
|
_freeze_frame_multiple = 1 ;
|
|
|
|
_freeze_frame_offset = 0 ;
|
|
|
|
_update_rate = 0.1 ;
|
|
|
|
_cycle_tics = (long long)(_update_rate * exec_get_time_tic_value()) ;
|
|
|
|
if (_cycle_tics == 0) {
|
|
|
|
_cycle_tics = 1;
|
2022-08-23 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
_next_tics = TRICK_MAX_LONG_LONG ;
|
|
|
|
_freeze_next_tics = TRICK_MAX_LONG_LONG ;
|
|
|
|
_byteswap = false ;
|
|
|
|
_validate_address = false ;
|
|
|
|
_send_stdio = false ;
|
2022-08-23 18:47:56 +00:00
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
_binary_data = false;
|
|
|
|
_byteswap = false;
|
|
|
|
_binary_data_nonames = false;
|
2022-08-23 18:47:56 +00:00
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
_exit_cmd = false;
|
|
|
|
_pause_cmd = false;
|
2022-08-23 18:47:56 +00:00
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
pthread_mutex_init(&_copy_mutex, NULL);
|
|
|
|
}
|
2022-08-23 18:47:56 +00:00
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
Trick::VariableServerSession::~VariableServerSession() {
|
|
|
|
for (unsigned int ii = 0 ; ii < _session_variables.size() ; ii++ ) {
|
|
|
|
delete _session_variables[ii];
|
|
|
|
}
|
|
|
|
}
|
2022-08-23 18:47:56 +00:00
|
|
|
|
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
void Trick::VariableServerSession::set_connection(ClientConnection * conn) {
|
|
|
|
_connection = conn;
|
2022-08-23 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Command to turn on log to varserver_log file
|
|
|
|
int Trick::VariableServerSession::set_log_on() {
|
2023-03-20 22:53:01 +00:00
|
|
|
_log = true;
|
2022-08-23 18:47:56 +00:00
|
|
|
return(0) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command to turn off log to varserver_log file
|
|
|
|
int Trick::VariableServerSession::set_log_off() {
|
2023-03-20 22:53:01 +00:00
|
|
|
_log = false;
|
2022-08-23 18:47:56 +00:00
|
|
|
return(0) ;
|
|
|
|
}
|
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
bool Trick::VariableServerSession::get_pause() {
|
|
|
|
return _pause_cmd ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trick::VariableServerSession::set_pause( bool on_off) {
|
|
|
|
_pause_cmd = on_off ;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Trick::VariableServerSession::get_exit_cmd() {
|
|
|
|
return _exit_cmd ;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trick::VariableServerSession::pause_copy() {
|
|
|
|
pthread_mutex_lock(&_copy_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trick::VariableServerSession::unpause_copy() {
|
|
|
|
pthread_mutex_unlock(&_copy_mutex);
|
|
|
|
}
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
void Trick::VariableServerSession::disconnect_references() {
|
2023-03-20 22:53:01 +00:00
|
|
|
for (VariableReference * variable : _session_variables) {
|
2022-08-23 18:47:56 +00:00
|
|
|
variable->tagAsInvalid();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
long long Trick::VariableServerSession::get_next_tics() const {
|
2023-03-20 22:53:01 +00:00
|
|
|
if ( ! _enabled ) {
|
2022-08-23 18:47:56 +00:00
|
|
|
return TRICK_MAX_LONG_LONG ;
|
|
|
|
}
|
2023-03-20 22:53:01 +00:00
|
|
|
return _next_tics ;
|
2022-08-23 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
long long Trick::VariableServerSession::get_freeze_next_tics() const {
|
2023-03-20 22:53:01 +00:00
|
|
|
if ( ! _enabled ) {
|
2022-08-23 18:47:56 +00:00
|
|
|
return TRICK_MAX_LONG_LONG ;
|
|
|
|
}
|
2023-03-20 22:53:01 +00:00
|
|
|
return _freeze_next_tics ;
|
2022-08-23 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
int Trick::VariableServerSession::handle_message() {
|
2022-08-23 18:47:56 +00:00
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
std::string received_message;
|
|
|
|
int nbytes = _connection->read(received_message);
|
|
|
|
if (nbytes > 0) {
|
2022-08-23 18:47:56 +00:00
|
|
|
ip_parse(received_message.c_str()); /* returns 0 if no parsing error */
|
|
|
|
}
|
2023-03-20 22:53:01 +00:00
|
|
|
|
|
|
|
return nbytes;
|
2022-08-23 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Trick::VariableReference * Trick::VariableServerSession::find_session_variable(std::string name) const {
|
2023-03-20 22:53:01 +00:00
|
|
|
for (VariableReference * ref : _session_variables) {
|
2022-08-23 18:47:56 +00:00
|
|
|
// Look for matching name
|
|
|
|
if (name.compare(ref->getName()) == 0) {
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
double Trick::VariableServerSession::get_update_rate() const {
|
2023-03-20 22:53:01 +00:00
|
|
|
return _update_rate;
|
2022-08-23 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VS_WRITE_MODE Trick::VariableServerSession::get_write_mode () const {
|
2023-03-20 22:53:01 +00:00
|
|
|
return _write_mode;
|
2022-08-23 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VS_COPY_MODE Trick::VariableServerSession::get_copy_mode () const {
|
2023-03-20 22:53:01 +00:00
|
|
|
return _copy_mode;
|
|
|
|
}
|
|
|
|
|
|
|
|
long long Trick::VariableServerSession::get_cycle_tics() const {
|
|
|
|
return _cycle_tics;
|
2022-08-23 18:47:56 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
int Trick::VariableServerSession::get_frame_multiple () const {
|
|
|
|
return _frame_multiple;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Trick::VariableServerSession::get_frame_offset () const {
|
|
|
|
return _frame_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Trick::VariableServerSession::get_freeze_frame_multiple () const {
|
|
|
|
return _freeze_frame_multiple;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Trick::VariableServerSession::get_freeze_frame_offset () const {
|
|
|
|
return _freeze_frame_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Trick::VariableServerSession::get_enabled () const {
|
|
|
|
return _enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trick::VariableServerSession::set_freeze_next_tics(long long tics) {
|
|
|
|
_freeze_next_tics = tics;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trick::VariableServerSession::set_next_tics(long long tics) {
|
|
|
|
_next_tics = tics;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Trick::VariableServerSession::set_exit_cmd() {
|
|
|
|
_exit_cmd = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-08-23 18:47:56 +00:00
|
|
|
std::ostream& Trick::operator<< (std::ostream& s, const Trick::VariableServerSession& session) {
|
2023-03-20 22:53:01 +00:00
|
|
|
if (session._binary_data) {
|
2022-08-23 18:47:56 +00:00
|
|
|
s << " \"format\":\"BINARY\",\n";
|
|
|
|
} else {
|
|
|
|
s << " \"format\":\"ASCII\",\n";
|
|
|
|
}
|
|
|
|
s << " \"update_rate\":" << session.get_update_rate() << ",\n";
|
|
|
|
|
|
|
|
s << " \"variables\":[\n";
|
|
|
|
|
2023-03-20 22:53:01 +00:00
|
|
|
int n_vars = (int)session._session_variables.size();
|
2022-08-23 18:47:56 +00:00
|
|
|
for (int i=0 ; i<n_vars ; i++) {
|
2023-03-20 22:53:01 +00:00
|
|
|
s << *(session._session_variables[i]);
|
2022-08-23 18:47:56 +00:00
|
|
|
if ((n_vars-i)>1) {
|
|
|
|
s << "," ;
|
|
|
|
}
|
|
|
|
s << "\n";
|
|
|
|
}
|
|
|
|
s << " ]\n";
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|