Back to home page

Enduro/X

 
 

    


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