From c503e42a355a361193fcbd360f931860cde9a5bc Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Wed, 14 Nov 2018 15:04:45 +0700
Subject: [PATCH] [*] Some renames, as usual [+] Node ctl object

---
 CMakeLists.txt                               |   6 +-
 dap_chain_net.c                              | 147 ++++++++++++++++++-
 dap_chain_net.h                              |  19 +++
 dap_chain_net_node.c => dap_chain_node.c     |   8 +-
 dap_chain_node.h                             |  89 +++++++++++
 dap_chain_node_ctl.c                         |  79 ++++++++++
 dap_chain_net_node.h => dap_chain_node_ctl.h |  38 ++---
 7 files changed, 363 insertions(+), 23 deletions(-)
 rename dap_chain_net_node.c => dap_chain_node.c (86%)
 create mode 100644 dap_chain_node.h
 create mode 100644 dap_chain_node_ctl.c
 rename dap_chain_net_node.h => dap_chain_node_ctl.h (63%)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 12c85a4d00..3aff75fe37 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -3,12 +3,14 @@ project (dap_chain_net)
   
 set(DAP_CHAIN_NET_SRCS 
 	dap_chain_net.c 
-	dap_chain_net_node.c 
+        dap_chain_node.c
+        dap_chain_node_ctl.c
         )
 
 set(DAP_CHAIN_NET_HEADERS
         dap_chain_net.h
-        dap_chain_net_node.h
+        dap_chain_node.h
+        dap_chain_node_ctl.h
     )
 
 add_library(${PROJECT_NAME} STATIC ${DAP_CHAIN_NET_SRCS} ${DAP_CHAIN_NET_HEADERS})
diff --git a/dap_chain_net.c b/dap_chain_net.c
index 4984355160..ec4ffeeb3a 100644
--- a/dap_chain_net.c
+++ b/dap_chain_net.c
@@ -22,17 +22,160 @@
     along with any DAP based project.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#include <stddef.h>
+#include <string.h>
+#include <pthread.h>
+
 #include "dap_common.h"
+#include "dap_config.h"
 #include "dap_chain_net.h"
+#include "dap_chain_node_ctl.h"
+#include "dap_module.h"
 
 #define LOG_TAG "chain_net"
 
+/**
+  * @struct dap_chain_net_pvt
+  * @details Private part of chain_net dap object
+  */
+typedef struct dap_chain_net_pvt{
+    pthread_t proc_tid;
+    pthread_cond_t proc_cond;
+    dap_chain_node_role_t node_role;
+    dap_chain_node_ctl_t * node;
+} dap_chain_net_pvt_t;
+
+#define PVT(a) ( (dap_chain_net_pvt_t *) a->pvt )
+#define PVT_S(a) ( (dap_chain_net_pvt_t *) a.pvt )
+
+size_t            s_net_configs_count = 0;
+
+/**
+ * @brief s_net_proc_thread
+ * @details Brings up and check the Dap Chain Network
+ * @param a_cfg Network1 configuration
+ * @return
+ */
+static void * s_net_proc_thread ( void * a_net)
+{
+    dap_chain_net_t * l_net = (dap_chain_net_t *) a_net;
+    return NULL;
+}
+
+/**
+ * @brief net_proc_start
+ * @param a_cfg
+ */
+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 ){
+        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");
+        }
+    }
+}
+
+/**
+ * @brief s_net_proc_kill
+ * @param a_net
+ */
+static void s_net_proc_kill( dap_chain_net_t * a_net )
+{
+    if ( PVT(a_net)->proc_tid ) {
+        pthread_cond_signal(& PVT(a_net)->proc_cond);
+        log_it(L_NOTICE,"Sent KILL signal to the net process thread %d, waiting for shutdown...",PVT(a_net)->proc_tid);
+        pthread_join( PVT(a_net)->proc_tid , NULL);
+        log_it(L_NOTICE,"Net process thread %d shutted down",PVT(a_net)->proc_tid);
+        PVT(a_net)->proc_tid = 0;
+    }
+}
+
+/**
+ * @brief dap_chain_net_new
+ * @param a_id
+ * @param a_name
+ * @param a_node_role
+ * @return
+ */
+dap_chain_net_t * dap_chain_net_new(const char * a_id, const char * a_name , const char * a_node_role)
+{
+    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 );
+    if ( sscanf(a_id,"0x%llu", &ret->pub.id.uint64 ) == 1 ){
+        if (strcmp (a_node_role, "root")==0){
+            PVT(ret)->node_role.enums = ROOT;
+            log_it (L_NOTICE, "Node role \"root\" selected");
+        }
+
+        PVT(ret)->node = dap_chain_node_ctl_new();
+    } else {
+        log_it (L_ERROR, "Wrong id format (\"%s\"). Must be like \"0x0123456789ABCDE\"" , a_id );
+    }
+
+}
+
+/**
+ * @brief dap_chain_net_delete
+ * @param a_net
+ */
+void dap_chain_net_delete( dap_chain_net_t * a_net )
+{
+    DAP_DELETE( PVT(a_net)->node);
+    DAP_DELETE( PVT(a_net) );
+}
+
+
+/**
+ * @brief dap_chain_net_init
+ * @return
+ */
 int dap_chain_net_init()
 {
-    return 0;
+    static dap_config_t *l_cfg=NULL;
+    if((l_cfg = dap_config_open( "network/default" ) ) == NULL) {
+        log_it(L_ERROR,"Can't open default network config");
+        return -1;
+    }else{
+        dap_chain_net_t * l_net = dap_chain_net_new(
+                                            dap_config_get_item_str(l_cfg , "general" , "id" ),
+                                            dap_config_get_item_str(l_cfg , "general" , "name" ),
+                                            dap_config_get_item_str(l_cfg , "general" , "node-role" )
+                                           );
+        switch ( PVT( l_net )->node_role.enums ) {
+            case ROOT:
+            case ROOT_DELEGATE:
+            case SHARD_DELEGATE:
+               // dap_chain_net_ca_load ( dap_config_get_item_str (""));
+            default:
+                log_it(L_DEBUG,"Net config loaded");
+
+        }
+
+        s_net_proc_start(l_net);
+        log_it(L_NOTICE, "Сhain network initialized");
+        return 0;
+    }
 }
 
 void dap_chain_net_deinit()
 {
-
 }
