Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Cache Show
0003  *
0004  * @file cmd_cs.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 #include <sys/param.h>
0039 
0040 #include <ndrstandard.h>
0041 #include <ndebug.h>
0042 #include <nstdutil.h>
0043 
0044 #include <ndrxdcmn.h>
0045 #include <atmi_int.h>
0046 #include <gencall.h>
0047 #include <utlist.h>
0048 #include <Exfields.h>
0049 
0050 #include "xa_cmn.h"
0051 #include "nclopt.h"
0052 #include <ndrx.h>
0053 #include <qcommon.h>
0054 #include <atmi_cache.h>
0055 #include <ubfutil.h>
0056 /*---------------------------Externs------------------------------------*/
0057 /*---------------------------Macros-------------------------------------*/
0058 /*---------------------------Enums--------------------------------------*/
0059 /*---------------------------Typedefs-----------------------------------*/
0060 /*---------------------------Globals------------------------------------*/
0061 /*---------------------------Statics------------------------------------*/
0062 /*---------------------------Prototypes---------------------------------*/
0063 
0064 /**
0065  * Print header
0066  * @return
0067  */
0068 exprivate void print_hdr(char *dbname)
0069 {
0070     fprintf(stderr, " ND      UTC DATE ADDED  HITS TY  DLEN KEY\n");
0071     fprintf(stderr, "--- ------------------- ----- -- ----- ----------------------------------------\n");
0072 }
0073 
0074 /**
0075  * List queue definitions
0076  * We will run in conversation mode.
0077  * @param svcnm
0078  * @return SUCCEED/FAIL
0079  */
0080 exprivate int print_buffer(UBFH *p_ub, char *dbname)
0081 {
0082     int ret = EXSUCCEED;
0083     ndrx_tpcache_data_t cdata;
0084     char *keydata = NULL;
0085     
0086     ndrx_debug_dump_UBF(log_debug, "Got reply buffer", p_ub);
0087     
0088     if (EXSUCCEED!=ndrx_cache_mgt_ubf2data(p_ub, &cdata, NULL, &keydata, NULL, NULL))
0089     {
0090         NDRX_LOG(log_error, "Failed to get mandatory UBF data!");
0091         EXFAIL_OUT(ret);
0092     }
0093     
0094     printf("%3d %19.19s %5.5s %2hd %5.5s %s\n"
0095             ,cdata.nodeid
0096             ,ndrx_get_strtstamp_from_sec(0, cdata.t)
0097             ,ndrx_decode_num(cdata.hits, 0, 0, 1)
0098             ,cdata.atmi_type_id
0099             ,ndrx_decode_num(cdata.atmi_buf_len, 1, 0, 1)
0100             ,keydata
0101             );
0102     
0103 out:
0104 
0105     if (NULL!=keydata)
0106     {
0107         NDRX_FREE(keydata);
0108     }
0109 
0110     return ret;
0111 }
0112 
0113 /**
0114  * Call cache server
0115  * @return
0116  */
0117 exprivate int call_cache(char *dbname)
0118 {
0119     UBFH *p_ub = (UBFH *)tpalloc("UBF", "", 1024);
0120     int ret=EXSUCCEED;
0121     int cd;
0122     long revent;
0123     int recv_continue = 1;
0124     int tp_errno;
0125     long rcv_count = 0;
0126     char *svcnm;
0127     char cmd = NDRX_CACHE_SVCMD_CLSHOW;
0128     
0129     
0130     svcnm = ndrx_cache_mgt_getsvc();
0131             
0132     /* Setup the call buffer... */
0133     if (NULL==p_ub)
0134     {
0135         NDRX_LOG(log_error, "Failed to alloc FB!");        
0136         EXFAIL_OUT(ret);
0137     }
0138     
0139     if (EXSUCCEED!=Bchg(p_ub, EX_CACHE_CMD, 0, &cmd, 0L))
0140     {
0141         NDRX_LOG(log_error, "Failed to install command code: %s", 
0142                 Bstrerror(Berror));
0143         EXFAIL_OUT(ret);
0144     }
0145     
0146     if (EXSUCCEED!=Bchg(p_ub, EX_CACHE_DBNAME, 0, dbname, 0L))
0147     {
0148         NDRX_LOG(log_error, "Failed to install db name to buffer: %s",
0149                 Bstrerror(Berror));
0150         EXFAIL_OUT(ret);
0151     }
0152     
0153     if (EXFAIL == (cd = tpconnect(svcnm,
0154                                     (char *)p_ub,
0155                                     0,
0156                                     TPNOTRAN |
0157                                     TPRECVONLY)))
0158     {
0159         NDRX_LOG(log_error, "Connect error [%s]", tpstrerror(tperrno) );
0160         fprintf(stderr, "Connect error [%s]\n", tpstrerror(tperrno));
0161         ret = EXFAIL;
0162         goto out;
0163     }
0164     NDRX_LOG(log_debug, "Connected OK, cd = %d", cd );
0165 
0166     while (recv_continue)
0167     {
0168         recv_continue=0;
0169         if (EXFAIL == tprecv(cd,
0170                             (char **)&p_ub,
0171                             0L,
0172                             0L,
0173                             &revent))
0174         {
0175             ret = EXFAIL;
0176             tp_errno = tperrno;
0177             if (TPEEVENT == tp_errno)
0178             {
0179                     if (TPEV_SVCSUCC == revent)
0180                             ret = EXSUCCEED;
0181                     else
0182                     {
0183                         NDRX_LOG(log_error,
0184                                  "Unexpected conv event %lx", revent );
0185                         EXFAIL_OUT(ret);
0186                     }
0187             }
0188             else
0189             {
0190                 NDRX_LOG(log_error, "recv error %d", tp_errno  );
0191                 EXFAIL_OUT(ret);
0192             }
0193         }
0194         else
0195         {
0196             if (EXSUCCEED!=print_buffer(p_ub, svcnm))
0197             {
0198                 EXFAIL_OUT(ret);
0199             }
0200             rcv_count++;
0201             recv_continue=1;
0202         }
0203     }
0204     
0205     fprintf(stderr, "%ld records cached in \"%s\" database\n", rcv_count, dbname);
0206 
0207 out:
0208 
0209     if (NULL!=p_ub)
0210     {
0211         tpfree((char *)p_ub);
0212     }
0213 
0214     return ret;
0215 }
0216 
0217 /**
0218  * Cache Show command
0219  * @param p_cmd_map
0220  * @param argc
0221  * @param argv
0222  * @return SUCCEED
0223  */
0224 expublic int cmd_cs(cmd_mapping_t *p_cmd_map, int argc, char **argv, int *p_have_next)
0225 {
0226     int ret=EXSUCCEED;
0227     char dbname[NDRX_CCTAG_MAX+1]={EXEOS};
0228     ncloptmap_t clopt[] =
0229     {
0230         {'d', BFLD_STRING, (void *)dbname, sizeof(dbname), 
0231                                 NCLOPT_MAND|NCLOPT_HAVE_VALUE, "Database name"},
0232         {0}
0233     };
0234         
0235     if (argc>=2 && '-'!=argv[1][0])
0236     {
0237         NDRX_STRCPY_SAFE(dbname, argv[1]);
0238     }
0239     else
0240     {
0241         /* parse command line */
0242         if (nstd_parse_clopt(clopt, EXTRUE,  argc, argv, EXFALSE))
0243         {
0244             fprintf(stderr, XADMIN_INVALID_OPTIONS_MSG);
0245             EXFAIL_OUT(ret);
0246         }
0247     }
0248 
0249     /* we need to init TP subsystem... */
0250     if (EXSUCCEED!=tpinit(NULL))
0251     {
0252         fprintf(stderr, "Failed to tpinit(): %s\n", tpstrerror(tperrno));
0253         EXFAIL_OUT(ret);
0254     }
0255     
0256     print_hdr(dbname);
0257     
0258     if (EXSUCCEED!=call_cache(dbname))
0259     {
0260         NDRX_LOG(log_debug, "Failed to call cache server for db [%s]", dbname);
0261         fprintf(stderr, "Failed to call cache server!\n");
0262         EXFAIL_OUT(ret);
0263     }
0264         
0265 out:
0266     return ret;
0267 }
0268 
0269 /* vim: set ts=4 sw=4 et smartindent: */