File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / example / tsrv.c
Revision 1.3.2.4: download - view: text, annotated - select for diffs - revision graph
Thu Sep 1 14:26:22 2011 UTC (13 years, 2 months ago) by misho
Branches: rpc1_3
Diff to: branchpoint 1.3: preferred, unified
test w/o BLOB

#include <stdio.h>
#include <pthread.h>
#include <aitrpc.h>



int aaa(rpc_func_t *f, int in, array_t *iv)
{
	rpc_blob_t *b;

	RPC_CALLBACK_CHECK_INPUT(f);

	printf("%s(%d): Piuk! %s\n", __func__, __LINE__, f->func_name);

	/*
	b = rpc_srv_registerBLOB(f->func_parent, BUFSIZ);
	if (!b)
		return -1;
	if (rpc_srv_blobMap(f->func_parent, b) == -1) {
		rpc_srv_blobFree(f->func_parent, b);
		return -1;
	}

	memset(b->blob_data, 0, b->blob_len);
	strcpy(b->blob_data, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAa dsfsfsfnskjfnk\n");

	rpc_srv_blobUnmap(b);
	*/
	return 0;
}

int BBB(rpc_func_t *f, int in, array_t *iv)
{
	ait_val_t *v;
	rpc_blob_t *b;
	array_t *arr;

	RPC_CALLBACK_CHECK_INPUT(f);

//	printf("%s(%d): Piuk! %s  %d %s %X\n", __func__, __LINE__, f->func_name, 
//			AIT_GET_I32(io_array(iv, 0, ait_val_t*)), AIT_GET_STR(io_array(iv, 1, ait_val_t*)), 
//			AIT_GET_BLOB(io_array(iv, 2, ait_val_t*)));
	printf("%s(%d): Piuk! %s  %d %s %X\n", __func__, __LINE__, f->func_name, 
			AIT_GET_I32(io_array(iv, 0, ait_val_t*)), AIT_GET_STR(io_array(iv, 1, ait_val_t*)));

	// input blob object
//	if (!(b = rpc_srv_getBLOB(f->func_parent, AIT_GET_BLOB(io_array(iv, 2, ait_val_t*)))))
//		return -1;
//	if (rpc_srv_blobMap(f->func_parent, b) == -1) {
//		rpc_srv_blobFree(f->func_parent, b);
//		return -1;
//	}
//	printf("VAR=%X(%d):: %s\n", b->blob_var, b->blob_len, b->blob_data);
//	rpc_srv_unregisterBLOB(f->func_parent, AIT_GET_BLOB(io_array(iv, 2, ait_val_t*)));


	rpc_srv_allocVars(f, 3);
	rpc_srv_getVars(f, &arr);
	v = io_array(arr, 0, ait_val_t*);
	AIT_SET_BUF(v, "00!oo", 6);
	v = io_array(arr, 1, ait_val_t*);
	AIT_SET_I8(v, 65);
	v = io_array(arr, 2, ait_val_t*);
	AIT_SET_STR(v, "Oho boho i cheburashka");

	// return blob object
//	b = rpc_srv_registerBLOB(f->func_parent, 128);
//	if (!b)
//		return -1;
//	else
//		AIT_SET_BLOB2(io_array(arr, 3, ait_val_t*), b);

//	if (rpc_srv_blobMap(f->func_parent, b) == -1) {
//		rpc_srv_blobFree(f->func_parent, b);
//		return -1;
//	}

//	printf("data=%p blen=%d\n", b->blob_data, b->blob_len);
//	memset(b->blob_data, 0, b->blob_len);
//	strcpy(b->blob_data, "Hello Worldzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz mzdnfdskf\naddfs\n");

//	rpc_srv_blobUnmap(b);
	return 33;
}

int main()
{
	rpc_srv_t *srv;
	rpc_func_t *f;
	pthread_t tid[2];
	int ret;

	srv = rpc_srv_initServer(100, 2, 1, 1024 * 10, AF_INET, "0.0.0.0", 11111);
//	srv = rpc_srv_initServer(100, 2, 1, AF_LOCAL, "0.0.0.0", 11111);
	if (!srv) {
		printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		return 1;
	}

	/*
	if (rpc_srv_initBLOBServer(srv, 0, "/tmp")) {
		printf("error:: errno=%d %s\n", rpc_GetErrno(), rpc_GetError());
		return 1;
	}
	*/

	if (!fork()) {
		setsid();

		rpc_srv_registerCall(srv, NULL, "aaa", 0);
		rpc_srv_registerCall(srv, NULL, "BBB", /*4*/ 3);
		rpc_srv_registerCall(srv, NULL, "dummy", 1);
		rpc_srv_registerCall(srv, NULL, "xYz", 2);

		/* dump register functions */
		for (f = srv->srv_funcs; f; f = f->func_next)
			printf("0.func::name=%s args=%d\n", f->func_name, io_arraySize(f->func_vars));

		rpc_srv_unregisterCall(srv, NULL, "dummy");

		/* dump register functions */
		for (f = srv->srv_funcs; f; f = f->func_next)
			printf("1.func::name=%s args=%d\n", f->func_name, io_arraySize(f->func_vars));

//		pthread_create(&tid[1], NULL, (void*(*)(void*)) rpc_srv_execBLOBServer, srv);
//		pthread_detach(tid[1]);
		rpc_srv_execServer(srv);
	}

//	rpc_srv_endBLOBServer(srv);
	rpc_srv_endServer(srv);
	return 0;
}

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