diff --git a/dap_chain_net.h b/dap_chain_net.h
index 9bb4c3c032..592db6f5c1 100644
--- a/dap_chain_net.h
+++ b/dap_chain_net.h
@@ -22,5 +22,24 @@
     along with any DAP based project.  If not, see <http://www.gnu.org/licenses/>.
 */
 #pragma once
+
+#include <stdint.h>
+#include "dap_chain_common.h"
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+typedef struct dap_chain_net{
+    struct {
+        dap_chain_net_id_t id;
+        char * name;
+    } pub;
+    uint8_t pvt[];
+} dap_chain_net_t;
+
 int dap_chain_net_init();
 void dap_chain_net_deinit();
+
+dap_chain_net_t * dap_chain_net_new (const char * a_id,  const char * a_name, const char* a_node_role );
+void dap_chain_net_delete( dap_chain_net_t * a_net);
+
diff --git a/dap_chain_net_node.c b/dap_chain_node.c
similarity index 86%
rename from dap_chain_net_node.c
rename to dap_chain_node.c
index 1a16bd284e..4a837c4d02 100644
--- a/dap_chain_net_node.c
+++ b/dap_chain_node.c
@@ -19,5 +19,11 @@
     along with any DAP based project.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "dap_chain_net_node.h"
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include "dap_chain_net.h"
+#include "dap_chain_node.h"
+
+#define LOG_TAG "chain_node"
 
diff --git a/dap_chain_node.h b/dap_chain_node.h
new file mode 100644
index 0000000000..bc93af7fd1
--- /dev/null
+++ b/dap_chain_node.h
@@ -0,0 +1,89 @@
+/*
+ * Authors:
+ * Dmitriy A. Gearasimov <naeper@demlabs.net>
+ * DeM Labs Inc.   https://demlabs.net
+
+ 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
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    DAP is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    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/>.
+*/
+
+#pragma once
+
+#include <stdint.h>
+#include <stddef.h>
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include "dap_common.h"
+#include "dap_chain_common.h"
+
+/**
+  * @struct Node address
+  *
+  */
+typedef union dap_chain_node_addr{
+    uint64_t uint64;
+    uint8_t raw[sizeof(uint64_t)];  // Access to selected octects
+} dap_chain_node_addr_t;
+
+/**
+  *  Node Declaration request
+  *
+  */
+
+#define DAP_CHAIN_NODE_DECL_REQ_INFO_SIZE 32
+typedef struct dap_chain_node_delc_req{
+    dap_chain_node_addr_t node_address;
+    uint64_t create_ts;
+    union{
+        uint8_t raw[DAP_CHAIN_NODE_DECL_REQ_INFO_SIZE];
+        char str[DAP_CHAIN_NODE_DECL_REQ_INFO_SIZE];
+    } info;
+} DAP_ALIGN_PACKED dap_chain_decl_req_t;
+
+/**
+  * @struct dap_chain_node decl
+  * @details New node declaration
+  *
+  */
+#define DAP_CHAIN_NODE_DECL_ACCEPT_INFO_SIZE 32
+typedef struct dap_chain_node_decl{
+    dap_chain_decl_req_t request;
+    uint64_t accept_ts;
+    struct in_addr accept_addr_v4;
+    struct in6_addr accept_addr_v6;
+    union{
+        uint8_t raw[DAP_CHAIN_NODE_DECL_ACCEPT_INFO_SIZE];
+        char str[DAP_CHAIN_NODE_DECL_ACCEPT_INFO_SIZE];
+    } accept_info;
+} DAP_ALIGN_PACKED dap_chain_node_decl_t;
+
+typedef struct dap_chain_node_info
+{
+    struct {
+        dap_chain_node_addr_t address;
+        dap_chain_shard_id_t shard_id;
+        uint32_t uplinks_number;
+        struct in_addr ext_addr_v4;
+        struct in6_addr ext_addr_v6;
+    } DAP_ALIGN_PACKED hdr;
+    dap_chain_addr_t uplinks[];
+} DAP_ALIGN_PACKED dap_chain_node_info_t;
+
+typedef struct dap_chain_node_publ{
+    dap_chain_hash_fast_t decl_hash;
+    dap_chain_node_info_t node_info;
+} DAP_ALIGN_PACKED dap_chain_node_publ_t;
diff --git a/dap_chain_node_ctl.c b/dap_chain_node_ctl.c
new file mode 100644
index 0000000000..ca0ff01111
--- /dev/null
+++ b/dap_chain_node_ctl.c
@@ -0,0 +1,79 @@
+/*
+ * Authors:
+ * Dmitriy A. Gearasimov <naeper@demlabs.net>
+ * DeM Labs Inc.   https://demlabs.net
+
+ 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
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    DAP is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    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/>.
+*/
+
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+#include "dap_config.h"
+#include "dap_chain_net.h"
+#include "dap_chain_node_ctl.h"
+
+#define LOG_TAG "chain_node_ctl"
+
+typedef struct dap_chain_node_ctl_pvt{
+    uint_fast64_t padding;
+} dap_chain_node_ctl_pvt_t;
+
+#define PVT(a) ( (dap_chain_node_ctl_pvt_t *) (a)->pvt )
+#define PVT_S(a) ( (dap_chain_node_ctl_pvt_t *) (a).pvt )
+
+
+/**
+ * @brief dap_chain_node_new
+ * @return
+ */
+dap_chain_node_ctl_t * dap_chain_node_ctl_new()
+{
+    dap_chain_node_ctl_t * ret = DAP_NEW_Z_SIZE(dap_chain_node_ctl_t, sizeof(ret->pub) + sizeof(dap_chain_node_ctl_pvt_t) );
+
+    return ret;
+}
+
+/**
+ * @brief dap_chain_node_delete
+ * @param a_node
+ */
+void dap_chain_node_ctl_delete(dap_chain_node_ctl_t * a_node)
+{
+    DAP_DELETE(a_node);
+}
+
+/**
+ * @brief dap_chain_node_ctl_open
+ * @param a_name
+ * @return
+ */
+dap_chain_node_ctl_t * dap_chain_node_ctl_open( const char * a_name )
+{
+   dap_chain_node_ctl_t * l_node = NULL;
+   const char c_node_folder[]="node";
+   size_t buf_size = 2+sizeof(a_name)+sizeof(c_node_folder);
+   char *buf= DAP_NEW_SIZE(char, buf_size);
+   snprintf(buf,buf_size,"%s/%s",c_node_folder,a_name);
+   dap_config_t * l_node_cfg = dap_config_open(buf);
+   if ( l_node_cfg ){
+       //dap_config_get_item_str_default()
+   } else {
+       log_it(L_ERROR,"Can't open node \"%s\". Check the configuration files path.",a_name);
+   }
+   DAP_DELETE(buf);
+   return l_node;
+}
diff --git a/dap_chain_net_node.h b/dap_chain_node_ctl.h
similarity index 63%
rename from dap_chain_net_node.h
rename to dap_chain_node_ctl.h
index 95b8060ce0..6be6300fa3 100644
--- a/dap_chain_net_node.h
+++ b/dap_chain_node_ctl.h
@@ -23,21 +23,23 @@
 
 #include <stdint.h>
 #include <stddef.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-
-typedef union dap_chain_node_addr{
-    uint64_t addr_raw;
-    uint8_t addr_oct[sizeof(uint64_t)]; // Access to selected octects
-} dap_chain_node_addr_t;
-
-typedef struct dap_chain_node{
-    dap_chain_node_addr_t addr;
-    dap_chain_node_addr_t *uplinks;
-    dap_chain_node_addr_t *downlinks;
-
-    struct in_addr *ipv4_addrs;
-    size_t ipv4_addrs_size;
-    struct in6_addr *ipv6_addrs;
-    size_t ipv6_addrs_size;
-} dap_chain_net_node_t;
+
+#include "dap_chain_common.h"
+#include "dap_chain_node.h"
+
+
+typedef struct dap_chain_node_ctl{
+    struct {
+        dap_chain_node_addr_t addr;
+        struct in_addr *ipv4_addrs;
+        size_t ipv4_addrs_size;
+        struct in6_addr *ipv6_addrs;
+        size_t ipv6_addrs_size;
+    } pub;
+    uint8_t pvt[];
+} dap_chain_node_ctl_t;
+
+dap_chain_node_ctl_t * dap_chain_node_ctl_new();
+dap_chain_node_ctl_t * dap_chain_node_ctl_open( const char * a_name );
+void dap_chain_node_ctl_delete(dap_chain_node_ctl_t * a_node);
+
-- 
GitLab