Example #1
0
	void test_cycle_buffer(){
		OPH();
		DEBUG("testing cycle buffer ......");
		char data[1024];
		for(uint64_t i=0; i<sizeof(data); ++i){
			data[i] =i;
		}

		CycleBuffer *cb =SafeNew<CycleBuffer>();
		cb->setCapacity(10240);
		cb->push(data, sizeof(data));

		char data1[1024] ={0};
		cb->pop(data1, 1024);
		CHECK_EXIT(0==memcmp(data1, data, sizeof(data)), 1);


		memset(data1, 0, sizeof(data1));
		cb->push(data, sizeof(data));
		cb->pop(data1, 512);
		cb->pop(data1+512, 512);
		CHECK_EXIT(0==memcmp(data1, data, sizeof(data)), 1);
	}
Example #2
0
	/** load unload **/
	bool ApplicationBase::on_load(const int argc, const char** argv){
		OPH();
		// prepare path
		if(argc > 1){
			INFO("data path is %s", argv[1]);
			setDataPath(STR(argv[1]));
		}
		else{
			INFO("data path is ../data/");
			setDataPath(STR("../data/"));
		}
		// load
		core::Network* net =core::Network::Instance();
		bool ok =net->load(_config_path("startup/network.lua"));
		CHECK_EXIT(ok && "load network failed", 1);

		ok =ServiceManager::Instance()->load(_config_path("startup/service_manager.lua"));
		CHECK_EXIT(ok && "load service manager failed", 1);

		ok =ActorManager::Instance()->load(_config_path("startup/actor_manager.lua"));
		CHECK_EXIT(ok && "load actor manager failed", 1);

		return ok;
	}
