From c50e1bac45000fe2f10303615659e99238e67d7f Mon Sep 17 00:00:00 2001
From: Alexandr Lysikov <alexander.lysikov@demlabs.net>
Date: Fri, 6 Nov 2020 23:02:11 +0500
Subject: [PATCH] added commands 'order static' and 'order recheck'

---
 modules/net/srv/dap_chain_net_srv.c           | 92 ++++++++++++++++++-
 modules/net/srv/dap_chain_net_srv_order.c     | 18 ++--
 .../net/srv/include/dap_chain_net_srv_order.h | 14 +++
 3 files changed, 113 insertions(+), 11 deletions(-)

diff --git a/modules/net/srv/dap_chain_net_srv.c b/modules/net/srv/dap_chain_net_srv.c
index 99fa9dcea3..b761a70a9c 100644
--- a/modules/net/srv/dap_chain_net_srv.c
+++ b/modules/net/srv/dap_chain_net_srv.c
@@ -37,6 +37,12 @@
 #include <io.h>
 #endif
 
+#ifdef DAP_OS_LINUX
+#include <dlfcn.h>
+#endif
+#include <json-c/json.h>
+#include <json-c/json_object.h>
+
 #include <pthread.h>
 #include <dirent.h>
 
@@ -101,7 +107,13 @@ int dap_chain_net_srv_init(dap_config_t * a_cfg)
         "        -price_unit <Price Unit> -price_token <Token ticker> [-node_addr <Node Address>] [-tx_cond <TX Cond Hash>] \\\n"
         "        [-expires <Unix time when expires>] [-ext <Extension with params>]\\\n"
         "        [-cert <cert name to sign order>]\\\n"
-        "\tOrder create\n" );
+        "\tOrder create\n"
+            "net_srv -net <chain net name> order static [save | delete]\\\n"
+            "\tStatic nodelist create/delete\n"
+            "net_srv -net <chain net name> order recheck\\\n"
+            "\tCheck the availability of orders\n"
+
+        );
 
     s_load_all();
     return 0;
@@ -167,6 +179,28 @@ void dap_chain_net_srv_deinit(void)
 }
 
 
