Skip to content
Snippets Groups Projects
Commit cc210b12 authored by dmitriy.gerasimov's avatar dmitriy.gerasimov
Browse files

[*] Node client ops

[*] Reworked node cli API
parent 3daaac68
No related branches found
No related tags found
No related merge requests found
...@@ -30,9 +30,11 @@ ...@@ -30,9 +30,11 @@
#include "utlist.h" #include "utlist.h"
#include "dap_common.h" #include "dap_common.h"
#include "dap_strfuncs.h"
#include "dap_config.h" #include "dap_config.h"
#include "dap_chain_utxo.h" #include "dap_chain_utxo.h"
#include "dap_chain_net.h" #include "dap_chain_net.h"
#include "dap_chain_node_client.h"
#include "dap_module.h" #include "dap_module.h"
#define _XOPEN_SOURCE 700 #define _XOPEN_SOURCE 700
...@@ -42,6 +44,12 @@ ...@@ -42,6 +44,12 @@
#define LOG_TAG "chain_net" #define LOG_TAG "chain_net"
typedef enum dap_chain_net_state{
NET_STATE_BEGIN = 0,
NET_STATE_LINKS_CONNECTING,
NET_STATE_LINKS_ESTABLISHED,
} dap_chain_net_state_t;
/** /**
* @struct dap_chain_net_pvt * @struct dap_chain_net_pvt
* @details Private part of chain_net dap object * @details Private part of chain_net dap object
...@@ -51,7 +59,9 @@ typedef struct dap_chain_net_pvt{ ...@@ -51,7 +59,9 @@ typedef struct dap_chain_net_pvt{
pthread_cond_t proc_cond; pthread_cond_t proc_cond;
dap_chain_node_role_t node_role; dap_chain_node_role_t node_role;
uint8_t padding[4]; uint8_t padding[4];
//dap_client_t client;
dap_chain_node_client_t * clients_by_node_addr;
dap_chain_net_state_t state;
} dap_chain_net_pvt_t; } dap_chain_net_pvt_t;
typedef struct dap_chain_net_item{ typedef struct dap_chain_net_item{
...@@ -96,27 +106,6 @@ static void s_net_proc_start( dap_chain_net_t * a_net ) ...@@ -96,27 +106,6 @@ static void s_net_proc_start( dap_chain_net_t * a_net )
{ {
if ( pthread_create(& PVT(a_net)->proc_tid ,NULL, s_net_proc_thread, a_net) == 0 ){ if ( pthread_create(& PVT(a_net)->proc_tid ,NULL, s_net_proc_thread, a_net) == 0 ){
log_it (L_NOTICE,"Network processing thread started"); log_it (L_NOTICE,"Network processing thread started");
dap_chain_node_role_t l_role;
switch (l_role.enums = PVT (a_net)->node_role.enums){
case ROOT:
log_it(L_DEBUG , "Root node functions initialized");
case ROOT_DELEGATE:
log_it(L_DEBUG , "Root delegate node functions initialized");
case ARCHIVE:
log_it(L_DEBUG , "Archive node functions initialized");
case SHARD_DELEGATE:
if ( l_role.enums != ARCHIVE ){
log_it(L_DEBUG , "Shard delegate node functions initialized");
}
case MASTER:
log_it(L_DEBUG , "Master node functions initialized");
case FULL:
log_it(L_DEBUG , "Full node functions initialized");
case LIGHT:
log_it(L_DEBUG , "Light node functions initialized");
default:
log_it(L_NOTICE, "Node role initialized");
}
} }
} }
...@@ -148,7 +137,7 @@ dap_chain_net_t * dap_chain_net_new(const char * a_id, const char * a_name , ...@@ -148,7 +137,7 @@ dap_chain_net_t * dap_chain_net_new(const char * a_id, const char * a_name ,
{ {
dap_chain_net_t * ret = DAP_NEW_Z_SIZE (dap_chain_net_t, sizeof (ret->pub)+ sizeof (dap_chain_net_pvt_t) ); dap_chain_net_t * ret = DAP_NEW_Z_SIZE (dap_chain_net_t, sizeof (ret->pub)+ sizeof (dap_chain_net_pvt_t) );
ret->pub.name = strdup( a_name ); ret->pub.name = strdup( a_name );
if ( sscanf(a_id,"0x%0llx", &ret->pub.id.uint64 ) == 1 ){ if ( sscanf(a_id,"0x%016lx", &ret->pub.id.uint64 ) == 1 ){
if (strcmp (a_node_role, "root")==0){ if (strcmp (a_node_role, "root")==0){
PVT(ret)->node_role.enums = ROOT; PVT(ret)->node_role.enums = ROOT;
log_it (L_NOTICE, "Node role \"root\" selected"); log_it (L_NOTICE, "Node role \"root\" selected");
...@@ -186,16 +175,29 @@ int dap_chain_net_init() ...@@ -186,16 +175,29 @@ int dap_chain_net_init()
dap_config_get_item_str(l_cfg , "general" , "name" ), dap_config_get_item_str(l_cfg , "general" , "name" ),
dap_config_get_item_str(l_cfg , "general" , "node-role" ) dap_config_get_item_str(l_cfg , "general" , "node-role" )
); );
// Do specific actions l_net->pub.gdb_groups_prefix = dap_strdup (
dap_config_get_item_str_default(l_cfg , "dag" , "gdb_groups_prefix","" ) );
// UTXO model
uint16_t l_utxo_flags = 0;
switch ( PVT( l_net )->node_role.enums ) { switch ( PVT( l_net )->node_role.enums ) {
case ROOT_MASTER:
case ROOT: case ROOT:
case ROOT_DELEGATE: case ARCHIVE:
case SHARD_DELEGATE: l_utxo_flags |= DAP_CHAIN_UTXO_CHECK_TOKEN_EMISSION;
// dap_chain_net_ca_load ( dap_config_get_item_str ("")); case MASTER:
default: l_utxo_flags |= DAP_CHAIN_UTXO_CHECK_CELLS_DS;
log_it(L_DEBUG,"Net config loaded"); case CELL_MASTER:
l_utxo_flags |= DAP_CHAIN_UTXO_CHECK_TOKEN_EMISSION;
case FULL:
case LIGHT:
l_utxo_flags |= DAP_CHAIN_UTXO_CHECK_LOCAL_DS;
} }
dap_chain_utxo_init(l_utxo_flags);
// Init chains // Init chains
size_t l_chains_path_size =strlen(dap_config_path())+1+strlen(l_net->pub.name)+1+strlen("network")+1; size_t l_chains_path_size =strlen(dap_config_path())+1+strlen(l_net->pub.name)+1+strlen("network")+1;
char * l_chains_path = DAP_NEW_Z_SIZE (char,l_chains_path_size); char * l_chains_path = DAP_NEW_Z_SIZE (char,l_chains_path_size);
...@@ -230,6 +232,38 @@ int dap_chain_net_init() ...@@ -230,6 +232,38 @@ int dap_chain_net_init()
return -2; return -2;
} }
// Do specific role actions post-chain created
switch ( PVT( l_net )->node_role.enums ) {
case ROOT_MASTER:{
// Set to process everything in datum pool
dap_chain_t * l_chain = NULL;
DL_FOREACH(l_net->pub.chains, l_chain ) l_chain->is_datum_pool_proc = true;
log_it(L_INFO,"Root master node role established");
} break;
case ROOT:{
// Set to process only zerochain
dap_chain_id_t l_chain_id = {{0}};
dap_chain_t * l_chain = dap_chain_find_by_id(l_net->pub.id,l_chain_id);
l_chain->is_datum_pool_proc = true;
log_it(L_INFO,"Root node role established");
} break;
case CELL_MASTER:
case MASTER:{
// Set to process only plasma chain (id 0x0000000000000001 )
dap_chain_id_t l_chain_id = { .raw = {0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x01} };
dap_chain_t * l_chain = dap_chain_find_by_id(l_net->pub.id, l_chain_id );
l_chain->is_datum_pool_proc = true;
log_it(L_INFO,"Master node role established");
} break;
case FULL:{
log_it(L_INFO,"Full node role established");
} break;
case LIGHT:
default:
log_it(L_INFO,"Light node role established");
}
// Add network to the list // Add network to the list
dap_chain_net_item_t * l_net_item = DAP_NEW_Z( dap_chain_net_item_t); dap_chain_net_item_t * l_net_item = DAP_NEW_Z( dap_chain_net_item_t);
...@@ -277,6 +311,11 @@ dap_chain_net_t * dap_chain_net_by_name( const char * a_name) ...@@ -277,6 +311,11 @@ dap_chain_net_t * dap_chain_net_by_name( const char * a_name)
return NULL; return NULL;
} }
/**
* @brief dap_chain_net_id_by_name
* @param a_name
* @return
*/
dap_chain_net_id_t dap_chain_net_id_by_name( const char * a_name) dap_chain_net_id_t dap_chain_net_id_by_name( const char * a_name)
{ {
dap_chain_net_t *l_net = dap_chain_net_by_name( a_name ); dap_chain_net_t *l_net = dap_chain_net_by_name( a_name );
...@@ -285,3 +324,8 @@ dap_chain_net_id_t dap_chain_net_id_by_name( const char * a_name) ...@@ -285,3 +324,8 @@ dap_chain_net_id_t dap_chain_net_id_by_name( const char * a_name)
l_ret.uint64 = l_net->pub.id.uint64; l_ret.uint64 = l_net->pub.id.uint64;
return l_ret; return l_ret;
} }
void dap_chain_net_proc_datapool (dap_chain_net_t * a_net)
{
}
...@@ -36,6 +36,7 @@ typedef struct dap_chain_net{ ...@@ -36,6 +36,7 @@ typedef struct dap_chain_net{
struct { struct {
dap_chain_net_id_t id; dap_chain_net_id_t id;
char * name; char * name;
char * gdb_groups_prefix;
dap_chain_t * chains; // double-linked list of chains dap_chain_t * chains; // double-linked list of chains
} pub; } pub;
uint8_t pvt[]; uint8_t pvt[];
...@@ -49,6 +50,7 @@ dap_chain_net_t * dap_chain_net_new (const char * a_id, const char * a_name, ...@@ -49,6 +50,7 @@ dap_chain_net_t * dap_chain_net_new (const char * a_id, const char * a_name,
void dap_chain_net_delete( dap_chain_net_t * a_net); void dap_chain_net_delete( dap_chain_net_t * a_net);
void dap_chain_net_proc_datapool (dap_chain_net_t * a_net);
dap_chain_net_t * dap_chain_net_by_name( const char * a_name); dap_chain_net_t * dap_chain_net_by_name( const char * a_name);
dap_chain_net_id_t dap_chain_net_id_by_name( const char * a_name); dap_chain_net_id_t dap_chain_net_id_by_name( const char * a_name);
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <assert.h>
//#include <glib.h> //#include <glib.h>
#ifndef _WIN32 #ifndef _WIN32
...@@ -60,56 +61,8 @@ typedef int SOCKET; ...@@ -60,56 +61,8 @@ typedef int SOCKET;
static SOCKET server_sockfd = -1; static SOCKET server_sockfd = -1;
static const COMMAND commands[] = static dap_chain_node_cmd_item_t * s_commands = NULL;
{
{ "global_db", com_global_db, "Work with global database",
"global_db node add -addr {<node address> | -alias <node alias>} -cell <cell id> {-ipv4 <ipv4 external address> | -ipv6 <ipv6 external address>}\n"
"global_db node del -addr <node address> | -alias <node alias>\n"
"global_db node link {add|del} {-addr <node address> | -alias <node alias>} -link <node address>\n"
"global_db node dump\n"
"global_db node dump -addr <node address> | -alias <node alias>\n"
"global_db node get\n"
"global_db node set -addr <node address> | -alias <node alias>\n"
"global_db node remote_set -addr <node address> | -alias <node alias>"
},
{ "node", com_node, "Work with node",
"node alias {<node address> | -alias <node alias>}\n"
"node connect {<node address> | -alias <node alias>}\n"
"node handshake {<node address> | -alias <node alias>}" },
{ "ping", com_ping, "Send ICMP ECHO_REQUEST to network hosts",
"ping [-c <count>] host" },
{ "traceroute", com_traceroute, "Print the hops and time of packets trace to network host",
"traceroute host" },
{ "tracepath", com_tracepath, "Traces path to a network host along this path",
"tracepath host" },
{ "help", com_help, "Description of command parameters", "" },
{ "?", com_help, "Synonym for 'help'", "" },
{ "wallet", com_tx_wallet, "Wallet info", "wallet [list | info -addr <addr> -w <wallet_name>]" },
{ "token_emit", com_token_emit, "Token emission",
"token_emit addr <addr> tokent <token> certs <cert> emission_value <val>" },
{ "tx_create", com_tx_create, "Make transaction",
"tx_create from_wallet_name <name> to_addr <addr> token <token> value <val> [fee <addr> value_fee <val>]" },
{ "tx_cond_create", com_tx_cond_create, "Make cond transaction",
"tx_cond_create todo" },
{ "tx_verify", com_tx_verify, "Verifing transaction",
"tx_verify -wallet <wallet name> [-path <wallet path>]" },
{ (char *) NULL, (cmdfunc_t *) NULL, (char *) NULL }
};
/**
* Look up NAME as the name of a command, and return a pointer to that
* command. Return a NULL pointer if NAME isn't a command name.
*/
const COMMAND* find_command(const char *name)
{
register int i;
for(i = 0; commands[i].name; i++)
if(strcmp(name, commands[i].name) == 0)
return (&commands[i]);
return ((COMMAND *) NULL);
}
/** /**
* Wait for data * Wait for data
...@@ -160,7 +113,7 @@ static bool is_valid_socket(SOCKET sock) ...@@ -160,7 +113,7 @@ static bool is_valid_socket(SOCKET sock)
// feature of disconnection under Unix (QNX) // feature of disconnection under Unix (QNX)
// under Windows, with socket closed res = 0, in Unix res = -1 // under Windows, with socket closed res = 0, in Unix res = -1
char buf[2]; char buf[2];
int res = recv(sock, buf, 1, MSG_PEEK); // MSG_PEEK The data is treated as unread and the next recv() function shall still return this data. long res = recv(sock, buf, 1, MSG_PEEK); // MSG_PEEK The data is treated as unread and the next recv() function shall still return this data.
if(res < 0) if(res < 0)
return false; return false;
// data in the buffer must be(count_desc>0), but read 0 bytes(res=0) // data in the buffer must be(count_desc>0), but read 0 bytes(res=0)
...@@ -176,10 +129,10 @@ static bool is_valid_socket(SOCKET sock) ...@@ -176,10 +129,10 @@ static bool is_valid_socket(SOCKET sock)
* timeout in milliseconds * timeout in milliseconds
* return the number of read bytes (-1 err or -2 timeout) * return the number of read bytes (-1 err or -2 timeout)
*/ */
int s_recv(SOCKET sock, unsigned char *buf, int bufsize, int timeout) long s_recv(SOCKET sock, unsigned char *buf, size_t bufsize, int timeout)
{ {
struct pollfd fds; struct pollfd fds;
int res; long res;
fds.fd = sock; fds.fd = sock;
fds.events = POLLIN; // | POLLNVAL | POLLHUP | POLLERR | POLLPRI;// | POLLRDNORM;//POLLOUT | fds.events = POLLIN; // | POLLNVAL | POLLHUP | POLLERR | POLLPRI;// | POLLRDNORM;//POLLOUT |
res = poll(&fds, 1, timeout); res = poll(&fds, 1, timeout);
...@@ -193,7 +146,7 @@ int s_recv(SOCKET sock, unsigned char *buf, int bufsize, int timeout) ...@@ -193,7 +146,7 @@ int s_recv(SOCKET sock, unsigned char *buf, int bufsize, int timeout)
// res = read(sock, (char*) buf, bufsize); // res = read(sock, (char*) buf, bufsize);
res = recv(sock, (char*) buf, bufsize, 0); //MSG_WAITALL res = recv(sock, (char*) buf, bufsize, 0); //MSG_WAITALL
if(res <= 0) { //EINTR=4 ENOENT=2 EINVAL=22 ECONNRESET=254 if(res <= 0) { //EINTR=4 ENOENT=2 EINVAL=22 ECONNRESET=254
printf("[s_recv] recv()=%d errno=%d\n", res, errno); printf("[s_recv] recv()=%ld errno=%d\n", res, errno);
} }
return res; return res;
} }
...@@ -209,18 +162,18 @@ int s_recv(SOCKET sock, unsigned char *buf, int bufsize, int timeout) ...@@ -209,18 +162,18 @@ int s_recv(SOCKET sock, unsigned char *buf, int bufsize, int timeout)
char* s_get_next_str(SOCKET nSocket, int *dwLen, const char *stop_str, bool del_stop_str, int timeout) char* s_get_next_str(SOCKET nSocket, int *dwLen, const char *stop_str, bool del_stop_str, int timeout)
{ {
bool bSuccess = false; bool bSuccess = false;
int nRecv = 0; // count of bytes received long nRecv = 0; // count of bytes received
int stop_str_len = (stop_str) ? strlen(stop_str) : 0; size_t stop_str_len = (stop_str) ? strlen(stop_str) : 0;
// if there is nothing to look for // if there is nothing to look for
if(!stop_str_len) if(!stop_str_len)
return NULL; return NULL;
int lpszBuffer_len = 256; size_t lpszBuffer_len = 256;
char *lpszBuffer = calloc(1, lpszBuffer_len); char *lpszBuffer = DAP_NEW_Z_SIZE(char, lpszBuffer_len);
// received string will not be larger than MAX_REPLY_LEN // received string will not be larger than MAX_REPLY_LEN
while(1) //nRecv < MAX_REPLY_LEN) while(1) //nRecv < MAX_REPLY_LEN)
{ {
// read one byte // read one byte
int ret = s_recv(nSocket, (unsigned char *) (lpszBuffer + nRecv), 1, timeout); long ret = s_recv(nSocket, (unsigned char *) (lpszBuffer + nRecv), 1, timeout);
//int ret = recv(nSocket,lpszBuffer+nRecv,1, 0); //int ret = recv(nSocket,lpszBuffer+nRecv,1, 0);
if(ret <= 0) if(ret <= 0)
{ {
...@@ -228,13 +181,13 @@ char* s_get_next_str(SOCKET nSocket, int *dwLen, const char *stop_str, bool del_ ...@@ -228,13 +181,13 @@ char* s_get_next_str(SOCKET nSocket, int *dwLen, const char *stop_str, bool del_
} }
nRecv += ret; nRecv += ret;
//printf("**debug** socket=%d read %d bytes '%0s'",nSocket, ret, (lpszBuffer + nRecv)); //printf("**debug** socket=%d read %d bytes '%0s'",nSocket, ret, (lpszBuffer + nRecv));
while((nRecv + 1) >= lpszBuffer_len) while((nRecv + 1) >= (long) lpszBuffer_len)
{ {
lpszBuffer_len *= 2; lpszBuffer_len *= 2;
lpszBuffer = (char*) realloc(lpszBuffer, lpszBuffer_len); lpszBuffer = (char*) realloc(lpszBuffer, lpszBuffer_len);
} }
// search for the required string // search for the required string
if(nRecv >= stop_str_len) { if(nRecv >= (long) stop_str_len) {
// found the required string // found the required string
if(!strncasecmp(lpszBuffer + nRecv - stop_str_len, stop_str, stop_str_len)) { if(!strncasecmp(lpszBuffer + nRecv - stop_str_len, stop_str, stop_str_len)) {
bSuccess = true; bSuccess = true;
...@@ -246,16 +199,16 @@ char* s_get_next_str(SOCKET nSocket, int *dwLen, const char *stop_str, bool del_ ...@@ -246,16 +199,16 @@ char* s_get_next_str(SOCKET nSocket, int *dwLen, const char *stop_str, bool del_
if(bSuccess) { if(bSuccess) {
// delete the searched string // delete the searched string
if(del_stop_str) { if(del_stop_str) {
lpszBuffer[nRecv - stop_str_len] = '\0'; lpszBuffer[nRecv - (long) stop_str_len] = '\0';
if(dwLen) if(dwLen)
*dwLen = nRecv - stop_str_len; *dwLen =(int) nRecv - (int) stop_str_len;
} }
else { else {
lpszBuffer[nRecv] = '\0'; lpszBuffer[nRecv] = '\0';
if(dwLen) if(dwLen)
*dwLen = nRecv; *dwLen = (int) nRecv;
} }
lpszBuffer = realloc(lpszBuffer, *dwLen + 1); lpszBuffer = DAP_REALLOC(lpszBuffer,(size_t) *dwLen + 1);
return lpszBuffer; return lpszBuffer;
} }
// in case of an error or missing string // in case of an error or missing string
...@@ -322,10 +275,10 @@ static void* thread_one_client_func(void *args) ...@@ -322,10 +275,10 @@ static void* thread_one_client_func(void *args)
list = dap_list_next(list); list = dap_list_next(list);
// execute command // execute command
char *str_cmd = dap_strdup_printf("%s", cmd_name); char *str_cmd = dap_strdup_printf("%s", cmd_name);
const COMMAND *command = find_command(cmd_name); dap_chain_node_cmd_item_t *l_cmd = dap_chain_node_cli_cmd_find(cmd_name);
int res = -1; int res = -1;
char *str_reply = NULL; char *str_reply = NULL;
if(command) if(l_cmd)
{ {
while(list) { while(list) {
str_cmd = dap_strdup_printf("%s;%s", str_cmd, list->data); str_cmd = dap_strdup_printf("%s;%s", str_cmd, list->data);
...@@ -336,8 +289,8 @@ static void* thread_one_client_func(void *args) ...@@ -336,8 +289,8 @@ static void* thread_one_client_func(void *args)
char **argv = dap_strsplit(str_cmd, ";", -1); char **argv = dap_strsplit(str_cmd, ";", -1);
// Call the command function // Call the command function
if(command && command->func) if(l_cmd && l_cmd->func)
res = (*(command->func))(argc, (const char **) argv, &str_reply); res = (*(l_cmd->func))(argc, (const char **) argv, &str_reply);
dap_strfreev(argv); dap_strfreev(argv);
} }
else else
...@@ -396,6 +349,96 @@ static void* thread_main_func(void *args) ...@@ -396,6 +349,96 @@ static void* thread_main_func(void *args)
return NULL; return NULL;
} }
/**
* Write text to reply string
*/
void dap_chain_node_cli_set_reply_text(char **str_reply, const char *str, ...)
{
if(str_reply) {
if(*str_reply) {
assert(!*str_reply);
DAP_DELETE(*str_reply);
*str_reply = NULL;
}
va_list args;
va_start(args, str);
*str_reply = dap_strdup_vprintf(str, args); //*str_reply = dap_strdup(str);
va_end(args);
}
}
/**
* find option value
*
* return index of string in argv, or 0 if not found
*/
int dap_chain_node_cli_find_option_val(const char** argv, int arg_start, int arg_end, const char *opt_name, const char **opt_value)
{
int arg_index = arg_start;
const char *arg_string;
while(arg_index < arg_end)
{
arg_string = argv[arg_index];
// find opt_name
if(arg_string && opt_name && !strcmp(arg_string, opt_name)) {
// find opt_value
if(opt_value) {
arg_string = argv[++arg_index];
if(arg_string) {
*opt_value = arg_string;
return arg_index;
}
}
else
// need only opt_name
return arg_index;
}
arg_index++;
}
return 0;
}
/**
* @brief s_cmd_item_create
* @param a_name
* @param func
* @param doc
* @param doc_ex
* @return
*/
void dap_chain_node_cli_cmd_item_create(const char * a_name, cmdfunc_t *a_func, const char *a_doc, const char *a_doc_ex)
{
dap_chain_node_cmd_item_t *l_cmd_item = DAP_NEW_Z(dap_chain_node_cmd_item_t);
snprintf(l_cmd_item->name,sizeof (l_cmd_item->name),"%s",a_name);
l_cmd_item->doc = strdup( a_doc);
l_cmd_item->doc_ex = strdup( a_doc_ex);
HASH_ADD_STR(s_commands,name,l_cmd_item);
log_it(L_DEBUG,"Added command %s",l_cmd_item->name);
}
/**
* @brief dap_chain_node_cli_command_get_first
* @return
*/
dap_chain_node_cmd_item_t* dap_chain_node_cli_cmd_get_first()
{
return s_commands;
}
/**
* @brief dap_chain_node_cli_command_find
* @param a_name
* @return
*/
dap_chain_node_cmd_item_t* dap_chain_node_cli_cmd_find(const char *a_name)
{
dap_chain_node_cmd_item_t *l_cmd_item = NULL;
HASH_FIND_STR(s_commands,a_name,l_cmd_item);
return l_cmd_item;
}
/** /**
* Initialization of the server side of the interaction * Initialization of the server side of the interaction
* with the console kelvin-node-cli * with the console kelvin-node-cli
...@@ -407,9 +450,41 @@ int dap_chain_node_cli_init(dap_config_t * g_config) ...@@ -407,9 +450,41 @@ int dap_chain_node_cli_init(dap_config_t * g_config)
struct sockaddr_un server = { AF_UNIX, UNIX_SOCKET_FILE }; struct sockaddr_un server = { AF_UNIX, UNIX_SOCKET_FILE };
//server.sun_family = AF_UNIX; //server.sun_family = AF_UNIX;
//strcpy(server.sun_path, SOCKET_FILE); //strcpy(server.sun_path, SOCKET_FILE);
dap_chain_node_cli_cmd_item_create ("global_db", com_global_db, "Work with global database",
"global_db wallet_info set -addr <wallet address> -cell <cell id> \n"
"global_db cells add -cell <cell id> \n"
"global_db node add -addr {<node address> | -alias <node alias>} -cell <cell id> {-ipv4 <ipv4 external address> | -ipv6 <ipv6 external address>}\n"
"global_db node del -addr <node address> | -alias <node alias>\n"
"global_db node link {add|del} {-addr <node address> | -alias <node alias>} -link <node address>\n"
"global_db node dump\n"
"global_db node dump -addr <node address> | -alias <node alias>\n"
"global_db node get\n"
"global_db node set -addr <node address> | -alias <node alias>\n"
"global_db node remote_set -addr <node address> | -alias <node alias>");
dap_chain_node_cli_cmd_item_create ("node", com_node, "Work with node",
"node alias {<node address> | -alias <node alias>}\n"
"node connect {<node address> | -alias <node alias>}\n"
"node handshake {<node address> | -alias <node alias>}");
dap_chain_node_cli_cmd_item_create ("ping", com_ping, "Send ICMP ECHO_REQUEST to network hosts",
"ping [-c <count>] host");
dap_chain_node_cli_cmd_item_create ("traceroute", com_traceroute, "Print the hops and time of packets trace to network host",
"traceroute host");
dap_chain_node_cli_cmd_item_create ("tracepath", com_tracepath, "Traces path to a network host along this path",
"tracepath host");
dap_chain_node_cli_cmd_item_create ("help", com_help, "Description of command parameters", "");
dap_chain_node_cli_cmd_item_create ("?", com_help, "Synonym for 'help'", "");
dap_chain_node_cli_cmd_item_create ("wallet", com_tx_wallet, "Wallet info", "wallet [list | info -addr <addr> -w <wallet_name>]");
dap_chain_node_cli_cmd_item_create ("token_emit", com_token_emit, "Token emission",
"token_emit addr <addr> tokent <token> certs <cert> emission_value <val>");
dap_chain_node_cli_cmd_item_create ("tx_create", com_tx_create, "Make transaction",
"tx_create from_wallet_name <name> to_addr <addr> token <token> value <val> [fee <addr> value_fee <val>]" );
dap_chain_node_cli_cmd_item_create ("tx_cond_create", com_tx_cond_create, "Make cond transaction",
"tx_cond_create todo" );
dap_chain_node_cli_cmd_item_create ("tx_verify", com_tx_verify, "Verifing transaction",
"tx_verify -wallet <wallet name> [-path <wallet path>]" );
// init client for handshake // init client for handshake
dap_chain_node_client_init();
SOCKET sockfd; SOCKET sockfd;
......
...@@ -18,30 +18,36 @@ ...@@ -18,30 +18,36 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
__BEGIN_DECLS
#include "dap_common.h" #include "dap_common.h"
#include "dap_config.h" #include "dap_config.h"
#include "uthash.h"
#define UNIX_SOCKET_FILE "/opt/kelvin-node/var/run/node_cli.sock" #define UNIX_SOCKET_FILE "/opt/kelvin-node/var/run/node_cli.sock"
//#define UNIX_SOCKET_FILE "/var/run/node_cli.sock" //#define UNIX_SOCKET_FILE "/var/run/node_cli.sock"
typedef int cmdfunc_t(int argc, const char ** argv, char **str_reply); typedef int cmdfunc_t(int argc, const char ** argv, char **str_reply);
typedef struct { typedef struct dap_chain_node_cmd_item{
char *name; /* User printable name of the function. */ char name[32]; /* User printable name of the function. */
cmdfunc_t *func; /* Function to call to do the job. */ cmdfunc_t *func; /* Function to call to do the job. */
char *doc; /* Documentation for this function. */ char *doc; /* Documentation for this function. */
char *doc_ex; /* Full documentation for this function. */ char *doc_ex; /* Full documentation for this function. */
} COMMAND; UT_hash_handle hh;
} dap_chain_node_cmd_item_t;
/** /**
* Look up NAME as the name of a command, and return a pointer to that * Look up NAME as the name of a command, and return a pointer to that
* command. Return a NULL pointer if NAME isn't a command name. * command. Return a NULL pointer if NAME isn't a command name.
*/ */
const COMMAND* find_command(const char *name); dap_chain_node_cmd_item_t* dap_chain_node_cli_cmd_get_first();
dap_chain_node_cmd_item_t* dap_chain_node_cli_cmd_find(const char *a_name);
void dap_chain_node_cli_cmd_item_create(const char * a_name, cmdfunc_t *a_func, const char *a_doc, const char *a_doc_ex);
void dap_chain_node_cli_set_reply_text(char **str_reply, const char *str, ...);
int dap_chain_node_cli_find_option_val(const char** argv, int arg_start, int arg_end, const char *opt_name, const char **opt_value);
/** /**
* Initialization of the server side of the interaction * Initialization of the server side of the interaction
...@@ -53,5 +59,3 @@ int dap_chain_node_cli_init(dap_config_t * g_config); ...@@ -53,5 +59,3 @@ int dap_chain_node_cli_init(dap_config_t * g_config);
* Deinitialization of the server side * Deinitialization of the server side
*/ */
void dap_chain_node_cli_delete(void); void dap_chain_node_cli_delete(void);
__END_DECLS
This diff is collapsed.
/* /*
* Authors: * Authors:
* Dmitriy A. Gearasimov <naeper@demlabs.net> * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
* DeM Labs Inc. https://demlabs.net * DeM Labs Inc. https://demlabs.net
* Kelvin Project https://github.com/kelvinblockchain
* Copyright (c) 2017-2019
* All rights reserved.
This file is part of DAP (Deus Applications Prototypes) the open source project This file is part of DAP (Deus Applications Prototypes) the open source project
DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify DAP (Deus Applicaions Prototypes) is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
(at your option) any later version. (at your option) any later version.
DAP is distributed in the hope that it will be useful, DAP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details. GNU General Public License for more details.
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with any DAP based project. If not, see <http://www.gnu.org/licenses/>. along with any DAP based project. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
...@@ -31,11 +34,6 @@ ...@@ -31,11 +34,6 @@
*/ */
dap_chain_node_addr_t* get_name_by_alias(const char *alias); dap_chain_node_addr_t* get_name_by_alias(const char *alias);
/**
* Look up NAME as the name of a command, and return a pointer to that
* command. Return a NULL pointer if NAME isn't a command name.
*/
const COMMAND* find_command(const char *name);
/** /**
......
...@@ -24,24 +24,27 @@ ...@@ -24,24 +24,27 @@
#include <pthread.h> #include <pthread.h>
#include <stdbool.h> #include <stdbool.h>
#include "uthash.h"
#include "dap_client.h" #include "dap_client.h"
#include "dap_chain_node.h" #include "dap_chain_node.h"
// connection states // connection states
enum { typedef enum dap_chain_node_client_state{
NODE_CLIENT_STATE_ERROR = -1, NODE_CLIENT_STATE_ERROR = -1,
NODE_CLIENT_STATE_INIT, NODE_CLIENT_STATE_INIT,
NODE_CLIENT_STATE_CONNECT,
NODE_CLIENT_STATE_CONNECTED,
//NODE_CLIENT_STATE_SEND,
//NODE_CLIENT_STATE_SENDED,
NODE_CLIENT_STATE_GET_NODE_ADDR, NODE_CLIENT_STATE_GET_NODE_ADDR,
NODE_CLIENT_STATE_SET_NODE_ADDR, NODE_CLIENT_STATE_SET_NODE_ADDR,
NODE_CLIENT_STATE_PING, NODE_CLIENT_STATE_PING,
NODE_CLIENT_STATE_PONG, NODE_CLIENT_STATE_PONG,
NODE_CLIENT_STATE_CONNECT,
NODE_CLIENT_STATE_CONNECTED,
//NODE_CLIENT_STATE_SEND,
//NODE_CLIENT_STATE_SENDED,
NODE_CLIENT_STATE_SYNC_GDB,
NODE_CLIENT_STATE_SYNC_CHAINS,
NODE_CLIENT_STATE_STANDBY,
NODE_CLIENT_STATE_END NODE_CLIENT_STATE_END
}; } dap_chain_node_client_state_t;
typedef struct dap_chain_node_client dap_chain_node_client_t; typedef struct dap_chain_node_client dap_chain_node_client_t;
...@@ -49,7 +52,8 @@ typedef void (*dap_chain_node_client_callback_t) (dap_chain_node_client_t *, voi ...@@ -49,7 +52,8 @@ typedef void (*dap_chain_node_client_callback_t) (dap_chain_node_client_t *, voi
// state for a client connection // state for a client connection
typedef struct dap_chain_node_client { typedef struct dap_chain_node_client {
int state; dap_chain_node_client_state_t state;
dap_chain_cell_id_t cell_id;
dap_client_t *client; dap_client_t *client;
dap_events_t *events; dap_events_t *events;
...@@ -58,12 +62,18 @@ typedef struct dap_chain_node_client { ...@@ -58,12 +62,18 @@ typedef struct dap_chain_node_client {
pthread_mutex_t wait_mutex; pthread_mutex_t wait_mutex;
uint8_t *recv_data; uint8_t *recv_data;
size_t recv_data_len; size_t recv_data_len;
// For hash indexing
UT_hash_handle hh;
dap_chain_node_addr_t remote_node_addr;
struct in_addr remote_ipv4;
struct in6_addr remote_ipv6;
} dap_chain_node_client_t; } dap_chain_node_client_t;
int dap_chain_node_client_init(void); int dap_chain_node_client_init(void);
void dap_chain_node_client_deinit(); void dap_chain_node_client_deinit(void);
/** /**
* Create handshake to server * Create handshake to server
...@@ -92,5 +102,5 @@ int dap_chain_node_client_send_chain_net_request(dap_chain_node_client_t *a_clie ...@@ -92,5 +102,5 @@ int dap_chain_node_client_send_chain_net_request(dap_chain_node_client_t *a_clie
* waited_state state which we will wait, sample NODE_CLIENT_STATE_CONNECT or NODE_CLIENT_STATE_SENDED * waited_state state which we will wait, sample NODE_CLIENT_STATE_CONNECT or NODE_CLIENT_STATE_SENDED
* return -1 false, 0 timeout, 1 end of connection or sending data * return -1 false, 0 timeout, 1 end of connection or sending data
*/ */
int chain_node_client_wait(dap_chain_node_client_t *client, int waited_state, int timeout_ms); int chain_node_client_wait(dap_chain_node_client_t *client, dap_chain_node_client_state_t waited_state, int timeout_ms);
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