Example #3
0
DAE_animation* DaeLoadAnimations(ge_File* fp, int* _nAnims){
	int nAnims = 0;
	DAE_animation* anims = NULL;
	char buf[4096] = "";

	while(geFileGets(fp, buf, 4096)){
		CHECK_EXIT("</library_animations>");
		if(strstr(buf, "<animation") && !strstr(buf, "<animation>")){
			anims = (DAE_animation*)geRealloc(anims, sizeof(DAE_animation)*(nAnims+1));
			memset(&anims[nAnims], 0x0, sizeof(DAE_animation));
			DaeLoadAnimation(fp, buf, &anims[nAnims]);
			nAnims++;
		}
	}

	*_nAnims = nAnims;
	return anims;
}
Example #4
0
void DaeLoadAnimation(ge_File* fp, char* buf, DAE_animation* anim){
	geGetParamString(buf, "id", anim->id, DAE_ID_LEN);
	geGetParamString(buf, "name", anim->name, DAE_ID_LEN);
	gePrintDebug(0x100, "  Animation id=\"%s\", name=\"%s\"\n", anim->id, anim->name);
	while(geFileGets(fp, buf, 4096)){
		CHECK_EXIT("</animation>");
		if(strstr(buf, "<source")){
			anim->sources = (DAE_source*)geRealloc(anim->sources, sizeof(DAE_source)*(anim->nSources+1));
			memset(&anim->sources[anim->nSources], 0x0, sizeof(DAE_source));
			DaeLoadSource(fp, buf, &anim->sources[anim->nSources]);
			anim->nSources++;
		}
		if(strstr(buf, "<sampler")){
			geGetParamString(buf, "id", anim->sampler.id, DAE_ID_LEN);
			gePrintDebug(0x100, "  Sampler id=\"%s\"\n", anim->sampler.id);
			DaeLoadInputs(fp, "</sampler>", &anim->sampler.inputs, &anim->sampler.nInputs);
		}
		if(strstr(buf, "<channel")){
			geGetParamString(buf, "source", anim->channel.source, DAE_ID_LEN);
			geGetParamString(buf, "target", anim->channel.target, DAE_ID_LEN);
			gePrintDebug(0x100, "  Channel source=\"%s\", target=\"%s\"\n", anim->channel.source, anim->channel.target);
		}
	}
}
Example #5
0
	void test_array(){
		OPH();
		DEBUG("testing array ......");

		Array* a =SafeNew<Array>();
		for(int64_t i=0; i<14; ++i){
			a->push_back(SafeNew<Int32>());
		}
		for(int64_t i=0; i<14; ++i){
			a->pop_back();
		}
		ASSERT(a->empty());
		
		// common array
		{
			Array* arr =SafeNew<Array>();

			// push_back, size, pop_front, front
			for(int i=0; i<100; ++i){
				arr->push_back(String::Format("%d", i));
			}
			for(int i=0; i<100; ++i){
				CHECK_EXIT(((String*)(arr->front()))->is(String::Format("%d", i)), 1);
				arr->pop_front();
			}
			CHECK_EXIT(arr->size()==0, 1);

			// push_front, size, pop_back, back
			for(int i=99; i>=0; --i){
				arr->push_front(String::Format("%d", i));
			}
			CHECK_EXIT(arr->size()==100, 1);
			for(int i=99; i>=0; --i){
				CHECK_EXIT(((String*)(arr->back()))->is(String::Format("%d", i)), 1);
				arr->pop_back();
			}
			CHECK_EXIT(arr->size()==0, 1);

			// insert, remove
			for(int i=0; i<100; ++i){
				arr->push_back(SafeNew<Int64, int64_t>(i));
			}
			arr->push_front(SafeNew<Int64, int64_t>(-1));
			arr->push_back(SafeNew<Int64, int64_t>(100));
			for(int i=0; i<102; ++i){
				CHECK_EXIT(((Int64*)(arr->get(i)))->getValue() == i-1, 1);
			}
			arr->insert(50, SafeNew<Int64, int64_t>(9999));
			CHECK_EXIT(((Int64*)(arr->get(50)))->getValue() == 9999, 1);
			CHECK_EXIT(((Int64*)(arr->get(51)))->getValue() == 49, 1);
			CHECK_EXIT(((Int64*)(arr->get(49)))->getValue() == 48, 1);
			arr->remove(102);
			arr->remove(50);
			arr->remove(0);
			for(int i=0; i<100; ++i){
				CHECK_EXIT(((Int64*)(arr->front()))->getValue() == i, 1);
				arr->pop_front();
			}
			CHECK_EXIT(arr->size()==0, 1);
		}

		// int64 array
		{
			Int64Array* arr =SafeNew<Int64Array>();

			// push_back, size, pop_front, front
			for(int i=0; i<100; ++i){
				arr->push_back(i);
			}
			for(int i=0; i<100; ++i){
				CHECK_EXIT(arr->front() == i, 1);
				arr->pop_front();
			}
			CHECK_EXIT(arr->size()==0, 1);

			// push_front, size, pop_back, back
			for(int i=99; i>=0; --i){
				arr->push_front(i);
			}
			CHECK_EXIT(arr->size()==100, 1);
			for(int i=99; i>=0; --i){
				CHECK_EXIT(arr->back() == i, 1);
				arr->pop_back();
			}
			CHECK_EXIT(arr->size()==0, 1);

			// insert, remove
			for(int i=0; i<100; ++i){
				arr->push_back(i);
			}
			arr->push_front(-1);
			arr->push_back(100);
			for(int i=0; i<102; ++i){
				CHECK_EXIT(arr->get(i) == i-1, 1);
			}
			arr->insert(50, 9999);
			CHECK_EXIT(arr->get(50) == 9999, 1);
			CHECK_EXIT(arr->get(51) == 49, 1);
			CHECK_EXIT(arr->get(49) == 48, 1);
			arr->remove(102);
			arr->remove(50);
			arr->remove(0);
			for(int i=0; i<100; ++i){
				CHECK_EXIT(arr->front() == i, 1);
				arr->pop_front();
			}
			CHECK_EXIT(arr->size()==0, 1);
		}
	}
