File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / lists.c
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Thu Nov 3 15:32:21 2011 UTC (12 years, 7 months ago) by misho
Branches: MAIN
CVS tags: rpc2_1, RPC2_0, HEAD
version 2.0

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

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