Annotation of libaitrpc/src/lists.c, revision 1.14.2.2

1.2       misho       1: /*************************************************************************
                      2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.14.2.2! misho       6: * $Id: lists.c,v 1.14.2.1 2013/07/15 14:54:40 misho Exp $
1.2       misho       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: 
1.13      misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
1.2       misho      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: /*
1.7       misho      50:  * rpc_srv_registerCall() - Register call to RPC server
                     51:  *
1.2       misho      52:  * @srv = RPC Server instance
1.10      misho      53:  * @tag = Function tag
                     54:  * @funcaddr = Function address
                     55:  * return: -1 error, 0 already registered tag or 1 register ok
1.2       misho      56:  */
                     57: int
1.10      misho      58: rpc_srv_registerCall(rpc_srv_t * __restrict srv, u_short tag, void *funcaddr)
1.2       misho      59: {
                     60:        rpc_func_t *func;
                     61: 
1.10      misho      62:        if (!srv || !funcaddr) {
1.7       misho      63:                rpc_SetErr(EINVAL, "Invalid parameter can`t register function");
1.2       misho      64:                return -1;
                     65:        }
1.10      misho      66: 
1.13      misho      67:        if (!(func = e_malloc(sizeof(rpc_func_t)))) {
1.2       misho      68:                LOGERR;
                     69:                return -1;
1.10      misho      70:        } else {
1.2       misho      71:                memset(func, 0, sizeof(rpc_func_t));
1.12      misho      72:                AIT_KEY(&func->func_name) = tag;
                     73:        }
                     74: 
                     75:        /* search for duplicate */
                     76:        if (AVL_FIND(tagRPCFuncs, &srv->srv_funcs, func)) {
1.13      misho      77:                e_free(func);
1.12      misho      78:                return 0;
1.2       misho      79:        }
                     80: 
1.12      misho      81:        func->func_parent = srv;
1.10      misho      82:        AIT_SET_PTR(&func->func_name, funcaddr, 0);
1.2       misho      83: 
1.7       misho      84:        /* add to list of functions */
1.12      misho      85:        RPC_FUNCS_LOCK(&srv->srv_funcs);
                     86:        SLIST_INSERT_HEAD(&srv->srv_funcs, func, func_next);
                     87:        AVL_INSERT(tagRPCFuncs, &srv->srv_funcs, func);
                     88:        RPC_FUNCS_UNLOCK(&srv->srv_funcs);
1.10      misho      89:        return 1;
1.2       misho      90: }
                     91: 
                     92: /*
1.7       misho      93:  * rpc_srv_unregisterCall() - Unregister call from RPC server
                     94:  *
1.2       misho      95:  * @srv = RPC Server instance
1.10      misho      96:  * @tag = Function tag
1.2       misho      97:  * return: -1 error, 0 not found call, 1 unregister ok
                     98:  */
                     99: int
1.10      misho     100: rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, u_short tag)
1.2       misho     101: {
1.10      misho     102:        rpc_func_t *f;
1.2       misho     103: 
1.10      misho     104:        if (!srv) {
1.7       misho     105:                rpc_SetErr(EINVAL, "Invalid parameter can`t unregister function");
1.2       misho     106:                return -1;
1.10      misho     107:        }
1.2       misho     108: 
1.10      misho     109:        f = rpc_srv_getCall(srv, tag);
1.9       misho     110:        if (!f)                 /* not found element for unregister */
1.2       misho     111:                return 0;
                    112: 
1.12      misho     113:        RPC_FUNCS_LOCK(&srv->srv_funcs);
                    114:        AVL_REMOVE(tagRPCFuncs, &srv->srv_funcs, f);
                    115:        SLIST_REMOVE(&srv->srv_funcs, f, tagRPCFunc, func_next);
                    116:        RPC_FUNCS_UNLOCK(&srv->srv_funcs);
1.10      misho     117: 
1.7       misho     118:        AIT_FREE_VAL(&f->func_name);
1.13      misho     119:        e_free(f);
1.2       misho     120:        return 1;
                    121: }
                    122: 
                    123: /*
1.7       misho     124:  * rpc_srv_getCall()  - Get registered call from RPC server
                    125:  *
1.2       misho     126:  * @srv = RPC Server instance
                    127:  * @tag = tag for function
                    128:  * return: NULL not found call, !=NULL return call
                    129:  */
1.14      misho     130: rpc_func_t *
1.10      misho     131: rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag)
1.2       misho     132: {
1.12      misho     133:        rpc_func_t tmp;
1.2       misho     134: 
                    135:        if (!srv) {
1.7       misho     136:                rpc_SetErr(EINVAL, "Invalid parameter can`t get function");
1.2       misho     137:                return NULL;
1.12      misho     138:        } else
                    139:                memset(&tmp, 0, sizeof tmp);
1.2       misho     140: 
1.12      misho     141:        AIT_KEY(&tmp.func_name) = tag;
                    142:        return AVL_FIND(tagRPCFuncs, &srv->srv_funcs, &tmp);
1.2       misho     143: }
                    144: 