+static void* get_cdb_func(const char *func_name)
+{
+    void *l_ref_func = NULL;
+    //  find func from dynamic library
+#if defined (DAP_OS_LINUX) && !defined (__ANDROID__)
+    const char * s_default_path_modules = "var/modules";
+    const char * l_cdb_so_name = "libcellframe-node-cdb.so";
+    char *l_lib_path = dap_strdup_printf("%s/%s/%s", g_sys_dir_path, s_default_path_modules, l_cdb_so_name);
+    void* l_cdb_handle = NULL;
+    l_cdb_handle = dlopen(l_lib_path, RTLD_NOW);
+    DAP_DELETE(l_lib_path);
+    if(!l_cdb_handle) {
+        log_it(L_ERROR, "Can't load %s module: %s", l_cdb_so_name, dlerror());
+    }
+    else {
+        l_ref_func = dlsym(l_cdb_handle, func_name);
+        dlclose(l_cdb_handle);
+    }
+#endif
+    return l_ref_func;
+}
+
 /**
  * @brief s_cli_net_srv
  * @param argc
@@ -195,7 +229,7 @@ static int s_cli_net_srv( int argc, char **argv, void *arg_func, char **a_str_re
 
         dap_string_t *l_string_ret = dap_string_new("");
         const char *l_order_str = NULL;
-        dap_chain_node_cli_find_option_val(argv, arg_index, argc, "order", &l_order_str);
+        int l_order_arg_pos = dap_chain_node_cli_find_option_val(argv, arg_index, argc, "order", &l_order_str);
 
         // Order direction
         const char *l_direction_str = NULL;
@@ -518,9 +552,59 @@ static int s_cli_net_srv( int argc, char **argv, void *arg_func, char **a_str_re
                 dap_string_append_printf( l_string_ret, "Missed some required params\n");
                 ret=-5;
             }
+        }else if( dap_strcmp( l_order_str, "recheck" ) == 0 ){
+            //int dap_chain_net_srv_vpn_cdb_server_list_check_orders(dap_chain_net_t *a_net);
+            int (*dap_chain_net_srv_vpn_cdb_server_list_check_orders)(dap_chain_net_t *a_net) = NULL;
+            *(void **) (&dap_chain_net_srv_vpn_cdb_server_list_check_orders) = get_cdb_func("dap_chain_net_srv_vpn_cdb_server_list_check_orders");
+            int l_init_res = dap_chain_net_srv_vpn_cdb_server_list_check_orders ? (*dap_chain_net_srv_vpn_cdb_server_list_check_orders)(l_net) : -5;
+            //int l_init_res = dap_chain_net_srv_vpn_cdb_server_list_check_orders(l_net);
+            ret = -10;
+
+        }else if( dap_strcmp( l_order_str, "static" ) == 0 ){
+            // find the subcommand directly after the 'order' command
+            int l_subcmd_save = dap_chain_node_cli_find_option_val(argv, l_order_arg_pos + 1, l_order_arg_pos + 2, "save", NULL);
+            int l_subcmd_del = dap_chain_node_cli_find_option_val(argv, l_order_arg_pos + 1, l_order_arg_pos + 2, "delete", NULL) |
+                               dap_chain_node_cli_find_option_val(argv, l_order_arg_pos + 1, l_order_arg_pos + 2, "del", NULL);
+
+            int (*dap_chain_net_srv_vpn_cdb_server_list_static_create)(dap_chain_net_t *a_net) = NULL;
+            int (*dap_chain_net_srv_vpn_cdb_server_list_static_delete)(dap_chain_net_t *a_net) = NULL;
+            //  find func from dinamic library
+            if(l_subcmd_save || l_subcmd_del) {
+                *(void **) (&dap_chain_net_srv_vpn_cdb_server_list_static_create) = get_cdb_func("dap_chain_net_srv_vpn_cdb_server_list_static_create");
+                *(void **) (&dap_chain_net_srv_vpn_cdb_server_list_static_delete) = get_cdb_func("dap_chain_net_srv_vpn_cdb_server_list_static_delete");
+            }
+            if(l_subcmd_save) {
+                int l_init_res = dap_chain_net_srv_vpn_cdb_server_list_static_create ? (*dap_chain_net_srv_vpn_cdb_server_list_static_create)(l_net) : -5;
+                //int l_init_res = dap_chain_net_srv_vpn_cdb_server_list_static_create(l_net);
+                if(l_init_res >= 0){
+                    dap_string_append_printf(l_string_ret, "Static node list saved, %d orders in list\n", l_init_res);
+                    ret = 0;
+                }
+                else{
+                    dap_string_append_printf(l_string_ret, "Static node list not saved, error code %d\n", l_init_res);
+                    ret = -11;
+                }
+
+            } else if(l_subcmd_del) {
+                int l_init_res = dap_chain_net_srv_vpn_cdb_server_list_static_delete ? (*dap_chain_net_srv_vpn_cdb_server_list_static_delete)(l_net) : -5;
+                //int l_init_res = dap_chain_net_srv_vpn_cdb_server_list_static_delete(l_net);
+                if(!l_init_res){
+                    dap_string_append_printf(l_string_ret, "Static node list deleted\n");
+                    ret = 0;
+                }
+                else if(l_init_res > 0){
+                    dap_string_append_printf(l_string_ret, "Static node list already deleted\n");
+                    ret = -12;
+                }
+                else
+                    dap_string_append_printf(l_string_ret, "Static node list not deleted, error code %d\n", l_init_res);
+            } else {
+                dap_string_append(l_string_ret, "not found subcommand 'save' or 'delete'\n");
+                ret = -13;
+            }
         } else {
-            dap_string_append_printf( l_string_ret, "Unknown subcommand \n");
-            ret=-3;
+            dap_string_append_printf(l_string_ret, "Unknown subcommand '%s'\n", l_order_str);
+            ret = -3;
         }
         dap_chain_node_cli_set_reply_text(a_str_reply, l_string_ret->str);
         dap_string_free(l_string_ret, true);
diff --git a/modules/net/srv/dap_chain_net_srv_order.c b/modules/net/srv/dap_chain_net_srv_order.c
index 869461dc4f..60be621e94 100644
--- a/modules/net/srv/dap_chain_net_srv_order.c
+++ b/modules/net/srv/dap_chain_net_srv_order.c
@@ -134,21 +134,21 @@ bool dap_chain_net_srv_order_set_continent_region(dap_chain_net_srv_order_t **a_
  * @param a_continent_num [out]
  * @param a_region [out]
  */
