File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / lists.c
Revision 1.18: download - view: text, annotated - select for diffs - revision graph
Thu Jul 2 22:28:15 2015 UTC (8 years, 11 months ago) by misho
Branches: MAIN
CVS tags: rpc9_6, rpc9_5, rpc9_4, rpc9_3, rpc9_2, RPC9_5, RPC9_4, RPC9_3, RPC9_2, RPC9_1, HEAD
version 9.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.18 2015/07/02 22:28:15 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 - 2015
   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: #pragma GCC visibility push(hidden)
   50: 
   51: static int
   52: rpc_funcs_cmp(struct tagRPCFunc *a, struct tagRPCFunc *b)
   53: {
   54: 	int ret;
   55: 
   56: 	assert(a && b);
   57: 
   58: 	ret = AIT_KEY(&a->func_name) - AIT_KEY(&b->func_name);
   59: 
   60: 	if (ret < 0)
   61: 		return -1;
   62: 	else if (ret > 0)
   63: 		return 1;
   64: 
   65: 	return ret;
   66: }
   67: 
   68: AVL_GENERATE(tagRPCFuncs, tagRPCFunc, func_node, rpc_funcs_cmp);
   69: 
   70: #pragma GCC visibility pop
   71: 
   72: /*
   73:  * rpc_srv_registerCall() - Register call to RPC server
   74:  *
   75:  * @srv = RPC Server instance
   76:  * @tag = Function tag
   77:  * @funcaddr = Function address
   78:  * return: -1 error, 0 already registered tag or 1 register ok
   79:  */
   80: int
   81: rpc_srv_registerCall(rpc_srv_t * __restrict srv, u_short tag, void *funcaddr)
   82: {
   83: 	rpc_func_t *func;
   84: 
   85: 	if (!srv || !funcaddr) {
   86: 		rpc_SetErr(EINVAL, "Invalid parameter can`t register function");
   87: 		return -1;
   88: 	}
   89: 
   90: 	if (!(func = e_malloc(sizeof(rpc_func_t)))) {
   91: 		LOGERR;
   92: 		return -1;
   93: 	} else {
   94: 		memset(func, 0, sizeof(rpc_func_t));
   95: 		AIT_KEY(&func->func_name) = tag;
   96: 	}
   97: 
   98: 	/* search for duplicate */
   99: 	if (AVL_FIND(tagRPCFuncs, &srv->srv_funcs, func)) {
  100: 		e_free(func);
  101: 		return 0;
  102: 	}
  103: 
  104: 	func->func_parent = srv;
  105: 	AIT_SET_PTR(&func->func_name, funcaddr, 0);
  106: 
  107: 	/* add to list of functions */
  108: 	RPC_FUNCS_LOCK(&srv->srv_funcs);
  109: 	SLIST_INSERT_HEAD(&srv->srv_funcs, func, func_next);
  110: 	AVL_INSERT(tagRPCFuncs, &srv->srv_funcs, func);
  111: 	RPC_FUNCS_UNLOCK(&srv->srv_funcs);
  112: 	return 1;
  113: }
  114: 
  115: /*
  116:  * rpc_srv_unregisterCall() - Unregister call from RPC server
  117:  *
  118:  * @srv = RPC Server instance
  119:  * @tag = Function tag
  120:  * return: -1 error, 0 not found call, 1 unregister ok
  121:  */
  122: int
  123: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, u_short tag)
  124: {
  125: 	rpc_func_t *f;
  126: 
  127: 	if (!srv) {
  128: 		rpc_SetErr(EINVAL, "Invalid parameter can`t unregister function");
  129: 		return -1;
  130: 	}
  131: 
  132: 	f = rpc_srv_getCall(srv, tag);
  133: 	if (!f) 		/* not found element for unregister */
  134: 		return 0;
  135: 
  136: 	RPC_FUNCS_LOCK(&srv->srv_funcs);
  137: 	AVL_REMOVE(tagRPCFuncs, &srv->srv_funcs, f);
  138: 	SLIST_REMOVE(&srv->srv_funcs, f, tagRPCFunc, func_next);
  139: 	RPC_FUNCS_UNLOCK(&srv->srv_funcs);
  140: 
  141: 	AIT_FREE_VAL(&f->func_name);
  142: 	e_free(f);
  143: 	return 1;
  144: }
  145: 
  146: /*
  147:  * rpc_srv_getCall()  - Get registered call from RPC server
  148:  *
  149:  * @srv = RPC Server instance
  150:  * @tag = tag for function
  151:  * return: NULL not found call, !=NULL return call
  152:  */
  153: rpc_func_t *
  154: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag)
  155: {
  156: 	rpc_func_t tmp;
  157: 
  158: 	if (!srv) {
  159: 		rpc_SetErr(EINVAL, "Invalid parameter can`t get function");
  160: 		return NULL;
  161: 	} else
  162: 		memset(&tmp, 0, sizeof tmp);
  163: 
  164: 	AIT_KEY(&tmp.func_name) = tag;
  165: 	return AVL_FIND(tagRPCFuncs, &srv->srv_funcs, &tmp);
  166: }
  167: 
  168: /* --------------------------------------------------------- */
  169: 
  170: /*
  171:  * rpc_srv_getBLOB() - Get registered BLOB 
  172:  *
  173:  * @srv = RPC Server instance
  174:  * @var = hash for variable
  175:  * return: NULL not found, !=NULL return blob var
  176:  */
  177: rpc_blob_t *
  178: rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var)
  179: {
  180: 	rpc_blob_t *b, *tmp;
  181: 
  182: 	if (!srv) {
  183: 		rpc_SetErr(EINVAL, "Invalid parameter can`t get BLOB variable");
  184: 		return NULL;
  185: 	}
  186: 
  187: 	TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp)
  188: 		if (b->blob_var == var)
  189: 			break;
  190: 
  191: 	return b;
  192: }
  193: 
  194: /*
  195:  * rpc_srv_registerBLOB() - Register new BLOB to server
  196:  *
  197:  * @srv = RPC Server instance
  198:  * @len = BLOB length
  199:  * @tout = BLOB live timeout in seconds
  200:  * return: NULL error or new registered BLOB
  201:  */
  202: rpc_blob_t *
  203: rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len, int tout)
  204: {
  205: 	rpc_blob_t *blob = NULL;
  206: 
  207: 	if (!srv || !len) {
  208: 		rpc_SetErr(EINVAL, "Invalid parameter can`t register BLOB variable");
  209: 		return blob;
  210: 	}
  211: 
  212: 	blob = rpc_srv_blobCreate(srv, len, tout);
  213: 
  214: 	TAILQ_INSERT_TAIL(&srv->srv_blob.blobs, blob, blob_node);
  215: 
  216: 	return blob;
  217: }
  218: 
  219: /*
  220:  * rpc_srv_unregisterBLOB() - Unregister BLOB from server
  221:  *
  222:  * @srv = RPC Server instance
  223:  * @var = BLOB Variable for unregister
  224:  * return: -1 error, 0 not found call, 1 unregister ok
  225:  */
  226: int
  227: rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var)
  228: {
  229: 	rpc_blob_t *b;
  230: 
  231: 	if (!srv) {
  232: 		rpc_SetErr(EINVAL, "Invalid parameter can`t unregister BLOB variable");
  233: 		return -1;
  234: 	}
  235: 
  236: 	b = rpc_srv_getBLOB(srv, var);
  237: 	if (!b)				/* not found element for unregister */
  238: 		return 0;
  239: 
  240: 	TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
  241: 
  242: 	rpc_srv_blobFree(srv, b);
  243: 	e_free(b);
  244: 	return 1;
  245: }

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