Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Common reply handler on requests
0003  *
0004  * @file cmd_reply.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 <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 
0039 #include <ndrstandard.h>
0040 #include <ndebug.h>
0041 #include <ndrxd.h>
0042 #include <ndrxdcmn.h>
0043 
0044 #include <cmd_processor.h>
0045 #include <atmi_int.h>
0046 /*---------------------------Externs------------------------------------*/
0047 /*---------------------------Macros-------------------------------------*/
0048 #define     REPLY_DEAD_TIMEOUT          3   /* Allow 3 sec for Q to process, otherwise assume it is dead! */
0049 /*---------------------------Enums--------------------------------------*/
0050 /*---------------------------Typedefs-----------------------------------*/
0051 /*---------------------------Globals------------------------------------*/
0052 /*---------------------------Statics------------------------------------*/
0053 /*---------------------------Prototypes---------------------------------*/
0054 
0055 /*
0056  * Generic reply provider
0057  */
0058 expublic int simple_command_reply(command_call_t * call,
0059                         int status, long flags,
0060                         mod_param_t *mod_params,
0061                         void (*p_mod)(command_reply_t *reply, size_t *send_size,
0062                                         mod_param_t *mod_params),
0063                         long userfld1, int error_code, char *error_msg)
0064 {
0065     int ret=EXSUCCEED;
0066     command_reply_t *reply;
0067     char *reply_buf = NULL;
0068     size_t reply_buf_size;
0069     size_t send_size = sizeof(command_reply_t);
0070 
0071     NDRX_SYSBUF_MALLOC_OUT(reply_buf, reply_buf_size, ret);
0072     
0073     memset(reply_buf, 0, sizeof(command_reply_t));
0074     reply = (command_reply_t *)reply_buf;
0075     
0076     if (call->flags & NDRXD_CALL_FLAGS_DEADQ)
0077     {
0078         NDRX_LOG(log_error, "Reply queue already dead - no reply back!");
0079     EXFAIL_OUT(ret);
0080     }
0081 
0082     /* form up the reply */
0083     reply->magic = NDRX_MAGIC;
0084     reply->command = call->command+1; /* Make reponse */
0085     NDRX_LOG(log_debug, "Reply command: %d", reply->command);
0086     reply->status = status;
0087     reply->msg_type = NDRXD_CALL_TYPE_GENERIC;
0088     reply->msg_src = NDRXD_SRC_NDRXD; /* from NDRXD */
0089     
0090     /* Response flags, echo back request flags too... */
0091     reply->flags = call->flags | flags;
0092     reply->userfld1 = userfld1;       /* pass back user field... */
0093     reply->error_code = error_code;
0094     
0095     if (NULL!=error_msg)
0096     {
0097         NDRX_STRCPY_SAFE(reply->error_msg, error_msg);
0098     }
0099 
0100     /* If error is set, then load the message */
0101     if (NDRXD_is_error())
0102     {
0103         /* put error details in response */
0104         reply->error_code = ndrxd_errno;
0105         /* Append with error message */
0106         NDRX_STRCPY_SAFE(reply->error_msg, ndrxd_strerror(reply->error_code));
0107     }
0108 
0109     /* apply modifications? */
0110     if (NULL!=p_mod)
0111         p_mod(reply, &send_size, mod_params);
0112     
0113     /* Do reply with ATMI helper function */
0114     ret = ndrx_generic_q_send_2(call->reply_queue, 
0115             (char *)reply, send_size, 0, REPLY_DEAD_TIMEOUT, 0);
0116     if (EXSUCCEED!=ret)
0117     {
0118         NDRX_LOG(log_error, "Marking reply queue as dead!");
0119         call->flags|=NDRXD_CALL_FLAGS_DEADQ;
0120         ret=EXFAIL;
0121     }
0122 out:
0123     
0124     if (NULL!=reply_buf)
0125     {
0126         NDRX_SYSBUF_FREE(reply_buf);
0127     }
0128     return ret;
0129 }
0130 
0131 
0132 
0133 
0134 /* vim: set ts=4 sw=4 et smartindent: */