Example #1
0
/** Create an object
    \verbatim gl('create', class, property, value, ...) \endverbatim
 **/
void cmex_create(int nlhs, mxArray *plhs[], /**< {properties} */
				 int nrhs, const mxArray *prhs[]) /**< (class, property1, value1, property2, value2, ..., propertyN, valueN) */
{
	CLASS *oclass;
	OBJECT *obj;
	CLASSNAME classname;
	unsigned int num_properties = (nrhs-1)/2;

	/* check return value */
	if (nlhs>1)
		output_error("create only returns one struct");
	/* check class name */
	else if (!mxIsChar(prhs[0]))
		output_error("class name (arg 1) must be a string");

	/* get class name */
	else if (mxGetString(prhs[0],classname,sizeof(classname)))
		output_error("unable to read class name (arg 1)");

	/* check the number of properties */
	else if (num_properties!=(nrhs/2))
		output_error("an object property value is missing");

	/* create object */
	else if ((oclass=class_get_class_from_classname(classname))==NULL)
		output_error("class '%s' is not registered", classname);

	/* create the object */
	else if (oclass->create(&obj,NULL)==FAILED)
		output_error("unable to create object of class %s", classname);

	/* copy properties */
	else
	{
		unsigned int i;
		for (i=0; i<num_properties; i++)
		{
			char name[256];
			char value[1024];
			unsigned int n=i*2+1;
			const mxArray *p[] = {prhs[n],prhs[n+1]};
			if (!mxIsChar(p[0]))
				output_error("property name (arg %d) must be a string", n);
			else if (mxGetString(p[0],name,sizeof(name)))
				output_error("property name (arg %d) couldn't be read", n);
			else if (mxIsChar(p[1]) && mxGetString(p[1],value,sizeof(value)))
				output_error("property %s (arg %d) value couldn't be read", name, n);
			else if (mxIsDouble(p[1]) && sprintf(value,"%lg",*(double*)mxGetPr(p[1]))<1)
				output_error("property %s (arg %d) value couldn't be converted from double", name, n);
			else if (mxIsComplex(p[1]) && sprintf(value,"%lg%+lgi",*(double*)mxGetPr(p[1]),*(double*)mxGetPi(p[1]))<1)
				output_error("property %s (arg %d) value couldn't be converted from complex", name, n);
			else if (object_set_value_by_name(obj,name,value)==0)
				output_error("property %s (arg %d) couldn't be set to '%s'", name, n, value);
			else if (nlhs>0 && (plhs[0]=get_object_data(obj))==NULL)
				output_error("couldn't get object data for %s", name);
		}
	}
}
Example #2
0
static void set_object_data(const mxArray *data)
{
	OBJECT *obj;
	mxArray *pId = mxGetField(data,0,"id");
	char id[256];
	if (pId==NULL)
		output_error("set_object_data(const mxArray *data={...}) did not find a required object id field");
	else if (mxGetString(pId,id,sizeof(id)))
		output_error("set_object_data(const mxArray *data={...}) couldn't read the object id field");
	else if ((obj=object_find_name(id))==NULL)
		output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't find object id", id);
	else
	{
		int i;
		for (i=0; i<mxGetNumberOfFields(data); i++)
		{	
			const char *name;
			const mxArray *pField = mxGetFieldByNumber(data,0,i);
			char value[4096];
			if ((name=mxGetFieldNameByNumber(data,i))==NULL)
				output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't read the name of field %d", id, i);
			else if (strcmp(name,"id")==0 || strcmp(name,"class")==0)
			{	/* these may not be changed */ }
			else if (pField==NULL)
				output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't read the object field '%s' for object '%s'", id, name);
			else if (mxIsChar(pField) && mxGetString(pField,value,sizeof(value)))
				output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't read the string value '%s' from field '%s'", id, value, name);
			else if (mxIsDouble(pField) && sprintf(value,"%lg",*(double*)mxGetPr(pField))<1)
				output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't read the double value '%lg' from field '%s'", id, *(double*)mxGetPr(pField), name);
			else if (mxIsComplex(pField) && sprintf(value,"%lg%+lgi",*(double*)mxGetPr(pField),*(double*)mxGetPi(pField))<1)
				output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't read the complex value '%lg%+lgi' from field '%s'", id, *(double*)mxGetPr(pField), *(double*)mxGetPi(pField), name);
			else if (mxIsUint32(pField) && sprintf(value,"%lu",*(unsigned int*)mxGetPr(pField))<1)
				output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't read the uint32 value '%lu' from field '%s'", id, *(unsigned int*)mxGetPr(pField), name);
			else if (strcmp(value,ERROR)==0)
				output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't use error value '%s'", id, value);
			else if (strcmp(value,NONE)==0 && strcpy(value,"")==NULL)
				output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't clear empty value '%s'", id, value);
			else if (!object_set_value_by_name(obj,name,value))
				output_error("set_object_data(const mxArray *data={id='%s',...}) couldn't read the value '%s' into property '%s'", id, value, name);
		}
	}
}
Example #3
0
/** Process an incoming XML data request
	@returns non-zero on success, 0 on failure (errno set)
 **/
