Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Benchmark tool server (echo responder)
0003  *
0004  * @file exbenchsv.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <unistd.h>
0038 #include <memory.h>
0039 
0040 #include <ndebug.h>
0041 #include <atmi.h>
0042 
0043 #include "Exfields.h"
0044 
0045 
0046 /*---------------------------Externs------------------------------------*/
0047 /*---------------------------Macros-------------------------------------*/
0048 /*---------------------------Enums--------------------------------------*/
0049 /*---------------------------Typedefs-----------------------------------*/
0050 /*---------------------------Globals------------------------------------*/
0051 /*---------------------------Statics------------------------------------*/
0052 exprivate int M_tran = EXFALSE; /**< use distr tran */
0053 exprivate int M_usleep = 0; /**< Number of microseconds to sleep */
0054 /*---------------------------Prototypes---------------------------------*/
0055 
0056 /**
0057  * Service entry
0058  * @return SUCCEED/FAIL
0059  */
0060 void EXBENCHSV (TPSVCINFO *p_svc)
0061 {
0062     char btype[16]={EXEOS};
0063     char stype[16]={EXEOS};
0064     CLIENTID cltid;
0065     long size;
0066     BFLDLEN len;
0067     int ret = TPSUCCESS;
0068     
0069     if (M_usleep > 0)
0070     {
0071         usleep(M_usleep);
0072     }
0073     
0074     size = tptypes (p_svc->data, btype, stype);
0075     
0076     len = sizeof(cltid.clientdata);
0077     
0078     if (0==strcmp("UBF", btype) && EXSUCCEED==Bget((UBFH*)p_svc->data, 
0079             EX_CLTID, 0, cltid.clientdata, &len))
0080     {
0081         /* send the notification... */
0082         if (EXFAIL==tpnotify(&cltid, p_svc->data, p_svc->len, 0))
0083         {
0084             ret = TPFAIL;
0085         }
0086     }
0087 
0088     /* Run the notification this is ubf buffer.. */
0089     tpreturn(  ret,
0090         0L,
0091         (char *)p_svc->data,
0092         0L,
0093         0L);
0094 }
0095 
0096 /**
0097  * Initialize the application
0098  * @param argc  argument count
0099  * @param argv  argument values
0100  * @return SUCCEED/FAIL
0101  */
0102 int init(int argc, char** argv)
0103 {
0104     int ret = EXSUCCEED;
0105     char svcnm[XATMI_SERVICE_NAME_LENGTH+1];
0106     char svcnm_base[XATMI_SERVICE_NAME_LENGTH+1]="EXBENCH";
0107     int c;
0108     int svcnum=0;
0109     char event[XATMI_EVENT_MAX+1]="";
0110     
0111     /* Parse command line, will use simple getopt */
0112     while ((c = getopt(argc, argv, "s:N:TU:e:--")) != EXFAIL)
0113     {
0114         switch(c)
0115         {
0116             case 'N':
0117                 svcnum = atoi(optarg);
0118                 break;
0119             case 's':
0120                 NDRX_STRCPY_SAFE(svcnm_base, optarg);
0121                 break;
0122             case 'U':
0123                 M_usleep = atoi(optarg);
0124                 break;
0125             case 'T':
0126                 M_tran = EXTRUE;
0127                 break;
0128             case 'e':
0129                 NDRX_STRCPY_SAFE(event, optarg);
0130                 break;
0131         }
0132     }
0133     
0134     if (svcnum > 0)
0135     {
0136         snprintf(svcnm, sizeof(svcnm), "%s%03d", svcnm_base, (tpgetsrvid() % 1000) % svcnum );
0137     }
0138     else
0139     {
0140         NDRX_STRCPY_SAFE(svcnm, svcnm_base);
0141     }
0142 
0143     /* Advertise our service */
0144     if (EXSUCCEED!=tpadvertise(svcnm, EXBENCHSV))
0145     {
0146         NDRX_LOG(log_error, "Failed to initialise EXBENCH: %s!", tpstrerror(tperrno));
0147         ret=EXFAIL;
0148         goto out;
0149     }
0150     
0151     if (EXEOS!=event[0])
0152     {
0153         TPEVCTL evctl;
0154         memset(&evctl, 0, sizeof(evctl));
0155         evctl.flags|=TPEVSERVICE;
0156         NDRX_STRCPY_SAFE(evctl.name1, svcnm);
0157         if (EXFAIL==tpsubscribe(event, NULL, &evctl, 0L))
0158         {
0159             NDRX_LOG(log_error, "Failed to subscribe to [%s] event: %s", 
0160                     event, tpstrerror(tperrno));
0161             EXFAIL_OUT(ret);
0162         }
0163     }
0164 
0165     if (M_tran && EXSUCCEED!=tpopen())
0166     {
0167         NDRX_LOG(log_error, "Failed to initialise tpopen: %s!", tpstrerror(tperrno));
0168         ret=EXFAIL;
0169         goto out;
0170     }
0171 
0172 out:
0173 
0174     return ret;
0175 }
0176 
0177 /**
0178  * Terminate the application
0179  */
0180 void uninit(void)
0181 {
0182     TP_LOG(log_info, "uninit");
0183     if (M_tran)
0184     {
0185         tpclose();
0186     }
0187 }
0188 
0189 /**
0190  * thread init
0191  * @param argc
0192  * @param argv
0193  * @return 
0194  */
0195 int thinit(int argc, char ** argv)
0196 {
0197     int ret = EXSUCCEED;
0198     
0199     if (M_tran && EXSUCCEED!=tpopen())
0200     {
0201         TP_LOG(log_error, "thinit: tailed to initialise tpopen: %s!", tpstrerror(tperrno));
0202         ret=EXFAIL;
0203         goto out;
0204     }
0205     
0206 out:
0207     return ret;
0208 }
0209 
0210 /**
0211  * Thread un-init
0212  */
0213 void thuninit(void)
0214 {
0215     TP_LOG(log_info, "thuninit");
0216     if (M_tran)
0217     {
0218         tpclose();
0219     }
0220 }
0221   
0222 
0223 /* Auto generated system advertise table */
0224 expublic struct tmdsptchtbl_t ndrx_G_tmdsptchtbl[] = {
0225     { "", "EXBENCHSV", EXBENCHSV, 0, 0 }
0226     , { NULL, NULL, NULL, 0, 0 }
0227 };
0228 
0229 /**
0230  * Server program main entry, multi-threaded support
0231  * @param argc  argument count
0232  * @param argv  argument values
0233  * @return SUCCEED/FAIL
0234  */
0235 int main( int argc, char** argv )
0236 {
0237     _tmbuilt_with_thread_option=EXTRUE;
0238     struct tmsvrargs_t tmsvrargs =
0239     {
0240         &tmnull_switch,
0241         &ndrx_G_tmdsptchtbl[0],
0242         0,
0243         init,
0244         uninit,
0245         NULL,
0246         NULL,
0247         NULL,
0248         NULL,
0249         NULL,
0250         thinit,
0251         thuninit
0252     };
0253     
0254     return( _tmstartserver( argc, argv, &tmsvrargs ));
0255     
0256 }
0257 
0258 
0259 
0260 /* vim: set ts=4 sw=4 et smartindent: */