Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief PQ Versoin of test server
0003  *
0004  * @file atmisv67_2.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 "test67.h"
0044 #include <libpq-fe.h>
0045 
0046 /*---------------------------Externs------------------------------------*/
0047 /*---------------------------Macros-------------------------------------*/
0048 /*---------------------------Enums--------------------------------------*/
0049 /*---------------------------Typedefs-----------------------------------*/
0050 /*---------------------------Globals------------------------------------*/
0051 /*---------------------------Statics------------------------------------*/
0052 /*---------------------------Prototypes---------------------------------*/
0053 
0054 /**
0055  * Shared test function add some data to DB
0056  */
0057 int shared_svc_func(TPSVCINFO *p_svc)
0058 {
0059     int ret=EXSUCCEED;
0060     char testbuf[1024];
0061     UBFH *p_ub = (UBFH *)p_svc->data;
0062     PGresult* res;
0063     PGconn *conn;
0064     const char *const values[] = {testbuf};
0065 
0066     tplogprintubf(log_debug, "Got UBF", p_ub);
0067     
0068     if (EXFAIL==CBget(p_ub, T_LONG_FLD, 0, testbuf, 0, BFLD_STRING))
0069     {
0070         NDRX_LOG(log_error, "TESTERROR: Failed to get T_LONG_FLD: %s", 
0071                  Bstrerror(Berror));
0072         EXFAIL_OUT(ret);
0073     }
0074     
0075     conn = (PGconn *)tpgetconn();
0076     
0077     if (NULL==conn)
0078     {
0079         NDRX_LOG(log_error, "TESTERROR: Failed to get connection: %s", 
0080                 tpstrerror(tperrno));
0081         EXFAIL_OUT(ret);
0082     }
0083     
0084     res = PQexecParams(conn, 
0085                "INSERT INTO EXTEST(userid) VALUES ($1)",
0086                1,
0087                NULL,
0088                values,
0089                NULL,
0090                NULL,
0091                0);
0092     
0093     if (PQresultStatus(res) != PGRES_COMMAND_OK)
0094     {
0095         NDRX_LOG(log_error, "SELECT failed: %s", PQerrorMessage(conn));
0096         PQclear(res);
0097         EXFAIL_OUT(ret);
0098     }
0099     
0100 out:
0101     return ret;
0102 }
0103 
0104 /**
0105  * Standard service entry.
0106  * transaction shall be open by caller.
0107  */
0108 void TESTSV (TPSVCINFO *p_svc)
0109 {
0110     int ret;
0111     ret = shared_svc_func(p_svc);
0112     
0113     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0114                 0L,
0115                 p_svc->data,
0116                 0L,
0117                 0L);
0118 }
0119 
0120 /**
0121  * Timeout service
0122  */
0123 void TOUTSV (TPSVCINFO *p_svc)
0124 {
0125     int ret=EXSUCCEED;
0126     UBFH *p_ub = (UBFH *)p_svc->data;
0127 
0128     sleep(60);
0129     
0130 out:
0131     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0132                 0L,
0133                 (char *)p_ub,
0134                 0L,
0135                 0L);
0136 }
0137 
0138 /**
0139  * Failure service
0140  */
0141 void FAILSV (TPSVCINFO *p_svc)
0142 {
0143     int ret;
0144     
0145     /* do some insert */
0146     ret = shared_svc_func(p_svc);
0147     
0148     /* fail the insert */
0149     tpreturn(  TPFAIL,
0150                 0L,
0151                 p_svc->data,
0152                 0L,
0153                 0L);
0154 }
0155 
0156 /**
0157  * Do initialisation
0158  */
0159 int NDRX_INTEGRA(tpsvrinit)(int argc, char **argv)
0160 {
0161     int ret = EXSUCCEED;
0162     NDRX_LOG(log_debug, "tpsvrinit called");
0163 
0164     /*
0165      * This opens connection to database
0166      */
0167     if (EXSUCCEED!=tpopen())
0168     {
0169         NDRX_LOG(log_error, "Failed to tpopen: %s", tpstrerror(tperrno));
0170         EXFAIL_OUT(ret);
0171     }
0172 
0173     if (EXSUCCEED!=tpadvertise("TESTSV", TESTSV))
0174     {
0175         NDRX_LOG(log_error, "Failed to initialise TESTSV: %s", tpstrerror(tperrno));
0176         EXFAIL_OUT(ret);
0177     }
0178     else if (EXSUCCEED!=tpadvertise("TOUTSV", TOUTSV))
0179     {
0180         NDRX_LOG(log_error, "Failed to initialise TOUTSV: %s", tpstrerror(tperrno));
0181         EXFAIL_OUT(ret);
0182     }
0183     else if (EXSUCCEED!=tpadvertise("FAILSV", FAILSV))
0184     {
0185         NDRX_LOG(log_error, "Failed to initialise FAILSV: %s", tpstrerror(tperrno));
0186         EXFAIL_OUT(ret);
0187     }
0188     
0189 
0190 out:
0191     return ret;
0192 }
0193 
0194 /**
0195  * Do de-initialisation
0196  */
0197 void NDRX_INTEGRA(tpsvrdone)(void)
0198 {
0199     NDRX_LOG(log_debug, "tpsvrdone called");
0200     tpclose();
0201 }
0202 
0203 /* vim: set ts=4 sw=4 et smartindent: */