Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Daemon server example. Deamon will print to log some messages...
0003  *
0004  * @file atmisv75_dmn.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 <stdio.h>
0035 #include <stdlib.h>
0036 #include <ndebug.h>
0037 #include <atmi.h>
0038 #include <ndrstandard.h>
0039 #include <ubf.h>
0040 #include <test.fd.h>
0041 #include <string.h>
0042 #include <unistd.h>
0043 #include <thlock.h>
0044 #include "test75.h"
0045 #include "ubf_int.h"
0046 #include <exassert.h>
0047 
0048 /*---------------------------Externs------------------------------------*/
0049 /*---------------------------Macros-------------------------------------*/
0050 /*---------------------------Enums--------------------------------------*/
0051 /*---------------------------Typedefs-----------------------------------*/
0052 /*---------------------------Globals------------------------------------*/
0053 /*---------------------------Statics------------------------------------*/
0054 
0055 int M_stopping1 = EXFALSE;
0056 int M_stopping2 = EXFALSE;
0057 /*---------------------------Prototypes---------------------------------*/
0058 
0059 /**
0060  * Standard service entry, daemon thread
0061  */
0062 void DMNSV1 (TPSVCINFO *p_svc)
0063 {
0064     int ret=EXSUCCEED;
0065     char tmp_buf[64];
0066     UBFH *p_ub = (UBFH *)p_svc->data;
0067 
0068     NDRX_LOG(log_debug, "%s got call", __func__);
0069     
0070     /* Just print the buffer */
0071     Bprint(p_ub);
0072     
0073     /* check that we got the data correctly */
0074     
0075     NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bget(p_ub, T_STRING_2_FLD, 0, tmp_buf, NULL)),
0076             "Failed to get T_STRING_2_FLD");
0077     
0078     NDRX_ASSERT_VAL_OUT((0==strcmp(tmp_buf, "TEST_UBF")),
0079             "Invalid value");
0080     
0081     /* Check the buffer... */
0082     while(!M_stopping1)
0083     {
0084         NDRX_LOG(log_debug, "Daemon running...");
0085         sleep(1);
0086     }
0087     
0088     M_stopping1=EXFALSE;
0089     
0090 out:
0091     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0092                 0L,
0093                 (char *)p_ub,
0094                 0L,
0095                 0L);
0096 }
0097 
0098 /**
0099  * Standard service entry, daemon thread
0100  */
0101 void DMNSV2 (TPSVCINFO *p_svc)
0102 {
0103     int ret=EXSUCCEED;
0104 
0105     NDRX_LOG(log_debug, "%s got call", __func__);
0106 
0107     /* Just print the buffer */
0108     if (NULL!=p_svc->data)
0109     {
0110         NDRX_LOG(log_error, "Expected NULL data");
0111         EXFAIL_OUT(ret);
0112     }
0113     
0114     /* Check the buffer... */
0115     while(!M_stopping2)
0116     {
0117         NDRX_LOG(log_debug, "Daemon running...");
0118         sleep(1);
0119     }
0120     
0121     M_stopping2=EXFALSE;
0122     
0123 out:
0124     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0125                 0L,
0126                 (char *)p_svc->data,
0127                 0L,
0128                 0L);
0129 }
0130 
0131 /**
0132  * Stop control service
0133  * @param p_svc
0134  */
0135 void DMNSV_CTL (TPSVCINFO *p_svc)
0136 {
0137     int ret=EXSUCCEED;
0138     UBFH *p_ub = (UBFH *)p_svc->data;
0139     UBFH *p_ub_tmp = NULL;
0140     short dmn_no;
0141     char cmd[16];
0142     
0143     
0144     /* control which daemon to stop... Test busy flag of two daemons
0145      * if one stopped, the busy shall stay
0146      * if another stopped, the busy shall be removed
0147      */
0148     
0149     if (EXSUCCEED!=Bget(p_ub, T_STRING_FLD, 0, cmd, NULL))
0150     {
0151         NDRX_LOG(log_error, "TESTERROR: Failed to get command: %s", 
0152                 Bstrerror(Berror));
0153         EXFAIL_OUT(ret);
0154     }
0155     
0156     if (EXSUCCEED!=Bget(p_ub, T_SHORT_FLD, 0, (char *)&dmn_no, 0))
0157     {
0158         NDRX_LOG(log_error, "TESTERROR: Failed to get command: %s", 
0159                 Bstrerror(Berror));
0160         EXFAIL_OUT(ret);
0161     }
0162     
0163     if (0==strcmp(cmd, "stop"))
0164     {
0165         switch (dmn_no)
0166         {
0167             case 1:
0168                 M_stopping1=EXTRUE;
0169                 break;
0170             case 2:
0171                 M_stopping2=EXTRUE;
0172                 break;
0173             default:
0174                 NDRX_LOG(log_error, "Invalid dmn_no=%hd", dmn_no);
0175                 break;
0176         }
0177     }
0178     else if (0==strcmp(cmd, "start"))
0179     {
0180         switch (dmn_no)
0181         {
0182             case 1:
0183                 
0184                 p_ub_tmp = (UBFH *)tpalloc("UBF", NULL, 1024);
0185                 
0186                 NDRX_ASSERT_TP_OUT((NULL!=p_ub_tmp), "Failed to alloc");
0187                 NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bchg(p_ub_tmp, T_STRING_2_FLD, 0, "TEST_UBF", 0)), 
0188                         "Failed to set");
0189                 NDRX_ASSERT_TP_OUT((EXSUCCEED==tpacall("DMNSV1", (char *)p_ub_tmp, 
0190                         0, TPNOREPLY)), "Failed to call service with UBF");
0191                 
0192                 break;
0193             case 2:
0194                 
0195                 NDRX_ASSERT_TP_OUT((EXSUCCEED==tpacall("DMNSV2", (char *)p_ub_tmp, 
0196                         0, TPNOREPLY)), "Failed to call service with UBF");
0197                 break;
0198             default:
0199                 NDRX_LOG(log_error, "Invalid dmn_no=%hd", dmn_no);
0200                 break;
0201         }
0202     }
0203     
0204 out:
0205                 
0206     if (NULL!=p_ub_tmp)
0207     {
0208         tpfree((char *)p_ub_tmp);
0209     }
0210 
0211     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0212                 0L,
0213                 (char *)p_svc->data,
0214                 0L,
0215                 0L);
0216 }
0217 
0218 /**
0219  * Do initialisation
0220  */
0221 int tpsvrinit(int argc, char **argv)
0222 {
0223     int ret = EXSUCCEED;
0224     UBFH *p_ub = NULL;
0225     NDRX_LOG(log_debug, "tpsvrinit called");
0226 
0227     if (EXSUCCEED!=tpadvertise("DMNSV1", DMNSV1))
0228     {
0229         NDRX_LOG(log_error, "TESTERROR Failed to initialise DMNSV!");
0230         EXFAIL_OUT(ret);
0231     }
0232     
0233     if (EXSUCCEED!=tpadvertise("DMNSV2", DMNSV2))
0234     {
0235         NDRX_LOG(log_error, "TESTERROR Failed to initialise DMNSV!");
0236         EXFAIL_OUT(ret);
0237     }
0238     
0239     /* start the service, send some data */
0240     
0241     p_ub = (UBFH *)tpalloc("UBF", NULL, 1024);
0242     
0243     if (NULL==p_ub)
0244     {
0245         NDRX_LOG(log_error, "TESTERROR Failed to alloc");
0246         EXFAIL_OUT(ret);
0247     }
0248     
0249     Bchg(p_ub, T_STRING_2_FLD, 0, "TEST_UBF", 0L);
0250     
0251     if (EXSUCCEED!=tpacall("DMNSV1", (char *)p_ub, 0, TPNOREPLY))
0252     {
0253         NDRX_LOG(log_error, "TESTERROR Failed to call DMNSV");
0254         EXFAIL_OUT(ret);
0255     }
0256     
0257     /* call twice... */
0258     if (EXSUCCEED!=tpacall("DMNSV2", NULL, 0, TPNOREPLY))
0259     {
0260         NDRX_LOG(log_error, "TESTERROR Failed to call DMNSV2");
0261         EXFAIL_OUT(ret);
0262     }
0263     
0264     /* call not advertised service, shall fail */
0265     if (EXSUCCEED==tpacall("DMNSV3", (char *)p_ub, 0, TPNOREPLY))
0266     {
0267         NDRX_LOG(log_error, "TESTERROR There shall be no DMNSV3!");
0268         EXFAIL_OUT(ret);
0269     }
0270     
0271     if (tperrno!=TPENOENT)
0272     {
0273         NDRX_LOG(log_error, "TESTERROR tperror expected %d but got %d!",
0274                 TPENOENT, tperrno);
0275         EXFAIL_OUT(ret);
0276     }
0277     
0278     
0279 out:
0280         
0281     if (NULL!=p_ub)
0282     {
0283         tpfree((char *)p_ub);
0284     }
0285 
0286     return ret;
0287 }
0288 
0289 /**
0290  * Do de-initialisation
0291  * After the server, thread pool is stopped
0292  */
0293 void tpsvrdone(void)
0294 {
0295     NDRX_LOG(log_debug, "tpsvrdone called");
0296     
0297     M_stopping1=EXTRUE;
0298     M_stopping2=EXTRUE;
0299 }
0300 
0301 /**
0302  * Do initialisation
0303  */
0304 int tpsvrthrinit(int argc, char **argv)
0305 {
0306     int ret = EXSUCCEED;
0307     
0308     /* will call few times, but nothing todo... just test */
0309     if (EXSUCCEED!=tpadvertise("DMNSV_CTL", DMNSV_CTL))
0310     {
0311         NDRX_LOG(log_error, "Failed to initialise DMNSV_CTL!");
0312         EXFAIL_OUT(ret);
0313     }
0314     
0315 out:
0316     return ret;
0317 }
0318 
0319 /**
0320  * Do de-initialisation
0321  */
0322 void tpsvrthrdone(void)
0323 {
0324     NDRX_LOG(log_debug, "tpsvrthrdone called");
0325 }
0326 
0327 /* Auto generated system advertise table */
0328 expublic struct tmdsptchtbl_t ndrx_G_tmdsptchtbl[] = {
0329     { NULL, NULL, NULL, 0, 0 }
0330 };
0331 /*---------------------------Prototypes---------------------------------*/
0332 
0333 /**
0334  * Main entry for tmsrv
0335  */
0336 int main( int argc, char** argv )
0337 {
0338     _tmbuilt_with_thread_option=EXTRUE;
0339     struct tmsvrargs_t tmsvrargs =
0340     {
0341         &tmnull_switch,
0342         &ndrx_G_tmdsptchtbl[0],
0343         0,
0344         tpsvrinit,
0345         tpsvrdone,
0346         NULL,
0347         NULL,
0348         NULL,
0349         NULL,
0350         NULL,
0351         tpsvrthrinit,
0352         tpsvrthrdone
0353     };
0354     
0355     return( _tmstartserver( argc, argv, &tmsvrargs ));
0356     
0357 }
0358 
0359 
0360 /* vim: set ts=4 sw=4 et smartindent: */