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

📄 nml.cc

📁 Source code for an Numeric Cmputer
💻 CC
📖 第 1 页 / 共 5 页
字号:
* 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::peek(){    error_type = NML_NO_ERROR;    if (fast_mode) {	cms->peek();	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;	}    }    if (NULL == cms) {	if (error_type != NML_INVALID_CONFIGURATION) {	    error_type = NML_INVALID_CONFIGURATION;	    rcs_print_error("NML::peek: CMS not configured.\n");	}	return (-1);    }    if (cms->is_phantom) {	if (NULL != phantom_peek) {	    return ((*phantom_peek) ());	} else {	    return (0);	}    }    if (!cms->force_raw) {	cms->set_mode(CMS_READ);    }    cms->peek();    if (!cms->force_raw) {	if (cms->status == CMS_READ_OK) {	    if (-1 == format_output()) {		error_type = NML_FORMAT_ERROR;		return (-1);	    }	}    }    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;    }}/************************************************************ NML Member Function: format_output()* Purpose: Formats the data read from a CMS buffer as required* by the process that created this NML. The formatting converts* messages from some platform indepent format to a platform* specific format or vice versa. (Performing byte-swapping etc.)* Returns:*  0  The format was successful.*  -1 An error occured.* Notes:*  1. There are 3 conditions under which format_output may be* called.*     i. The data is being read out as is. (cms->mode == CMS_RAW_OUT).*    ii. A user needs the data in the local platform-specific or raw format*        but the buffer has been encoded in a platform-independant or*         neutral format.   (cms->mode == CMS_DECODE).*   iii. An NML_SERVER needs the data encoded in a platform-independant*        or neutral format to send it out over the network but the buffer*        is in a local platform-specific or raw format.*         (cms->mode == CMS_ENCODE)*  2. This function uses a list of format functions supplied* by the user and stored int the format_chain.* Returns:* 0 = Success.* -1 = Error.***********************************************************/int NML::format_output(){    NMLTYPE new_type;    long new_size;    /* Check pointers */    if (NULL == cms) {	rcs_print_error("NML: cms is NULL.\n");	return (-1);    }    if (cms->force_raw) {	return 0;    }    if (forced_type > 0) {	new_type = forced_type;    }    switch (cms->mode) {    case CMS_RAW_OUT:	break;    case CMS_DECODE:	/* Check the status of CMS. */	if (cms->status == CMS_READ_OK) {	    /* Handle the generic part of the message. */	    cms->format_low_ptr = cms->format_high_ptr = (char *) NULL;	    cms->rewind();	/* Move to the start of encoded buffer. */	    cms->update(new_type);	/* Get the message type from encoded					   buffer. */	    cms->update(new_size);	/* Get the message size from encoded					   buffer. */	    if (forced_type > 0) {		new_type = forced_type;	    }	    ((NMLmsg *) cms->subdiv_data)->type = new_type;	/* Store type 								   in								   message. */	    ((NMLmsg *) cms->subdiv_data)->size = new_size;	/* Store size 								   in								   message. */	    if (new_size > cms->max_message_size) {		rcs_print_error("NML: Message %ld of size  %ld \n", new_type,		    new_size);		rcs_print_error		    ("     too large for local buffer of %s of size %d.\n",		    cms->BufferName, cms->max_message_size);		if (verbose_nml_error_messages) {		    rcs_print_error			("Check that all processes agree on buffer size.\n");		}		cms->status = CMS_INSUFFICIENT_SPACE_ERROR;		return (-1);	    }	    /* Check the list of format functions. */	    if (!ignore_format_chain) {		cms->format_low_ptr = (char *) cms->subdiv_data;		cms->format_high_ptr = cms->format_low_ptr + cms->size;		if (NULL == format_chain) {		    rcs_print_error("NML::read: Format chain is NULL.\n");		    return (-1);		}		/* Run through the list of format functions. */		if (-1 == run_format_chain(new_type, cms->subdiv_data)) {		    rcs_print_error("NMLread: NMLformat error\n");		    if (verbose_nml_error_messages) {			rcs_print_error("   (Buffer = %s, Process = %s)\n",			    cms->BufferName, cms->ProcessName);		    }		    return (-1);		}	    }	}	break;    case CMS_ENCODE:	/* Check the status of CMS. */	if (cms->status != CMS_MISC_ERROR) {	    cms->format_low_ptr = cms->format_high_ptr = (char *) NULL;	    cms->rewind();	/* Move to the start of the encoded buffer. */	    /* Get the type and size from the message. */	    new_type = ((NMLmsg *) cms->subdiv_data)->type;	    new_size = ((NMLmsg *) cms->subdiv_data)->size;	    if (forced_type > 0) {		new_type = forced_type;		((NMLmsg *) cms->subdiv_data)->type = forced_type;	    }	    /* Store the type and size in the encoded buffer. */	    cms->update(new_type);	    cms->update(new_size);	    if (new_size > cms->max_message_size) {		rcs_print_error("NML: Message %ld of size  %ld\n", new_type,		    new_size);		rcs_print_error		    ("     too large for local buffer of %s of size %d.\n",		    cms->BufferName, cms->max_message_size);		if (verbose_nml_error_messages) {		    rcs_print_error			("Check that all processes agree on buffer size.\n");		}		cms->status = CMS_INSUFFICIENT_SPACE_ERROR;		return (-1);	    }	    /* Check the list of format functions. */	    if (!ignore_format_chain) {		cms->format_low_ptr = (char *) cms->subdiv_data;		cms->format_high_ptr = cms->format_low_ptr + cms->size;		if (NULL == format_chain) {		    rcs_print_error("NML::read: Format chain is NULL.\n");		    return (-1);		}		/* Run through the list of format functions. */		if (-1 == run_format_chain(new_type, cms->subdiv_data)) {		    rcs_print_error("NMLread: NMLformat error\n");		    if (verbose_nml_error_messages) {			rcs_print_error("   (Buffer = %s, Process = %s)\n",			    cms->BufferName, cms->ProcessName);		    }		    return (-1);		}		/* Get the new size of the message now that it's been		   encoded. */		cms->get_encoded_msg_size();	    }	}	break;    default:	rcs_print_error("NML::format_output: invalid format mode. (%d)\n",	    cms->mode);	return (-1);    }    if (forced_type > 0) {	new_type = forced_type;	((NMLmsg *) cms->subdiv_data)->type = forced_type;    }    return (((int) cms->status < 0) ? -1 : 0);}/************************************************************** NML Member Function: write()* Purpose: This write function provides users with an alternative* style of writing a message.* Parameters:* NMLmsg &nml_msg - Reference to the message to be written.* Returns:*  0 - The message was successfully written.*  -1 - An error occured. (Timeouts are considered errors.)*************************************************************/int NML::write(NMLmsg & nml_msg){    return (write(&nml_msg));	/* Call the other NML::write() */}/************************************************************** NML Member Function: write()* Purpose: Writes a message to the global buffer.* Parameters:* NMLmsg *nml_msg - Address of the message to be written.* Returns:*  0 - The message was successfully written.*  -1 - An error occured. (Timeouts are considered errors.)*************************************************************/int NML::write(NMLmsg * nml_msg){    error_type = NML_NO_ERROR;    if (fast_mode) {	*cms_inbuffer_header_size = nml_msg->size;	cms->write(nml_msg);	if (*cms_status == CMS_WRITE_OK) {	    return (0);	}	set_error();	return (-1);    }    /* Check pointers. */    if (NULL == cms) {	if (error_type != NML_INVALID_CONFIGURATION) {	    error_type = NML_INVALID_CONFIGURATION;	    rcs_print_error("NML::write: CMS not configured.\n");	}	return (-1);    }    if (NULL == nml_msg) {	error_type = NML_INVALID_MESSAGE_ERROR;	rcs_print_error("NML::write: Message is NULL.\n");	return (-1);    }    if ((nml_msg->size == 0 || nml_msg->type == 0) && !cms->isserver) {	error_type = NML_INVALID_MESSAGE_ERROR;	rcs_print_error("NML::write: Message size or type is zero.\n");	rcs_print_error	    ("NML: Check that the message was properly constructed.\n");    }    /* Handle Phantom Buffers. */    if (cms->is_phantom) {	if (NULL != phantom_write) {	    return ((*phantom_write) (nml_msg));	} else {	    return (0);	}    }    /* Set CMS to a write mode. */    cms->set_mode(CMS_WRITE);    /* Format the message if neccessary. */    if (-1 == format_input(nml_msg)) {	error_type = NML_FORMAT_ERROR;	return -1;    }    if (CMS_RAW_IN == cms->mode) {	cms->write(nml_msg);	/* Write the unformatted message.  */    } else {	cms->write(cms->subdiv_data);	/* Write the formatted message.  */    }    if (CMS_WRITE_OK == cms->status) {	error_type = NML_NO_ERROR;	return (0);    }    return set_error();}/************************************************************** NML Member Function: set_error* Purpose: This write function provides users with an alternative* style of writing a message.* Parameters:* NMLmsg &nml_msg - Reference to the message to be written.* Returns:*  0 - The message was successfully written.*  -1 - An error occured. (Timeouts are considered errors.)* Check error_type for more info.*************************************************************/int NML::set_error(){    if (error_type != NML_NO_ERROR) {	return -1;    }    if (NULL == cms) {	error_type = NML_INVALID_CONFIGURATION;	return 0;    }    /* Choose return value. */    switch (cms->status) {    case CMS_TIMED_OUT:	error_type = NML_TIMED_OUT;	return (-1);    case CMS_QUEUE_FULL:	error_type = NML_QUEUE_FULL_ERROR;	break;    case CMS_NO_MASTER_ERROR:	error_type = NML_NO_MASTER_ERROR;	break;    case CMS_WRITE_WAS_BLOCKED:	error_type = NML_BUFFER_NOT_READ;	break;    case CMS_STATUS_NOT_SET:	/* The status variable has not been set yet. */    case CMS_READ_OLD:		/* Read successful, but data is old. */    case CMS_READ_OK:		/* Read successful so far. */    case CMS_WRITE_OK:		/* Write successful so far. */    case CMS_CLEAR_OK:		/* A clear operation was successful.  */	error_type = NML_NO_ERROR;	break;    case CMS_RESOURCE_CONFLICT_ERROR:    case CMS_CREATE_ERROR:    case CMS_CONFIG_ERROR:	error_type = NML_INVALID_CONFIGURATION;	break;    case CMS_MISC_ERROR:    default:	error_type = NML_INTERNAL_CMS_ERROR;	break;    }    if (error_type == NML_NO_ERROR) {	return 0;    }    if (!info_printed) {	print_info();    }    return -1;}/************************************************************** NML Member Function: write_if_read()* Purpose: This write function provides users with an alternative* style of writing a message.* Parameters:* NMLmsg &nml_msg - Reference to the message to be written.* Returns:*  0 - The message was successfully written.*  -1 - An error occured. (Timeouts are considered errors.)* Check error_type for more info.*************************************************************/int NML::write_if_read(NMLmsg & nml_msg){    return (write_if_read(&nml_msg));}/************************************************************ NML Member Function: write_if_read()* Purpose: Write a message to the global buffer, but do not*  over-write another message if that message is unread.* Parameters:*  NMLmsg *nml_msg - Address of the message to be written.* Returns:*  0 - The message was successfully written.

⌨️ 快捷键说明

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