File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / example / tcli.c
Revision 1.22: download - view: text, annotated - select for diffs - revision graph
Wed Mar 20 17:32:30 2024 UTC (7 months, 2 weeks ago) by misho
Branches: MAIN
CVS tags: rpc9_7, rpc9_6, RPC9_6, RPC9_5, HEAD
Version 9.5

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <pthread.h>
#include <aitrpc_cli.h>
#include "rc.h"


int main(int argc, char **argv)
{
	rpc_cli_t *cli = NULL, *cli2 = NULL;
	int c, i, ret;
	ait_val_t tval, *v = NULL;
	array_t *valz, *arr;
	char *mem, *blob43 = e_malloc(512);
	struct timeval after, before;

//	printf("MM model=%d\n", elwix_mm_inuse());
//	getchar();

	if (argc > 1) {
		if (argc > 2)
			cli = rpc_cli_openClient(2, 1024 * 10, "127.0.0.1", 0, SOCK_RAW);
		else
			cli = rpc_cli_openClient(2, 1024 * 10, "127.0.0.1", 11112, SOCK_DGRAM);
	} else
//		cli = rpc_cli_openClient(2, 1024 * 10, "93.123.104.1", 11111, 0);
		cli = rpc_cli_openClient(2, 1024 * 10, "127.0.0.1", 11112, 0);
	if (!cli) {
		printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		e_free(blob43);
		return 1;
	}

	cli2 = rpc_cli_openBLOBClient(cli, 0);
	if (!cli2) {
		printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		rpc_cli_closeClient(&cli);
		e_free(blob43);
		return 1;
	}

//#if 0
	printf("\n\n1 pass>>> RPC ping\n\n");
	for (i = 0; i < 1000; i++) {
		gettimeofday(&before, NULL);
		if ((ret = rpc_cli_ping(cli)) == -1) {
			printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
			rpc_cli_closeBLOBClient(&cli2);
			rpc_cli_closeClient(&cli);
			e_free(blob43);
			return 127;
		}
		gettimeofday(&after, NULL);
		printf("RPC ping seq[0x%04x] #%d --- %f\n", ret, i, 
				(after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1.e6);
	}
//#if 0
	printf("\n\n2 pass>>> RPC ping without reply!!!\n\n");
	for (i = 0; i < 10; i++) {
		gettimeofday(&before, NULL);
		if (rpc_cli_execCall(cli, RPC_NOREPLY, CALL_SRVPING, NULL, &arr) == -1) {
			printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
			rpc_cli_closeBLOBClient(&cli2);
			rpc_cli_closeClient(&cli);
			e_free(blob43);
			return 127;
		}
		gettimeofday(&after, NULL);
		rpc_cli_freeCall(&arr);
		printf("RPC ping #%d --- %f\n", i, (after.tv_sec - before.tv_sec) + 
				(after.tv_usec - before.tv_usec) / 1.e6);
	}
#if 0
	printf("\n\n3 pass>>> RPC ping\n\n");
	for (i = 0; i < 10000; i++) {
		gettimeofday(&before, NULL);
		if ((ret = rpc_cli_ping(cli)) == -1) {
			printf("error %d:: errno=%d %s\n", i, rpc_GetErrno(), rpc_GetError());
			rpc_cli_closeBLOBClient(&cli2);
			rpc_cli_closeClient(&cli);
			e_free(blob43);
			return 127;
		}
		gettimeofday(&after, NULL);
		printf("RPC ping seq[0x%04x] #%d --- %f\n", ret, i, 
				(after.tv_sec - before.tv_sec) + (after.tv_usec - before.tv_usec) / 1.e6);
	}
#endif
//#endif

	memset(blob43, 0, 512);
	strlcpy(blob43, "aaaaaaaaaaaaaaa cddddddddddddddd zzzzzzzzzzzzzzzzzzzzz !!!!\n\n\n675675676...\n", 512);

	valz = ait_allocVars(3);
	v = array(valz, 0, ait_val_t*);
	AIT_SET_I32(v, 12345678);
	v = array(valz, 1, ait_val_t*);
	AIT_SET_STR(v, "gniah_blah SHMINK!");
	v = array(valz, 2, ait_val_t*);
	AIT_NEW_BLOB(v, 512);
	if (rpc_cli_sendBLOB(cli2, v, blob43, 0) == -1) {
		printf("!!!!! blob error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		ait_freeVars(&valz);
		rpc_cli_closeBLOBClient(&cli2);
		rpc_cli_closeClient(&cli);
		e_free(blob43);
		return 2;
	} else
		printf("Get BLOB = %x\n", AIT_GET_BLOB(v));

	/* test case for unknown blob ... 
	AIT_SET_BLOB(&tval, 0x554C1BC7, BUFSIZ);
	printf("test not found blob=%d\n", rpc_cli_getBLOB(cli2, &tval, (void**) &mem));
	if (mem)
		e_free(mem);
	*/

//	for (i = 0; i < 1000000; i++)
		if ((ret = rpc_cli_execCall(cli, RPC_REPLY, RC_BBB, valz, &arr)) < 0) {
			printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
			ait_freeVars(&valz);
			rpc_cli_closeBLOBClient(&cli2);
			rpc_cli_closeClient(&cli);
			e_free(blob43);
			return 2;
		} else {
			printf("ret=%d\n", ret);
			if (arr) {
				v = array(arr, 0, ait_val_t*);
				printf("ret_val0:: %p %s len=%d type=%d\n", 
						v, AIT_GET_BUF(v), AIT_LEN(v), AIT_TYPE(v));
				v = array(arr, 1, ait_val_t*);
				printf("ret_val1:: %d len=%d type=%d\n", 
						AIT_GET_I8(v), AIT_LEN(v), AIT_TYPE(v));
				v = array(arr, 2, ait_val_t*);
				printf("ret_val2:: is_empty? %d %s len=%d type=%d\n", 
						AIT_ISEMPTY(v), AIT_GET_STR(v), AIT_LEN(v), AIT_TYPE(v));
//#if 0
				v = array(arr, 3, ait_val_t*);
				printf("ret_val3:: %X %d\n", AIT_GET_BLOB(v), AIT_LEN(v));
				printf("test found blob=%d\n", rpc_cli_getBLOB(cli2, v, (void**) &mem));
//				printf("+++++++++ test found blob=%d\n", rpc_cli_recvBLOB(cli2, v, (void**) &mem));
				if (mem) {
					printf("+++++++ BLOB=%s", mem);
					e_free(mem);
				}
//#endif
				rpc_cli_freeCall(&arr);
			}
		}
	ait_freeVars(&valz);
	e_free(blob43);

	if (rpc_cli_execCall(cli, RPC_REPLY, RC_aaa, NULL, &arr) < 0) {
		printf("error:: \"aaa\" errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		return 3;
	}
	printf("return=%d aaa arr=%p\n", arr ? array_Size(arr) : 42424242, arr);

	valz = ait_allocVars(1);
	v = ait_getVars(&valz, 0);
	printf("cli_id=%d\n", cli->cli_id);
	AIT_SET_I32(v, (cli->cli_id == SOCK_STREAM || cli->cli_id == SOCK_EXT) ? 50000 : 5000);
	if (rpc_cli_execCall(cli, RPC_REPLY, RC_big, valz, &arr) < 0) {
		printf("error:: \"big\" errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
	} else
		printf("return=%d big arr=%p\n", arr ? array_Size(arr) : 42424242, arr);
	ait_freeVars(&arr);
	ait_freeVars(&valz);


	valz = ait_allocVars(1);
	v = array(valz, 0, ait_val_t*);
	AIT_SET_DATA(v, "1234567890", 11);
	array_Set(valz, 0, v);
	if (rpc_cli_execCall(cli, RPC_NOREPLY, RC_xxx, valz, &arr) < 0) {
		printf("0) error:: \"xxx\" errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		ait_freeVars(&valz);
		if (arr)
			rpc_cli_freeCall(&arr);
		rpc_cli_closeBLOBClient(&cli2);
		rpc_cli_closeClient(&cli);
		return 3;
	}
	printf("0) return=%d xxx arr=%p\n", arr ? array_Size(arr) : 42424242, arr);
	ait_freeVars(&valz);
	if (arr) {
		printf("...REPLY --- ");
		v = array(arr, 0, ait_val_t*);
		printf("ret_val0:: %p %s len=%d type=%d\n", 
				v, AIT_GET_BUF(v), AIT_LEN(v), AIT_TYPE(v));
		rpc_cli_freeCall(&arr);
	}

	valz = ait_allocVars(1);
	v = array(valz, 0, ait_val_t*);
	AIT_SET_DATA(v, "0987654321", 11);
	array_Set(valz, 0, v);
	if (rpc_cli_execCall(cli, RPC_REPLY, RC_xxx, valz, &arr) < 0) {
		printf("1) error:: \"xxx\" errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		ait_freeVars(&valz);
		if (arr)
			rpc_cli_freeCall(&arr);
		rpc_cli_closeBLOBClient(&cli2);
		rpc_cli_closeClient(&cli);
		return 3;
	}
	printf("1) return=%d xxx arr=%p\n", arr ? array_Size(arr) : 42424242, arr);
	ait_freeVars(&valz);
	if (arr) {
		printf("@REPLY --- ");
		v = array(arr, 0, ait_val_t*);
		printf("ret_val0:: %p %s len=%d type=%d\n", 
				v, AIT_GET_BUF(v), AIT_LEN(v), AIT_TYPE(v));
		rpc_cli_freeCall(&arr);
	}


	if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVCLIENTS, NULL, &arr) < 0) {
		printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		rpc_cli_closeBLOBClient(&cli2);
		rpc_cli_closeClient(&cli);
		return 4;
	}
	printf("return=%d registeredClient\n", (arr ? array_Size(arr) : 0));
	for (i = 0; i < (arr ? array_Size(arr) : 0); i++)
		printf("registeredClient(%d)=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
	rpc_cli_freeCall(&arr);

	if (rpc_cli_execCall(cli, RPC_REPLY, CALL_BLOBCLIENTS, NULL, &arr) < 0) {
		printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		rpc_cli_closeBLOBClient(&cli2);
		rpc_cli_closeClient(&cli);
		return 4;
	}
	printf("return=%d registeredBLOBClient\n", (arr ? array_Size(arr) : 0));
	for (i = 0; i < (arr ? array_Size(arr) : 0); i++)
		printf("registeredBLOBClient(%d)=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
	rpc_cli_freeCall(&arr);
	
	if (rpc_cli_execCall(cli, RPC_REPLY, CALL_BLOBVARS, NULL, &arr) < 0) {
		printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		rpc_cli_closeBLOBClient(&cli2);
		rpc_cli_closeClient(&cli);
		return 5;
	}
	for (i = 0; i < (arr ? array_Size(arr) : 0); i++)
		printf("registeredBLOBVars(%d)=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
	rpc_cli_freeCall(&arr);

	if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVCALLS, NULL, &arr) < 0) {
		printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		rpc_cli_closeBLOBClient(&cli2);
		rpc_cli_closeClient(&cli);
		return 5;
	}
	for (i = 0; i < (arr ? array_Size(arr) : 0); i++)
		printf("registeredCall(%d)=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
	rpc_cli_freeCall(&arr);

	if (rpc_cli_execCall(cli, RPC_REPLY, CALL_SRVSESSIONS, NULL, &arr) < 0) {
		printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		rpc_cli_closeBLOBClient(&cli2);
		rpc_cli_closeClient(&cli);
		return 6;
	}
	printf("c=%d v=%p\n", array_Size(arr), arr);
	printf("session(%d:%d) clients=%d\n", (uint8_t) AIT_GET_U8(array(arr, 0, ait_val_t*)), 
			AIT_GET_U8(array(arr, 1, ait_val_t*)), AIT_GET_I32(array(arr, 2, ait_val_t*)));
	rpc_cli_freeCall(&arr);

	printf("shutdown rpc server? (y/n): ");
	if (getchar() == 'y')
		rpc_cli_execCall(cli, RPC_NOREPLY, CALL_SRVSHUTDOWN, NULL, NULL);

	rpc_cli_closeBLOBClient(&cli2);
	rpc_cli_closeClient(&cli);
	return 0;
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>