2022-08-23 18:47:56 +00:00
# include "trick/VariableServerSession.hh"
# include "trick/TrickConstant.hh"
# include "trick/exec_proto.h"
2023-04-18 21:27:32 +00:00
# include "trick/Message_proto.hh"
2022-08-23 18:47:56 +00:00
# include "trick/message_proto.h"
# include "trick/input_processor_proto.h"
# include "trick/realtimesync_proto.h"
2023-04-18 21:27:32 +00:00
int Trick : : VariableServerSession : : instance_counter = 0 ;
2023-04-28 20:42:42 +00:00
std : : string Trick : : VariableServerSession : : log_subdir = " session_logs " ;
2022-08-23 18:47:56 +00:00
2023-03-20 22:53:01 +00:00
Trick : : VariableServerSession : : VariableServerSession ( ) {
_debug = 0 ;
_enabled = true ;
_log = false ;
2023-04-28 20:42:42 +00:00
_session_log = false ;
_session_log_msg_stream = - 1 ;
2023-04-18 21:27:32 +00:00
_info_msg = false ;
2023-03-20 22:53:01 +00:00
_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-04-18 21:27:32 +00:00
_instance_num = instance_counter + + ;
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 ;
2023-04-18 21:27:32 +00:00
log_connection_opened ( ) ;
}
2023-04-28 20:42:42 +00:00
bool Trick : : VariableServerSession : : is_session_log_open ( ) {
return _session_log_msg_stream ! = - 1 ;
2023-04-18 21:27:32 +00:00
}
void Trick : : VariableServerSession : : open_session_log ( ) {
std : : string name = " VSSession " + std : : to_string ( _instance_num ) ;
2023-04-28 20:42:42 +00:00
_session_log_msg_stream = open_custom_message_file ( log_subdir + " / " + name + " .log " , name ) ;
2022-08-23 18:47:56 +00:00
}
// Command to turn on log to varserver_log file
2023-04-28 20:42:42 +00:00
int Trick : : VariableServerSession : : set_log ( bool on_off ) {
_log = on_off ;
2022-08-23 18:47:56 +00:00
return ( 0 ) ;
}
2023-04-28 20:42:42 +00:00
// Command to turn on individual session log file
int Trick : : VariableServerSession : : set_session_log ( bool on_off ) {
_session_log = on_off ;
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-04-18 21:27:32 +00:00
void Trick : : VariableServerSession : : log_connection_opened ( ) {
2023-04-28 20:42:42 +00:00
if ( _session_log ) {
if ( ! is_session_log_open ( ) ) {
2023-04-18 21:27:32 +00:00
open_session_log ( ) ;
}
2023-04-28 20:42:42 +00:00
message_publish ( _session_log_msg_stream , " Variable Server Session started with %s:%d \n " , _connection - > getClientHostname ( ) . c_str ( ) , _connection - > getClientPort ( ) ) ;
2023-04-18 21:27:32 +00:00
}
}
void Trick : : VariableServerSession : : log_received_message ( const std : : string & msg ) {
if ( _log ) {
message_publish ( MSG_PLAYBACK , " tag=<%s> time=%f %s " , _connection - > getClientTag ( ) . c_str ( ) , exec_get_sim_time ( ) , msg . c_str ( ) ) ;
2023-04-28 20:42:42 +00:00
}
if ( _session_log ) {
if ( ! is_session_log_open ( ) )
open_session_log ( ) ;
2023-04-18 21:27:32 +00:00
2023-04-28 20:42:42 +00:00
message_publish ( _session_log_msg_stream , " tag=<%s> time=%f %s " , _connection - > getClientTag ( ) . c_str ( ) , exec_get_sim_time ( ) , msg . c_str ( ) ) ;
2023-04-18 21:27:32 +00:00
}
if ( _debug > = 3 ) {
message_publish ( MSG_DEBUG , " %p tag=<%s> var_server received bytes = msg_len = %d \n " , _connection , _connection - > getClientTag ( ) . c_str ( ) , msg . size ( ) ) ;
}
if ( _debug > = 1 | | _info_msg ) {
message_publish ( MSG_DEBUG , " tag=<%s> time=%f %s " , _connection - > getClientTag ( ) . c_str ( ) , exec_get_sim_time ( ) , msg . c_str ( ) ) ;
}
}
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 ) {
2023-04-18 21:27:32 +00:00
log_received_message ( received_message ) ;
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 ;
}
2023-04-18 21:27:32 +00:00
int Trick : : VariableServerSession : : set_info_message ( bool on ) {
_info_msg = on ;
return 0 ;
}
2022-08-23 18:47:56 +00:00
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 ;
}