--- libaitrpc/src/srv.c 2012/05/17 15:18:48 1.9.2.24 +++ libaitrpc/src/srv.c 2012/05/17 16:47:35 1.9.2.28 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: srv.c,v 1.9.2.24 2012/05/17 15:18:48 misho Exp $ +* $Id: srv.c,v 1.9.2.28 2012/05/17 16:47:35 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -62,7 +62,9 @@ closeClient(sched_task_t *task) /* free buffer */ AIT_FREE_VAL(&c->cli_buf); - io_arrayDel(s->srv_clients, c->cli_id, 42); + io_arrayDel(s->srv_clients, c->cli_id, 0); + if (c) + free(c); return NULL; } @@ -314,7 +316,9 @@ closeBLOBClient(sched_task_t *task) /* free buffer */ AIT_FREE_VAL(&c->cli_buf); - io_arrayDel(s->srv_blob.clients, c->cli_id, 42); + io_arrayDel(s->srv_blob.clients, c->cli_id, 0); + if (c) + free(c); return NULL; } @@ -592,47 +596,13 @@ rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_s * @srv = RPC Server instance * return: none */ -void +inline void rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv) { - rpc_cli_t *c; - register int i; - rpc_blob_t *b, *tmp; - if (!srv) return; - if (srv->srv_blob.tid) { - pthread_cancel(srv->srv_blob.tid); - pthread_join(srv->srv_blob.tid, NULL); - } - - /* close all clients connections & server socket */ - for (i = 0; i < io_arraySize(srv->srv_blob.clients); i++) { - c = io_array(srv->srv_blob.clients, i, rpc_cli_t*); - if (c) { - shutdown(c->cli_sock, SHUT_RDWR); - close(c->cli_sock); - - schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL); - AIT_FREE_VAL(&c->cli_buf); - } - io_arrayDel(srv->srv_blob.clients, i, 42); - } - io_arrayDestroy(&srv->srv_blob.clients); - - close(srv->srv_blob.server.cli_sock); - - /* detach blobs */ - TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) { - TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node); - - rpc_srv_blobFree(srv, b); - free(b); - } - - schedEnd(&srv->srv_blob.root); - AIT_FREE_VAL(&srv->srv_blob.dir); + srv->srv_blob.kill = 1; } /* @@ -644,6 +614,11 @@ rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv) int rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv) { + rpc_cli_t *c; + register int i; + rpc_blob_t *b, *tmp; + struct timespec ts = { RPC_SCHED_POLLING, 0 }; + if (!srv || srv->srv_kill) { rpc_SetErr(EINVAL, "Invalid parameter can`t start BLOB server"); return -1; @@ -663,8 +638,36 @@ rpc_srv_loopBLOBServer(rpc_srv_t * __restrict srv) return -1; } + schedPolling(srv->srv_blob.root, &ts, NULL); /* main rpc loop */ schedRun(srv->srv_blob.root, &srv->srv_blob.kill); + + /* close all clients connections & server socket */ + for (i = 0; i < io_arraySize(srv->srv_blob.clients); i++) { + c = io_array(srv->srv_blob.clients, i, rpc_cli_t*); + if (c) { + shutdown(c->cli_sock, SHUT_RDWR); + close(c->cli_sock); + + schedCancelby(srv->srv_blob.root, taskMAX, CRITERIA_ARG, c, NULL); + AIT_FREE_VAL(&c->cli_buf); + } + io_arrayDel(srv->srv_blob.clients, i, 42); + } + io_arrayDestroy(&srv->srv_blob.clients); + + close(srv->srv_blob.server.cli_sock); + + /* detach blobs */ + TAILQ_FOREACH_SAFE(b, &srv->srv_blob.blobs, blob_node, tmp) { + TAILQ_REMOVE(&srv->srv_blob.blobs, b, blob_node); + + rpc_srv_blobFree(srv, b); + free(b); + } + + schedEnd(&srv->srv_blob.root); + AIT_FREE_VAL(&srv->srv_blob.dir); return 0; } @@ -789,45 +792,19 @@ err: /* error condition */ * @psrv = RPC Server instance * return: none */ -void +inline void rpc_srv_endServer(rpc_srv_t ** __restrict psrv) { - rpc_cli_t *c; - register int i; - rpc_func_t *f, *tmp; - if (!psrv || !*psrv) return; + /* if send kill to blob server */ if (!(*psrv)->srv_blob.kill) rpc_srv_endBLOBServer(*psrv); - /* close all clients connections & server socket */ - for (i = 0; i < io_arraySize((*psrv)->srv_clients); i++) { - c = io_array((*psrv)->srv_clients, i, rpc_cli_t*); - if (c) { - shutdown(c->cli_sock, SHUT_RDWR); - close(c->cli_sock); + (*psrv)->srv_kill = 1; + sleep(RPC_SCHED_POLLING); - schedCancelby((*psrv)->srv_root, taskMAX, CRITERIA_ARG, c, NULL); - AIT_FREE_VAL(&c->cli_buf); - } - io_arrayDel((*psrv)->srv_clients, i, 42); - } - io_arrayDestroy(&(*psrv)->srv_clients); - - close((*psrv)->srv_server.cli_sock); - - /* detach exported calls */ - TAILQ_FOREACH_SAFE(f, &(*psrv)->srv_funcs, func_node, tmp) { - TAILQ_REMOVE(&(*psrv)->srv_funcs, f, func_node); - - io_freeVars(&f->func_vars); - AIT_FREE_VAL(&f->func_name); - free(f); - } - - schedEnd(&(*psrv)->srv_root); free(*psrv); *psrv = NULL; } @@ -841,6 +818,11 @@ rpc_srv_endServer(rpc_srv_t ** __restrict psrv) int rpc_srv_loopServer(rpc_srv_t * __restrict srv) { + rpc_cli_t *c; + register int i; + rpc_func_t *f, *tmp; + struct timespec ts = { RPC_SCHED_POLLING, 0 }; + if (!srv) { rpc_SetErr(EINVAL, "Invalid parameter can`t start RPC server"); return -1; @@ -859,8 +841,36 @@ rpc_srv_loopServer(rpc_srv_t * __restrict srv) return -1; } + schedPolling(srv->srv_root, &ts, NULL); /* main rpc loop */ schedRun(srv->srv_root, &srv->srv_kill); + + /* close all clients connections & server socket */ + for (i = 0; i < io_arraySize(srv->srv_clients); i++) { + c = io_array(srv->srv_clients, i, rpc_cli_t*); + if (c) { + shutdown(c->cli_sock, SHUT_RDWR); + close(c->cli_sock); + + schedCancelby(srv->srv_root, taskMAX, CRITERIA_ARG, c, NULL); + AIT_FREE_VAL(&c->cli_buf); + } + io_arrayDel(srv->srv_clients, i, 42); + } + io_arrayDestroy(&srv->srv_clients); + + close(srv->srv_server.cli_sock); + + /* detach exported calls */ + TAILQ_FOREACH_SAFE(f, &srv->srv_funcs, func_node, tmp) { + TAILQ_REMOVE(&srv->srv_funcs, f, func_node); + + io_freeVars(&f->func_vars); + AIT_FREE_VAL(&f->func_name); + free(f); + } + + schedEnd(&srv->srv_root); return 0; }