1.10      misho     145: /* --------------------------------------------------------- */
1.2       misho     146: 
                    147: /*
1.7       misho     148:  * rpc_srv_getBLOB() - Get registered BLOB 
                    149:  *
1.2       misho     150:  * @srv = RPC Server instance
                    151:  * @var = hash for variable
                    152:  * return: NULL not found, !=NULL return blob var
                    153:  */
1.14      misho     154: rpc_blob_t *
1.2       misho     155: rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var)
                    156: {
1.10      misho     157:        rpc_blob_t *b, *tmp;
1.2       misho     158: 
                    159:        if (!srv) {
1.7       misho     160:                rpc_SetErr(EINVAL, "Invalid parameter can`t get BLOB variable");
1.2       misho     161:                return NULL;
                    162:        }
                    163: 
1.10      misho     164:        TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp)
1.2       misho     165:                if (b->blob_var == var)
                    166:                        break;
                    167: 
                    168:        return b;
                    169: }
                    170: 
1.14.2.1  misho     171: static void *
                    172: toutBLOB(sched_task_t *task)
                    173: {
                    174:        rpc_cli_t *c = TASK_ARG(task);
                    175: 
                    176:        rpc_srv_unregisterBLOB((rpc_srv_t*) c->cli_parent, (uint32_t) TASK_DATA(task));
                    177: 
                    178:        return NULL;
                    179: }
                    180: 
1.2       misho     181: /*
1.7       misho     182:  * rpc_srv_registerBLOB() - Register new BLOB to server
                    183:  *
1.2       misho     184:  * @srv = RPC Server instance
                    185:  * @len = BLOB length
1.14.2.1  misho     186:  * @tout = BLOB live timeout in seconds
1.14.2.2! misho     187:  * @c = RPC Client
1.2       misho     188:  * return: NULL error or new registered BLOB
                    189:  */
                    190: rpc_blob_t *
1.14.2.1  misho     191: rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len, int tout, 
1.14.2.2! misho     192:                rpc_cli_t * __restrict c)
1.2       misho     193: {
                    194:        rpc_blob_t *blob = NULL;
1.14.2.1  misho     195:        struct timespec ts = { tout ? tout : RPC_BLOB_TIMEOUT, 0 };
1.2       misho     196: 
1.14.2.2! misho     197:        if (!srv || !len || !c) {
1.7       misho     198:                rpc_SetErr(EINVAL, "Invalid parameter can`t register BLOB variable");
1.2       misho     199:                return blob;
                    200:        }
                    201: 
                    202:        blob = rpc_srv_blobCreate(srv, len);
                    203: 
1.10      misho     204:        TAILQ_INSERT_TAIL(&srv->srv_blob.blobs, blob, blob_node);
1.14.2.1  misho     205: 
1.14.2.2! misho     206:        schedTimer(srv->srv_blob.root, toutBLOB, c, ts, 
1.14.2.1  misho     207:                        (void*) (intptr_t) blob->blob_var, blob->blob_len);
1.2       misho     208:        return blob;
                    209: }
                    210: 
                    211: /*
1.7       misho     212:  * rpc_srv_unregisterBLOB() - Unregister BLOB from server
                    213:  *
1.2       misho     214:  * @srv = RPC Server instance
                    215:  * @var = BLOB Variable for unregister
                    216:  * return: -1 error, 0 not found call, 1 unregister ok
                    217:  */
                    218: int
                    219: rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var)
                    220: {
1.10      misho     221:        rpc_blob_t *b;
1.2       misho     222: 
                    223:        if (!srv) {
1.7       misho     224:                rpc_SetErr(EINVAL, "Invalid parameter can`t unregister BLOB variable");
1.2       misho     225:                return -1;
                    226:        }
                    227: 
                    228:        b = rpc_srv_getBLOB(srv, var);
1.3       misho     229:        if (!b)                         /* not found element for unregister */
1.2       misho     230:                return 0;
                    231: 
1.10      misho     232:        TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node);
                    233: 
1.14.2.2! misho     234:        schedCancelby(srv->srv_blob.root, taskTIMER, CRITERIA_DATA, 
        !           235:                        (void*) (intptr_t) b->blob_var, NULL);
        !           236: 
1.2       misho     237:        rpc_srv_blobFree(srv, b);
1.13      misho     238:        e_free(b);
1.2       misho     239:        return 1;
                    240: }

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