Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Conversational server, multi-threaded. This also tests ability of
0003  *  multi-threaded servers to receive tpbroadcast notifications...
0004  *
0005  * @file atmisv75_conv.c
0006  */
0007 /* -----------------------------------------------------------------------------
0008  * Enduro/X Middleware Platform for Distributed Transaction Processing
0009  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0010  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0011  * This software is released under one of the following licenses:
0012  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0013  * See LICENSE file for full text.
0014  * -----------------------------------------------------------------------------
0015  * AGPL license:
0016  * 
0017  * This program is free software; you can redistribute it and/or modify it under
0018  * the terms of the GNU Affero General Public License, version 3 as published
0019  * by the Free Software Foundation;
0020  *
0021  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0022  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0023  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0024  * for more details.
0025  *
0026  * You should have received a copy of the GNU Affero General Public License along 
0027  * with this program; if not, write to the Free Software Foundation, Inc., 
0028  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0029  *
0030  * -----------------------------------------------------------------------------
0031  * A commercial use license is available from Mavimax, Ltd
0032  * contact@mavimax.com
0033  * -----------------------------------------------------------------------------
0034  */
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <ndebug.h>
0038 #include <atmi.h>
0039 #include <ndrstandard.h>
0040 #include <ubf.h>
0041 #include <test.fd.h>
0042 #include <string.h>
0043 #include <unistd.h>
0044 #include <thlock.h>
0045 #include "test75.h"
0046 #include "ubf_int.h"
0047 #include <exassert.h>
0048 
0049 /*---------------------------Externs------------------------------------*/
0050 /*---------------------------Macros-------------------------------------*/
0051 /*---------------------------Enums--------------------------------------*/
0052 /*---------------------------Typedefs-----------------------------------*/
0053 /*---------------------------Globals------------------------------------*/
0054 /*---------------------------Statics------------------------------------*/
0055 exprivate __thread volatile int M_notifs = 0;
0056 /*---------------------------Prototypes---------------------------------*/
0057 
0058 /**
0059  * Set notification handler 
0060  * @return 
0061  */
0062 void notification_callback (char *data, long len, long flags)
0063 {
0064     M_notifs++;
0065 }
0066 
0067 /**
0068  * Conversational server
0069  */
0070 void CONVSV1 (TPSVCINFO *p_svc)
0071 {
0072     int ret=EXSUCCEED;
0073     char *buf = p_svc->data;
0074     long revent, len;
0075     NDRX_LOG(log_debug, "%s got call", __func__);
0076     M_notifs = 0;
0077     
0078     buf = tprealloc(buf, 1024);
0079     NDRX_ASSERT_TP_OUT((NULL!=buf), "Failed to realloc");
0080     
0081     /* client: connect 4x (4x threads):  Using string buffer ... */
0082     NDRX_ASSERT_VAL_OUT((strcmp(buf, "HELLO")==0), "Expected HELLO at connection point");
0083     
0084     /* we shall receive some stuff... */
0085     NDRX_ASSERT_TP_OUT((EXFAIL==tprecv(p_svc->cd, &buf, &len, 0, &revent)), "Expected failure");
0086     NDRX_ASSERT_TP_OUT( (TPEEVENT==tperrno),  "Expected TPEEVENT");
0087     NDRX_ASSERT_TP_OUT( (TPEV_SENDONLY==revent),  "Expected TPEV_SENDONLY");
0088     NDRX_ASSERT_VAL_OUT((strcmp(buf, "CLWAIT")==0), "Expected CLWAIT at connection point");
0089     
0090     /* Wait for notification... */
0091     tperrno = 0;
0092     while (EXFAIL!=tpchkunsol() && M_notifs < 9)
0093     {
0094         usleep(1000);
0095     }
0096     
0097     NDRX_ASSERT_TP_OUT( (0==tperrno), "Expected no error");
0098 
0099     /* send number back... */
0100     
0101     buf = tprealloc(buf, 1024);
0102     NDRX_ASSERT_TP_OUT((NULL!=buf), "Failed to realloc");
0103     snprintf(buf, sizeof(buf), "%d", M_notifs);
0104     
0105     NDRX_ASSERT_TP_OUT( (EXSUCCEED==tpsend(p_svc->cd, buf, 0, 0, &revent)), 
0106                     "Failed send nr of notifs...");
0107     /* OK finish off... */
0108 out:
0109     tpreturn(  ret==EXSUCCEED?TPSUCCESS:TPFAIL,
0110                 0L,
0111                 (char *)buf,
0112                 0L,
0113                 0L);
0114 }
0115 
0116 /**
0117  * Do initialisation
0118  */
0119 int tpsvrinit(int argc, char **argv)
0120 {
0121     int ret = EXSUCCEED;
0122     NDRX_LOG(log_debug, "tpsvrinit called");
0123 
0124     NDRX_ASSERT_TP_OUT((EXSUCCEED==tpadvertise("CONVSV1", CONVSV1)), 
0125             "Failed to advertise");
0126     
0127 out:
0128         
0129     return ret;
0130 }
0131 
0132 /**
0133  * Do de-initialisation
0134  * After the server, thread pool is stopped
0135  */
0136 void tpsvrdone(void)
0137 {
0138     NDRX_LOG(log_debug, "tpsvrdone called");
0139 }
0140 
0141 /**
0142  * Do initialisation
0143  */
0144 int tpsvrthrinit(int argc, char **argv)
0145 {
0146     int ret = EXSUCCEED;
0147     
0148     NDRX_ASSERT_TP_OUT((NULL==tpsetunsol(notification_callback)), 
0149             "Invalid previous unsol handler");
0150     
0151 out:
0152     return ret;
0153 }
0154 
0155 /**
0156  * Do de-initialisation
0157  */
0158 void tpsvrthrdone(void)
0159 {
0160     NDRX_LOG(log_debug, "tpsvrthrdone called");
0161 }
0162 
0163 /* Auto generated system advertise table */
0164 expublic struct tmdsptchtbl_t ndrx_G_tmdsptchtbl[] = {
0165     { NULL, NULL, NULL, 0, 0 }
0166 };
0167 /*---------------------------Prototypes---------------------------------*/
0168 
0169 /**
0170  * Main entry for tmsrv
0171  */
0172 int main( int argc, char** argv )
0173 {
0174     _tmbuilt_with_thread_option=EXTRUE;
0175     struct tmsvrargs_t tmsvrargs =
0176     {
0177         &tmnull_switch,
0178         &ndrx_G_tmdsptchtbl[0],
0179         0,
0180         tpsvrinit,
0181         tpsvrdone,
0182         NULL,
0183         NULL,
0184         NULL,
0185         NULL,
0186         NULL,
0187         tpsvrthrinit,
0188         tpsvrthrdone
0189     };
0190     
0191     return( _tmstartserver( argc, argv, &tmsvrargs ));
0192     
0193 }
0194 
0195 
0196 /* vim: set ts=4 sw=4 et smartindent: */