app_ruby: removed local implementation for KSR.pv sub-module
authorDaniel-Constantin Mierla <miconda@gmail.com>
Mon, 3 Jun 2019 10:25:45 +0000 (12:25 +0200)
committerDaniel-Constantin Mierla <miconda@gmail.com>
Mon, 3 Jun 2019 10:25:45 +0000 (12:25 +0200)
- switch to use the one from core

src/modules/app_ruby/app_ruby_api.c

index 92b45fb..a346cee 100644 (file)
@@ -248,333 +248,6 @@ int sr_kemi_ruby_return_int(sr_kemi_t *ket, int rc)
 /**
  *
  */
-static VALUE sr_kemi_ruby_return_none(int rmode)
-{
-       if(rmode==1) {
-               return rb_str_new_cstr("<<null>>");
-       } else if(rmode==2) {
-               return rb_str_new_cstr("");
-       }
-       return Qnil;
-}
-
-/**
- *
- */
-static VALUE app_ruby_pv_get_mode(int argc, VALUE* argv, VALUE self, int rmode)
-{
-       str pvn;
-       pv_spec_t *pvs;
-       pv_value_t val;
-       sr_ruby_env_t *env_R;
-       int pl;
-
-       env_R = app_ruby_sr_env_get();
-
-       if(env_R==NULL || env_R->msg==NULL || argc!=1) {
-               LM_ERR("invalid ruby environment attributes or parameters\n");
-               return sr_kemi_ruby_return_none(rmode);
-       }
-
-       if(!RB_TYPE_P(argv[0], T_STRING)) {
-               LM_ERR("invalid parameter type\n");
-               return sr_kemi_ruby_return_none(rmode);
-       }
-
-       pvn.s = StringValuePtr(argv[0]);
-       if(pvn.s==NULL)
-               return sr_kemi_ruby_return_none(rmode);
-       pvn.len = strlen(pvn.s);
-
-       LM_DBG("pv get: %s\n", pvn.s);
-       pl = pv_locate_name(&pvn);
-       if(pl != pvn.len) {
-               LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
-               return sr_kemi_ruby_return_none(rmode);
-       }
-       pvs = pv_cache_get(&pvn);
-       if(pvs==NULL) {
-               LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
-               return sr_kemi_ruby_return_none(rmode);
-       }
-       memset(&val, 0, sizeof(pv_value_t));
-       if(pv_get_spec_value(env_R->msg, pvs, &val) != 0) {
-               LM_ERR("unable to get pv value for [%s]\n", pvn.s);
-               return sr_kemi_ruby_return_none(rmode);
-       }
-       if(val.flags&PV_VAL_NULL) {
-               return sr_kemi_ruby_return_none(rmode);
-       }
-       if(val.flags&PV_TYPE_INT) {
-               return INT2NUM(val.ri);
-       }
-       return rb_str_new(val.rs.s, val.rs.len);
-}
-
-/**
- *
- */
-static VALUE app_ruby_pv_get(int argc, VALUE* argv, VALUE self)
-{
-       return app_ruby_pv_get_mode(argc, argv, self, 0);
-}
-
-/**
- *
- */
-static VALUE app_ruby_pv_getw(int argc, VALUE* argv, VALUE self)
-{
-       return app_ruby_pv_get_mode(argc, argv, self, 1);
-}
-
-/**
- *
- */
-static VALUE app_ruby_pv_gete(int argc, VALUE* argv, VALUE self)
-{
-       return app_ruby_pv_get_mode(argc, argv, self, 2);
-}
-
-/**
- *
- */
-static VALUE app_ruby_pv_seti(int argc, VALUE* argv, VALUE self)
-{
-       str pvn;
-       pv_spec_t *pvs;
-       pv_value_t val;
-       sr_ruby_env_t *env_R;
-       int pl;
-
-       env_R = app_ruby_sr_env_get();
-
-       if(env_R==NULL || env_R->msg==NULL || argc!=2) {
-               LM_ERR("invalid ruby environment attributes or parameters\n");
-               return Qfalse;
-       }
-
-       if(!RB_TYPE_P(argv[0], T_STRING)) {
-               LM_ERR("invalid pv name parameter type\n");
-               return Qfalse;
-       }
-
-       if(!RB_TYPE_P(argv[1], T_FIXNUM)) {
-               LM_ERR("invalid pv val parameter type\n");
-               return Qfalse;
-       }
-
-       pvn.s = StringValuePtr(argv[0]);
-       if(pvn.s==NULL)
-               return Qfalse;
-       pvn.len = strlen(pvn.s);
-
-       LM_DBG("pv get: %s\n", pvn.s);
-       pl = pv_locate_name(&pvn);
-       if(pl != pvn.len) {
-               LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
-               return Qfalse;
-       }
-       pvs = pv_cache_get(&pvn);
-       if(pvs==NULL) {
-               LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
-               return Qfalse;
-       }
-
-       memset(&val, 0, sizeof(pv_value_t));
-       val.ri = NUM2INT(argv[1]);
-       val.flags |= PV_TYPE_INT|PV_VAL_INT;
-
-       if(pv_set_spec_value(env_R->msg, pvs, 0, &val)<0) {
-               LM_ERR("unable to set pv [%s]\n", pvn.s);
-               return Qfalse;
-       }
-
-       return Qtrue;
-}
-
-/**
- *
- */
-static VALUE app_ruby_pv_sets(int argc, VALUE* argv, VALUE self)
-{
-       str pvn;
-       pv_spec_t *pvs;
-       pv_value_t val;
-       sr_ruby_env_t *env_R;
-       int pl;
-
-       env_R = app_ruby_sr_env_get();
-
-       if(env_R==NULL || env_R->msg==NULL || argc!=2) {
-               LM_ERR("invalid ruby environment attributes or parameters\n");
-               return Qfalse;
-       }
-
-       if(!RB_TYPE_P(argv[0], T_STRING)) {
-               LM_ERR("invalid pv name parameter type\n");
-               return Qfalse;
-       }
-
-       if(!RB_TYPE_P(argv[1], T_STRING)) {
-               LM_ERR("invalid pv val parameter type\n");
-               return Qfalse;
-       }
-
-       pvn.s = StringValuePtr(argv[0]);
-       if(pvn.s==NULL)
-               return Qfalse;
-       pvn.len = strlen(pvn.s);
-
-       LM_DBG("pv get: %s\n", pvn.s);
-       pl = pv_locate_name(&pvn);
-       if(pl != pvn.len) {
-               LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
-               return Qfalse;
-       }
-       pvs = pv_cache_get(&pvn);
-       if(pvs==NULL) {
-               LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
-               return Qfalse;
-       }
-
-       memset(&val, 0, sizeof(pv_value_t));
-       val.rs.s = StringValuePtr(argv[1]);
-       if(val.rs.s==NULL) {
-               LM_ERR("invalid str value\n");
-               return Qfalse;
-       }
-       val.rs.len = strlen(val.rs.s);
-       val.flags |= PV_VAL_STR;
-
-       if(pv_set_spec_value(env_R->msg, pvs, 0, &val)<0) {
-               LM_ERR("unable to set pv [%s]\n", pvn.s);
-               return Qfalse;
-       }
-
-       return Qtrue;
-}
-
-/**
- *
- */
-static VALUE app_ruby_pv_unset(int argc, VALUE* argv, VALUE self)
-{
-       str pvn;
-       pv_spec_t *pvs;
-       pv_value_t val;
-       sr_ruby_env_t *env_R;
-       int pl;
-
-       env_R = app_ruby_sr_env_get();
-
-       if(env_R==NULL || env_R->msg==NULL || argc!=1) {
-               LM_ERR("invalid ruby environment attributes or parameters\n");
-               return Qfalse;
-       }
-
-       if(!RB_TYPE_P(argv[0], T_STRING)) {
-               LM_ERR("invalid parameter type\n");
-               return Qfalse;
-       }
-
-       pvn.s = StringValuePtr(argv[0]);
-       if(pvn.s==NULL)
-               return Qfalse;
-       pvn.len = strlen(pvn.s);
-
-       LM_DBG("pv get: %s\n", pvn.s);
-       pl = pv_locate_name(&pvn);
-       if(pl != pvn.len) {
-               LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
-               return Qfalse;
-       }
-       pvs = pv_cache_get(&pvn);
-       if(pvs==NULL) {
-               LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
-               return Qfalse;
-       }
-
-       memset(&val, 0, sizeof(pv_value_t));
-       val.flags |= PV_VAL_NULL;
-       if(pv_set_spec_value(env_R->msg, pvs, 0, &val)<0)
-       {
-               LM_ERR("unable to unset pv [%s]\n", pvn.s);
-               return Qfalse;
-       }
-
-       return Qtrue;
-}
-
-/**
- *
- */
-static VALUE app_ruby_pv_is_null(int argc, VALUE* argv, VALUE self)
-{
-       str pvn;
-       pv_spec_t *pvs;
-       pv_value_t val;
-       sr_ruby_env_t *env_R;
-       int pl;
-
-       env_R = app_ruby_sr_env_get();
-
-       if(env_R==NULL || env_R->msg==NULL || argc!=1) {
-               LM_ERR("invalid ruby environment attributes or parameters\n");
-               return Qfalse;
-       }
-
-       if(!RB_TYPE_P(argv[0], T_STRING)) {
-               LM_ERR("invalid parameter type\n");
-               return Qfalse;
-       }
-
-       pvn.s = StringValuePtr(argv[0]);
-       if(pvn.s==NULL)
-               return Qfalse;
-       pvn.len = strlen(pvn.s);
-
-       LM_DBG("pv get: %s\n", pvn.s);
-       pl = pv_locate_name(&pvn);
-       if(pl != pvn.len) {
-               LM_ERR("invalid pv [%s] (%d/%d)\n", pvn.s, pl, pvn.len);
-               return Qfalse;
-       }
-       pvs = pv_cache_get(&pvn);
-       if(pvs==NULL) {
-               LM_ERR("cannot get pv spec for [%s]\n", pvn.s);
-               return Qfalse;
-       }
-
-       memset(&val, 0, sizeof(pv_value_t));
-       if(pv_get_spec_value(env_R->msg, pvs, &val) != 0) {
-               LM_NOTICE("unable to get pv value for [%s]\n", pvn.s);
-               return Qtrue;
-       }
-       if(val.flags&PV_VAL_NULL) {
-               return Qtrue;
-       } else {
-               pv_value_destroy(&val);
-               return Qfalse;
-       }
-}
-
-/**
- *
- */
-static ksr_ruby_export_t _sr_kemi_pv_R_Map[] = {
-       {"PV", "get", app_ruby_pv_get},
-       {"PV", "getw", app_ruby_pv_getw},
-       {"PV", "gete", app_ruby_pv_gete},
-       {"PV", "seti", app_ruby_pv_seti},
-       {"PV", "sets", app_ruby_pv_sets},
-       {"PV", "unset", app_ruby_pv_unset},
-       {"PV", "is_null", app_ruby_pv_is_null},
-       {0, 0, 0}
-};
-
-/**
- *
- */
 static VALUE app_ruby_sr_modf(int argc, VALUE* argv, VALUE self)
 {
        int ret;
@@ -747,6 +420,35 @@ static VALUE ksr_ruby_exec_callback(VALUE ptr)
 /**
  *
  */
+VALUE sr_kemi_ruby_return_xval(sr_kemi_t *ket, sr_kemi_xval_t *rx)
+{
+       switch(rx->vtype) {
+               case SR_KEMIP_NONE:
+                       return Qnil;
+               case SR_KEMIP_INT:
+                       return sr_kemi_ruby_return_int(ket, rx->v.n);
+               case SR_KEMIP_STR:
+                       return rb_str_new(rx->v.s.s, rx->v.s.len);
+               case SR_KEMIP_BOOL:
+                       if(rx->v.n!=SR_KEMI_FALSE) {
+                               return Qtrue;
+                       } else {
+                               return Qfalse;
+                       }
+               case SR_KEMIP_XVAL:
+                       /* unknown content - return false */
+                       return Qfalse;
+               case SR_KEMIP_NULL:
+                       return Qnil;
+               default:
+                       /* unknown type - return false */
+                       return Qfalse;
+       }
+}
+
+/**
+ *
+ */
 VALUE sr_kemi_ruby_exec_func_ex(ksr_ruby_context_t *R, sr_kemi_t *ket, int argc,
                VALUE* argv, VALUE self)
 {
@@ -756,6 +458,7 @@ VALUE sr_kemi_ruby_exec_func_ex(ksr_ruby_context_t *R, sr_kemi_t *ket, int argc,
        str *mname;
        int i;
        int ret = -1;
+       sr_kemi_xval_t *xret;
 
        env_R = app_ruby_sr_env_get();
        if(env_R==NULL || env_R->msg==NULL || ket==NULL) {
@@ -764,8 +467,13 @@ VALUE sr_kemi_ruby_exec_func_ex(ksr_ruby_context_t *R, sr_kemi_t *ket, int argc,
        }
 
        if(argc==0 && ket->ptypes[0]==SR_KEMIP_NONE) {
-               ret = ((sr_kemi_fm_f)(ket->func))(env_R->msg);
-               return sr_kemi_ruby_return_int(ket, ret);
+               if(ket->rtype==SR_KEMIP_XVAL) {
+                       xret = ((sr_kemi_xfm_f)(ket->func))(env_R->msg);
+                       return sr_kemi_ruby_return_xval(ket, xret);
+               } else {
+                       ret = ((sr_kemi_fm_f)(ket->func))(env_R->msg);
+                       return sr_kemi_ruby_return_int(ket, ret);
+               }
        }
        fname = &ket->fname;
        mname = &ket->mname;
@@ -811,11 +519,21 @@ VALUE sr_kemi_ruby_exec_func_ex(ksr_ruby_context_t *R, sr_kemi_t *ket, int argc,
        switch(i) {
                case 1:
                        if(ket->ptypes[0]==SR_KEMIP_INT) {
-                               ret = ((sr_kemi_fmn_f)(ket->func))(env_R->msg, vps[0].n);
-                               return sr_kemi_ruby_return_int(ket, ret);
+                               if(ket->rtype==SR_KEMIP_XVAL) {
+                                       xret = ((sr_kemi_xfmn_f)(ket->func))(env_R->msg, vps[0].n);
+                                       return sr_kemi_ruby_return_xval(ket, xret);
+                               } else {
+                                       ret = ((sr_kemi_fmn_f)(ket->func))(env_R->msg, vps[0].n);
+                                       return sr_kemi_ruby_return_int(ket, ret);
+                               }
                        } else if(ket->ptypes[0]==SR_KEMIP_STR) {
-                               ret = ((sr_kemi_fms_f)(ket->func))(env_R->msg, &vps[0].s);
-                               return sr_kemi_ruby_return_int(ket, ret);
+                               if(ket->rtype==SR_KEMIP_XVAL) {
+                                       xret = ((sr_kemi_xfms_f)(ket->func))(env_R->msg, &vps[0].s);
+                                       return sr_kemi_ruby_return_xval(ket, xret);
+                               } else {
+                                       ret = ((sr_kemi_fms_f)(ket->func))(env_R->msg, &vps[0].s);
+                                       return sr_kemi_ruby_return_int(ket, ret);
+                               }
                        } else {
                                LM_ERR("invalid parameters for: %.*s\n",
                                                fname->len, fname->s);
@@ -825,11 +543,21 @@ VALUE sr_kemi_ruby_exec_func_ex(ksr_ruby_context_t *R, sr_kemi_t *ket, int argc,
                case 2:
                        if(ket->ptypes[0]==SR_KEMIP_INT) {
                                if(ket->ptypes[1]==SR_KEMIP_INT) {
-                                       ret = ((sr_kemi_fmnn_f)(ket->func))(env_R->msg, vps[0].n, vps[1].n);
-                                       return sr_kemi_ruby_return_int(ket, ret);
+                                       if(ket->rtype==SR_KEMIP_XVAL) {
+                                               xret = ((sr_kemi_xfmnn_f)(ket->func))(env_R->msg, vps[0].n, vps[1].n);
+                                               return sr_kemi_ruby_return_xval(ket, xret);
+                                       } else {
+                                               ret = ((sr_kemi_fmnn_f)(ket->func))(env_R->msg, vps[0].n, vps[1].n);
+                                               return sr_kemi_ruby_return_int(ket, ret);
+                                       }
                                } else if(ket->ptypes[1]==SR_KEMIP_STR) {
-                                       ret = ((sr_kemi_fmns_f)(ket->func))(env_R->msg, vps[0].n, &vps[1].s);
-                                       return sr_kemi_ruby_return_int(ket, ret);
+                                       if(ket->rtype==SR_KEMIP_XVAL) {
+                                               xret = ((sr_kemi_xfmns_f)(ket->func))(env_R->msg, vps[0].n, &vps[1].s);
+                                               return sr_kemi_ruby_return_xval(ket, xret);
+                                       } else {
+                                               ret = ((sr_kemi_fmns_f)(ket->func))(env_R->msg, vps[0].n, &vps[1].s);
+                                               return sr_kemi_ruby_return_int(ket, ret);
+                                       }
                                } else {
                                        LM_ERR("invalid parameters for: %.*s\n",
                                                        fname->len, fname->s);
@@ -837,11 +565,21 @@ VALUE sr_kemi_ruby_exec_func_ex(ksr_ruby_context_t *R, sr_kemi_t *ket, int argc,
                                }
                        } else if(ket->ptypes[0]==SR_KEMIP_STR) {
                                if(ket->ptypes[1]==SR_KEMIP_INT) {
-                                       ret = ((sr_kemi_fmsn_f)(ket->func))(env_R->msg, &vps[0].s, vps[1].n);
-                                       return sr_kemi_ruby_return_int(ket, ret);
+                                       if(ket->rtype==SR_KEMIP_XVAL) {
+                                               xret = ((sr_kemi_xfmsn_f)(ket->func))(env_R->msg, &vps[0].s, vps[1].n);
+                                               return sr_kemi_ruby_return_xval(ket, xret);
+                                       } else {
+                                               ret = ((sr_kemi_fmsn_f)(ket->func))(env_R->msg, &vps[0].s, vps[1].n);
+                                               return sr_kemi_ruby_return_int(ket, ret);
+                                       }
                                } else if(ket->ptypes[1]==SR_KEMIP_STR) {
-                                       ret = ((sr_kemi_fmss_f)(ket->func))(env_R->msg, &vps[0].s, &vps[1].s);
-                                       return sr_kemi_ruby_return_int(ket, ret);
+                                       if(ket->rtype==SR_KEMIP_XVAL) {
+                                               xret = ((sr_kemi_xfmss_f)(ket->func))(env_R->msg, &vps[0].s, &vps[1].s);
+                                               return sr_kemi_ruby_return_xval(ket, xret);
+                                       } else {
+                                               ret = ((sr_kemi_fmss_f)(ket->func))(env_R->msg, &vps[0].s, &vps[1].s);
+                                               return sr_kemi_ruby_return_int(ket, ret);
+                                       }
                                } else {
                                        LM_ERR("invalid parameters for: %.*s\n",
                                                        fname->len, fname->s);
@@ -1534,15 +1272,6 @@ int app_ruby_kemi_export_libs(void)
        }
 
        m = 0;
-       /* pv submodule */
-       _ksr_mSMD[m] = rb_define_module_under(_ksr_mKSR, "PV");
-       for(i=0; _sr_kemi_pv_R_Map[i].fname!=0; i++) {
-               LM_DBG("exporting KSR.PV.%s(...)\n", _sr_kemi_pv_R_Map[i].fname);
-               rb_define_singleton_method(_ksr_mSMD[m], _sr_kemi_pv_R_Map[i].fname,
-                               _sr_kemi_pv_R_Map[i].func, -1);
-       }
-       LM_DBG("initialized kemi sub-module: KSR.PV\n");
-       m++;
 
        /* x submodule */
        _ksr_mSMD[m] = rb_define_module_under(_ksr_mKSR, "X");
@@ -1557,10 +1286,6 @@ int app_ruby_kemi_export_libs(void)
        /* registered kemi modules */
        if(emods_size>1) {
                for(k=1; k<emods_size; k++) {
-                       if(emods[k].kexp == sr_kemi_exports_get_pv()) {
-                               LM_DBG("skip registering the core KSR.pv module\n");
-                               continue;
-                       }
                        n++;
                        _sr_crt_R_KSRMethods = _sr_R_KSRMethods + n;
                        ksr_app_ruby_toupper(emods[k].kexp[0].mname.s, rmname);