File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / lists.c
Revision 1.4.2.2: download - view: text, annotated - select for diffs - revision graph
Wed Aug 31 17:11:58 2011 UTC (12 years, 11 months ago) by misho
Branches: rpc1_3
Diff to: branchpoint 1.4: preferred, unified
totaly reworked all RPC!!!!!!
changed type of core elements to array_t!!!!

*** NOT TESTED *****!!!

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: lists.c,v 1.4.2.2 2011/08/31 17:11:58 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: 
   48: 
   49: /*
   50:  * rpc_srv_returnVars() Init return variables for RPC call and zeroed values
   51: 					(for safe handling return values, use this!)
   52:  * @call = RPC function call
   53:  * @varnum = Number of return variables
   54:  * return: NULL error, !=NULL array with return values for RPC call with varnum items
   55:  */
   56: inline array_t *
   57: rpc_srv_returnVars(rpc_func_t * __restrict call, int varnum)
   58: {
   59: 	array_t *v = NULL;
   60: 
   61: 	if (rpc_srv_allocVars(call, varnum) == -1)
   62: 		return NULL;
   63: 	if (rpc_srv_getVars(call, &v) == -1)
   64: 		return NULL;
   65: 
   66: 	return v;
   67: }
   68: 
   69: /*
   70:  * rpc_srv_allocVars() Allocate array for call variables, 
   71: 				if already allocated memory for RPC call reallocate used space
   72:  * @call = RPC function call
   73:  * @varnum = Number of variables, if ==0 free previous allocated variables
   74:  * return: -1 error, !=-1 return varnum value
   75:  */
   76: inline int
   77: rpc_srv_allocVars(rpc_func_t * __restrict call, int varnum)
   78: {
   79: 	register int i;
   80: 	ait_val_t *v;
   81: 
   82: 	if (!call || varnum < 0) {
   83: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t allocate variables for RPC call...\n");
   84: 		return -1;
   85: 	}
   86: 
   87: 	if (call->func_vars)
   88: 		for (i = 0; i < io_arraySize(call->func_vars); i++)
   89: 			if (io_arrayGet(call->func_vars, i))
   90: 				AIT_FREE_VAL(io_array(call->func_vars, i, ait_val_t*));
   91: 	io_arrayFree(call->func_vars);
   92: 	io_arrayDestroy(&call->func_vars);
   93: 
   94: 	if (varnum) {
   95: 		if (!(call->func_vars = io_arrayInit(varnum)))
   96: 			return -1;
   97: 
   98: 		for (i = 0; i < io_arraySize(call->func_vars); i++) {
   99: 			v = malloc(sizeof(ait_val_t));
  100: 			if (!v) {
  101: 				LOGERR;
  102: 				rpc_srv_freeVars(call);
  103: 				return -1;
  104: 			} else {
  105: 				memset(v, 0, sizeof(ait_val_t));
  106: 				io_arraySet(call->func_vars, i, v);
  107: 			}
  108: 		}
  109: 
  110: 		varnum = io_arraySize(call->func_vars);
  111: 	}
  112: 
  113: 	return varnum;
  114: }
  115: 
  116: /*
  117:  * rpc_srv_zeroVars() Clean values from variables of RPC call
  118:  * @call = RPC function call
  119:  * return: -1 error, !=-1 Returned number of cleaned RPC variables
  120:  */
  121: inline int
  122: rpc_srv_zeroVars(rpc_func_t * __restrict call)
  123: {
  124: 	if (!call) {
  125: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t delete variables ...\n");
  126: 		return -1;
  127: 	}
  128: 
  129: 	io_arrayFree(call->func_vars);
  130: 	io_arrayZero(call->func_vars);
  131: 	return io_arraySize(call->func_vars);
  132: }
  133: 
  134: /*
  135:  * rpc_srv_copyVars() Copy variables for RPC call to new variable array
  136:  * @call = RPC function call
  137:  * @newvars = New allocated variables array, must be free after use
  138:  * return: -1 error, !=-1 Returned number of copied RPC variables
  139:  */
  140: inline int
  141: rpc_srv_copyVars(rpc_func_t * __restrict call, array_t ** __restrict newvars)
  142: {
  143: 	if (!call || !newvars) {
  144: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t copy variables to new array\n");
  145: 		return -1;
  146: 	}
  147: 
  148: 	if (io_arrayCopy(newvars, call->func_vars) == -1)
  149: 		return -1;
  150: 
  151: 	return io_arraySize(*newvars);
  152: }
  153: 
  154: /*
  155:  * rpc_srv_getVars() Get variables array for RPC call
  156:  * @call = RPC function call
  157:  * @vars = Returned variables array, may be NULL
  158:  * return: -1 error, !=-1 Number of returned variables
  159:  */
  160: inline int
  161: rpc_srv_getVars(rpc_func_t * __restrict call, array_t ** __restrict vars)
  162: {
  163: 	if (!call) {
  164: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get variables ...\n");
  165: 		return -1;
  166: 	}
  167: 
  168: 	if (vars)
  169: 		*vars = call->func_vars;
  170: 	return (call->func_vars ? io_arraySize(call->func_vars) : 0);
  171: }
  172: 
  173: // ---------------------------------------------------------
  174: 
  175: /*
  176:  * rpc_srv_registerCall() Register call to RPC server
  177:  * @srv = RPC Server instance
  178:  * @csModule = Module name, if NULL self binary
  179:  * @csFunc = Function name
  180:  * @args = Number of return function arguments, use for restriction case!
  181:  * return: -1 error or 0 register ok
  182:  */
  183: int
  184: rpc_srv_registerCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc, u_short args)
  185: {
  186: 	rpc_func_t *func;
  187: 	u_char str[MAXPATHLEN + UCHAR_MAX + 1];
  188: 
  189: 	memset(str, 0, sizeof str);
  190: 	if (!srv || !csFunc) {
  191: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t register function to RPC server ...\n");
  192: 		return -1;
  193: 	}
  194: 	if (!(func = malloc(sizeof(rpc_func_t)))) {
  195: 		LOGERR;
  196: 		return -1;
  197: 	} else {
  198: 		memset(func, 0, sizeof(rpc_func_t));
  199: 		strlcpy((char*) func->func_name, csFunc, sizeof func->func_name);
  200: 	}
  201: 	if (csModule) {
  202: 		strlcpy((char*) func->func_file, csModule, sizeof func->func_file);
  203: 		strlcpy((char*) str, csModule, sizeof str);
  204: 	}
  205: 	strlcat((char*) str, "__", sizeof str);
  206: 	strlcat((char*) str, csFunc, sizeof str);
  207: 
  208: 	func->func_tag = crcFletcher16((u_short*) str, sizeof str / 2);
  209: 	func->func_hash = hash_fnv((char*) str, sizeof str);
  210: 
  211: 	func->func_parent = srv;
  212: 
  213: 	if (args > 0 && rpc_srv_allocVars(func, args) == -1) {
  214: 		free(func);
  215: 		return -1;
  216: 	}
  217: 
  218: 	pthread_mutex_lock(&srv->srv_mtx);
  219: 	func->func_next = srv->srv_funcs;
  220: 	srv->srv_funcs = func;
  221: 	pthread_mutex_unlock(&srv->srv_mtx);
  222: 	return 0;
  223: }
  224: 
  225: /*
  226:  * rpc_srv_unregisterCall() Unregister call from RPC server
  227:  * @srv = RPC Server instance
  228:  * @csModule = Module name, if NULL self binary
  229:  * @csFunc = Function name
  230:  * return: -1 error, 0 not found call, 1 unregister ok
  231:  */
  232: int
  233: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
  234: {
  235: 	rpc_func_t func, *f, *curr;
  236: 	u_char str[MAXPATHLEN + UCHAR_MAX + 1];
  237: 
  238: 	memset(&func, 0, sizeof(rpc_func_t));
  239: 	memset(str, 0, sizeof str);
  240: 	if (!srv || !csFunc) {
  241: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t unregister function from RPC server ...\n");
  242: 		return -1;
  243: 	} else
  244: 		strlcpy((char*) func.func_name, csFunc, sizeof func.func_name);
  245: 	if (csModule) {
  246: 		strlcpy((char*) func.func_file, csModule, sizeof func.func_file);
  247: 		strlcpy((char*) str, csModule, sizeof str);
  248: 	}
  249: 	strlcat((char*) str, "__", sizeof str);
  250: 	strlcat((char*) str, csFunc, sizeof str);
  251: 
  252: 	func.func_tag = crcFletcher16((u_short*) str, sizeof str / 2);
  253: 	func.func_hash = hash_fnv((char*) str, sizeof str);
  254: 
  255: 	f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
  256: 	if (!f)				/* not found element for unregister */
  257: 		return 0;
  258: 
  259: 	pthread_mutex_lock(&srv->srv_mtx);
  260: 	if (srv->srv_funcs == f) {	/* if is 1st element */
  261: 		srv->srv_funcs = srv->srv_funcs->func_next;
  262: 
  263: 		if (f->func_args && f->func_vars)
  264: 			free(f->func_vars);
  265: 		free(f);
  266: 	} else {
  267: 		for (curr = srv->srv_funcs; curr->func_next != f; curr = curr->func_next);
  268: 		curr->func_next = curr->func_next->func_next;
  269: 
  270: 		if (f->func_args && f->func_vars)
  271: 			free(f->func_vars);
  272: 		free(f);
  273: 	}
  274: 	pthread_mutex_unlock(&srv->srv_mtx);
  275: 
  276: 	return 1;
  277: }
  278: 
  279: /*
  280:  * rpc_srv_getCall() Get registered call from RPC server
  281:  * @srv = RPC Server instance
  282:  * @tag = tag for function
  283:  * @hash = hash for function
  284:  * return: NULL not found call, !=NULL return call
  285:  */
  286: inline rpc_func_t *
  287: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag, uint32_t hash)
  288: {
  289: 	rpc_func_t *f;
  290: 
  291: 	if (!srv) {
  292: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
  293: 		return NULL;
  294: 	}
  295: 
  296: 	for (f = srv->srv_funcs; f; f = f->func_next)
  297: 		if (f->func_tag == tag && f->func_hash == hash)
  298: 			break;
  299: 
  300: 	return f;
  301: }
  302: 
  303: /*
  304:  * rpc_srv_getFunc() Get registered call from RPC server by Name
  305:  * @srv = RPC Server instance
  306:  * @csModule = Module name, if NULL self binary
  307:  * @csFunc = Function name
  308:  * return: NULL not found call, !=NULL return call
  309:  */
  310: rpc_func_t *
  311: rpc_srv_getFunc(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
  312: {
  313: 	rpc_func_t func;
  314: 	u_char str[MAXPATHLEN + UCHAR_MAX + 1];
  315: 
  316: 	memset(&func, 0, sizeof(rpc_func_t));
  317: 	memset(str, 0, sizeof str);
  318: 	if (!srv || !csFunc) {
  319: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get function from RPC server ...\n");
  320: 		return NULL;
  321: 	} else
  322: 		strlcpy((char*) func.func_name, csFunc, sizeof func.func_name);
  323: 	if (csModule) {
  324: 		strlcpy((char*) func.func_file, csModule, sizeof func.func_file);
  325: 		strlcpy((char*) str, csModule, sizeof str);
  326: 	}
  327: 	strlcat((char*) str, "__", sizeof str);
  328: 	strlcat((char*) str, csFunc, sizeof str);
  329: 
  330: 	func.func_tag = crcFletcher16((u_short*) str, sizeof str / 2);
  331: 	func.func_hash = hash_fnv((char*) str, sizeof str);
  332: 
  333: 	return rpc_srv_getCall(srv, func.func_tag, func.func_hash);
  334: }
  335: 
  336: // ---------------------------------------------------------
  337: 
  338: /*
  339:  * rpc_srv_getBLOB() Get registered BLOB 
  340:  * @srv = RPC Server instance
  341:  * @var = hash for variable
  342:  * return: NULL not found, !=NULL return blob var
  343:  */
  344: inline rpc_blob_t *
  345: rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var)
  346: {
  347: 	rpc_blob_t *b;
  348: 
  349: 	if (!srv) {
  350: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t get variable from BLOB server ...\n");
  351: 		return NULL;
  352: 	}
  353: 
  354: 	pthread_mutex_lock(&srv->srv_blob.mtx);
  355: 	for (b = srv->srv_blob.blobs; b; b = b->blob_next) {
  356: 		if (b->blob_var == var)
  357: 			break;
  358: 	}
  359: 	pthread_mutex_unlock(&srv->srv_blob.mtx);
  360: 
  361: 	return b;
  362: }
  363: 
  364: /*
  365:  * rpc_srv_registerBLOB() Register new BLOB to server
  366:  * @srv = RPC Server instance
  367:  * @len = BLOB length
  368:  * return: NULL error or new registered BLOB
  369:  */
  370: rpc_blob_t *
  371: rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len)
  372: {
  373: 	rpc_blob_t *blob = NULL;
  374: 
  375: 	if (!srv || !len) {
  376: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t register BLOB to server ...\n");
  377: 		return blob;
  378: 	}
  379: 
  380: 	blob = rpc_srv_blobCreate(srv, len);
  381: 	if (blob) {
  382: 		pthread_mutex_lock(&srv->srv_blob.mtx);
  383: 		blob->blob_next = srv->srv_blob.blobs;
  384: 		srv->srv_blob.blobs = blob;
  385: 		pthread_mutex_unlock(&srv->srv_blob.mtx);
  386: 	}
  387: 
  388: 	return blob;
  389: }
  390: 
  391: /*
  392:  * rpc_srv_unregisterBLOB() Unregister BLOB from server
  393:  * @srv = RPC Server instance
  394:  * @var = BLOB Variable for unregister
  395:  * return: -1 error, 0 not found call, 1 unregister ok
  396:  */
  397: int
  398: rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var)
  399: {
  400: 	rpc_blob_t *b, *curr;
  401: 
  402: 	if (!srv) {
  403: 		rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t unregister BLOB from server ...\n");
  404: 		return -1;
  405: 	}
  406: 
  407: 	b = rpc_srv_getBLOB(srv, var);
  408: 	if (!b)				/* not found element for unregister */
  409: 		return 0;
  410: 
  411: 	pthread_mutex_lock(&srv->srv_blob.mtx);
  412: 	if (srv->srv_blob.blobs == b) {	/* if is 1st element */
  413: 		srv->srv_blob.blobs = srv->srv_blob.blobs->blob_next;
  414: 	} else {
  415: 		for (curr = srv->srv_blob.blobs; curr->blob_next != b; curr = curr->blob_next);
  416: 		curr->blob_next = curr->blob_next->blob_next;
  417: 	}
  418: 	rpc_srv_blobFree(srv, b);
  419: 	free(b);
  420: 	pthread_mutex_unlock(&srv->srv_blob.mtx);
  421: 
  422: 	return 1;
  423: }

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