Annotation of libaitrpc/inc/aitrpc.h, revision 1.27.2.2

1.1       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.27.2.2! misho       6: * $Id: aitrpc.h,v 1.27.2.1 2015/06/29 22:29:25 misho Exp $
1.1       misho       7: *
1.2       misho       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.26      misho      15: Copyright 2004 - 2015
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: */
1.1       misho      46: #ifndef __AITRPC_H
                     47: #define __AITRPC_H
                     48: 
                     49: 
                     50: #include <assert.h>
1.27.2.2! misho      51: //#include <stdlib.h>
        !            52: //#include <string.h>
        !            53: //#include <errno.h>
        !            54: //#include <sys/types.h>
        !            55: //#include <sys/param.h>
        !            56: //#if !defined(__NetBSD__)
        !            57: //#include <sys/limits.h>
        !            58: //#endif
        !            59: //#include <sys/socket.h>
        !            60: #include <pthread.h>
1.9       misho      61: #include <sys/queue.h>
1.27.2.2! misho      62: #include <aitrpc_pkt.h>
        !            63: #include <aitrpc_cli.h>
1.6       misho      64: #include <aitsched.h>
1.1       misho      65: 
                     66: 
1.9       misho      67: #define RPC_CALLBACK_CHECK_INPUT(x)    do { \
                     68:                                                assert((x)); \
                     69:                                                if (!(x)) { \
1.6       misho      70:                                                        rpc_SetErr(EINVAL, \
                     71:                                                                        "Invalid callback parameters ..."); \
1.2       misho      72:                                                        return -1; \
                     73:                                                } \
                     74:                                        } while (0)
1.1       misho      75: 
                     76: 
1.27.2.2! misho      77: /* Network RPC server elements */
1.1       misho      78: 
1.9       misho      79: /* RPC function registration element! */
                     80: typedef struct tagRPCFunc {
                     81:        ait_val_t               func_name;
1.1       misho      82: 
1.9       misho      83:        void                    *func_parent;
1.11      misho      84: 
                     85:        SLIST_ENTRY(tagRPCFunc) func_next;
                     86:        AVL_ENTRY(tagRPCFunc)   func_node;
1.9       misho      87: } rpc_func_t;
                     88: #define RPC_FUNC_SERVER(x)     ((rpc_srv_t*) (x)->func_parent)
1.8       misho      89: 
1.11      misho      90: /* Tree root node */
                     91: typedef struct tagRPCFuncs {
                     92:        pthread_mutex_t         mtx;
                     93: 
                     94:        struct tagRPCFunc       *slh_first;
                     95:        struct tagRPCFunc       *avlh_root;
                     96: } rpc_funcs_t;
                     97: #define RPC_FUNCS_LOCK(x)      pthread_mutex_lock(&(x)->mtx)
                     98: #define RPC_FUNCS_UNLOCK(x)    pthread_mutex_unlock(&(x)->mtx)
                     99: #define RPC_FUNCS_ISEMPTY(x)   AVL_EMPTY((x))
                    100: 
1.1       misho     101: 
1.9       misho     102: /* BLOB register element */
1.2       misho     103: typedef struct tagBLOB {
1.9       misho     104:        uint32_t                blob_var;       /* BLOB id */
1.2       misho     105: 
1.9       misho     106:        size_t                  blob_len;       /* size of allocated BLOB data */
                    107:        void                    *blob_data;     /* mapped BLOB data */
1.2       misho     108: 
1.9       misho     109:        TAILQ_ENTRY(tagBLOB)    blob_node;
1.2       misho     110: } rpc_blob_t;
                    111: 
1.9       misho     112: 
1.1       misho     113: typedef struct {
1.9       misho     114:        rpc_sess_t                      srv_session;    /* RPC session registration info */
                    115:        int                             srv_netbuf;     /* size of network buffer */
1.12      misho     116:        int                             srv_proto;      /* Server protocol */
1.2       misho     117: 
1.9       misho     118:        pthread_t                       srv_tid;        /* RPC exec pthread */
                    119:        sched_root_task_t               *srv_root;      /* RPC server scheduler */
                    120:        intptr_t                        srv_kill;       /* Scheduler condition variable */
                    121: 
                    122:        rpc_cli_t                       srv_server;     /* RPC server socket */
                    123:        array_t                         *srv_clients;   /* connected rpc client sockets */
                    124: 
1.11      misho     125:        rpc_funcs_t                     srv_funcs;      /* RPC functions */
1.2       misho     126: 
                    127:        struct {
1.9       misho     128:                pthread_t                       tid;            /* BLOB exec pthread */
                    129:                sched_root_task_t               *root;          /* BLOB server scheduler */
                    130:                intptr_t                        kill;           /* BLOB server state: ==0 disable | !=0 enable */
1.2       misho     131: 
1.9       misho     132:                ait_val_t                       dir;            /* BLOB states directory */
1.1       misho     133: 
1.9       misho     134:                rpc_cli_t                       server;         /* BLOB server socket */
                    135:                array_t                         *clients;       /* connected blob client sockets */
                    136: 
                    137:                TAILQ_HEAD(, tagBLOB)           blobs;          /* registered blob variables list */
                    138:        }                               srv_blob;
1.1       misho     139: } rpc_srv_t;
                    140: 
                    141: 
