celix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pnol...@apache.org
Subject [celix] branch develop updated: Improved deletion of http admin services (#47)
Date Tue, 10 Sep 2019 19:14:42 GMT
This is an automated email from the ASF dual-hosted git repository.

pnoltes pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/celix.git


The following commit(s) were added to refs/heads/develop by this push:
     new 49f0fd8  Improved deletion of http admin services (#47)
49f0fd8 is described below

commit 49f0fd8c47aa45fe007af47c121147d2b39200d4
Author: dhbfischer <52166446+dhbfischer@users.noreply.github.com>
AuthorDate: Tue Sep 10 21:14:37 2019 +0200

    Improved deletion of http admin services (#47)
---
 bundles/http_admin/http_admin/src/http_admin.c     |  2 +-
 bundles/http_admin/http_admin/src/service_tree.c   | 51 +++++++++++-----------
 bundles/http_admin/http_admin/src/service_tree.h   |  3 +-
 .../http_admin/http_admin/src/websocket_admin.c    |  4 +-
 4 files changed, 30 insertions(+), 30 deletions(-)

diff --git a/bundles/http_admin/http_admin/src/http_admin.c b/bundles/http_admin/http_admin/src/http_admin.c
index 5f0fad4..1f8bf6d 100755
--- a/bundles/http_admin/http_admin/src/http_admin.c
+++ b/bundles/http_admin/http_admin/src/http_admin.c
@@ -173,7 +173,7 @@ void http_admin_removeHttpService(void *handle, void *svc __attribute__((unused)
         node = findServiceNodeInTree(&admin->http_svc_tree, uri);
 
         if(node != NULL){
-            destroyServiceNode(node, &admin->http_svc_tree.tree_node_count, &admin->http_svc_tree.tree_svc_count);
+            destroyServiceNode(&admin->http_svc_tree, node, &admin->http_svc_tree.tree_node_count,
&admin->http_svc_tree.tree_svc_count);
         } else {
             printf("Couldn't remove HTTP service with URI: %s, it doesn't exist\n", uri);
         }
diff --git a/bundles/http_admin/http_admin/src/service_tree.c b/bundles/http_admin/http_admin/src/service_tree.c
index 8225cfb..b1e7039 100644
--- a/bundles/http_admin/http_admin/src/service_tree.c
+++ b/bundles/http_admin/http_admin/src/service_tree.c
@@ -156,7 +156,7 @@ void destroyChildrenFromServiceNode(service_tree_node_t *parent, int *tree_item_
         while(child != NULL) {
             if (child->children != NULL) {
                 //Delete children first
-                destroyChildrenFromServiceNode(parent->children, tree_item_count, tree_svc_count);
+                destroyChildrenFromServiceNode(child, tree_item_count, tree_svc_count);
 
             } else {
                 //Delete child first
@@ -177,28 +177,7 @@ void destroyChildrenFromServiceNode(service_tree_node_t *parent, int
*tree_item_
     }
 }
 
-void destroyServiceNodeTree(service_tree_node_t *node, int *tree_item_count, int *tree_svc_count)
{
-    if(node != NULL && tree_item_count != NULL && tree_svc_count != NULL)
{
-        service_tree_node_t *current = node;
-
-        destroyChildrenFromServiceNode(current, tree_item_count, tree_svc_count);
-
-        while (current->next != NULL) {
-            destroyChildrenFromServiceNode(current->next, tree_item_count, tree_svc_count);
-            current = current->next;
-        }
-
-        if(node->svc_data != NULL) {
-            //Decrement service count if a service was present
-            if(node->svc_data->service != NULL) (*tree_svc_count)--;
-            free(node->svc_data->sub_uri);
-            free(node->svc_data);
-        }
-        free(node);
-    }
-}
-
-void destroyServiceNode(service_tree_node_t *node, int *tree_item_count, int *tree_svc_count)
{
+void destroyServiceNode(service_tree_t *svc_tree, service_tree_node_t *node, int *tree_item_count,
int *tree_svc_count) {
     if(node != NULL && tree_item_count != NULL && tree_svc_count != NULL)
{
         bool has_underlaying_services = false;
 
@@ -235,6 +214,11 @@ void destroyServiceNode(service_tree_node_t *node, int *tree_item_count,
int *tr
                 node->parent->children = node->next;
             }
 
+            //If current node to delete is the root node, set new root node pointer
+            if(svc_tree->root_node == node) {
+                svc_tree->root_node = node->next;
+            }
+
             //Set new previous pointer for the next node if present.
             if(node->next != NULL) {
                 node->next->prev = node->prev;
@@ -255,10 +239,27 @@ void destroyServiceNode(service_tree_node_t *node, int *tree_item_count,
int *tr
 
 void destroyServiceTree(service_tree_t *svc_tree) {
     if(svc_tree != NULL) {
-        if(svc_tree->tree_node_count > 0) {
-            destroyServiceNodeTree(svc_tree->root_node, &svc_tree->tree_node_count,
&svc_tree->tree_svc_count);
+        if(svc_tree->tree_node_count > 0 && svc_tree->root_node != NULL)
{
+            service_tree_node_t *current = svc_tree->root_node;
+
+            while(current != NULL) {
+                destroyChildrenFromServiceNode(current, &svc_tree->tree_node_count,
&svc_tree->tree_svc_count);
+
+                service_tree_node_t *next = current->next;
+                if(current->svc_data != NULL) {
+                    if(current->svc_data->service != NULL) svc_tree->tree_svc_count--;
+                    free(current->svc_data->sub_uri);
+                    free(current->svc_data);
+                }
+                free(current);
+
+                //Iterate to next node in similar level as root node
+                current = next;
+            }
+
             svc_tree->tree_node_count = 0;
             svc_tree->tree_svc_count = 0;
+            svc_tree->root_node = NULL;
         }
     }
 }
diff --git a/bundles/http_admin/http_admin/src/service_tree.h b/bundles/http_admin/http_admin/src/service_tree.h
index 038828b..eeb2460 100644
--- a/bundles/http_admin/http_admin/src/service_tree.h
+++ b/bundles/http_admin/http_admin/src/service_tree.h
@@ -54,8 +54,7 @@ typedef struct service_tree {
 service_tree_node_t *createServiceNode(service_tree_node_t *parent, service_tree_node_t *children,
service_tree_node_t *next, service_tree_node_t *prev, const char *uri, void *svc);
 bool addServiceNode(service_tree_t *svc_tree, const char *uri, void *svc);
 void destroyChildrenFromServiceNode(service_tree_node_t *parent, int *tree_item_count, int
*tree_svc_count);
-void destroyServiceNodeTree(service_tree_node_t *node, int *tree_item_count, int *tree_svc_count);
-void destroyServiceNode(service_tree_node_t *node, int *tree_item_count, int *tree_svc_count);
+void destroyServiceNode(service_tree_t *svc_tree, service_tree_node_t *node, int *tree_item_count,
int *tree_svc_count);
 void destroyServiceTree(service_tree_t *svc_tree);
 service_tree_node_t *findServiceNodeInTree(service_tree_t *svc_tree, const char *uri);
 
diff --git a/bundles/http_admin/http_admin/src/websocket_admin.c b/bundles/http_admin/http_admin/src/websocket_admin.c
index aee8b50..9ec9a21 100644
--- a/bundles/http_admin/http_admin/src/websocket_admin.c
+++ b/bundles/http_admin/http_admin/src/websocket_admin.c
@@ -112,7 +112,7 @@ void websocket_admin_removeWebsocketService(void *handle, void *svc __attribute_
         node = findServiceNodeInTree(&admin->sock_svc_tree, uri);
 
         if(node != NULL){
-            destroyServiceNode(node, &admin->sock_svc_tree.tree_node_count, &admin->sock_svc_tree.tree_svc_count);
+            destroyServiceNode(&admin->sock_svc_tree, node, &admin->sock_svc_tree.tree_node_count,
&admin->sock_svc_tree.tree_svc_count);
         } else {
             printf("Couldn't remove websocket service with URI: %s, it doesn't exist\n",
uri);
         }
@@ -213,4 +213,4 @@ void websocket_close_handler(const struct mg_connection *connection, void
*handl
             }
         }
     }
-}
\ No newline at end of file
+}


Mime
View raw message