-bool dap_chain_net_srv_order_get_continent_region(dap_chain_net_srv_order_t *a_order, uint8_t *a_continent_num, char **a_region)
+bool dap_chain_net_srv_order_get_continent_region(dap_chain_net_srv_order_t *a_order_static, uint8_t *a_continent_num, char **a_region)
 {
-    if(!a_order || !a_order->ext_size || a_order->ext[0]!=0x52)
+    if(!a_order_static || !a_order_static->ext_size || a_order_static->ext[0]!=0x52)
         return false;
     if(a_continent_num) {
-       if((uint8_t)a_order->ext[1]!=0xff)
-           memcpy(a_continent_num, a_order->ext + 1, sizeof(uint8_t));
+       if((uint8_t)a_order_static->ext[1]!=0xff)
+           memcpy(a_continent_num, a_order_static->ext + 1, sizeof(uint8_t));
         else
            a_continent_num = 0;
     }
     if(a_region) {
-        size_t l_size = a_order->ext_size - sizeof(uint8_t) - 1;
+        size_t l_size = a_order_static->ext_size - sizeof(uint8_t) - 1;
         if(l_size > 0) {
             *a_region = DAP_NEW_SIZE(char, l_size);
-            memcpy(*a_region, a_order->ext + 1 + sizeof(uint8_t), l_size);
+            memcpy(*a_region, a_order_static->ext + 1 + sizeof(uint8_t), l_size);
         }
         else
             *a_region = NULL;
@@ -203,9 +203,12 @@ const char* dap_chain_net_srv_order_continent_to_str(int8_t a_num)
 int8_t dap_chain_net_srv_order_continent_to_num(const char *a_continent_str)
 {
     int8_t l_count = dap_chain_net_srv_order_continents_count();
+    // convert to to upper case
     char *l_continent_str = dap_strup(a_continent_str, -1);
     for(int8_t i = 1; i < l_count; i++) {
+        // convert to to upper case
         char *l_server_continents = dap_strup(s_server_continents[i], -1);
+        // compare strings in upper case
         if(!dap_strcmp(l_continent_str, l_server_continents)) {
             DAP_DELETE(l_server_continents);
             DAP_DELETE(l_continent_str);
@@ -214,7 +217,8 @@ int8_t dap_chain_net_srv_order_continent_to_num(const char *a_continent_str)
         DAP_DELETE(l_server_continents);
     }
     DAP_DELETE(l_continent_str);
-    return -1;
+    // none
+    return 0;
 }
 
 char * dap_chain_net_srv_order_create(
diff --git a/modules/net/srv/include/dap_chain_net_srv_order.h b/modules/net/srv/include/dap_chain_net_srv_order.h
index f4a65545ea..a39ef94d3b 100644
--- a/modules/net/srv/include/dap_chain_net_srv_order.h
+++ b/modules/net/srv/include/dap_chain_net_srv_order.h
@@ -124,3 +124,17 @@ DAP_STATIC_INLINE char * dap_chain_net_srv_order_get_gdb_group(dap_chain_net_t *
    }
    return NULL;
 }
+
+/**
+* @brief dap_chain_net_srv_order_get_gdb_group_mempool
+* @param l_chain
+* @return
+*/
+DAP_STATIC_INLINE char * dap_chain_net_srv_order_get_nodelist_group(dap_chain_net_t * a_net)
+{
+   if ( a_net ) {
+       const char c_srv_order_group_str[]="service.orders.static_nodelist";
+       return dap_strdup_printf("%s.%s",a_net->pub.gdb_groups_prefix,c_srv_order_group_str);
+   }
+   return NULL;
+}
-- 
GitLab