Back to home page

Enduro/X

 
 

    


0001 /**
0002  *
0003  * @file atmisv17.c
0004  */
0005 /* -----------------------------------------------------------------------------
0006  * Enduro/X Middleware Platform for Distributed Transaction Processing
0007  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0008  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0009  * This software is released under one of the following licenses:
0010  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0011  * See LICENSE file for full text.
0012  * -----------------------------------------------------------------------------
0013  * AGPL license:
0014  *
0015  * This program is free software; you can redistribute it and/or modify it under
0016  * the terms of the GNU Affero General Public License, version 3 as published
0017  * by the Free Software Foundation;
0018  *
0019  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0020  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0021  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0022  * for more details.
0023  *
0024  * You should have received a copy of the GNU Affero General Public License along 
0025  * with this program; if not, write to the Free Software Foundation, Inc.,
0026  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0027  *
0028  * -----------------------------------------------------------------------------
0029  * A commercial use license is available from Mavimax, Ltd
0030  * contact@mavimax.com
0031  * -----------------------------------------------------------------------------
0032  */
0033 
0034 #include <unistd.h>     /* Symbolic Constants */
0035 #include <sys/types.h>  /* Primitive System Data Types */ 
0036 #include <errno.h>      /* Errors */
0037 #include <stdio.h>      /* Input/Output */
0038 #include <stdlib.h>     /* General Utilities */
0039 #include <pthread.h>    /* POSIX Threads */
0040 
0041 
0042 #include <ndebug.h>
0043 #include <atmi.h>
0044 #include <ndrstandard.h>
0045 #include <ubf.h>
0046 #include <test.fd.h>
0047 #include <string.h>
0048 #include <exthpool.h>
0049 
0050 struct thread_server
0051 {
0052     char *context_data; /* malloced by enduro/x */
0053     char *buffer; /* buffer data, managed by enduro/x */
0054 };
0055 /* note we must malloc this struct too. */
0056 typedef struct thread_server thread_server_t;
0057 
0058 
0059 threadpool M_thpool; /* threads for service */
0060 
0061 /* threaded function... */
0062 void _TH_TESTSVFN (void *ptr, int *p_finish_off)
0063 {
0064     int ret=EXSUCCEED;
0065     double d;
0066     int i;
0067     thread_server_t *thread_data = (thread_server_t *)ptr;
0068     UBFH *p_ub = (UBFH *)thread_data->buffer;
0069     
0070     if (EXSUCCEED!=tpinit(NULL))
0071     {
0072         NDRX_LOG(log_error, "Failed to init worker client");
0073         exit(1);
0074     }
0075     
0076     /* restore context. */
0077     if (EXSUCCEED!=tpsrvsetctxdata(thread_data->context_data, SYS_SRV_THREAD))
0078     {
0079         NDRX_LOG(log_error, "Failed to set context");
0080         exit(1);
0081     }
0082     
0083     /* free up the transport data.*/
0084     tpsrvfreectxdata(thread_data->context_data);
0085     free(thread_data);
0086     
0087     /* !!!************* OK. we are ready to go! **********************!!!*/
0088     
0089     NDRX_LOG(log_debug, "TESTSVFN got call");
0090 
0091     /* Just print the buffer 
0092     Bprint(p_ub);*/
0093     if (NULL==(p_ub = (UBFH *)tprealloc((char *)p_ub, 4096))) /* allocate some stuff for more data to put in  */
0094     {
0095         ret=EXFAIL;
0096         goto out;
0097     }
0098     
0099     if (EXFAIL==Bget(p_ub, T_DOUBLE_FLD, Boccur(p_ub, T_DOUBLE_FLD)-1, (char *)&d, 0))
0100     {
0101         ret=EXFAIL;
0102         goto out;
0103     }
0104 
0105     d+=1;
0106 
0107     if (EXFAIL==Badd(p_ub, T_DOUBLE_FLD, (char *)&d, 0))
0108     {
0109         ret=EXFAIL;
0110         goto out;
0111     }
0112     
0113     if (EXFAIL==ret)
0114         NDRX_LOG(log_debug, "WE GOT FAIL TESTERROR!!!!!");
0115 
0116     
0117 out:
0118     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0119                 0L,
0120                 (char *)p_ub,
0121                 0L,
0122                 0L);
0123 
0124 }
0125 
0126 
0127 /* main server thread... 
0128  * NOTE: p_svc - this is local variable of enduro's main thread (on stack).
0129  * but p_svc->data - is auto buffer, will be freed when main thread returns.
0130  *                      Thus we need a copy of buffer for thread.
0131  */
0132 void TESTSVFN (TPSVCINFO *p_svc)
0133 {
0134     int ret=EXSUCCEED;
0135     UBFH *p_ub = (UBFH *)p_svc->data; /* this is auto-buffer */
0136     pthread_t thread;
0137     pthread_attr_t attr; 
0138     long size;
0139     char btype[16];
0140     char stype[16];
0141     
0142     pthread_attr_init(&attr);
0143     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
0144 
0145     if (0==(size = tptypes (p_svc->data, btype, stype)))
0146     {
0147         NDRX_LOG(log_error, "Zero buffer received!");
0148         exit(1);
0149     }
0150     
0151     thread_server_t *thread_data = malloc(sizeof(thread_server_t));
0152     
0153     /* not using sub-type - on tpreturn/forward for thread it will be auto-free */
0154     thread_data->buffer =  tpalloc(btype, NULL, size);
0155     
0156     
0157     if (NULL==thread_data->buffer)
0158     {
0159         NDRX_LOG(log_error, "tpalloc failed of type %s size %ld", btype, size);
0160         exit(1);
0161     }
0162     
0163     /* copy off the data */
0164     memcpy(thread_data->buffer, p_svc->data, size);
0165     
0166     thread_data->context_data = tpsrvgetctxdata();
0167     
0168     /*
0169     if (SUCCEED!=pthread_create (&thread, &attr, (void *) &_TH_TESTSVFN, thread_data))
0170     {
0171         ret=FAIL;
0172         goto out;
0173     }
0174      */
0175     ndrx_thpool_add_work(M_thpool, (void*)_TH_TESTSVFN, (void *)thread_data);
0176     
0177     
0178 out:
0179     if (EXSUCCEED==ret)
0180     {
0181         /* serve next.. */
0182         tpcontinue();
0183     }
0184     else
0185     {
0186         /* return error back */
0187         tpreturn(  TPFAIL,
0188                 0L,
0189                 (char *)p_ub,
0190                 0L,
0191                 0L);
0192     }
0193 }
0194 /*
0195  * Do initialization
0196  */
0197 int NDRX_INTEGRA(tpsvrinit)(int argc, char **argv)
0198 {
0199     int ret = EXSUCCEED;
0200     NDRX_LOG(log_debug, "tpsvrinit called");
0201 
0202         /* service request handlers */
0203     if (NULL==(M_thpool = ndrx_thpool_init(10, NULL, NULL, NULL, 0, NULL)))
0204     {
0205         NDRX_LOG(log_error, "Failed to initialize thread pool (cnt: 10)!");
0206         EXFAIL_OUT(ret);
0207     }
0208 
0209     
0210     if (EXSUCCEED!=tpadvertise("TESTSV", TESTSVFN))
0211     {
0212         NDRX_LOG(log_error, "Failed to initialize TESTSV (first)!");
0213         ret=EXFAIL;
0214     }
0215     
0216 out:    
0217     return ret;
0218 }
0219 
0220 /**
0221  * Do de-initialization
0222  */
0223 void NDRX_INTEGRA(tpsvrdone)(void)
0224 {
0225     NDRX_LOG(log_debug, "tpsvrdone called");
0226 }
0227 /* vim: set ts=4 sw=4 et smartindent: */