File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / lists.c
Revision 1.8: download - view: text, annotated - select for diffs - revision graph
Thu Mar 29 01:34:16 2012 UTC (12 years, 3 months ago) by misho
Branches: MAIN
CVS tags: rpc3_2, RPC3_1, HEAD
version 3.1

    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.8 2012/03/29 01:34:16 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, 2012
   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_allocVars() - Allocate array for call variables
   51:  *
   52:  * @call = RPC function call
   53:  * @varnum = Number of variables, if ==0 return already allocated variables number
   54:  * return: -1 error, !=-1 return varnum value
   55:  */
   56: static inline int
   57: rpc_srv_allocVars(rpc_func_t * __restrict call, int varnum)
   58: {
   59: 	if (!call || varnum < 0) {
   60: 		rpc_SetErr(EINVAL, "Invalid parameter can`t allocate variables for RPC call");
   61: 		return -1;
   62: 	}
   63: 
   64: 	if (varnum) {
   65: 		call->func_vars = io_allocVars(varnum);
   66: 		if (!call->func_vars)
   67: 			return -1;
   68: 	}
   69: 
   70: 	return io_arraySize(call->func_vars);
   71: }
   72: 
   73: /*
   74:  * rpc_srv_getVars() - Get variables array for RPC call
   75:  *
   76:  * @call = RPC function call
   77:  * @vars = Returned variables array, may be NULL
   78:  * return: -1 error, !=-1 Number of returned variables
   79:  */
   80: inline int
   81: rpc_srv_getVars(rpc_func_t * __restrict call, array_t ** __restrict vars)
   82: {
   83: 	if (!call) {
   84: 		rpc_SetErr(EINVAL, "Invalid parameter can`t get variables");
   85: 		return -1;
   86: 	}
   87: 
   88: 	if (vars)
   89: 		*vars = call->func_vars;
   90: 	return io_arraySize(call->func_vars);
   91: }
   92: 
   93: /* --------------------------------------------------------- */
   94: 
   95: /*
   96:  * rpc_srv_registerCall() - Register call to RPC server
   97:  *
   98:  * @srv = RPC Server instance
   99:  * @csModule = Module name, if NULL self binary
  100:  * @csFunc = Function name
  101:  * @args = Number of return function arguments, use for restriction case!
  102:  * return: -1 error or 0 register ok
  103:  */
  104: int
  105: rpc_srv_registerCall(rpc_srv_t * __restrict srv, const char *csModule, 
  106: 		const char *csFunc, u_short args)
  107: {
  108: 	rpc_func_t *func;
  109: 
  110: 	if (!srv || !csFunc) {
  111: 		rpc_SetErr(EINVAL, "Invalid parameter can`t register function");
  112: 		return -1;
  113: 	}
  114: 	if (!(func = malloc(sizeof(rpc_func_t)))) {
  115: 		LOGERR;
  116: 		return -1;
  117: 	} else
  118: 		memset(func, 0, sizeof(rpc_func_t));
  119: 
  120: 	/* calculate hashes */
  121: 	if (rpc_calcHashes(func, csModule, csFunc) == -1) {
  122: 		AIT_FREE_VAL(&func->func_name);
  123: 		AIT_FREE_VAL(&func->func_file);
  124: 		free(func);
  125: 		return -1;
  126: 	}
  127: 
  128: 	func->func_parent = srv;
  129: 
  130: 	/* allocate return variables */
  131: 	if (args > 0 && rpc_srv_allocVars(func, args) == -1) {
  132: 		AIT_FREE_VAL(&func->func_name);
  133: 		AIT_FREE_VAL(&func->func_file);
  134: 		free(func);
  135: 		return -1;
  136: 	}
  137: 
  138: 	/* add to list of functions */
  139: 	pthread_mutex_lock(&srv->srv_mtx);
  140: 	func->func_next = srv->srv_funcs;
  141: 	srv->srv_funcs = func;
  142: 	pthread_mutex_unlock(&srv->srv_mtx);
  143: 	return 0;
  144: }
  145: 
  146: /*
  147:  * rpc_srv_unregisterCall() - Unregister call from RPC server
  148:  *
  149:  * @srv = RPC Server instance
  150:  * @csModule = Module name, if NULL self binary
  151:  * @csFunc = Function name
  152:  * return: -1 error, 0 not found call, 1 unregister ok
  153:  */
  154: int
  155: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
  156: {
  157: 	rpc_func_t func, *f, *curr;
  158: 
  159: 	if (!srv || !csFunc) {
  160: 		rpc_SetErr(EINVAL, "Invalid parameter can`t unregister function");
  161: 		return -1;
  162: 	} else
  163: 		memset(&func, 0, sizeof func);
  164: 
  165: 	/* calculate hashes */
  166: 	if (rpc_calcHashes(&func, csModule, csFunc) == -1)
  167: 		return -1;
  168: 
  169: 	pthread_mutex_lock(&srv->srv_mtx);
  170: 	f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
  171: 	AIT_FREE_VAL(&func.func_name);
  172: 	AIT_FREE_VAL(&func.func_file);
  173: 	if (!f) {			/* not found element for unregister */
  174: 		pthread_mutex_unlock(&srv->srv_mtx);
  175: 		return 0;
  176: 	}
  177: 
  178: 	/* remove from list of functions */
  179: 	if (srv->srv_funcs == f)	/* if is 1st element */
  180: 		srv->srv_funcs = srv->srv_funcs->func_next;
  181: 	else {
  182: 		for (curr = srv->srv_funcs; curr->func_next != f; curr = curr->func_next);
  183: 		curr->func_next = curr->func_next->func_next;
  184: 	}
  185: 	io_freeVars(&f->func_vars);
  186: 	AIT_FREE_VAL(&f->func_name);
  187: 	AIT_FREE_VAL(&f->func_file);
  188: 	free(f);
  189: 	pthread_mutex_unlock(&srv->srv_mtx);
  190: 
  191: 	return 1;
  192: }
  193: 
  194: /*
  195:  * rpc_srv_getCall()  - Get registered call from RPC server
  196:  *
  197:  * @srv = RPC Server instance
  198:  * @tag = tag for function
  199:  * @hash = hash for function
  200:  * return: NULL not found call, !=NULL return call
  201:  */
  202: inline rpc_func_t *
  203: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag, uint32_t hash)
  204: {
  205: 	rpc_func_t *f;
  206: 
  207: 	if (!srv) {
  208: 		rpc_SetErr(EINVAL, "Invalid parameter can`t get function");
  209: 		return NULL;
  210: 	}
  211: 
  212: 	for (f = srv->srv_funcs; f; f = f->func_next)
  213: 		if (f->func_tag == tag && f->func_hash == hash)
  214: 			break;
  215: 
  216: 	return f;
  217: }
  218: 
  219: /*
  220:  * rpc_srv_getFunc() - Get registered call from RPC server by Name
  221:  *
  222:  * @srv = RPC Server instance
  223:  * @csModule = Module name, if NULL self binary
  224:  * @csFunc = Function name
  225:  * return: NULL not found call, !=NULL return call
  226:  */
  227: rpc_func_t *
  228: rpc_srv_getFunc(rpc_srv_t * __restrict srv, const char *csModule, const char *csFunc)
  229: {
  230: 	rpc_func_t func, *f = NULL;
  231: 
  232: 	if (!srv || !csFunc) {
  233: 		rpc_SetErr(EINVAL, "Invalid parameter can`t get function");
  234: 		return NULL;
  235: 	} else
  236: 		memset(&func, 0, sizeof(rpc_func_t));
  237: 
  238: 	/* calculate hashes */
  239: 	if (rpc_calcHashes(&func, csModule, csFunc) == -1)
  240: 		return NULL;
  241: 
  242: 	f = rpc_srv_getCall(srv, func.func_tag, func.func_hash);
  243: 
  244: 	AIT_FREE_VAL(&func.func_name);
  245: 	AIT_FREE_VAL(&func.func_file);
  246: 	return f;
  247: }
  248: 
  249: // ---------------------------------------------------------
  250: 
  251: /*
  252:  * rpc_srv_getBLOB() - Get registered BLOB 
  253:  *
  254:  * @srv = RPC Server instance
  255:  * @var = hash for variable
  256:  * return: NULL not found, !=NULL return blob var
  257:  */
  258: inline rpc_blob_t *
  259: rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var)
  260: {
  261: 	rpc_blob_t *b;
  262: 
  263: 	if (!srv) {
  264: 		rpc_SetErr(EINVAL, "Invalid parameter can`t get BLOB variable");
  265: 		return NULL;
  266: 	}
  267: 
  268: 	for (b = srv->srv_blob.blobs; b; b = b->blob_next) {
  269: 		if (b->blob_var == var)
  270: 			break;
  271: 	}
  272: 
  273: 	return b;
  274: }
  275: 
  276: /*
  277:  * rpc_srv_registerBLOB() - Register new BLOB to server
  278:  *
  279:  * @srv = RPC Server instance
  280:  * @len = BLOB length
  281:  * return: NULL error or new registered BLOB
  282:  */
  283: rpc_blob_t *
  284: rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len)
  285: {
  286: 	rpc_blob_t *blob = NULL;
  287: 
  288: 	if (!srv || !len) {
  289: 		rpc_SetErr(EINVAL, "Invalid parameter can`t register BLOB variable");
  290: 		return blob;
  291: 	}
  292: 
  293: 	blob = rpc_srv_blobCreate(srv, len);
  294: 	if (blob) {
  295: 		pthread_mutex_lock(&srv->srv_blob.mtx);
  296: 		blob->blob_next = srv->srv_blob.blobs;
  297: 		srv->srv_blob.blobs = blob;
  298: 		pthread_mutex_unlock(&srv->srv_blob.mtx);
  299: 	}
  300: 
  301: 	return blob;
  302: }
  303: 
  304: /*
  305:  * rpc_srv_unregisterBLOB() - Unregister BLOB from server
  306:  *
  307:  * @srv = RPC Server instance
  308:  * @var = BLOB Variable for unregister
  309:  * return: -1 error, 0 not found call, 1 unregister ok
  310:  */
  311: int
  312: rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var)
  313: {
  314: 	rpc_blob_t *b, *curr;
  315: 
  316: 	if (!srv) {
  317: 		rpc_SetErr(EINVAL, "Invalid parameter can`t unregister BLOB variable");
  318: 		return -1;
  319: 	}
  320: 
  321: 	b = rpc_srv_getBLOB(srv, var);
  322: 	if (!b)				/* not found element for unregister */
  323: 		return 0;
  324: 	/* if BLOB is unmapped force to unmap object */
  325: 	if (b->blob_data)
  326: 		rpc_srv_blobUnmap(b);
  327: 
  328: 	pthread_mutex_lock(&srv->srv_blob.mtx);
  329: 	if (srv->srv_blob.blobs == b) {	/* if is 1st element */
  330: 		srv->srv_blob.blobs = srv->srv_blob.blobs->blob_next;
  331: 	} else {
  332: 		for (curr = srv->srv_blob.blobs; curr->blob_next != b; curr = curr->blob_next);
  333: 		curr->blob_next = curr->blob_next->blob_next;
  334: 	}
  335: 	pthread_mutex_unlock(&srv->srv_blob.mtx);
  336: 	rpc_srv_blobFree(srv, b);
  337: 	free(b);
  338: 
  339: 	return 1;
  340: }
  341: 
  342: /*
  343:  * rpc_calcHashes() - Calculate hashes for RPC call
  344:  *
  345:  * @func = function
  346:  * @csModule = Module name, if NULL self binary
  347:  * @csFunc = Function name
  348:  * return: -1 error or 0 ok
  349:  */
  350: int
  351: rpc_calcHashes(rpc_func_t * __restrict func, const char *csModule, const char *csFunc)
  352: {
  353: 	char *str = NULL;
  354: 	int len = 0;
  355: 
  356: 	assert(func && csFunc);
  357: 
  358: 	/* set function name */
  359: 	AIT_SET_STR(&func->func_name, csFunc);
  360: 	len = strlen(csFunc) + 3;	/* extra 3 bytes, because add string "__" and 0 */
  361: 	/* set module name if exists */
  362: 	if (csModule) {
  363: 		AIT_SET_STR(&func->func_file, csModule);
  364: 		len += strlen(csModule);
  365: 	}
  366: 	/* align len to 2 */
  367: 	len = io_align(len, 1);
  368: 
  369: 	/* prepare hash source string */
  370: 	str = malloc(len);
  371: 	if (!str) {
  372: 		LOGERR;
  373: 		return -1;
  374: 	} else {
  375: 		memset(str, 0, len);
  376: 		if (csModule)
  377: 			strlcpy((char*) str, csModule, len);
  378: 		strlcat((char*) str, "__", len);
  379: 		strlcat((char*) str, csFunc, len);
  380: 	}
  381: 
  382: 	func->func_tag = crcFletcher16((u_short*) str, len / 2);
  383: 	func->func_hash = hash_fnv((char*) str, len);
  384: 
  385: 	free(str);
  386: 	return len;
  387: }
  388: 

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