Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief VIEW buffer type support - structure management
0003  *
0004  * @file view_struct.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 <errno.h>
0039 #include <dirent.h>
0040 #include <limits.h>
0041 
0042 #include <ndrstandard.h>
0043 #include <ubfview.h>
0044 #include <ndebug.h>
0045 
0046 #include <userlog.h>
0047 #include <view_cmn.h>
0048 
0049 #include "Exfields.h"
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 #define API_ENTRY {ndrx_Bunset_error(); \
0053 }\
0054 
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 expublic ndrx_typedview_t *ndrx_G_view_hash = NULL;
0059 /*---------------------------Statics------------------------------------*/
0060 /*---------------------------Prototypes---------------------------------*/
0061 
0062 /**
0063  * Update checksum from given string
0064  * @param v view object
0065  * @param str string to add to add to checksum
0066  * @return 
0067  */
0068 expublic void ndrx_view_cksum_update(ndrx_typedview_t *v, char *str, int len)
0069 {
0070     int i;
0071     uint32_t s;
0072     long l;
0073     
0074 #if 0
0075     UBF_DUMP(log_debug, "ADDING TO CHECKSUM", str, len);
0076 #endif
0077     
0078     for (i=0; i<len; i++)
0079     {
0080         s = (uint32_t)str[i];
0081         v->cksum=ndrx_rotl32b(v->cksum, 1);
0082         
0083         /* xor the value with intput... */
0084         v->cksum^=s;
0085     }
0086     
0087     l = v->cksum;
0088 #if 0
0089     UBF_LOG(log_debug, "Current checksum: %ld", l);
0090 #endif
0091 }
0092 
0093 /**
0094  * Resolve view by name
0095  * @param vname view name
0096  * @return  NULL or ptr to view object
0097  */
0098 expublic ndrx_typedview_t * ndrx_view_get_view(char *vname)
0099 {
0100     ndrx_typedview_t *ret;
0101     
0102     EXHASH_FIND_STR(ndrx_G_view_hash, vname, ret);
0103     
0104     return ret;
0105 }
0106 
0107 /**
0108  * Resolve field by name
0109  * @param v view object
0110  * @param cname field name to resolve
0111  * @return  NULL or ptr to view object
0112  */
0113 expublic ndrx_typedview_field_t * ndrx_view_get_field(ndrx_typedview_t *v, char *cname)
0114 {
0115     ndrx_typedview_field_t *ret;
0116     
0117     EXHASH_FIND_STR(v->fields_h, cname, ret);
0118     
0119     return ret;
0120 }
0121 
0122 
0123 /**
0124  * Return handle to current view objects
0125  * @return ndrx_G_view_hash var
0126  */
0127 expublic ndrx_typedview_t * ndrx_view_get_handle(void)
0128 {
0129     return ndrx_G_view_hash;
0130 }
0131 
0132 /**
0133  * Delete all objects from memory
0134  * @return 
0135  */
0136 expublic void ndrx_view_deleteall(void)
0137 {
0138     ndrx_typedview_t * vel, *velt;
0139     ndrx_typedview_field_t * fld, *fldt;
0140     
0141     EXHASH_ITER(hh, ndrx_G_view_hash, vel, velt)
0142     {
0143         /* Delete all from hash */
0144         EXHASH_ITER(hh, vel->fields_h, fld, fldt)
0145         {
0146             EXHASH_DEL(vel->fields_h, fld);
0147         }
0148         
0149         /* Delete all from linked-list */
0150         DL_FOREACH_SAFE(vel->fields, fld, fldt)
0151         {
0152             DL_DELETE(vel->fields, fld);
0153             
0154             NDRX_FREE(fld);
0155         }
0156         
0157         EXHASH_DEL(ndrx_G_view_hash, vel);
0158         
0159         NDRX_FREE(vel);
0160     }
0161     
0162 }
0163 
0164 /**
0165  * Update view object properties
0166  * @param vname view name
0167  * @param ssize struct size
0168  * @return EXSUCCEED/EXFAIL
0169  */
0170 expublic int ndrx_view_update_object(char *vname, long ssize)
0171 {
0172     int ret = EXSUCCEED;
0173     ndrx_typedview_t * v;
0174 
0175     v = ndrx_view_get_view(vname);
0176     
0177     if (NULL==v)
0178     {
0179         UBF_LOG(log_error, "Failed to get view object by [%s]", vname);
0180         UBF_LOG(log_error, "View not found [%s]", vname);
0181         EXFAIL_OUT(ret);
0182     }
0183     
0184     v->ssize = ssize;
0185     
0186     UBF_LOG(log_info, "View [%s] struct size %ld", vname, v->ssize);
0187     
0188 out:
0189     return ret;
0190 }
0191 
0192 /**
0193  * Update view offsets
0194  * @param vname view name 
0195  * @param p offsets table
0196  * @return EXSUCCEED/EXFAIL
0197  */
0198 expublic int ndrx_view_update_offsets(char *vname, ndrx_view_offsets_t *p)
0199 {
0200     int ret = EXSUCCEED;
0201     ndrx_typedview_t * v;
0202     ndrx_typedview_field_t *f;
0203     
0204     /* Get the handler and iterate over the hash and here.. */
0205     v = ndrx_view_get_view(vname);
0206     
0207     if (NULL==v)
0208     {
0209         UBF_LOG(log_error, "Failed to get view object by [%s]", vname);
0210         UBF_LOG(log_error, "View not found [%s]", vname);
0211         EXFAIL_OUT(ret);
0212     }
0213     
0214     DL_FOREACH(v->fields, f)
0215     {
0216         if (NULL==p->cname)
0217         {
0218             UBF_LOG(log_error, "Field descriptor table does not match v object");
0219             EXFAIL_OUT(ret);
0220         }
0221         else if (0!=strcmp(f->cname, p->cname))
0222         {
0223             UBF_LOG(log_error, "Invalid field name, loaded object [%s] "
0224                     "vs compiled code [%s]",
0225                         f->cname, p->cname);
0226             EXFAIL_OUT(ret);
0227         }
0228         
0229         f->offset=p->offset;
0230         f->fldsize=p->fldsize;
0231         f->count_fld_offset=p->count_fld_offset;
0232         f->length_fld_offset=p->length_fld_offset;
0233         
0234         p++;
0235     }
0236     
0237 out:
0238     return ret;
0239     
0240 }
0241 /* vim: set ts=4 sw=4 et smartindent: */