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

datum check in mempool

parent 8d037387
No related branches found
No related tags found
No related merge requests found
...@@ -142,6 +142,8 @@ void client_mempool_close(client_mempool_t *mempool) ...@@ -142,6 +142,8 @@ void client_mempool_close(client_mempool_t *mempool)
{ {
if(mempool) if(mempool)
{ {
// TODO send last request for dehandshake with "SessionCloseAfterRequest=true"
// ...
dap_client_pvt_t *l_client_internal = DAP_CLIENT_PVT(mempool->a_client); dap_client_pvt_t *l_client_internal = DAP_CLIENT_PVT(mempool->a_client);
DAP_DELETE(l_client_internal->uplink_addr); DAP_DELETE(l_client_internal->uplink_addr);
dap_client_delete(mempool->a_client); dap_client_delete(mempool->a_client);
...@@ -152,19 +154,64 @@ void client_mempool_close(client_mempool_t *mempool) ...@@ -152,19 +154,64 @@ void client_mempool_close(client_mempool_t *mempool)
} }
} }
/**
* datum add in mempool
*
* return -1 not connected, 1 Send packet OK
*/
int client_mempool_send_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool) int client_mempool_send_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool)
{ {
if(!mempool || !datum_mempool || mempool->state<CLIENT_MEMPOOL_CONNECTED)
return -1;
const char * a_path = "mempool"; const char * a_path = "mempool";
const char *a_suburl = "mempool"; //"enc_init"; const char *a_suburl = "mempool"; //"enc_init";
const char* a_query = ""; const char* a_query = "";
uint8_t action = DAP_DATUM_MEMPOOL_ADD;
size_t a_request_size = 0; size_t a_request_size = 0;
uint8_t *a_request = dap_datum_mempool_serialize(datum_mempool, &a_request_size); uint8_t *a_request = dap_datum_mempool_serialize(datum_mempool, &a_request_size);
uint8_t *a_request_out = DAP_NEW_Z_SIZE(uint8_t, a_request_size * 2); uint8_t *a_request_out = DAP_NEW_Z_SIZE(uint8_t, a_request_size * 2 + 1); // a_request + 1 byte for type action
bin2hex(a_request_out, a_request, a_request_size); *((uint8_t*) a_request_out) = action;
//uint8_t *a_request = "1234567\089012345643634346i34itkreghrth"; bin2hex(a_request_out + 1, a_request, a_request_size);
//size_t a_request_size = 20; mempool->state = CLIENT_MEMPOOL_SEND;
dap_client_request_enc(mempool->a_client, a_path, a_suburl, a_query, a_request_out, a_request_size*2, dap_client_request_enc(mempool->a_client, a_path, a_suburl, a_query, a_request_out, a_request_size * 2 + 1,
a_response_proc, a_response_error); a_response_proc, a_response_error);
//DAP_DELETE(a_request); DAP_DELETE(a_request);
DAP_DELETE(a_request_out);
return 1; return 1;
} }
/**
* datum check in mempool
*
* return -1 not connected or error, 1 present in mempool, 0 absent in mempool
*/
int client_mempool_check_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool)
{
if(!mempool || !datum_mempool || mempool->state<CLIENT_MEMPOOL_CONNECTED)
return -1;
const char * a_path = "mempool";
const char *a_suburl = "mempool"; //"enc_init";
const char* a_query = "";
uint8_t action = DAP_DATUM_MEMPOOL_CHECK;
size_t a_request_size = 0;
uint8_t *a_request = dap_datum_mempool_serialize(datum_mempool, &a_request_size);
uint8_t *a_request_out = DAP_NEW_Z_SIZE(uint8_t, a_request_size * 2 + 1); // a_request + 1 byte for type action
*((uint8_t*) a_request_out) = action;
bin2hex(a_request_out + 1, a_request, a_request_size);
dap_client_request_enc(mempool->a_client, a_path, a_suburl, a_query, a_request_out, a_request_size * 2 + 1,
a_response_proc, a_response_error);
DAP_DELETE(a_request);
DAP_DELETE(a_request_out);
return 1;
}
/**
* datum delete from mempool
*/
int client_mempool_del_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool)
{
if(!mempool || !datum_mempool || mempool->state<CLIENT_MEMPOOL_CONNECTED)
return -1;
return 0;
}
...@@ -6,7 +6,13 @@ ...@@ -6,7 +6,13 @@
// connection states // connection states
enum { enum {
CLIENT_MEMPOOL_ERROR = -1, CLIENT_MEMPOOL_INIT, CLIENT_MEMPOOL_CONNECT, CLIENT_MEMPOOL_CONNECTED, CLIENT_MEMPOOL_SENDED, CLIENT_MEMPOOL_END CLIENT_MEMPOOL_ERROR = -1,
CLIENT_MEMPOOL_INIT,
CLIENT_MEMPOOL_CONNECT,
CLIENT_MEMPOOL_CONNECTED,
CLIENT_MEMPOOL_SEND,
CLIENT_MEMPOOL_SENDED,
CLIENT_MEMPOOL_END
}; };
// state for a client connection with mempool // state for a client connection with mempool
...@@ -21,7 +27,6 @@ typedef struct client_mempool_t { ...@@ -21,7 +27,6 @@ typedef struct client_mempool_t {
int client_mempool_init(void); int client_mempool_init(void);
void client_mempool_deinit(); void client_mempool_deinit();
client_mempool_t* client_mempool_connect(const char *addr); client_mempool_t* client_mempool_connect(const char *addr);
void client_mempool_close(client_mempool_t *mempool); void client_mempool_close(client_mempool_t *mempool);
...@@ -31,5 +36,15 @@ void client_mempool_close(client_mempool_t *mempool); ...@@ -31,5 +36,15 @@ void client_mempool_close(client_mempool_t *mempool);
*/ */
int client_mempool_wait(client_mempool_t *mempool, int waited_state, int timeout_ms); int client_mempool_wait(client_mempool_t *mempool, int waited_state, int timeout_ms);
/**
* datum add in mempool
*/
int client_mempool_send_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool); int client_mempool_send_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool);
/**
* datum check in mempool
*/
int client_mempool_check_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool);
/**
* datum delete from mempool
*/
int client_mempool_del_datum(client_mempool_t *mempool, dap_datum_mempool_t *datum_mempool);
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
#define FILE_MEMPOOL_DB "1.db" // TODO get from settings #define FILE_MEMPOOL_DB "1.db" // TODO get from settings
#define LOG_TAG "MEMPOOL"
uint8_t* dap_datum_mempool_serialize(dap_datum_mempool_t *datum_mempool, size_t *size) uint8_t* dap_datum_mempool_serialize(dap_datum_mempool_t *datum_mempool, size_t *size)
{ {
size_t a_request_size = 2 * sizeof(uint16_t), shift_size = 0; size_t a_request_size = 2 * sizeof(uint16_t), shift_size = 0;
...@@ -86,14 +88,14 @@ void dap_datum_mempool_free(dap_datum_mempool_t *datum) ...@@ -86,14 +88,14 @@ void dap_datum_mempool_free(dap_datum_mempool_t *datum)
/** /**
* *
*/ */
char* calc_datum_hash(const char *datum_str, size_t datum_size) static char* calc_datum_hash(const char *datum_str, size_t datum_size)
{ {
dap_chain_hash_t a_hash; dap_chain_hash_t a_hash;
dap_hash((char*) datum_str, datum_size, a_hash.raw, sizeof(a_hash.raw), DAP_HASH_TYPE_SLOW_0); dap_hash((char*) datum_str, datum_size, a_hash.raw, sizeof(a_hash.raw), DAP_HASH_TYPE_SLOW_0);
size_t a_str_max = sizeof(a_hash.raw) * 2; size_t a_str_max = (sizeof(a_hash.raw) + 1) * 2 + 2; /* heading 0x */
char *a_str = DAP_NEW_Z_SIZE(char, a_str_max); char *a_str = DAP_NEW_Z_SIZE(char, a_str_max);
size_t hash_len = dap_chain_hash_to_str(&a_hash, a_str, a_str_max); size_t hash_len = dap_chain_hash_to_str(&a_hash, a_str, a_str_max);
if(hash_len) { if(!hash_len) {
DAP_DELETE(a_str); DAP_DELETE(a_str);
return NULL; return NULL;
} }
...@@ -136,7 +138,7 @@ int hex2bin(char *out, const unsigned char *in, int len) ...@@ -136,7 +138,7 @@ int hex2bin(char *out, const unsigned char *in, int len)
// 'a'-'f' = 0x61-0x66 // 'a'-'f' = 0x61-0x66
// 'A'-'F' = 0x41-0x46 // 'A'-'F' = 0x41-0x46
int ct = len; int ct = len;
if(!in || !out || len < 0 || len & 1) if(!in || !out || len < 0 || (len & 1))
return -1; return -1;
while(ct > 0) while(ct > 0)
{ {
...@@ -162,28 +164,64 @@ void chain_mempool_proc(struct dap_http_simple *cl_st, void * arg) ...@@ -162,28 +164,64 @@ void chain_mempool_proc(struct dap_http_simple *cl_st, void * arg)
char *request_str = dg->request_str; char *request_str = dg->request_str;
int request_size = dg->request_size; int request_size = dg->request_size;
printf("!!***!!! chain_mempool_proc arg=%d url=%s str=%s len=%d\n", arg, url, request_str, request_size); printf("!!***!!! chain_mempool_proc arg=%d url=%s str=%s len=%d\n", arg, url, request_str, request_size);
if(request_str && request_size > 0) { if(request_str && request_size > 1) {
uint8_t action = *(uint8_t*) request_str;
request_str++;
request_size--;
dap_datum_mempool_t *datum_mempool = dap_datum_mempool_deserialize(request_str, (size_t) request_size); dap_datum_mempool_t *datum_mempool = dap_datum_mempool_deserialize(request_str, (size_t) request_size);
if(datum_mempool) if(datum_mempool)
{ {
dap_datum_mempool_free(datum_mempool); dap_datum_mempool_free(datum_mempool);
char *a_key = calc_datum_hash(request_str, (size_t) request_size); char *a_key = calc_datum_hash(request_str, (size_t) request_size);
char *a_value = DAP_NEW_Z_SIZE(char, request_size * 2); char *a_value;
bin2hex((char*) a_value, (const unsigned char*) request_str, request_size); switch (action)
if(dap_chain_global_db_set(a_key, a_value)) { {
case DAP_DATUM_MEMPOOL_ADD:
a_value = DAP_NEW_Z_SIZE(char, request_size * 2);
bin2hex((char*) a_value, (const unsigned char*) request_str, request_size);
if(dap_chain_global_db_set(a_key, a_value)) {
*return_code = Http_Status_OK;
}
log_it(L_NOTICE, "Insert hash: key=%s result:%s", a_key,
(*return_code == Http_Status_OK) ? "OK" : "False!");
DAP_DELETE(a_value);
break;
case DAP_DATUM_MEMPOOL_CHECK:
strcpy(cl_st->reply_mime, "text/text");
char *str = dap_chain_global_db_get(a_key);
if(str) {
*return_code = Http_Status_OK;
dg->request = strdup("1");
; //cl_st->reply = strdup("1");
DAP_DELETE(str);
}
else
dg->request = strdup("0"); //cl_st->reply = strdup("0");
dg->in_query_size = strlen(dg->request); //cl_st->reply_size = strlen(cl_st->reply);
enc_http_reply_encode(cl_st, dg);
log_it(L_NOTICE, "Check hash: key=%s result:%s", a_key,
(*return_code == Http_Status_OK) ? "Present" : "Absent");
break;
case DAP_DATUM_MEMPOOL_DEL:
*return_code = Http_Status_OK; *return_code = Http_Status_OK;
log_it(L_NOTICE, "Delete hash: key=%s result:%s", a_key,
(*return_code == Http_Status_OK) ? "OK" : "False!");
break;
default:
log_it(L_NOTICE, "Unknown request=%d! key=%s", action, a_key);
DAP_DELETE(a_key); DAP_DELETE(a_key);
DAP_DELETE(a_value); enc_http_delegate_delete(dg);
return; return;
} }
DAP_DELETE(a_key); DAP_DELETE(a_key);
DAP_DELETE(a_value);
} }
else else
*return_code = Http_Status_InternalServerError; *return_code = Http_Status_InternalServerError;
} }
else else
*return_code = Http_Status_BadRequest; *return_code = Http_Status_BadRequest;
enc_http_delegate_delete(dg);
} }
else { else {
*return_code = Http_Status_Unauthorized; *return_code = Http_Status_Unauthorized;
......
...@@ -16,10 +16,15 @@ ...@@ -16,10 +16,15 @@
#define DAP_DATUM_MEMPOOL_VERSION "01" #define DAP_DATUM_MEMPOOL_VERSION "01"
// action
enum {
DAP_DATUM_MEMPOOL_ADD = 1, DAP_DATUM_MEMPOOL_CHECK, DAP_DATUM_MEMPOOL_DEL
};
// datum mempool structure // datum mempool structure
typedef struct dap_datum_mempool { typedef struct dap_datum_mempool {
uint16_t version; // structure version uint16_t version; // structure version
uint16_t datum_count;// datums count uint16_t datum_count; // datums count
dap_chain_datum_t **data;// mass of datums dap_chain_datum_t **data;// mass of datums
}DAP_ALIGN_PACKED dap_datum_mempool_t; }DAP_ALIGN_PACKED dap_datum_mempool_t;
......
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