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

📄 nml.cc

📁 Source code for an Numeric Cmputer
💻 CC
📖 第 1 页 / 共 5 页
字号:
*  -1 - An error occured.* (Timeouts, and unread buffers  are considered errors.)* Check error_type for more info.************************************************************/int NML::write_if_read(NMLmsg * nml_msg){    error_type = NML_NO_ERROR;    if (fast_mode) {	cms->header.in_buffer_size = nml_msg->size;	cms->write(nml_msg);	if (cms->status == CMS_WRITE_OK) {	    return (0);	}	set_error();	return (-1);    }    if (NULL == cms) {	if (error_type != NML_INVALID_CONFIGURATION) {	    error_type = NML_INVALID_CONFIGURATION;	    rcs_print_error("NML::write_if_read: CMS not configured.\n");	}	return (-1);    }    if (NULL == nml_msg) {	error_type = NML_INVALID_MESSAGE_ERROR;	rcs_print_error("NML::write_if_read: 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_if_read: Message size or type is zero.\n");	if (verbose_nml_error_messages) {	    rcs_print_error		("NML: Check that the message was properly constructed.\n");	}    }    if (cms->is_phantom) {	if (NULL != phantom_write_if_read) {	    return ((*phantom_write_if_read) (nml_msg));	} else {	    return (0);	}    }    cms->set_mode(CMS_WRITE);    if (-1 == format_input(nml_msg)) {	error_type = NML_FORMAT_ERROR;	return -1;    }    if (CMS_RAW_IN == cms->mode) {	cms->write_if_read(nml_msg);    } else {	cms->write_if_read(cms->subdiv_data);    }    return (set_error());}/******************************************************************** NML Member Function: format_input()* Purpose: Formats the in an NML message to be writen to a CMS buffer*  as required by the configuration file. The formatting converts*  messages from some platform indepent format to a platform*  specific format or vice versa. (Performing byte-swapping etc.)* Parameters:* NMLmsg *nml_msg - The address of the NML message.* Returns:* 0 = Success.* -1 = Error.* Notes:*  1. There are 3 conditions that format_input may be called under.*    i. The message will be written to the buffer without any formatting.*       (cms->mode == CMS_RAW_IN)*    ii. The message is in a native or raw format and needs to be encoded*  in a neutral format before being sent over the network or into a*  neutral buffer.*        (cms->mode == CMS_ENCODE)*   iii. The process calling this is a server which recieved a neutrally* encoded buffer over the network which must be converted to native* format before being written into a raw buffer.*        (cms->mode == CMS_DECODE)*  2. This function is for internal NML use only.******************************************************************/int NML::format_input(NMLmsg * nml_msg){    NMLTYPE new_type;    long new_size;    if (NULL == cms) {	return -1;    }    if (cms->force_raw) {	cms->mode = CMS_RAW_IN;    }    switch (cms->mode) {    case CMS_RAW_IN:	/* Make sure the message size is not larger than the buffer size. */	if (nml_msg->size > cms->max_message_size) {	    rcs_print_error("NML: Message size(%d) too large for"		" CMS buffer size of %d.\n",		nml_msg->size, cms->max_message_size);	    cms->status = CMS_INSUFFICIENT_SPACE_ERROR;	    return (-1);	}	cms->header.in_buffer_size = nml_msg->size;	break;    case CMS_ENCODE:	/* Make sure the message size is not larger than the buffer size. */	if (nml_msg->size > cms->max_message_size) {	    rcs_print_error("NML: Message size(%d) too large for"		" CMS buffer size of %d.\n",		nml_msg->size, cms->max_message_size);	    cms->status = CMS_INSUFFICIENT_SPACE_ERROR;	    return (-1);	}	cms->format_low_ptr = (char *) nml_msg;	cms->format_high_ptr = cms->format_low_ptr + nml_msg->size;	/* Handle the generic part of the message. */	cms->rewind();		/* Move to the start of the encoded buffer. */	cms->update(nml_msg->type);	/* Store message type in encoded					   buffer. */	cms->update(nml_msg->size);	/* Store message size in encoded					   buffer. */	/* Check list of format functions. */	if (!ignore_format_chain) {	    if (NULL == format_chain) {		rcs_print_error("NML::read: Format chain is NULL.\n");		return (-1);	    }	    /* Run through list of format functions. */	    if (-1 == run_format_chain(nml_msg->type, nml_msg)) {		rcs_print_error("NMLwrite: format error\n");		if (verbose_nml_error_messages) {		    rcs_print_error("   (Buffer = %s, Process = %s)\n",			cms->BufferName, cms->ProcessName);		}		return (-1);	    }	}	/* Determine the new size of the message now that its encoded. */	cms->header.in_buffer_size = cms->get_encoded_msg_size();	break;    case CMS_DECODE:	cms->format_low_ptr = cms->format_high_ptr = (char *) NULL;	cms->rewind();		/* Move to the start of the encoded buffer. */	cms->update(new_type);	/* Get message type from encoded buffer. */	cms->update(new_size);	/* Get message size from encoded buffer. */	/* Make sure the message size is not larger than the buffer size. */	if (new_size > cms->max_message_size) {	    rcs_print_error("NMLwrite: Message size(%d) too large for"		" CMS buffer size of %d.\n", new_size, cms->max_message_size);	    cms->status = CMS_INSUFFICIENT_SPACE_ERROR;	    return (-1);	}	cms->format_low_ptr = (char *) cms->subdiv_data;	cms->format_high_ptr = cms->format_low_ptr + cms->size;	/* Store the new type and size in the raw message. */	((NMLmsg *) cms->subdiv_data)->type = new_type;	((NMLmsg *) cms->subdiv_data)->size = new_size;	/* Check the list of format functions. */	if (!ignore_format_chain) {	    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("NMLwrite: format error\n");		rcs_print_error("   (Buffer = %s, Process = %s)\n",		    cms->BufferName, cms->ProcessName);		return (-1);	    }	}	/* Choose a size that will ensure the entire message will be read	   out. */	if (cms->format_size < ((long) sizeof(NMLmsg))) {	    cms->format_size = sizeof(NMLmsg);	}	if (cms->format_size > new_size) {	    ((NMLmsg *) cms->subdiv_data)->size = (long) cms->format_size;	}	cms->header.in_buffer_size = ((NMLmsg *) cms->subdiv_data)->size;	break;    default:	rcs_print_error("NML::format_input: invalid mode (%d).\n", cms->mode);	return (-1);    }    return (((int) cms->status < 0) ? -1 : 0);}int NML::run_format_chain(NMLTYPE type, void *buf){    NML_FORMAT_PTR format_function;    format_function = (NML_FORMAT_PTR) format_chain->get_head();    while (NULL != format_function) {	switch ((*format_function) (type, buf, cms)) {	case -1:	    return (-1);	case 0:	    break;	case 1:	    return (0);	}	format_function = (NML_FORMAT_PTR) format_chain->get_next();    }    return (0);}int NML::prefix_format_chain(NML_FORMAT_PTR f_ptr){    if (NULL == format_chain) {	format_chain = new LinkedList;    }    if (NULL != format_chain) {	format_chain->store_at_head((void *) f_ptr, 0, 0);    }    return (0);}/*************************************************************************** NML member function: msg2str* Parameter: NMLmsg &msg -- Reference to message to be converted into a string.* Returns: Returns a pointer to the cms->encoded_data buffer if successful* since this should contain the string or NULL if there was an error.***************************************************************************/const char *NML::msg2str(NMLmsg & msg){    return msg2str(&msg);}/*************************************************************************** NML member function: msg2str* Parameter: NMLmsg *msg -- Pointer to message to be converted into a string.* Returns: Returns a pointer to the cms->encoded_data buffer if successful* since this should contain the string or NULL if there was an error.***************************************************************************/const char *NML::msg2str(NMLmsg * nml_msg){    CMS *orig_cms = cms;    char *str = NULL;    if (NULL == nml_msg) {	return NULL;    }    if (NULL == cms) {	int msg_length = nml_msg->size;	if (NULL != cms_for_msg_string_conversions) {	    if ((cms_for_msg_string_conversions->size > 16 * msg_length &&		    cms_for_msg_string_conversions->size > 2048) ||		cms_for_msg_string_conversions->size < 4 * msg_length) {		delete cms_for_msg_string_conversions;		cms_for_msg_string_conversions = 0;	    }	}	if (NULL == cms_for_msg_string_conversions) {	    cms_for_msg_string_conversions =		new CMS(nml_msg->size * 4 + 16 + (16 - (nml_msg->size % 16)));	}	cms = cms_for_msg_string_conversions;    }    cms->set_temp_updater(CMS_DISPLAY_ASCII_ENCODING);    cms->set_mode(CMS_ENCODE);    if (-1 == format_input(nml_msg)) {	cms->restore_normal_updater();	error_type = NML_FORMAT_ERROR;	cms = orig_cms;	return ((char *) NULL);    }    cms->restore_normal_updater();    str = (char *) cms->encoded_data;    cms = orig_cms;    return (const char *) str;}/*************************************************************************** NML member function: str2msg* Parameter: NMLmsg *msg -- Pointer to message to be converted into a NMLmsg.* Returns: Returns a pointer to the cms->encoded_data buffer if successful* since this should contain the string or NULL if there was an error.***************************************************************************/NMLTYPE NML::str2msg(const char *string){    CMS *orig_cms = cms;    if (NULL == string) {	return -1;    }    if (NULL == cms) {	int string_length = strlen(string);	if (NULL != cms_for_msg_string_conversions) {	    if ((cms_for_msg_string_conversions->size > 16 * string_length &&		    cms_for_msg_string_conversions->size > 2048) ||		cms_for_msg_string_conversions->size < 4 * string_length) {		delete cms_for_msg_string_conversions;		cms_for_msg_string_conversions = 0;	    }	}	if (NULL == cms_for_msg_string_conversions) {	    cms_for_msg_string_conversions =		new CMS(string_length * 4 + 16 + (16 - (string_length % 16)));	}	cms = cms_for_msg_string_conversions;    }    cms->set_temp_updater(CMS_DISPLAY_ASCII_ENCODING);    cms->set_mode(CMS_DECODE);    strcpy((char *) cms->encoded_data, (const char *) string);    cms->status = CMS_READ_OK;    if (-1 == format_output()) {	cms->restore_normal_updater();	error_type = NML_FORMAT_ERROR;	cms = orig_cms;	return -1;    }    cms->restore_normal_updater();    cms = orig_cms;    switch (cms->status) {    case CMS_READ_OLD:	error_type = NML_NO_ERROR;	return (0);    case CMS_READ_OK:	error_type = NML_NO_ERROR;	return (((NMLmsg *) cms->subdiv_data)->type);    case CMS_TIMED_OUT:	error_type = NML_TIMED_OUT;	return -1;    case CMS_MISC_ERROR:    case CMS_NO_MASTER_ERROR:	error_type = NML_INTERNAL_CMS_ERROR;    default:	return -1;    }}static int info_message_printed = 0;char cwd_buf[256];char host_name_buf[MAXHOSTNAMELEN];char *get_ip_address(char *hostname){    struct sockaddr_in socket_address;    /* Get the IP address of the server using it's BufferHost. */    struct hostent *host_entry;    host_entry = gethostbyname(hostname);    if (NULL == host_entry) {	return "UNKNOWN";    }    socket_address.sin_addr.s_addr = *((int *) host_entry->h_addr_list[0]);    socket_address.sin_family = host_entry->h_addrtype;    return inet_ntoa(socket_address.sin_addr);}char last_bufname[10];char last_procname[10];char last_cfg_file[40];/*************************************************************************** NML member function: print_info()* Prints the buffer, process names and configuration file information.***************************************************************************/void NML::print_info(char *bufname, char *procname, char *cfg_file){    info_printed = 1;    if (!verbose_nml_error_messages) {	return;    }    if (NULL == cms || error_type != NML_NO_ERROR) {	if (max_rcs_errors_to_print <= rcs_errors_printed &&	    max_rcs_errors_to_print >= 0) {	    return;	}    }    if (error_type == NML_QUEUE_FULL_ERROR && !cms_print_queue_full_messages) {	return;    }    if (NULL != cms) {	if (cms->status < 0) {	    if (max_rcs_errors_to_print <= rcs_errors_printed &&		max_rcs_errors_to_print >= 0) {		return;	    }	}    }    if (NULL != bufname && NULL != procname && NULL != cfg_file) {	if (!strncmp(bufname, last_bufname, 10)	    && !strncmp(procname, last_procname, 10)	    && !strncmp(cfg_file, last_cfg_file, 40)) {	    return;	}	strncpy(last_bufname, bufname, 10);	strncpy(last_procname, procname, 10);	strncpy(last_cfg_file, cfg_file, 40);    }    if (!info_message_printed) {	rcs_print	    ("\n**********************************************************\n");	rcs_print("* Current Directory = %s\n", getcwd(cwd_buf, 256));	if (nml_print_hostname_on_error) {	    gethostname(host_name_buf, MAXHOSTNAMELEN);	    if (host_name_buf[0] != 0) {		rcs_print("* Host = %s\n", host_name_buf);	    }	}	rcs_print("* ");	info_message_printed = 1;    }    rcs_print	("\n**********************************************************\n");    if (NULL != cms) {	rcs_print("* BufferName = %s\n", cms->BufferName);	rcs_print("* BufferType = %d\n", cms->BufferType);	rcs_print("* ProcessName = %s\n", cms->ProcessName);	rcs_print("* Configuration File = %s\n", cfgfilename);	rcs_print("* CMS Status = %d (%s)\n", cms->status,	    cms->status_string(cms->status));	rcs_print("* Recent errors repeated:\n");	rcs_print("%s\n", last_error_bufs[0]);	rcs_print("%s\n", last_error_bu

⌨️ 快捷键说明

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