Back to home page

Enduro/X

 
 

    


0001 /**
0002  *
0003  * @file atmisv1.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 <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 
0044 void SOFTTOUT(TPSVCINFO *p_svc)
0045 {
0046     /* Return Software generated timeout */
0047     tpreturn (TPFAIL, 0L, (char *)p_svc->data, 0L, TPSOFTTIMEOUT);
0048 }
0049 
0050 /**
0051  * Service does not return anything...
0052  */
0053 void NULLSV (TPSVCINFO *p_svc)
0054 {
0055    tpreturn (TPSUCCESS, 0L, NULL, 0L, 0L);
0056 }
0057 
0058 /**
0059  * Service should receive NULL buffer.
0060  * Replies back some data
0061  * @param p_svc
0062  */
0063 void RETSOMEDATA(TPSVCINFO *p_svc)
0064 {
0065     int first=1;
0066     static UBFH *p_ub;
0067 
0068     if (NULL!=p_svc->data)
0069     {
0070         NDRX_LOG(log_error, "TESTERROR: RETSOMEDATA svc should got NULL call!");
0071         tpreturn (TPFAIL, 0L, NULL, 0L, 0L);
0072     }
0073 
0074     if (first)
0075     {
0076         if (NULL==(p_ub=(UBFH *)tpalloc("UBF", NULL, 1024)))
0077         {
0078             NDRX_LOG(log_error, "TESTERROR: Failed to allocate 1024 bytes: %s",
0079                                             tpstrerror(tperrno));
0080             tpreturn (TPFAIL, 0L, NULL, 0L, 0L);
0081         }
0082 
0083         if (EXSUCCEED!=Bchg(p_ub, T_STRING_2_FLD, 0, "RESPONSE DATA 1", 0))
0084         {
0085             NDRX_LOG(log_error, "TESTERROR: Failed to set s: T_STRING_2_FLD%s",
0086                                             Bstrerror(Berror));
0087             tpreturn (TPFAIL, 0L, NULL, 0L, 0L);
0088         }
0089     }
0090 
0091     /* Return OK */
0092     tpreturn (TPSUCCESS, 0L, (char *)p_ub, 0L, 0L);
0093 }
0094 
0095 
0096 /**
0097  * Service should receive NULL buffer.
0098  * Replies back some data
0099  * @param p_svc
0100  */
0101 void ECHO(TPSVCINFO *p_svc)
0102 {
0103     UBFH *p_ub = (UBFH *)p_svc->data;
0104     
0105     /* Return OK */
0106     tpreturn (TPSUCCESS, 0L, (char *)p_ub, 0L, 0L);
0107 }
0108 
0109 /**
0110  * Do some sleep.
0111  */
0112 void SLEEP20(TPSVCINFO *p_svc)
0113 {
0114     int first=1;
0115 
0116     UBFH *p_ub = (UBFH *)p_svc->data;
0117     
0118     sleep(20);
0119 
0120     /* Return OK */
0121     tpreturn (TPSUCCESS, 0L, (char *)p_ub, 0L, 0L);
0122 }
0123 
0124 
0125 /**
0126  * This service basically raises timeout!
0127  * @param p_svc
0128  */
0129 void TIMEOUTSV (TPSVCINFO *p_svc)
0130 {
0131     int ret=EXSUCCEED;
0132     UBFH *p_ub = (UBFH *)p_svc->data;
0133 
0134     /* Make some deadly sleep! */
0135     sleep(4);
0136 
0137     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0138                 0L,
0139                 (char *)p_ub,
0140                 0L,
0141                 0L);
0142 }
0143 
0144 void TESTSVFN (TPSVCINFO *p_svc)
0145 {
0146     int ret=EXSUCCEED;
0147 
0148     static double d = 55.66;
0149 
0150     UBFH *p_ub = (UBFH *)p_svc->data;
0151 
0152     NDRX_LOG(log_debug, "TESTSVFN got call");
0153 
0154     /* Just print the buffer */
0155     Bprint(p_ub);
0156     if (EXFAIL==Bget(p_ub, T_DOUBLE_FLD, Boccur(p_ub, T_DOUBLE_FLD)-1, (char *)&d, 0))
0157     {
0158         ret=EXFAIL;
0159         goto out;
0160     }
0161 
0162     if (NULL==(p_ub = (UBFH *)tprealloc((char *)p_ub, 4096))) /* allocate some stuff for more data to put in  */
0163     {
0164         ret=EXFAIL;
0165         goto out;
0166     }
0167 
0168     d+=1;
0169 
0170     if (EXFAIL==Badd(p_ub, T_DOUBLE_FLD, (char *)&d, 0))
0171     {
0172         ret=EXFAIL;
0173         goto out;
0174     }
0175 
0176 out:
0177     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0178                 0L,
0179                 (char *)p_ub,
0180                 0L,
0181                 0L);
0182 }
0183 
0184 int get_infos(UBFH **pp_ub, char *command)
0185 {
0186     int ret=EXSUCCEED;
0187     char data_out[1035];
0188     FILE *fp;
0189     int len;
0190 
0191     if (NULL==(*pp_ub = (UBFH *)tprealloc((char *)*pp_ub, 4096))) /* allocate some stuff for more data to put in  */
0192     {
0193         ret=EXFAIL;
0194         goto out;
0195     }
0196 
0197     /* Open the command for reading. */
0198     fp = popen(command, "r");
0199     if (fp == NULL)
0200     {
0201     NDRX_LOG(log_error, "Failed to run command!");
0202     ret=EXFAIL;
0203         goto out;
0204     }
0205 
0206     /* Read the output a line at a time - output it. */
0207     while (fgets(data_out, sizeof(data_out)-1, fp) != NULL)
0208     {
0209         data_out[strlen(data_out)-1] = 0; /*strip trailing stuff */
0210     len = strlen(data_out);
0211 
0212     if (len > 0 && 9==data_out[len-1])
0213     {
0214         data_out[len-1] = 0;
0215     }
0216 
0217         if (EXFAIL==Badd(*pp_ub, T_STRING_FLD, data_out, 0))
0218         {  
0219             ret=EXFAIL;
0220             goto out;
0221         }
0222     }
0223 
0224     /* close */
0225      pclose(fp);
0226 
0227 
0228 out:
0229     return ret;
0230 }
0231 
0232 /**
0233  * UNIXINFO service
0234  */
0235 void UNIXINFO (TPSVCINFO *p_svc)
0236 {
0237     int ret=EXSUCCEED;
0238 
0239     UBFH *p_ub = (UBFH *)p_svc->data;
0240 
0241     NDRX_LOG(log_debug, "UNIXINFO got call");
0242     if (EXSUCCEED!=get_infos(&p_ub, "uname -a") ||
0243         EXSUCCEED!=get_infos(&p_ub, "uptime"))
0244     {
0245         ret=EXFAIL;
0246     }
0247 
0248 out:
0249     /* Forward to info2 server!!! */
0250     if (EXSUCCEED==ret)
0251     {
0252         tpforward(  "UNIX2",
0253                     (char *)p_ub,
0254                     0L,
0255                     0L);
0256     }
0257     else
0258     {
0259     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0260                 0L,
0261                 (char *)p_ub,
0262                 0L,
0263                 0L);
0264         }
0265 }
0266 
0267 /**
0268  * UNIXINFO2 service
0269  */
0270 void UNIX2 (TPSVCINFO *p_svc)
0271 {
0272     int ret=EXSUCCEED;
0273 
0274     UBFH *p_ub = (UBFH *)p_svc->data;
0275 
0276     NDRX_LOG(log_debug, "UNIX2 got call");
0277     if (EXSUCCEED!=get_infos(&p_ub, "uname -a") ||
0278     EXSUCCEED!=get_infos(&p_ub, "uptime"))
0279     {
0280         ret=EXFAIL;
0281     }
0282 
0283 out:
0284     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0285                 0L,
0286                 (char *)p_ub,
0287                 0L,
0288                 0L);
0289 }
0290 
0291 /*
0292  * Do initialization
0293  */
0294 int NDRX_INTEGRA(tpsvrinit)(int argc, char **argv)
0295 {
0296     char svcnm[16];
0297     int i;
0298     NDRX_LOG(log_debug, "tpsvrinit called");
0299 
0300     if (EXSUCCEED!=tpadvertise("TIMEOUTSV", TIMEOUTSV))
0301     {
0302         NDRX_LOG(log_error, "Failed to initialize TIMEOUTSV!");
0303     }
0304     else if (EXSUCCEED!=tpadvertise("TESTSV", TESTSVFN))
0305     {
0306         NDRX_LOG(log_error, "Failed to initialize TESTSV (first)!");
0307     }
0308     else if (EXSUCCEED!=tpadvertise("TESTSV", TESTSVFN))
0309     {
0310         NDRX_LOG(log_error, "Failed to initialize TESTSV (second)!");
0311     }
0312     else if (EXSUCCEED!=tpadvertise("NULLSV", NULLSV))
0313     {
0314         NDRX_LOG(log_error, "Failed to initialize NULLSV!");
0315     }
0316     else if (EXSUCCEED!=tpadvertise("ECHO", ECHO))
0317     {
0318         NDRX_LOG(log_error, "Failed to initialize ECHO!");
0319     }
0320     else if (EXSUCCEED!=tpadvertise("RETSOMEDATA", RETSOMEDATA))
0321     {
0322         NDRX_LOG(log_error, "Failed to initialize RETSOMEDATA!");
0323     }
0324     else if (EXSUCCEED!=tpadvertise("UNIXINFO", UNIXINFO))
0325     {
0326         NDRX_LOG(log_error, "Failed to initialize UNIXINFO!");
0327     }
0328     else if (EXSUCCEED!=tpadvertise("UNIX2", UNIX2))
0329     {
0330         NDRX_LOG(log_error, "Failed to initialize UNIX2!");
0331     }
0332     else if (EXSUCCEED!=tpadvertise("SLEEP20", SLEEP20))
0333     {
0334         NDRX_LOG(log_error, "Failed to initialize SLEEP20!");
0335     }
0336     else if (EXSUCCEED!=tpadvertise("SOFTTOUT", SOFTTOUT))
0337     {
0338         NDRX_LOG(log_error, "Failed to advertise SOFTTOUT!");
0339     }
0340     
0341 #if 0
0342     for (i=0; i<40000; i++)
0343     {
0344         sprintf(svcnm, "SV%d", i);
0345         if (EXSUCCEED!=tpadvertise(svcnm, UNIX2))
0346         {
0347            NDRX_LOG(log_error, "Failed to initialize %s!", svcnm);
0348            exit(1);
0349         }
0350     }
0351 #endif
0352 
0353     return EXSUCCEED;
0354 }
0355 
0356 /**
0357  * Do de-initialization
0358  */
0359 void NDRX_INTEGRA(tpsvrdone)(void)
0360 {
0361     NDRX_LOG(log_debug, "tpsvrdone called");
0362 }
0363 /* vim: set ts=4 sw=4 et smartindent: */