1.2       misho     142: /* 
1.6       misho     143:  * (*rpc_callback_t)() - Callback type definition for RPC call in server process
                    144:  *
1.9       misho     145:  * @arg1 = RPC client
                    146:  * @arg2 = RPC packet header
1.2       misho     147:  * @arg3 = input array with values from RPC call execution request
                    148:  * return: -1 error or >-1 success execution
                    149:  */
1.9       misho     150: typedef int (*rpc_callback_t)(rpc_cli_t *, struct tagRPCCall *, array_t *);
1.1       misho     151: 
1.24      misho     152: #define RPC_CALL_DEFINE(x)     int (x)(rpc_cli_t*, struct tagRPCCall*, array_t*)
                    153: #define RPC_CALL_ARGS(arg1, arg2, arg3)        rpc_cli_t* arg1, struct tagRPCCall* arg2, array_t* arg3
                    154: #define RPC_CALL_STDARGS       RPC_CALL_ARGS(cli, rpc, iv)
                    155: 
1.1       misho     156: 
1.9       misho     157: /* ----------------------------------------------------------------------- */
1.1       misho     158: 
1.9       misho     159: /*
                    160:  * rpc_register_srvPing() - Register ping service function
                    161:  *
                    162:  * @srv = RPC server instance
                    163:  * return: -1 error or 0 ok
                    164:  */
1.15      misho     165: int rpc_register_srvPing(rpc_srv_t * __restrict srv);
1.9       misho     166: /*
                    167:  * rpc_register_srvServices() - Register internal service functions
                    168:  *
                    169:  * @srv = RPC server instance
                    170:  * return: -1 error or 0 ok
                    171:  */
                    172: int rpc_register_srvServices(rpc_srv_t * __restrict srv);
                    173: /*
                    174:  * rpc_register_blobServices() - Register internal service functions
                    175:  *
                    176:  * @srv = RPC server instance
                    177:  * return: -1 error or 0 ok
                    178:  */
                    179: int rpc_register_blobServices(rpc_srv_t * __restrict srv);
1.5       misho     180: 
                    181: 
1.1       misho     182: /* RPC Server side functions */
                    183: 
                    184: /*
1.6       misho     185:  * rpc_srv_initServer() - Init & create RPC Server
                    186:  *
1.14      misho     187:  * @InstID = Instance for authentication & recognition
1.1       misho     188:  * @concurentClients = Concurent clients at same time to this server
1.9       misho     189:  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
1.3       misho     190:  * @csHost = Host name or address for bind server, if NULL any address
1.1       misho     191:  * @Port = Port for bind server, if Port == 0 default port is selected
1.12      misho     192:  * @proto = Protocol, if == 0 choose SOCK_STREAM
1.1       misho     193:  * return: NULL == error or !=NULL bind and created RPC server instance
                    194:  */
1.14      misho     195: rpc_srv_t *rpc_srv_initServer(unsigned char InstID, int concurentClients, int netBuf, 
1.12      misho     196:                const char *csHost, unsigned short Port, int proto);
1.1       misho     197: /*
1.6       misho     198:  * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
                    199:  *
1.5       misho     200:  * @psrv = RPC Server instance
1.1       misho     201:  * return: none
                    202:  */
1.15      misho     203: void rpc_srv_endServer(rpc_srv_t ** __restrict psrv);
1.1       misho     204: /*
1.6       misho     205:  * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
                    206:  *
1.1       misho     207:  * @srv = RPC Server instance
                    208:  * return: -1 error or 0 ok, infinite loop ...
                    209:  */
1.4       misho     210: int rpc_srv_loopServer(rpc_srv_t * __restrict srv);
1.25      misho     211: #define rpc_srv_execServer(_srv, _sync) \
                    212:        do { assert((_srv)); \
                    213:                if (!(_srv)->srv_kill) { \
                    214:                        pthread_create(&(_srv)->srv_tid, NULL, (void*(*)(void*)) \
                    215:                                        rpc_srv_loopServer, (_srv)); \
                    216:                        if ((_sync)) \
                    217:                                pthread_join((_srv)->srv_tid, (void**) (_sync)); \
                    218:                        else \
                    219:                                pthread_detach((_srv)->srv_tid); \
                    220:        } } while (0)
                    221: #define rpc_srv_killServer(_srv) \
                    222:        (assert((_srv)), (_srv)->srv_blob.kill = 1, (_srv)->srv_kill = 1)