int http_xml_request(HTTP *http,char *uri)
{
	char arg1[1024]="", arg2[1024]="";
	int nargs = sscanf(uri,"%1023[^/=\r\n]/%1023[^\r\n=]",arg1,arg2);
	char *value = strchr(uri,'=');
	char buffer[1024]="";
	OBJECT *obj=NULL;
	char *id;

	/* value */
	if (value) *value++;

	/* decode %.. */
	http_decode(arg1);
	http_decode(arg2);
	if (value) http_decode(value);

	/* process request */
	switch (nargs) {

	/* get global variable */
	case 1: 

		/* find the variable */
		if (global_getvar(arg1,buffer,sizeof(buffer))==NULL)
		{
			output_error("global variable '%s' not found", arg1);
			return 0;
		}

		/* assignment, if any */
		if (value) global_setvar(arg1,value);
		
		/* post the response */
		http_format(http,"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
		http_format(http,"<globalvar>\n\t<name>%s</name>\n\t<value>%s</value>\n</globalvar>\n",
			arg1, http_unquote(buffer));
		http_type(http,"text/xml");
		return 1;

	/* get object property */
	case 2:

		/* find the object */
		id = strchr(arg1,':');
		if ( id==NULL )
			obj = object_find_name(arg1);
		else
			obj = object_find_by_id(atoi(id+1));
		if ( obj==NULL )
		{
			output_error("object '%s' not found", arg1);
			return 0;
		}

		/* post the current value */
		if ( !object_get_value_by_name(obj,arg2,buffer,sizeof(buffer)) )
		{
			output_error("object '%s' property '%s' not found", arg1, arg2);
			return 0;
		}

		/* assignment, if any */
		if ( value && !object_set_value_by_name(obj,arg2,value) )
		{
			output_error("cannot set object '%s' property '%s' to '%s'", arg1, arg2, value);
			return 0;
		}

		/* post the response */
		http_format(http,"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<property>\n");
		http_format(http,"\t<object>%s</object>\n", arg1);
		http_format(http,"\t<name>%s</name>\n", arg2);
		http_format(http,"\t<value>%s</value>\n", http_unquote(buffer));
		/* TODO add property type info */
		http_format(http,"</property>\n");
		http_type(http,"text/xml");
		return 1;

	default:
		return 0;
	}
	return 0;
}
Example #4
0
STATUS original_test_start(int argc, char *argv[])
{
	OBJECT *obj[6];
	MODULE *network;
	CLASS *node, *link;
	MODULE *tape;
	CLASS *player, *recorder, *collector;

	network = module_load("network",argc,argv);
	if (network==NULL)
	{
#ifndef WIN32
		fprintf(stderr,"%s\n",dlerror());
#else
		perror("network module load failed");
#endif
		return FAILED;
	}
	output_verbose("network module loaded ok");

	node = class_get_class_from_classname("node");
	if (node==NULL)
	{
		output_fatal("network module does not implement class node");
		/*	TROUBLESHOOT
			The <b>network</b> module test can't find the <b>node</b>
			class definition.  This is probably caused by either
			an internal system error or a version of the network module
			that doesn't implement node object as expected (or at all).
		 */
		return FAILED;
	}
	output_verbose("class node implementation loaded ok");

	link = class_get_class_from_classname("link");
	if (node==NULL || link==NULL)
	{
		output_fatal("network module does not implement class link");
		/*	TROUBLESHOOT
			The <b>network</b> module test can't find the <b>link</b>
			class definition.  This is probably caused by either
			an internal system error or a version of the network module
			that doesn't implement link object as expected (or at all).
		 */
		return FAILED;
	}
	output_verbose("class link implementation loaded ok");

	tape = module_load("tape",argc,argv);
	if (tape==NULL)
	{
#ifndef WIN32
		fprintf(stderr,"%s\n",dlerror());
#else
		perror("tape module load failed");
#endif
		return FAILED;
	}

	player = class_get_class_from_classname("player");
	if (player==NULL)
	{
		output_fatal("tape module does not implement class player");
		/*	TROUBLESHOOT
			The <b>tape</b> module test can't find the <b>player</b>
			class definition.  This is probably caused by either
			an internal system error or a version of the tape module
			that doesn't implement player object as expected (or at all).
		 */
		return FAILED;
	}
	recorder = class_get_class_from_classname("recorder");
	if (recorder==NULL)
	{
		output_fatal("tape module does not implement class recorder");
		/*	TROUBLESHOOT
			The <b>tape</b> module test can't find the <b>recorder</b>
			class definition.  This is probably caused by either
			an internal system error or a version of the tape module
			that doesn't implement recorder object as expected (or at all).
		 */
		return FAILED;
	}
	collector = class_get_class_from_classname("collector");
	if (collector==NULL)
	{
		output_fatal("tape module does not implement class collector");
		/*	TROUBLESHOOT
			The <b>tape</b> module test can't find the <b>collector</b>
			class definition.  This is probably caused by either
			an internal system error or a version of the tape module
			that doesn't implement collector object as expected (or at all).
		 */
		return FAILED;
	}

	if (module_import(network,"../test/pnnl2bus.cdf")<=0)
		return FAILED;

	/* tape player */
	if ((*player->create)(&obj[3],object_get_first())==FAILED)
	{
		output_fatal("player creation failed");
		/*	TROUBLESHOOT
			The <b>tape</b> module test can't create a <b>player</b>
			object.  This is probably caused by either
			an internal system error or a version of the tape module
			that doesn't implement player object as expected (or at all).
		 */
		return FAILED;
	}
	object_set_value_by_name(obj[3],"loop","3600"); /* 18000 is about 12y at 1h steps */
	object_set_value_by_name(obj[3],"property","S");

	/* tape recorder */
	if ((*recorder->create)(&obj[4],object_get_first())==FAILED)
	{
		output_fatal("recorder creation failed");
		/*	TROUBLESHOOT
			The <b>tape</b> module test can't create a <b>recorder</b>
			object.  This is probably caused by either
			an internal system error or a version of the tape module
			that doesn't implement recorder object as expected (or at all).
		 */
		return FAILED;
	}
	object_set_value_by_name(obj[4],"property","V,S");
	object_set_value_by_name(obj[4],"interval","0");
	object_set_value_by_name(obj[4],"limit","1000");

	/* tape collector */
	if ((*collector->create)(&obj[5],NULL)==FAILED)
	{
		output_fatal("collector creation failed");
		/*	TROUBLESHOOT
			The <b>tape</b> module test can't create a <b>collector</b>
			object.  This is probably caused by either
			an internal system error or a version of the tape module
			that doesn't implement collector object as expected (or at all).
		 */
		return FAILED;
	}
	object_set_value_by_name(obj[5],"property","count(V.mag),min(V.mag),avg(V.mag),std(V.mag),max(V.mag),min(V.ang),avg(V.ang),std(V.ang),max(V.ang)");
	object_set_value_by_name(obj[5],"interval","0");
	object_set_value_by_name(obj[5],"limit","1000");
	object_set_value_by_name(obj[5],"group","class=node;");

	module_check(network);

	return SUCCESS;
}
char *gld_loadHndl::read_object_prop(char *buffer, size_t len){
	//wchar_t wbuff[1024];
	//char tbuff[1024];
	char *rand_ptr = NULL;
	char *rand_mode_ptr = NULL;
	char *unit_ptr = NULL;
	double realval = 0.0;
	UNIT *unit=NULL;
	SAXParseException *e = NULL;
	void *addr = object_get_addr(obj, propname); /* get the & to set later */
	if(this->obj == NULL){
		sprintf(errmsg, "Null object pointer in read_object_prop(%s)", buffer);
		return errmsg;	//	no object
	}
	if(this->prop == NULL){
		if (strcmp(propname, "parent")==0){
			if (strcmp(propname, "root")==0){
				obj->parent = NULL;
			} else {
				add_unresolved(obj,PT_object,(void*)&obj->parent,oclass,buffer,"XML",42,UR_RANKS); 
			}
		} else if (strcmp(propname, "rank")==0){
			obj->rank = atoi(buffer);
		} else if (strcmp(propname, "clock")==0){
			obj->clock = atoi64(buffer);
		} else if (strcmp(propname, "latitude")==0){
			obj->latitude = load_latitude(buffer);
		} else if (strcmp(propname, "longitude")==0){
			obj->longitude = load_longitude(buffer);
		} else if (strcmp(propname, "in")==0){
			obj->in_svc = convert_to_timestamp(buffer);
		} else if (strcmp(propname, "out")==0){
			obj->out_svc = convert_to_timestamp(buffer);
		} else {
			sprintf(errmsg, "Null property pointer in read_object_prop(%s)", buffer);
			return errmsg;
		}
		return NULL;
	}
	//	determine property type
	switch(prop->ptype){
		case PT_double:
			//	scan for "random"
			if(strncmp("random.", buffer, 7) == 0){
				char temp[256];
				char *modep = NULL;
				double first = 0.0, second = 0.0;
				RANDOMTYPE rt;
				strncpy(temp, buffer, 256);
				modep = strtok(temp+7, "(");
				if(modep == NULL){
					//output_error("XML_Load: misformed random() value");
					load_state = false;
					sprintf(errmsg, "Misformed random() value in read_object_prop(%s)", buffer);
					return errmsg;
				}
				rt = random_type(modep);
				if(rt == 0){
					//output_message("XML_Load: '%s' ~ %s is not a valid random distribution", buffer, modep);
					load_state = false;
					sprintf(errmsg, "Invalid random distribution in read_object_prop(%s)", buffer);
					return errmsg;
				} else {
					first = atof(strtok(NULL, ","));
					second = atof(strtok(NULL, ")"));
					realval = random_value(rt, first, second);
				}
				if(strlen(strchr(buffer, ')')+1) > 0){ /* look for units */
					unit = unit_find(strchr(buffer, ')') + 2);
					if (unit!=NULL && prop->unit!=NULL && unit_convert_ex(unit,prop->unit,&realval)==0){
						sprintf(errmsg, "Cannot convert units from %s to %s in read_object_prop(%s)", unit->name,prop->unit->name, buffer);
						load_state = false;
						return errmsg;
					}
				}
			} else {
				unit_ptr = NULL;
				realval = strtod(buffer, &unit_ptr);
				if(unit_ptr != NULL){
					while(*unit_ptr == ' ') ++unit_ptr;
					unit = unit_find(unit_ptr);
					if(strlen(unit_ptr) > 0){
						if (unit!=NULL && prop->unit!=NULL && unit_convert_ex(unit,prop->unit,&realval)==0){
							sprintf(errmsg, "Cannot convert units from %s to %s in read_object_prop(%s)", unit->name,prop->unit->name, buffer);
							load_state = false;
							return errmsg;
						}
					}
				}
			}
			/* if((unit_ptr != NULL) && (*unit_ptr != '\0')){;} */
			if(object_set_double_by_name(obj, propname, realval) == 0){
				sprintf(errmsg, "Could not set \"%s\" to %f in read_object_prop(%s)", propname, realval, buffer);
				load_state = false;
				return errmsg;
			} else {
				return NULL; /* success */
			}
			break;
		case PT_object:
			if(add_unresolved(obj,PT_object,(void*)addr,oclass,buffer,"XML",42,UR_NONE) == NULL){
				sprintf(errmsg, "Failure with add_unresolved() in read_object_prop(%s)", buffer);
				return errmsg;
			}
			break;
		default:
			if(prop->ptype < _PT_LAST){	//	set value by name
				if (object_set_value_by_name(obj, propname, buffer)==0)	{
					//output_error("XML_Load: property %s of %s:%s could not be set to '%s'", propname, obj->oclass->name, obj->id, buffer);
					sprintf(errmsg, "Property %s of %s:%i could not be set to \"%s\" in read_object_prop()", propname, obj->oclass->name, obj->id, buffer);
					load_state = false;
					return errmsg;
				} else {
					;
				}
			} else {
				sprintf(errmsg, "Invalid property id = %i in read_object_prop(%s)", prop->ptype, buffer);
				return errmsg;
			}
	}
	return 0;
}
Example #6
0
/** Set a value
	\verbatim gl('set',id,property,value) \endverbatim
 **/
void cmex_set(int nlhs, mxArray *plhs[], /**< () */
				int nrhs, const mxArray *prhs[]) /**< (object_id,property_name,value) */
{
	if (nlhs>0)
		output_error("set does not return a value");
	else if (nrhs==1 && mxIsStruct(prhs[0]))
		set_object_data(prhs[0]);
	else if (nrhs!=3)
		output_error("set requires either a structure, or object id, property name and value");
	else if (!mxIsChar(prhs[0]))
		output_error("object id (arg 0) must be a string");
	else if (mxIsChar(prhs[1]))
	{
		char value[1024];
		PROPERTY* prop;
		OBJECT *obj=NULL;
		GLOBALVAR *var=NULL;
		if (mxGetString(prhs[0],value,sizeof(value))!=0)
			output_error("object name (arg 0) too long");
		else if (strcmp(value,"global")==0)
		{
			if (!mxIsChar(prhs[1]))
				output_error("global variable name is not a string");
			else if (!mxIsChar(prhs[2]))
				output_error("global value is not is not a string");
			else
			{
				char name[32], value[1024];
				mxGetString(prhs[1],name,sizeof(name));
				mxGetString(prhs[2],value,sizeof(value));
				if (global_setvar(name,value)!=SUCCESS)
					output_error("unable to set global '%s' to '%s'", name,value);
			}
		}
		else if (convert_to_object(value,&obj,NULL)==0)
			output_error("object (arg 0) %s not found",value);
		else if (mxGetString(prhs[1],value,sizeof(value))!=0)
			output_error("property name (arg 1) too long");
		else if ((prop=object_get_property(obj,value))==NULL)
			output_error("property name (arg 1) %s not found in object %s:%d", value,obj->oclass->name, obj->id);
		else if (mxIsChar(prhs[2]))
		{
			if (mxGetString(prhs[2],value,sizeof(value))!=0)
				output_error("value (arg 2) is too long");
			else if (object_set_value_by_name(obj,prop->name,value)==0)
				output_error("unable to set %s:%d/%s to %s", obj->oclass->name, obj->id, prop->name, value);
			else
				return; /* ok */
		}
		else if (mxIsDouble(prhs[2]) && prop->ptype==PT_double)
		{
			double v = *mxGetPr(prhs[2]);
			if (prop->ptype==PT_double && object_set_double_by_name(obj,prop->name,v)==0)
				output_error("unable to set %s:%d/%s to %lg", obj->oclass->name, obj->id, prop->name, v);
		}
		else if (mxIsComplex(prhs[2]) || mxIsDouble(prhs[2]))
		{
			sprintf(value,"%lg%+lgi",*mxGetPr(prhs[2]),mxIsComplex(prhs[2])?*mxGetPi(prhs[2]):0);
			if (object_set_value_by_name(obj,prop->name,value)==0)
				output_error("unable to set %s:%d/%s to %s", obj->oclass->name, obj->id, prop->name, value);
		}
		else 
			output_error("value (arg 2) has an unsupported data type");
	}
	else
		output_error("property or data (arg 1) type is not valid");
}