Example #6
0
int main( int argc, const char* argv[] ){
	MQTTAsync 					client;
	MQTTAsync_connectOptions 	conn_opts = MQTTAsync_connectOptions_initializer;
	MQTTAsync_disconnectOptions disc_opts = MQTTAsync_disconnectOptions_initializer;
	MQTTAsync_message 			pubmsg 	  = MQTTAsync_message_initializer;

	int 			rc = ICP_SUCCESS;
	char 			str[128];
	char 			topic[128];
	char 			ch = 'a';
	char 			cmd[128] = "";
	char 			sub_cmd[8][8];
	char 			*pch;
	int 			i;

	mac_addr_t 		receiver;

	printf("\n\n\n\n\n");

	// MQTT setup, connection and subscribe's
	MQTTAsync_create(&client, ADDRESS, mac_addrs[local_mac_addr], MQTTCLIENT_PERSISTENCE_NONE, NULL);
	MQTTAsync_setCallbacks(client, NULL, connlost, msgarrvd, NULL);
	
	p_client = &client;
	CHECK_MEM(p_client);
	mqtt_enable_debug_output(TRUE);

	conn_opts.keepAliveInterval = 20;
	conn_opts.cleansession = 1;
	conn_opts.onSuccess = onConnect;
	conn_opts.onFailure = onConnectFailure;
	conn_opts.context = client;
	if ((rc = MQTTAsync_connect(client, &conn_opts)) != MQTTASYNC_SUCCESS){
		MQTT_DEBUG("Failed to start connect, return code %d\n", rc);
		exit(-1);	
	}
	while	(!connected);

	// CHECK_ERR(print_packet(NULL) == -1, "failed with p_packet=NULL");
	// CHECK_ERR(print_packet(&packet) == 0, "failed with valid args");

 // 	CHECK_ERR(icp_bytes_out_cb( (const uint8_t *)NULL, 10, NULL) == 0, "failed with output_byte_array=NULL");
 // 	CHECK_ERR(icp_bytes_out_cb( (const uint8_t *)str, 0, NULL) == 0, "failed with array_len=0");

 // 	strcpy(str, "Hello");
 // 	CHECK_ERR(icp_bytes_out_cb( (const uint8_t *)str, 10, NULL) == 0, "failed with array_len:10!=strlen(output_byte_array):%s", str);

 // 	strcpy(str, "Hello");
 // 	CHECK_ERR(icp_bytes_out_cb( (const uint8_t *)str, strlen(str), NULL) == strlen(str), "failed with valid args");
   
	// CHECK_ERR(icp_packet_received_cb(NULL, NULL) == -1, "failed with p_packet=NULL");
	// CHECK_ERR(icp_packet_received_cb(&packet, NULL) == 0, "failed with valid args");

	// CHECK_ERR(icp_error_cb(ICP_SUCCESS, packet.data, packet.data_length, NULL) == -1, "failed with invalid packet");
	// packet.data = (uint8_t *)&str;
	// CHECK_ERR(icp_error_cb(ICP_SUCCESS, packet.data, packet.data_length, NULL) == 0, "failed with valid args");

	// while(TRUE);

	if (finished)
		goto exit;

if (FALSE) {
	LOG_INFO("Sending clean...");
	sent = FALSE;
	strcpy(str, "clean");
	strcpy(topic, topic_names[TOPIC_SIMULATOR_BASE]);
	pubmsg.payload = (char*)str;
	pubmsg.payloadlen = strlen(str);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	sendmsg(client, topic, &pubmsg);
	while (!sent)
		;

	LOG_INFO("Sending 'eps' to pc...");
	sent = FALSE;
	strcpy(str, "eps");
	strcpy(topic, topic_names[TOPIC_SIMULATOR_BASE]);
	strcat(topic, mac_addrs[NODE_PC]);
	pubmsg.payload = (char*)str;
	pubmsg.payloadlen = strlen(str);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	sendmsg(client, topic, &pubmsg);
	while (!sent)
		;

	LOG_INFO("Sending 'cdhs' to cam...");
	sent = FALSE;
	strcpy(str, "cdhs");
	strcpy(topic, topic_names[TOPIC_SIMULATOR_BASE]);
	strcat(topic, mac_addrs[NODE_CAM]);
	pubmsg.payload = (char*)str;
	pubmsg.payloadlen = strlen(str);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	sendmsg(client, topic, &pubmsg);
	while (!sent)
		;

	LOG_INFO("Sending 'pc' to cdhs...");
	sent = FALSE;
	strcpy(str, "pc");
	strcpy(topic, topic_names[TOPIC_SIMULATOR_BASE]);
	strcat(topic, mac_addrs[NODE_CDHS]);
	pubmsg.payload = (char*)str;
	pubmsg.payloadlen = strlen(str);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	sendmsg(client, topic, &pubmsg);
	while (!sent)
		;

	LOG_INFO("Sending 'cdhs' to eps...");
	sent = FALSE;
	strcpy(str, "cdhs");
	strcpy(topic, topic_names[TOPIC_SIMULATOR_BASE]);
	strcat(topic, mac_addrs[NODE_EPS]);
	pubmsg.payload = (char*)str;
	pubmsg.payloadlen = strlen(str);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	sendmsg(client, topic, &pubmsg);
	while (!sent)
		;

	LOG_INFO("Sending update...");
	sent = FALSE;
	strcpy(str, "update");
	strcpy(topic, topic_names[TOPIC_SIMULATOR_BASE]);
	pubmsg.payload = (char*)str;
	pubmsg.payloadlen = strlen(str);
	pubmsg.qos = QOS;
	pubmsg.retained = 0;
	sendmsg(client, topic, &pubmsg);
	while (!sent)
		;
}

	LOG_INFO("Running...");

	while (TRUE) {
		ch = getchar();

		if (strncmp(&ch, "\n", 1) != 0) {
			strcat(cmd, &ch);
		} else {
			LOG_INFO("Got cmd: %s", cmd);

		  	pch = strtok (cmd," ");
		  	i = 0;
		  	while (pch != NULL)
		  	{
		    	strcpy(sub_cmd[i++], pch);
		    	DEBUG("sub_cmd[%d] = %s - %s", i-1, sub_cmd[i-1], (strncmp(sub_cmd[i-1], "set", 3) == 0 ? "TRUE" : "FALSE"));
		    	pch = strtok (NULL, " ");
		  	}

			receiver = INVALID_MAC_ADDR;
			if ( strncmp(sub_cmd[0], "cam", 3) == 0 ) {
				receiver = NODE_CAM;
			} else if ( strncmp(sub_cmd[0], "cdhs", 4) == 0 ) {
				receiver = NODE_CDHS;
			} else if ( strncmp(sub_cmd[0], "pc", 2) == 0 ) {
				receiver = NODE_PC;
			} else if ( strncmp(sub_cmd[0], "eps", 3) == 0 ) {
				receiver = NODE_EPS;
			} else if ( strncmp(sub_cmd[0], "test", 4) == 0 ) {
				receiver = NODE_TEST;
			} else if ( strncmp(sub_cmd[0], "bus", 3) == 0 ) {
				receiver = NODE_BUS;
			} else if ( strncmp(sub_cmd[0], "all", 4) == 0 ) {
				receiver = BROADCAST;
			} 

			strcpy(topic, topic_names[TOPIC_SIMULATOR_BASE]);

			if (receiver != INVALID_MAC_ADDR && receiver != BROADCAST) {
				strcat(topic, mac_addrs[receiver]);
			}

			if (strncmp(sub_cmd[0], "q", 1) == 0) {
				LOG_INFO("Quiting...");
				break;
			} else if (strlen(cmd) == 0) {
				LOG_INFO("Empty cmd...");
				goto end;
			}else if (strncmp(sub_cmd[0], "clean", 5) == 0 ||
				strncmp(sub_cmd[0], "update", 6) == 0 ||
				strncmp(sub_cmd[0], "reset", 5) == 0 ||
				strncmp(sub_cmd[0], "stopupdate", 10) == 0 ||
				strncmp(sub_cmd[0], "sync", 4) == 0 ||
				strncmp(sub_cmd[0], "time", 4) == 0) {
				LOG_INFO("Sending '%s' to all.", sub_cmd[0]);
				sent = FALSE;
				strcpy(str, sub_cmd[0]);
				if (strncmp(sub_cmd[0], "sync", 4) == 0) {
					synctime();
				}
			} else if (strncmp(sub_cmd[1], "clean", 5) == 0 ||
				strncmp(sub_cmd[1], "update", 6) == 0||
				strncmp(sub_cmd[1], "stopupdate", 10) == 0 ||
				strncmp(sub_cmd[1], "reset", 5) == 0 ||
				strncmp(sub_cmd[1], "time", 4) == 0) {
				CHECK_EXIT(receiver != NUM_OF_NODES, "Invalid receiver.");
				LOG_INFO("Sending '%s' to %s.", sub_cmd[1], mac_addrs[receiver]);
				strcpy(str, sub_cmd[1]);
			}else if (strncmp(sub_cmd[1], "ping", 4) == 0 ) {
				CHECK_EXIT(receiver != NUM_OF_NODES, "Invalid receiver.");

				if ( strncmp(sub_cmd[2], "cdhs", 4) == 0 ||
					 strncmp(sub_cmd[2], "cam", 3)  == 0 ||
					 strncmp(sub_cmd[2], "pc", 2)   == 0 ||
					 strncmp(sub_cmd[2], "eps", 3)  == 0 ||
					 strncmp(sub_cmd[2], "bus", 3)  == 0 ) {
					LOG_INFO("Sending '%s' to %s...", sub_cmd[2], mac_addrs[receiver]);
				} else {
					LOG_INFO("Failed to cmd to send.")
					goto fail;
				}

				strcpy(str, sub_cmd[2]);
			}else if (strncmp(sub_cmd[1], "set", 3) == 0 ) {
				CHECK_EXIT(receiver != INVALID_MAC_ADDR, "Invalid receiver.");

				if ( (strncmp(sub_cmd[2], "icp", 3) == 0 ||
					 strncmp(sub_cmd[2], "mqtt", 4)  == 0 ||
					 strncmp(sub_cmd[2], "sys", 3)   == 0 ||
					 strncmp(sub_cmd[2], "all", 3)   == 0) &&
					(strncmp(sub_cmd[3], "debug", 5) == 0 ||
					 strncmp(sub_cmd[3], "err", 3)  == 0 ||
					 strncmp(sub_cmd[3], "warn", 4)   == 0 ||
					 strncmp(sub_cmd[3], "info", 4)   == 0 ||
					 strncmp(sub_cmd[3], "all", 3)   == 0) &&
					(strncmp(sub_cmd[4], "true", 4) == 0 ||
					 strncmp(sub_cmd[4], "false", 5)  == 0)) {
					LOG_INFO("Sending '%s %s %s %s' to %s...", sub_cmd[1], sub_cmd[2], sub_cmd[3], sub_cmd[4], mac_addrs[receiver]);
					strcpy(str, sub_cmd[1]);
					strcat(str, " ");
					strcat(str, sub_cmd[2]);
					strcat(str, " ");
					strcat(str, sub_cmd[3]);
					strcat(str, " ");
					strcat(str, sub_cmd[4]);
				} else /*if ( (strncmp(sub_cmd[2], "icp", 3) == 0 ||
					 strncmp(sub_cmd[2], "mqtt", 4)  == 0 ||
					 strncmp(sub_cmd[2], "sys", 3)   == 0) &&
					(strncmp(sub_cmd[3], "debug", 5) == 0 ||
					 strncmp(sub_cmd[3], "err", 3)  == 0 ||
					 strncmp(sub_cmd[3], "warn", 4)   == 0 ||
					 strncmp(sub_cmd[3], "info", 4)   == 0) &&
					(strncmp(sub_cmd[4], "true", 4) == 0 ||
					 strncmp(sub_cmd[4], "false", 5)  == 0)) {
					LOG_INFO("Sending '%s %s %s %s' to %s...", sub_cmd[1], sub_cmd[2], sub_cmd[3], sub_cmd[4], mac_addrs[receiver]);
				 */{
					LOG_WARN("Failed to send cmd.")
					LOG_INFO("command example: [eps|pc|...|all] set [icp|sys|mqtt|all] [debug|err|warn|info|all] [true|false]");
					goto fail;
				}

			} else {
				goto fail;
			}
			sent = FALSE;
			pubmsg.payload = (char*)str;
			pubmsg.payloadlen = strlen(str);
			pubmsg.qos = QOS;
			pubmsg.retained = 0;
			LOG_INFO("Sending '%s' to '%s'", str, topic);
			sendmsg(client, topic, &pubmsg);
			while (!sent)
				;
		fail:
		end:
			LOG_INFO("Clearing cmd buffer.");
			strcpy(cmd, "");
			for(i=0;i<8;i++)
		  	{
		    	strcpy(sub_cmd[i], "");
		  	}
		}