1.1       misho     223: 
                    224: /*
1.6       misho     225:  * rpc_srv_initBLOBServer() - Init & create BLOB Server
                    226:  *
1.3       misho     227:  * @srv = RPC server instance
1.2       misho     228:  * @Port = Port for bind server, if Port == 0 default port is selected
                    229:  * @diskDir = Disk place for BLOB file objects
                    230:  * return: -1 == error or 0 bind and created BLOB server instance
                    231:  */
1.9       misho     232: int rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, unsigned short Port, const char *diskDir);
1.2       misho     233: /*
1.6       misho     234:  * rpc_srv_endBLOBServer() - Destroy BLOB server, close all opened sockets and free resources
                    235:  *
1.2       misho     236:  * @srv = RPC Server instance
                    237:  * return: none
                    238:  */
1.15      misho     239: void rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv);
1.2       misho     240: /*
1.6       misho     241:  * rpc_srv_loopBLOB() - Execute Main BLOB server loop and wait for clients requests
                    242:  *
1.2       misho     243:  * @srv = RPC Server instance
                    244:  * return: -1 error or 0 ok, infinite loop ...
                    245:  */
1.9       misho     246: int rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv);
1.25      misho     247: #define rpc_srv_execBLOBServer(_srv) \
                    248:        do { assert((_srv)); \
                    249:                if (!(_srv)->srv_kill && !(_srv)->srv_blob.kill) { \
                    250:                        pthread_create(&(_srv)->srv_blob.tid, NULL, \
                    251:                                        (void*(*)(void*)) rpc_srv_loopBLOBServer, (_srv)); \
                    252:                        pthread_detach((_srv)->srv_blob.tid); \
                    253:                } \
                    254:        } while (0)
                    255: 
                    256: /*
                    257:  * rpc_srv_initServer2() - Init & create layer2 RPC Server
                    258:  *
                    259:  * @InstID = Instance for authentication & recognition
                    260:  * @concurentClients = Concurent clients at same time to this server
                    261:  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
                    262:  * @csIface = Interface name for bind server, if NULL first interface on host
                    263:  * return: NULL == error or !=NULL bind and created RPC server instance
                    264:  */
                    265: rpc_srv_t *rpc_srv_initServer2(u_char InstID, int concurentClients, int netBuf, 
                    266:                const char *csIface);
1.2       misho     267: 
                    268: /*
1.26      misho     269:  * rpc_srv_initServerExt() - Init & create pipe RPC Server
                    270:  *
                    271:  * @InstID = Instance for authentication & recognition
                    272:  * @netBuf = Network buffer length (min:512 bytes), if =0 == BUFSIZ (also meaning max RPC packet)
                    273:  * @fd = File descriptor
                    274:  * return: NULL == error or !=NULL bind and created RPC server instance
                    275:  */
                    276: rpc_srv_t *rpc_srv_initServerExt(u_char InstID, int netBuf, int fd);
                    277: 
                    278: /*
1.6       misho     279:  * rpc_srv_registerCall() - Register call to RPC server
                    280:  *
1.1       misho     281:  * @srv = RPC Server instance
1.9       misho     282:  * @tag = Function tag
                    283:  * @funcaddr = Function address
                    284:  * return: -1 error, 0 already registered tag or 1 register ok
1.1       misho     285:  */
1.9       misho     286: int rpc_srv_registerCall(rpc_srv_t * __restrict srv, unsigned short tag, void *funcaddr);
1.1       misho     287: /*
1.6       misho     288:  * rpc_srv_unregisterCall() - Unregister call from RPC server
                    289:  *
1.1       misho     290:  * @srv = RPC Server instance
1.9       misho     291:  * @tag = Function tag
1.1       misho     292:  * return: -1 error, 0 not found call, 1 unregister ok
                    293:  */
1.9       misho     294: int rpc_srv_unregisterCall(rpc_srv_t * __restrict srv, unsigned short tag);
1.1       misho     295: /*
1.9       misho     296:  * rpc_srv_getCall()  - Get registered call from RPC server
1.6       misho     297:  *
1.1       misho     298:  * @srv = RPC Server instance
                    299:  * @tag = tag for function
                    300:  * return: NULL not found call, !=NULL return call
                    301:  */
