Skip to content
Snippets Groups Projects
Commit 3cfb35df authored by Aleksandr Lysikov's avatar Aleksandr Lysikov
Browse files

update to merge with to master

parent e9014161
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......@@ -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);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment