File:  [ELWIX - Embedded LightWeight unIX -] / libaitrpc / src / lists.c
Revision 1.5.2.2: download - view: text, annotated - select for diffs - revision graph
Thu Nov 3 14:57:56 2011 UTC (12 years, 9 months ago) by misho
Branches: rpc2_0
Diff to: branchpoint 1.5: preferred, unified
remove func

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

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