int Trick::VariableServerThread::var_exists(std::string in_name) { char buf1[5] ; bool error = false ; unsigned int msg_type ; REF2* var_ref = ref_attributes(const_cast<char*>(in_name.c_str())); if ( var_ref == (REF2*)NULL ) { error = true; } if (binary_data) { /* send binary 1 or 0 */ msg_type = VS_VAR_EXISTS ; memcpy(buf1, &msg_type , sizeof(msg_type)) ; buf1[4] = (error==false); if (debug >= 2) { message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending 1 binary byte\n", &connection, connection.client_tag); } tc_write(&connection, (char *) buf1, 5); } else { /* send ascii "1" or "0" */ sprintf(buf1, "%d\t%d\n", VS_VAR_EXISTS, (error==false)); if (debug >= 2) { message_publish(MSG_DEBUG, "%p tag=<%s> var_server sending:", &connection, connection.client_tag) ; message_publish(MSG_NORMAL, "%s\n", buf1); } tc_write(&connection, (char *) buf1, strlen(buf1)); } return(0) ; }
int Trick::VariableServerThread::var_add(std::string in_name) { VariableReference * new_var ; REF2 * new_ref ; if ( in_name.compare("time") == 0 ) { new_ref = make_time_ref() ; } else { new_ref = ref_attributes(const_cast<char*>(in_name.c_str())) ; } if ( new_ref == NULL ) { message_publish(MSG_ERROR, "Variable Server could not find variable %s.\n", in_name.c_str()); new_ref = make_error_ref(in_name); } else if ( new_ref->attr ) { if ( new_ref->attr->type == TRICK_STRUCTURED ) { message_publish(MSG_ERROR, "Variable Server: var_add cant add \"%s\" because its a composite variable.\n", in_name.c_str()); // Replace the REF2 object we got from ref_attributes with an error-ref. if (new_ref->attr) { free(new_ref->attr); } free(new_ref); new_ref = make_error_ref(in_name); } } else { message_publish(MSG_ERROR, "Variable Server: BAD MOJO - Missing ATTRIBUTES."); new_ref = make_error_ref(in_name); } new_var = new VariableReference(new_ref) ; vars.push_back(new_var) ; return(0) ; }
int restore_stl(std::stack<std::string> & in_stl , std::string object_name , std::string var_name ) { unsigned int ii ; unsigned int cont_size ; REF2 * items_ref ; char ** items ; std::replace_if(object_name.begin(), object_name.end(), std::ptr_fun<int,int>(&std::ispunct), '_'); //message_publish(1, "RESTORE_STL_STACK %s_%s\n", object_name.c_str() , var_name.c_str()) ; cont_size = in_stl.size() ; for ( ii = 0 ; ii < cont_size ; ii++ ) { in_stl.pop() ; } items_ref = ref_attributes((char *)(object_name + std::string("_") + var_name).c_str()) ; if ( items_ref != NULL ) { items = (char **)items_ref->address ; cont_size = get_size((char *)items) ; for ( ii = cont_size - 1 ; ii < cont_size ; ii-- ) { in_stl.push( items[ii] ) ; } delete_stl( in_stl , object_name , var_name ) ; } return 0 ; }
Trick::MonteVarCalculated::MonteVarCalculated(std::string in_name, std::string in_unit) { this->name = in_name; this->unit = in_unit; ref2 = ref_attributes((char *)name.c_str()); if (ref2 == NULL) { message_publish(MSG_ERROR, "Monte : MonteVarCalculated could not find parameter %s.\n", name.c_str()) ; } }
void Trick::delete_trick_map_stl( std::string obj_name , unsigned int stl_id ) { char var_declare[128] ; REF2 * items_ref ; sprintf(var_declare, "%s_%06d_keys" , obj_name.c_str(), stl_id) ; items_ref = ref_attributes(var_declare) ; if ( items_ref != NULL ) { TMM_delete_var_n(var_declare) ; ref_free(items_ref) ; free(items_ref) ; } sprintf(var_declare, "%s_%06d_data" , obj_name.c_str(), stl_id) ; items_ref = ref_attributes(var_declare) ; if ( items_ref != NULL ) { TMM_delete_var_n(var_declare) ; ref_free(items_ref) ; free(items_ref) ; } }
/** @details -# The simulation output directory is retrieved from the CommandLineArguments -# The log header file is created -# The endianness of the log file is written to the log header. -# The names of the parameters contained in the log file are written to the header. -# Memory buffers are allocated to store simulation data -# The DataRecordGroupObject (a derived SimObject) is added to the Scheduler. */ int Trick::DataRecordGroup::init() { unsigned int jj ; int ret ; // reset counter here so we can "re-init" our recording buffer_num = writer_num = total_bytes_written = 0 ; output_dir = command_line_args_get_output_dir() ; /* this is the common part of the record file name, the format specific will add the correct suffix */ file_name = output_dir + "/log_" + group_name ; pthread_mutex_init(&buffer_mutex, NULL); // Allocate recording space for time. rec_buffer[0]->buffer = (char *)calloc(max_num , rec_buffer[0]->ref->attr->size) ; /* Loop through all variables looking up names. Allocate recording space according to size of the variable */ for (jj = 1; jj < rec_buffer.size() ; jj++) { Trick::DataRecordBuffer * drb = rec_buffer[jj] ; if ( drb->ref_searched == false ) { REF2 * ref2 ; ref2 = ref_attributes((char *)drb->name.c_str()) ; if ( ref2 == NULL || ref2->attr == NULL ) { message_publish(MSG_WARNING, "Could not find Data Record variable %s.\n", drb->name.c_str()) ; rec_buffer.erase(rec_buffer.begin() + jj--) ; delete drb ; continue ; } else { drb->ref = ref2 ; } } if ( drb->alias.compare("") ) { drb->ref->reference = strdup(drb->alias.c_str()) ; } drb->last_value = (char *)calloc(1 , drb->ref->attr->size) ; drb->buffer = (char *)calloc(max_num , drb->ref->attr->size) ; drb->ref_searched = true ; } ret = write_header() ; // call format specific initialization to open destination and write header ret = format_specific_init() ; // set the inited flag to true when all initialization is done if ( ret == 0 ) { inited = true ; } return(0) ; }
//Command to create a new condition using a model variable (or reset an existing condition variable), num is index starting at 0. int Trick::IPPythonEvent::condition_var(int num, std::string varname, std::string comment) { /** @par Detailed Design: */ /** @li Find the variable reference for the varname string and pass it to condition() */ REF2* ref = ref_attributes((char*)varname.c_str()); /** @li Emit an error if specified varname does not exist. */ if (ref==NULL) { message_publish(MSG_WARNING, "Event condition variable %s not found. Setting condition to False.\n", varname.c_str()) ; condition(num, "False", comment, NULL, NULL ); } else { condition(num, varname, comment, ref, NULL ); } return(0); }
void Trick::IPPythonEvent::restart() { int jj ; for (jj=0; jj<condition_count; jj++) { if (condition_list[jj]->cond_type==1) { // condition variable condition_list[jj]->ref = ref_attributes((char*)condition_list[jj]->str.c_str()); } if (condition_list[jj]->cond_type==2) { // condition job condition_list[jj]->job = exec_get_job(condition_list[jj]->str.c_str(),1); } } for (jj=0; jj<action_count; jj++) { if (action_list[jj]->act_type!=0) { // action job action_list[jj]->job = exec_get_job(action_list[jj]->str.c_str(),1); } } }
int Trick::DataRecordGroup::add_change_variable( std::string in_name ) { REF2 * ref2 ; ref2 = ref_attributes((char *)in_name.c_str()) ; if ( ref2 == NULL || ref2->attr == NULL ) { message_publish(MSG_WARNING, "Could not find Data Record change variable %s.\n", in_name.c_str()) ; return(-1) ; } Trick::DataRecordBuffer * new_var = new Trick::DataRecordBuffer ; new_var->ref = ref2 ; new_var->buffer = (char *)malloc(ref2->attr->size) ; new_var->last_value = NULL ; memcpy(new_var->buffer , ref2->address , ref2->attr->size) ; change_buffer.push_back(new_var) ; return(0) ; }
/* @details -# if the path is empty after /vars -# loop through all allocations in the memory manager -# if the allocation has a name create an item in the reply message for that allocation. -# else -# convert "/" characters to "." in the path and strip trailing "/" characters. -# add converted variable name to the reply message. -# call ref_attributes to find the variable. -# if the variable exists -# if the variable type is a structure. -# loop through the attributes and add each of the struct/class variables as links in the reply message. -# else add the variables value and units to the reply message. -# add the description text to the reply message -# add the type information to the reply message -# add the type io specification to the reply message -# if the variable has dimensions add the dimension information to the reply message. -# else add an error message to the reply message */ int Trick::JSONVariableServerThread::get_vars( std::stringstream & body , char * path ) { int ret = 200 ; //std::cout << "get_vars " << path << std::endl ; if ( path[0] == '\0' ) { Trick::ALLOC_INFO_MAP_ITER aim_it ; bool first_item = true ; for ( aim_it = trick_MM->alloc_info_map_begin() ; aim_it != trick_MM->alloc_info_map_end() ; aim_it++ ) { ALLOC_INFO * alloc_info = (*aim_it).second ; if ( alloc_info->name != NULL and alloc_info->user_type_name != NULL ) { if ( first_item == true ) { first_item = false ; } else { body << " ," << std::endl ; } body << " \"" << alloc_info->name << "\" : \"" << alloc_info->user_type_name << "\"" ; } } body << std::endl ; } else { //TODO: More path character translations for characters like "[]" if ( path[strlen(path) - 1] == '/') { path[strlen(path) - 1] = '\0' ; } for ( unsigned int ii = 1 ; ii < strlen(path) ; ii++ ) { if ( path[ii] == '/') { path[ii] = '.' ; } } body << " \"name\" : \"" << &path[1] << "\" ," << std::endl ; //std::cout << "getting var " << path << std::endl ; // skip leading "/" REF2 * ref = ref_attributes( &path[1] ) ; if ( ref != NULL and ref->attr != NULL ) { ret = 200 ; //std::cout << "num_index " << ref->num_index << std::endl ; if ( ref->attr->type == TRICK_STRUCTURED ) { ATTRIBUTES * attr = (ATTRIBUTES *)ref->attr->attr ; body << " \"links\" : [" << std::endl ; for ( int ii = 0 ; attr[ii].name[0] != '\0' ; ii++ ) { if ( ii != 0 ) { body << " ," << std::endl ; } body << " \"" << attr[ii].name << "\"" ; } body << std::endl << " ] ," << std::endl ; } else { body << " \"value\" : " ; Trick::PythonPrint::write_rvalue(body, ref->address, ref->attr, ref->num_index, 0, false, true) ; body << " ," << std::endl ; if ( ref->attr->units != NULL ) { body << " \"units\" : \"" << ref->attr->units << "\" ," << std::endl ; } } if ( ref->attr->des != NULL ) { body << " \"description\" : \"" << ref->attr->des << "\" ," << std::endl ; } body << " \"type_name\" : " ; if ( ref->attr->type_name != NULL ) { body << "\"" << ref->attr->type_name << "\" ," << std::endl ; } else { body << "\"(null)\" ," << std::endl ; } body << " \"type\" : " << ref->attr->type << " ," << std::endl ; body << " \"io\" : " << ref->attr->io ; if ( (ref->attr->num_index - ref->num_index) > 0 ) { body << " ," << std::endl ; body << " \"num_index\" : " << ref->attr->num_index - ref->num_index << " ," << std::endl ; body << " \"index\" : [" << std::endl ; for ( int ii = ref->num_index , jj = 0 ; ii < ref->attr->num_index ; ii++ , jj++ ) { if ( jj != 0 ) { body << " ," << std::endl ; } body << " {" << std::endl ; body << " \"size\" : " << ref->attr->index[ii].size << " ," << std::endl ; body << " \"start\" : " << ref->attr->index[ii].start << std::endl ; body << " }" ; } body << std::endl << " ]" << std::endl ; } else { body << std::endl ; } free(ref) ; } else { body << " \"message\" : \"Not Found\"" << std::endl ; ret = 404 ; } } return ret ; }
int Trick::VariableServerThread::copy_sim_data() { unsigned int ii ; VariableReference * curr_var ; if ( vars.size() == 0 ) { return 0 ; } if ( pthread_mutex_trylock(©_mutex) == 0 ) { // Get the simulation time we start this copy time = (double)exec_get_time_tics() / exec_get_time_tic_value() ; for ( ii = 0 ; ii < vars.size() ; ii++ ) { curr_var = vars[ii] ; // if this variable is unresolved, try to resolve it if ( retry_bad_ref ) { if (curr_var->ref->address == &bad_ref_int) { REF2 *new_ref = ref_attributes(const_cast<char*>(curr_var->ref->reference)); if (new_ref != NULL) { curr_var->ref = new_ref; } } } // if there's a pointer somewhere in the address path, follow it in case pointer changed if ( curr_var->ref->pointer_present == 1 ) { curr_var->address = follow_address_path(curr_var->ref) ; if ( curr_var->address == NULL ) { std::string save_name(curr_var->ref->reference) ; if ( curr_var->ref->attr) { free(curr_var->ref->attr) ; } free(curr_var->ref) ; curr_var->ref = make_error_ref(save_name) ; curr_var->address = curr_var->ref->address ; } else { curr_var->ref->address = curr_var->address ; } } // if this variable is a string we need to get the raw character string out of it. if (( curr_var->string_type == TRICK_STRING ) && !curr_var->need_deref) { std::string * str_ptr = (std::string *)curr_var->ref->address ; curr_var->address = (void *)(str_ptr->c_str()) ; } // if this variable itself is a pointer, dereference it if ( curr_var->need_deref) { curr_var->address = *(void**)curr_var->ref->address ; } // handle c++ string and char* if ( curr_var->string_type == TRICK_STRING ) { if (curr_var->address == NULL) { curr_var->size = 0 ; } else { curr_var->size = strlen((char*)curr_var->address) + 1 ; } } // handle c++ wstring and wchar_t* if ( curr_var->string_type == TRICK_WSTRING ) { if (curr_var->address == NULL) { curr_var->size = 0 ; } else { curr_var->size = wcslen((wchar_t *)curr_var->address) * sizeof(wchar_t); } } memcpy( curr_var->buffer_in , curr_var->address , curr_var->size ) ; } retry_bad_ref = false ; // Indicate that sim data has been written and is now ready in the buffer_in's of the vars variable list. var_data_staged = true; packets_copied++ ; pthread_mutex_unlock(©_mutex) ; } return (0) ; }