示例#1
0
文件: cmpt.c 项目: OpenJAUS/openjaus
// Function: 	cmptStartup
// Access:		Public	
// Description: This function allows the abstracted component functionality contained in this file to be started from an external source.
// 				It must be called first before the component state machine and node manager interaction will begin
//				Each call to "cmptStartup" should be followed by one call to the "cmptShutdown" function
int cmptStartup(void)
{
	FILE * propertyFile;
	pthread_attr_t attr;	// Thread attributed for the component threads spawned in this function
	
	if(cmpt->state == JAUS_SHUTDOWN_STATE)	// Execute the startup routines only if the component is not running
	{
		propertyFile = fopen("./config/cmpt.conf", "r");
		if(propertyFile)
		{
			cmptProperties = propertiesCreate();
			cmptProperties = propertiesLoad(cmptProperties, propertyFile);
			fclose(propertyFile);
		}
		else
		{
			cError("cmpt: Cannot find or open properties file\n");
			return CMPT_LOAD_CONFIGURATION_ERROR;
		}
		
		// Check in to the Node Manager and obtain Instance, Node and Subsystem IDs
		cmptNode = jausNodeCreate();
		cmptSubsystem = jausSubsystemCreate();
		cmptNode->subsystem = cmptSubsystem;
		cmpt->node = cmptNode;
		cmpt->services = jausServicesCreate();
		cmpt->state = JAUS_INITIALIZE_STATE; // Set the state of the JAUS state machine to INITIALIZE
		
		cmptNmi = nodeManagerOpen(cmpt); 
		if(cmptNmi == NULL)
		{
			cError("cmpt: Could not open connection to node manager\n");
			return CMPT_NODE_MANAGER_OPEN_ERROR; 
		}

		pthread_attr_init(&attr);
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

		cmptRun = TRUE;

		if(pthread_create(&cmptThreadId, &attr, cmptThread, NULL) != 0)
		{
			cError("cmpt: Could not create cmptThread\n");
			cmptShutdown();
			pthread_attr_destroy(&attr);
			return CMPT_THREAD_CREATE_ERROR;
		}
		pthread_attr_destroy(&attr);
	}
	else
	{
		cError("cmpt: Attempted startup while not shutdown\n");
		return CMPT_STARTUP_BEFORE_SHUTDOWN_ERROR;
	}
	
	return 0;
}
示例#2
0
文件: ojCmpt.c 项目: pfg/qgc
OjCmpt ojCmptCreate(char *name, JausByte id, double stateFrequencyHz)
{
	OjCmpt ojCmpt;
	int i;

	ojCmpt = (OjCmpt)malloc(sizeof(struct OjCmptStruct));

	ojCmpt->jaus = jausComponentCreate();
	ojCmpt->jaus->identification = (char *)calloc(strlen(name) + 1, 1);
	strcpy(ojCmpt->jaus->identification, name);
	ojCmpt->jaus->address->component = id;
	ojCmptSetState(ojCmpt, JAUS_UNDEFINED_STATE);
	ojCmptSetFrequencyHz(ojCmpt, stateFrequencyHz);

	for(i=0; i<OJ_CMPT_MAX_STATE_COUNT; i++)
	{
		ojCmpt->stateCallback[i] = NULL;
	}
	ojCmpt->mainCallback = NULL;
	ojCmpt->processMessageCallback = NULL;
	ojCmpt->messageCallback = NULL;
	ojCmpt->messageCallbackCount = 0;
	
	ojCmpt->run = FALSE;

	if (!jausServiceAddCoreServices(ojCmpt->jaus->services))	// Add core services
	{
		free(ojCmpt->jaus->identification);
		ojCmpt->jaus->identification = NULL;
		jausComponentDestroy(ojCmpt->jaus);
		free(ojCmpt);
		return NULL;
	}

	for(i=0; i<OJ_CMPT_MAX_INCOMING_SC_COUNT; i++)
	{
		ojCmpt->inConnection[i] = NULL;
	}
	
	ojCmpt->nmi = nodeManagerOpen(ojCmpt->jaus);
	if(ojCmpt->nmi == NULL)
	{
		free(ojCmpt->jaus->identification);
		ojCmpt->jaus->identification = NULL;
		jausComponentDestroy(ojCmpt->jaus);
		free(ojCmpt);
		return NULL; 
	}

	return ojCmpt;
}
示例#3
0
文件: wd.c 项目: OpenJAUS/openjaus
// Function: 	wdStartup
// Access:		Public	
// Description: This function allows the abstracted component functionality contained in this file to be started from an external source.
// 				It must be called first before the component state machine and node manager interaction will begin
//				Each call to "wdStartup" should be followed by one call to the "wdShutdown" function
int wdStartup(void)
{
	FILE * propertyFile;
	pthread_attr_t attr;	// Thread attributed for the component threads spawned in this function
	char fileName[128] = {0};

	if(!wd)
	{
		wd = jausComponentCreate();
		wd->address->component = JAUS_GLOBAL_WAYPOINT_DRIVER;
		wd->identification  = "Waypoint Driver";
		wd->state = JAUS_SHUTDOWN_STATE;
	}

	if(wd->state == JAUS_SHUTDOWN_STATE)	// Execute the startup routines only if the component is not running
	{
		sprintf(fileName, "%swd.conf", CONFIG_DIRECTORY);
		propertyFile = fopen(fileName, "r");
		if(propertyFile)
		{
			wdProperties = propertiesCreate();
			wdProperties = propertiesLoad(wdProperties, propertyFile);
			fclose(propertyFile);
		}
		else
		{
			//cError("wd: Cannot find or open properties file\n");
			return WD_LOAD_CONFIGURATION_ERROR;
		}
		
		// Check in to the Node Manager and obtain Instance, Node and Subsystem IDs
		wdNode = jausNodeCreate();
		wdSubsystem = jausSubsystemCreate();
		wdNode->subsystem = wdSubsystem;
		wd->node = wdNode;
		wd->services = jausServicesCreate();
		wd->state = JAUS_INITIALIZE_STATE; // Set the state of the JAUS state machine to INITIALIZE
		
		wdNmi = nodeManagerOpen(wd); 
		if(wdNmi == NULL)
		{
			//cError("wd: Could not open connection to node manager\n");
			return WD_NODE_MANAGER_OPEN_ERROR; 
		}

		pthread_attr_init(&attr);
		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

		wdRun = TRUE;

		if(pthread_create(&wdThreadId, &attr, wdThread, NULL) != 0)
		{
			//cError("wd: Could not create wdThread\n");
			wdShutdown();
			pthread_attr_destroy(&attr);
			return WD_THREAD_CREATE_ERROR;
		}
		pthread_attr_destroy(&attr);
	}
	else
	{
		//cError("wd: Attempted startup while not shutdown\n");
		return WD_STARTUP_BEFORE_SHUTDOWN_ERROR;
	}
	
	return 0;
}