From 3cfb35dfbec2d8cc7f57066a8794dc6525adbb6a Mon Sep 17 00:00:00 2001
From: Aleksandr Lysikov <lysikov@inbox.ru>
Date: Sun, 24 Feb 2019 15:42:18 +0500
Subject: [PATCH] update to merge with to master

---
 dap_chain_node_remote.c | 103 +++++++++++-----------------------------
 dap_chain_node_remote.h |  27 +++++------
 2 files changed, 40 insertions(+), 90 deletions(-)

diff --git a/dap_chain_node_remote.c b/dap_chain_node_remote.c
index 43cf26bc2e..9ba16a3989 100644
--- a/dap_chain_node_remote.c
+++ b/dap_chain_node_remote.c
@@ -22,116 +22,69 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <glib.h>
 #include <pthread.h>
 
-#include "uthash.h"
 #include "dap_chain_node_remote.h"
 
-typedef struct list_linked_item {
-    dap_chain_node_addr_t address;
-    chain_node_client_t *client;
-    UT_hash_handle hh;
-} list_linked_item_t;
 
 // List of connections
-static list_linked_item_t *conn_list = NULL;
+static GList *connect_list = NULL;
 
 // for separate access to connect_list
 static pthread_mutex_t connect_list_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /**
- * Add new established connection to the list
- *
- * return 0 OK, -1 error, -2 already present
+ * Add new established connection in the list
  */
-int chain_node_client_list_add(dap_chain_node_addr_t *address, chain_node_client_t *client)
+bool chain_node_client_list_add(dap_chain_node_client_t *client)
 {
-    int ret = 0;
-    if(!address || !client)
-        return -1;
-    list_linked_item_t *item_tmp = NULL;
+    if(!client)
+        return false;
     pthread_mutex_lock(&connect_list_mutex);
-    HASH_FIND(hh, conn_list, address, sizeof(dap_chain_node_addr_t), item_tmp); // address already in the hash?
-    if(item_tmp == NULL) {
-        item_tmp = DAP_NEW(list_linked_item_t);
-        item_tmp->address.uint64 = address->uint64;
-        item_tmp->client = client;
-        HASH_ADD(hh, conn_list, address, sizeof(dap_chain_node_addr_t), item_tmp); // address: name of key field
-        ret = 0;
-    }
-    // connection already present
-    else
-        ret = -2;
-    //connect_list = g_list_append(connect_list, client);
+    connect_list = g_list_append(connect_list, client);
     pthread_mutex_unlock(&connect_list_mutex);
-    return ret;
+    return true;
 }
 
 /**
  * Delete established connection from the list
- *
- * return 0 OK, -1 error, -2 address not found
  */
-int chain_node_client_list_del(dap_chain_node_addr_t *address)
+bool chain_node_client_list_del(dap_chain_node_client_t *client)
 {
-    int ret = -1;
-    if(!address)
-        return -1;
-    list_linked_item_t *item_tmp;
     pthread_mutex_lock(&connect_list_mutex);
-    HASH_FIND(hh, conn_list, address, sizeof(dap_chain_node_addr_t), item_tmp);
-    if(item_tmp != NULL) {
-        HASH_DEL(conn_list, item_tmp);
-        ret = 0;
-    }
-    else
-        // address not found in the hash
-        ret = -2;
+    GList *list = g_list_find(connect_list, client);
+    // found
+    if(list)
+        connect_list = g_list_remove(connect_list, client);
     pthread_mutex_unlock(&connect_list_mutex);
-    if(!ret) {
-        // close connection
-        chain_node_client_close(item_tmp->client);
-        // del struct for hash
-        DAP_DELETE(item_tmp);
-    }
-    return ret;
+    if(list)
+        return true;
+    return false;
 }
 
 /**
- * Delete all established connection from the list
+ * Get one established connection
+ *
+ * n - the position of the established connection, counting from 0
+ *
+ * return client, or NULL if the position is off the end of the list
  */
-void chain_node_client_list_del_all(void)
+dap_chain_node_client_t* chain_node_client_list_get_item(int n)
 {
-    int ret = -1;
-    list_linked_item_t *iter_current, *item_tmp;
     pthread_mutex_lock(&connect_list_mutex);
-    HASH_ITER(hh, conn_list , iter_current, item_tmp) {
-        // close connection
-        chain_node_client_close(iter_current->client);
-        // del struct for hash
-        HASH_DEL(conn_list, iter_current);
-    }
+    dap_chain_node_client_t *client = g_list_nth_data(connect_list, (guint) n);
     pthread_mutex_unlock(&connect_list_mutex);
+    return client;
 }
-
 /**
- * Get present established connection by address
- *
- * return client, or NULL if the position is off the end of the list
+ * Get the number of established connections
  */
-const chain_node_client_t* chain_node_client_find(dap_chain_node_addr_t *address)
+int chain_node_client_list_count(void)
 {
-    int ret = 0;
-    if(!address)
-        return NULL;
-    chain_node_client_t *client_ret = NULL;
-    list_linked_item_t *item_tmp;
     pthread_mutex_lock(&connect_list_mutex);
-    HASH_FIND(hh, conn_list, address, sizeof(dap_chain_node_addr_t), item_tmp); // address already in the hash?
-    if(item_tmp != NULL) {
-        client_ret = item_tmp->client;
-    }
+    int len = g_list_length(connect_list);
     pthread_mutex_unlock(&connect_list_mutex);
-    return client_ret;
+    return len;
 }
 
diff --git a/dap_chain_node_remote.h b/dap_chain_node_remote.h
index 7f709584fd..0ce1cfdb52 100644
--- a/dap_chain_node_remote.h
+++ b/dap_chain_node_remote.h
@@ -23,31 +23,28 @@
 
 #include <stdbool.h>
 
-#include "dap_chain_node.h"
-#include "dap_chain_node_cli_connect.h"
+#include "dap_chain_node_client.h"
 
 /**
- * Add new established connection to the list
- *
- * return 0 OK, -1 error, -2 already present
+ * Add new established connection in the list
  */
-int chain_node_client_list_add(dap_chain_node_addr_t *address, chain_node_client_t *client);
+bool chain_node_client_list_add(dap_chain_node_client_t *client);
 
 /**
  * Delete established connection from the list
- *
- * return 0 OK, -1 error, -2 address not found
  */
-int chain_node_client_list_del(dap_chain_node_addr_t *address);
+bool chain_node_client_list_del(dap_chain_node_client_t *client);
 
 /**
- * Delete all established connection from the list
+ * Get one established connection
+ *
+ * n - the position of the established connection, counting from 0
+ *
+ * return client, or NULL if the position is off the end of the list
  */
-void chain_node_client_list_del_all(void);
+dap_chain_node_client_t* chain_node_client_list_get_item(int n);
 
 /**
- * Get present established connection by address
- *
- * return client, or NULL if the position is off the end of the list
+ * Get the number of established connections
  */
-const chain_node_client_t* chain_node_client_find(dap_chain_node_addr_t *address);
+int chain_node_client_list_count(void);
-- 
GitLab