⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nml.cc

📁 Source code for an Numeric Cmputer
💻 CC
📖 第 1 页 / 共 5 页
字号:
	    }	}	CMS_STATUS return_value;	error_type = NML_NO_ERROR;	if (((int) (return_value = cms->clear())) > 0) {	    error_type = NML_INTERNAL_CMS_ERROR;	}	if (cms->status == CMS_TIMED_OUT) {	    error_type = NML_TIMED_OUT;	}	return (((int) return_value < 0) ? -1 : 0);    }}/************************************************************* NML Member Function: clean_buffers()*  Tells cms to set buffers to zero so that we eliminate strange* history effects. -- Should only be used by progams testing CMS/NML.************************************************************/void NML::clean_buffers(){    if (NULL != cms) {	cms->clean_buffers();    }}/************************************************************ NML Member Function: check_if_read()* Purpose:*  Returns 1 if the buffer has been read since the last write,* 0 if it hadn't, or -1 if some communications error prevented* it from finding out.* Notes:*  1. Some buffers can be identified as PHANTOM in the config file.* this will cause cms->is_phantom to be set. This will cause this* function to call the function pointed to by phantom_check_if_read* if it is not NULL rather than using CMS.*  2. If an error occurs, users can check error_type before* making any other NML calls************************************************************/int NML::check_if_read(){    if (NULL == cms) {	error_type = NML_INVALID_CONFIGURATION;	return (-1);    } else {	if (cms->is_phantom) {	    if (NULL != phantom_check_if_read) {		return ((*phantom_check_if_read) ());	    } else {		return (0);	    }	}	int return_value;	error_type = NML_NO_ERROR;	if ((return_value = cms->check_if_read()) == -1) {	    error_type = NML_INTERNAL_CMS_ERROR;	}	if (cms->status == CMS_TIMED_OUT) {	    error_type = NML_TIMED_OUT;	}	return (return_value);    }}/************************************************************ NML Member Function: get_queue_length()* Purpose:*  Returns the number of messages queued in the buffer if queing* was enabled for this buffer. 0 otherwise.************************************************************/int NML::get_queue_length(){    if (NULL == cms) {	error_type = NML_INVALID_CONFIGURATION;	return (-1);    } else {	error_type = NML_NO_ERROR;	return cms->get_queue_length();    }}/************************************************************ NML Member Function: get_space_available()* Purpose:*  Returns the approximate number of bytes that can be written* to a queued buffer.*************************************************************/int NML::get_space_available(){    if (NULL == cms) {	error_type = NML_INVALID_CONFIGURATION;	return (-1);    } else {	error_type = NML_NO_ERROR;	return cms->get_space_available();    }}/************************************************************** NML Member Function: valid()* Purpose:* Provides a check which can be used after an NML object is* constructed to determine if everthing is in order.* Returns: 1 if everything is O.K. 0 otherwise.*************************************************************/int NML::valid(){    if (NULL == cms) {	error_type = NML_INVALID_CONFIGURATION;	return (0);    }    if (cms->is_phantom) {	error_type = NML_NO_ERROR;	return (1);    }    if (CMS_MISC_ERROR == cms->status) {	error_type = NML_INTERNAL_CMS_ERROR;	return (0);    }    if (CMS_NO_MASTER_ERROR == cms->status) {	error_type = NML_NO_MASTER_ERROR;	return (0);    }    if (NULL == cms->data) {	error_type = NML_INVALID_CONFIGURATION;	return (0);    }    if (cms->neutral && (NULL == cms->encoded_data) && !cms->isserver) {	error_type = NML_INVALID_CONFIGURATION;	return (0);    }    if (!ignore_format_chain) {	if (NULL == format_chain) {	    error_type = NML_INVALID_CONFIGURATION;	    return (0);	}    }    error_type = NML_NO_ERROR;    return (1);}/************************************************************ NML Member Function: read()* Purpose: Reads an NMLmsg from a CMS buffer.* Returns:*  0 The read was successful but the data was not updated since the last read.*  -1 The buffer could not be read.*  o.w. The type of the new NMLmsg is returned.* Notes:*   1. Users need to call NML::get_address in order to access the* messages that were read.*  2. Some buffers can be identified as PHANTOM in the config file.* this will cause cms->is_phantom to be set. This will cause this* function to call the function pointed to by phantom_read* if it is not NULL rather than using CMS.*  3. If an error occurs, users can check error_type before* making any other NML calls***********************************************************/NMLTYPE NML::read(){    error_type = NML_NO_ERROR;    if (fast_mode) {	cms->read();	switch (cms->status) {	case CMS_READ_OLD:	    return (0);	case CMS_READ_OK:	    if (((NMLmsg *) cms->subdiv_data)->type <= 0 && !cms->isserver) {		rcs_print_error		    ("NML: New data recieved but type of %d is invalid.\n",		    ((NMLmsg *) cms->subdiv_data)->type);		return -1;	    }	    return (((NMLmsg *) cms->subdiv_data)->type);	default:	    set_error();	    return -1;	}    }    /* Check pointers. */    if (NULL == cms) {	if (error_type != NML_INVALID_CONFIGURATION) {	    error_type = NML_INVALID_CONFIGURATION;	    rcs_print_error("NML::read: CMS not configured.\n");	}	return (-1);    }    /* Handle PHANTOMs */    if (cms->is_phantom) {	if (NULL != phantom_read) {	    return ((*phantom_read) ());	} else {	    return (0);	}    }    /* Read using CMS */    if (!cms->force_raw) {	cms->set_mode(CMS_READ);    }    cms->read();    if (!cms->force_raw) {	if (cms->status == CMS_READ_OK) {	    if (-1 == format_output()) {		error_type = NML_FORMAT_ERROR;		return (-1);	    }	}    }    /* Choose the return value. */    switch (cms->status) {    case CMS_READ_OLD:	error_type = NML_NO_ERROR;	return (0);    case CMS_READ_OK:	error_type = NML_NO_ERROR;	if (((NMLmsg *) cms->subdiv_data)->type <= 0 && !cms->isserver) {	    rcs_print_error		("NML: New data recieved but type of %d is invalid.\n",		((NMLmsg *) cms->subdiv_data)->type);	    return -1;	}	return (((NMLmsg *) cms->subdiv_data)->type);    default:	set_error();	return -1;    }}/************************************************************ NML Member Function: blocking_read(double timeout)* Purpose: Reads an NMLmsg from a CMS buffer after waiting up to timeout seconds for new data.* Returns:*  0 The read was successful but the data was not updated since the last read.*  -1 The buffer could not be read.*  o.w. The type of the new NMLmsg is returned.* Notes:*   1. Users need to call NML::get_address in order to access the* messages that were read.*  2. Some buffers can be identified as PHANTOM in the config file.* this will cause cms->is_phantom to be set. This will cause this* function to call the function pointed to by phantom_read* if it is not NULL rather than using CMS.*  3. If an error occurs, users can check error_type before* making any other NML calls***********************************************************/NMLTYPE NML::blocking_read(double blocking_timeout){    error_type = NML_NO_ERROR;    if (fast_mode) {	cms->blocking_read(blocking_timeout);	switch (cms->status) {	case CMS_READ_OLD:	    return (0);	case CMS_READ_OK:	    if (((NMLmsg *) cms->subdiv_data)->type <= 0 && !cms->isserver) {		rcs_print_error		    ("NML: New data recieved but type of %d is invalid.\n",		    ((NMLmsg *) cms->subdiv_data)->type);		return -1;	    }	    return (((NMLmsg *) cms->subdiv_data)->type);	case CMS_TIMED_OUT:	    error_type = NML_NO_ERROR;	    return 0;	default:	    set_error();	    return (-1);	}    }    /* Check pointers. */    if (NULL == cms) {	if (error_type != NML_INVALID_CONFIGURATION) {	    error_type = NML_INVALID_CONFIGURATION;	    rcs_print_error("NML::blocking_read: CMS not configured.\n");	}	return (-1);    }    /* Handle PHANTOMs */    if (cms->is_phantom) {	if (NULL != phantom_read) {	    return ((*phantom_read) ());	} else {	    return (0);	}    }    /* Read using CMS */    if (!cms->force_raw) {	cms->set_mode(CMS_READ);    }    if (cms->BufferType == CMS_SHMEM_TYPE) {	cms->blocking_read(blocking_timeout);    } else {	double time_elapsed = 0.0;	double start_time = 0.0;	if (blocking_timeout > 0.0) {	    start_time = etime();	}	double current_brpi = blocking_read_poll_interval;	cms->status = CMS_READ_OLD;	if (current_brpi < 2e-2) {	    current_brpi = 2e-2;	}	if (current_brpi > blocking_timeout / 2.0 && blocking_timeout > 1e-6) {	    current_brpi = blocking_timeout / 2.0;	}	while (cms->status == CMS_READ_OLD &&	    (time_elapsed < blocking_timeout || blocking_timeout < 0.0)) {	    esleep(current_brpi);	    cms->read();	    if (blocking_timeout > 0.0 && cms->status == CMS_READ_OLD) {		time_elapsed = etime() - start_time;	    }	    if (time_elapsed < 0.0) {		break;	    }	}    }    if (!cms->force_raw) {	if (cms->status == CMS_READ_OK) {	    if (-1 == format_output()) {		error_type = NML_FORMAT_ERROR;		return (-1);	    }	}    }    /* Choose the return value. */    switch (cms->status) {    case CMS_READ_OLD:	return (0);    case CMS_READ_OK:	if (((NMLmsg *) cms->subdiv_data)->type <= 0 && !cms->isserver) {	    rcs_print_error		("NML: New data recieved but type of %d is invalid.\n",		((NMLmsg *) cms->subdiv_data)->type);	    return -1;	}	return (((NMLmsg *) cms->subdiv_data)->type);    case CMS_TIMED_OUT:	error_type = NML_NO_ERROR;	return 0;    default:	set_error();	return (-1);    }}void NML::reconnect(){    if (NULL != cms) {	cms->reconnect();    }}void NML::disconnect(){    if (NULL != cms) {	cms->disconnect();    }}/* Same as the read with no arguments except that the data is stored in a user supplied location . */NMLTYPE NML::read(void *temp_data, long temp_size){    NMLTYPE return_value = 0;    void *original_data;    long original_size = cms->size;    long original_max_message_size = cms->max_message_size;    original_data = cms->data;    cms->data = temp_data;    cms->size = temp_size;    if (cms->max_message_size > ((long) temp_size)) {	cms->max_message_size = temp_size;    }    return_value = peek();    cms->data = original_data;    cms->size = original_size;    cms->max_message_size = original_max_message_size;    return return_value;}/* Same as the peek with no arguments except that the data is stored in a user supplied location . */NMLTYPE NML::peek(void *temp_data, long temp_size){    NMLTYPE return_value = 0;    void *original_data;    long original_size = cms->size;    long original_max_message_size = cms->max_message_size;    original_data = cms->data;    cms->data = temp_data;    cms->size = temp_size;    if (cms->max_message_size > ((long) temp_size)) {	cms->max_message_size = temp_size;    }    return_value = peek();    cms->data = original_data;    cms->size = original_size;    cms->max_message_size = original_max_message_size;    return return_value;}/************************************************************ NML Member Function: peek()* Purpose: Reads an NMLmsg from a CMS buffer without setting the* was_read flag. The was_read flag is used by check_if_read and* write_if_read.* Returns:*  0 The read was successful but the data was not updated since the last read.*  -1 The buffer could not be read.*  o.w. The type of the new NMLmsg is returned.* Notes:*   1. Users need to call NML::get_address in order to access the

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -