From 0f701ae34043ee9821aa50119022a2a4909bac01 Mon Sep 17 00:00:00 2001
From: Aleksandr Lysikov <lysikov@inbox.ru>
Date: Tue, 14 May 2019 22:21:20 +0500
Subject: [PATCH] added traffic_callback

---
 dap_chain_net_srv.c        |  5 +++--
 dap_chain_net_srv.h        | 13 +++--------
 dap_chain_net_srv_common.c | 46 ++++++++++++++++++++++++++++++++++++++
 dap_chain_net_srv_common.h | 39 +++++++++++++++++++++++++++++++-
 4 files changed, 90 insertions(+), 13 deletions(-)

diff --git a/dap_chain_net_srv.c b/dap_chain_net_srv.c
index 652031d..5748f83 100755
--- a/dap_chain_net_srv.c
+++ b/dap_chain_net_srv.c
@@ -1,6 +1,7 @@
 /*
  * Authors:
- * Dmitriy A. Gearasimov <kahovski@gmail.com>
+ * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
+ * Aleksandr Lysikov <alexander.lysikov@demlabs.net>
  * DeM Labs Inc.   https://demlabs.net
  * DeM Labs Open source community https://github.com/demlabsinc
  * Copyright  (c) 2017-2018
@@ -80,7 +81,7 @@ void dap_chain_net_srv_add(dap_chain_net_srv_t * a_srv)
         l_sdata = DAP_NEW_Z(service_list_t);
         memcpy(&l_sdata->uid, &a_srv->uid, sizeof(dap_chain_net_srv_uid_t));
         l_sdata->srv = DAP_NEW(dap_chain_net_srv_t);
-        memcpy(&l_sdata->srv, a_srv, sizeof(dap_chain_net_srv_t));    ;
+        memcpy(&l_sdata->srv, a_srv, sizeof(dap_chain_net_srv_t));
         HASH_ADD(hh, s_srv_list, uid, sizeof(a_srv->uid), l_sdata);
     }
     pthread_mutex_unlock(&s_srv_list_mutex);
diff --git a/dap_chain_net_srv.h b/dap_chain_net_srv.h
index 9f326f3..f3a13e3 100755
--- a/dap_chain_net_srv.h
+++ b/dap_chain_net_srv.h
@@ -1,6 +1,7 @@
 /*
  * Authors:
  * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
+ * Aleksandr Lysikov <alexander.lysikov@demlabs.net>
  * DeM Labs Inc.   https://demlabs.net
  * Kelvin Project https://github.com/kelvinblockchain
  * Copyright  (c) 2017-2018
@@ -25,16 +26,6 @@
 
 #include "dap_chain_net_srv_common.h"
 
-typedef struct dap_chain_net_srv
-{
-    dap_chain_net_srv_uid_t uid; // Unique ID for service.
-    dap_chain_net_srv_abstract_t srv_common;
-
-    void * _internal;
-    void * _inhertor;
-} dap_chain_net_srv_t;
-
-
 
 int dap_chain_net_srv_init(void);
 void dap_chain_net_srv_deinit(void);
@@ -46,3 +37,5 @@ dap_chain_net_srv_t * dap_chain_net_srv_get(dap_chain_net_srv_uid_t *a_uid);
 size_t dap_chain_net_srv_count(void);
 const dap_chain_net_srv_uid_t * dap_chain_net_srv_list(void);
 
+// callback for traffic
+void dap_chain_net_srv_traffic_callback(dap_server_t *a_server);
diff --git a/dap_chain_net_srv_common.c b/dap_chain_net_srv_common.c
index bbb4c4c..e0b927f 100755
--- a/dap_chain_net_srv_common.c
+++ b/dap_chain_net_srv_common.c
@@ -1,9 +1,35 @@
+/*
+ * Authors:
+ * Dmitriy A. Gearasimov <gerasimov.dmitriy@demlabs.net>
+ * Aleksandr Lysikov <alexander.lysikov@demlabs.net>
+ * DeM Labs Inc.   https://demlabs.net
+ * Kelvin Project https://github.com/kelvinblockchain
+ * Copyright  (c) 2019
+ * All rights reserved.
+
+ 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 <stdint.h>
 #include "dap_strfuncs.h"
 #include "rand/dap_rand.h"
 #include "dap_chain_net_srv_common.h"
 #include "dap_chain_datum_tx_items.h"
 #include "dap_chain_utxo.h"
+#include "dap_stream.h"
 
 /**
  * copy a_value_dst to a_uid_src
@@ -90,3 +116,23 @@ uint64_t dap_chain_net_srv_client_auth(char *a_addr_base58, uint8_t *a_sign, siz
         *a_cond_out = (const dap_chain_net_srv_abstract_t*) l_cond;
     return l_value;
 }
+
+// callback for traffic
+void dap_chain_net_srv_traffic_callback(dap_server_t *a_server)
+{
+    pthread_mutex_lock(&a_server->mutex_on_hash);
+    dap_client_remote_t *l_client = a_server->clients;
+    if(l_client) {
+        dap_stream_t *l_stream = DAP_STREAM(l_client);
+        if(l_stream)
+            for(int i = 0; i < l_stream->channel_count; i++) {
+                dap_stream_ch_t * ch = l_stream->channel[i];
+                dap_chain_net_srv_t *net_srv = (dap_chain_net_srv_t*) (ch->internal);
+                // callback for service
+                if(net_srv && net_srv->callback_trafic)
+                    net_srv->callback_trafic(l_client, ch);
+            }
+    }
+    pthread_mutex_unlock(&a_server->mutex_on_hash);
+}
+
diff --git a/dap_chain_net_srv_common.h b/dap_chain_net_srv_common.h
index 9600c48..58e94f3 100755
--- a/dap_chain_net_srv_common.h
+++ b/dap_chain_net_srv_common.h
@@ -1,8 +1,34 @@
+/*
+ * Authors:
+ * Aleksandr Lysikov <alexander.lysikov@demlabs.net>
+ * DeM Labs Inc.   https://demlabs.net
+ * Kelvin Project https://github.com/kelvinblockchain
+ * Copyright  (c) 2019
+ * All rights reserved.
+
+ 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 <stdbool.h>
 #include "dap_common.h"
 #include "dap_math_ops.h"
+#include "dap_server.h"
+#include "dap_stream_ch.h"
 
 #define DAP_CHAIN_NET_SRV_UID_SIZE 16
 typedef union {
@@ -57,6 +83,18 @@ typedef struct dap_chain_net_srv_abstract
     char decription[128];
 }DAP_ALIGN_PACKED dap_chain_net_srv_abstract_t;
 
+typedef void (*dap_chain_callback_trafic_t)(dap_client_remote_t *, dap_stream_ch_t *);
+
+typedef struct dap_chain_net_srv
+{
+    dap_chain_net_srv_uid_t uid; // Unique ID for service.
+    dap_chain_net_srv_abstract_t srv_common;
+
+    dap_chain_callback_trafic_t callback_trafic;
+    void * _internal;
+    //void * _inhertor;
+} dap_chain_net_srv_t;
+
 // Initialize dap_chain_net_srv_abstract_t structure
 void dap_chain_net_srv_abstract_set(dap_chain_net_srv_abstract_t *a_cond, uint8_t a_class, uint128_t a_type_id,
         uint64_t a_price, uint8_t a_price_units, const char *a_decription);
@@ -67,6 +105,5 @@ void dap_chain_net_srv_uid_set(dap_chain_net_srv_uid_t *a_uid_src, uint128_t a_v
 // generate new dap_chain_net_srv_uid_t
 bool dap_chain_net_srv_gen_uid(uint8_t *a_srv, size_t a_srv_size);
 
-
 uint64_t dap_chain_net_srv_client_auth(char *a_addr_base58, uint8_t *a_sign, size_t a_sign_size,
         const dap_chain_net_srv_abstract_t **a_cond_out);
-- 
GitLab