1.15      misho     302: rpc_func_t *rpc_srv_getCall(rpc_srv_t * __restrict srv, uint16_t tag);
1.1       misho     303: /*
1.9       misho     304:  * rpc_srv_execCall() Execute registered call from RPC server
1.6       misho     305:  *
1.9       misho     306:  * @cli = RPC client
1.1       misho     307:  * @rpc = IN RPC call structure
1.9       misho     308:  * @funcname = Execute RPC function
1.4       misho     309:  * @args = IN RPC calling arguments from RPC client
1.1       misho     310:  * return: -1 error, !=-1 ok
                    311:  */
1.9       misho     312: int rpc_srv_execCall(rpc_cli_t * __restrict cli, struct tagRPCCall * __restrict rpc, 
                    313:                ait_val_t funcname, array_t * __restrict args);
1.1       misho     314: 
                    315: 
1.2       misho     316: /*
1.9       misho     317:  * rpc_srv_blobCreate() - Create and map blob to memory region and return object
1.6       misho     318:  *
1.2       misho     319:  * @srv = RPC Server instance
                    320:  * @len = BLOB length object
1.16      misho     321:  * @tout = BLOB live timeout in seconds
1.2       misho     322:  * return: NULL error or !=NULL allocated BLOB object
                    323:  */
1.16      misho     324: rpc_blob_t *rpc_srv_blobCreate(rpc_srv_t * __restrict srv, int len, int tout);
1.2       misho     325: /*
1.6       misho     326:  * rpc_srv_blobMap() - Map blob to memory region 
                    327:  *
1.2       misho     328:  * @srv = RPC Server instance
                    329:  * @blob = Map to this BLOB element
                    330:  * return: -1 error or 0 ok
                    331:  */
1.15      misho     332: int rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob);
1.2       misho     333: /*
1.6       misho     334:  * rpc_srv_blobUnmap() - Unmap blob memory region 
                    335:  *
1.2       misho     336:  * @blob = Mapped BLOB element
                    337:  * return: none
                    338:  */
1.15      misho     339: void rpc_srv_blobUnmap(rpc_blob_t * __restrict blob);
1.2       misho     340: /*
1.6       misho     341:  * rpc_srv_blobFree() - Free blob from disk & memory
                    342:  *
1.2       misho     343:  * @srv = RPC Server instance
                    344:  * @blob = Mapped BLOB element
                    345:  * return: -1 error or 0 ok
                    346:  */
1.15      misho     347: int rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob);
1.2       misho     348: 
                    349: /*
1.6       misho     350:  * rpc_srv_registerBLOB() - Register new BLOB to server
                    351:  *
1.2       misho     352:  * @srv = RPC Server instance
                    353:  * @len = BLOB length
1.16      misho     354:  * @tout = BLOB live timeout in seconds
1.2       misho     355:  * return: NULL error or new registered BLOB
                    356:  */
1.16      misho     357: rpc_blob_t *rpc_srv_registerBLOB(rpc_srv_t * __restrict srv, size_t len, int tout);
1.2       misho     358: /*
1.6       misho     359:  * rpc_srv_unregisterBLOB() - Unregister BLOB from server
                    360:  *
1.2       misho     361:  * @srv = RPC Server instance
                    362:  * @var = BLOB Variable for unregister
                    363:  * return: -1 error, 0 not found call, 1 unregister ok
                    364:  */
                    365: int rpc_srv_unregisterBLOB(rpc_srv_t * __restrict srv, uint32_t var);
                    366: /*
1.6       misho     367:  * rpc_srv_getBLOB() - Get registered BLOB 
                    368:  *
1.2       misho     369:  * @srv = RPC Server instance
                    370:  * @var = hash for variable
                    371:  * return: NULL not found, !=NULL return blob var
                    372:  */
1.15      misho     373: rpc_blob_t *rpc_srv_getBLOB(rpc_srv_t * __restrict srv, uint32_t var);
1.2       misho     374: 
                    375: /*
1.6       misho     376:  * rpc_srv_sendBLOB() - Send mapped BLOB to client
                    377:  *
1.2       misho     378:  * @cli = Client instance
                    379:  * @blob = Mapped BLOB element
                    380:  * return: -1 error, 0 ok
                    381:  */
                    382: int rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob);
                    383: /*
1.6       misho     384:  * rpc_srv_recvBLOB() - Receive BLOB from client
                    385:  *
1.2       misho     386:  * @cli = Client instance
                    387:  * @blob = Mapped BLOB element
                    388:  * return: -1 error, 0 ok, >0 unreceived data from client, may be error?
                    389:  */
                    390: int rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob);
                    391: 
1.26      misho     392: 
1.1       misho     393: #endif

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