From 151460f832efe1089c0bf539b60f5674a234dc7f Mon Sep 17 00:00:00 2001 From: Gabriele Gemmi Date: Sun, 15 Apr 2018 01:53:08 +0200 Subject: [PATCH] Enforced Linux Kernel style using clang-format --- .clang-format | 20 + .gitignore | 1 + graph-parser_c/include/biconnected.h | 58 +- graph-parser_c/include/brandes.h | 40 +- graph-parser_c/include/graph/graph.h | 48 +- graph-parser_c/include/graph/list.h | 102 +- graph-parser_c/include/graph_parser.h | 17 +- graph-parser_c/include/network_change.h | 63 +- graph-parser_c/src/biconnected.c | 2251 +++++++++++------------ graph-parser_c/src/brandes.c | 1812 +++++++++--------- graph-parser_c/src/graph/graph.c | 301 ++- graph-parser_c/src/graph/list.c | 545 +++--- graph-parser_c/src/graph_parser.c | 124 +- graph-parser_c/src/network_change.c | 349 ++-- graph-parser_c/src/test.c | 208 +-- prince/data/jsoninfo.json | 1416 ++++++++++++++ prince/data/jsoninfo2.json | 1416 ++++++++++++++ prince/data/netjson.json | 654 +++++++ prince/data/topolgy.json | 1 + prince/include/common.h | 16 +- prince/include/config.h | 6 +- prince/include/config_graph.h | 18 +- prince/include/config_proto.h | 20 +- prince/include/load_plugin.h | 3 +- prince/include/plugin_interface.h | 10 +- prince/include/prince.h | 10 +- prince/include/prince_handler.h | 28 +- prince/include/socket.h | 2 +- prince/include/topology.h | 16 +- prince/include/topology_parser.h | 24 +- prince/lib/olsrd/olsr.c | 172 +- prince/lib/olsrd/olsr.h | 2 +- prince/lib/oonf/oonf.c | 97 +- prince/lib/oonf/oonf.h | 4 +- prince/lib/ospf/ospf.c | 86 +- prince/lib/testlib/test.c | 80 +- prince/src/config.c | 370 ++-- prince/src/config_graph.c | 53 +- prince/src/config_proto.c | 63 +- prince/src/load_plugin.c | 153 +- prince/src/prince.c | 128 +- prince/src/prince_handler.c | 165 +- prince/src/socket.c | 81 +- prince/src/test/data/prince_0_16_3.json | 19 + prince/src/test/data/prova.log | 60 + prince/src/topology.c | 132 +- prince/src/topology_parser.c | 233 ++- 47 files changed, 7575 insertions(+), 3902 deletions(-) create mode 100644 .clang-format create mode 100644 prince/data/jsoninfo.json create mode 100644 prince/data/jsoninfo2.json create mode 100644 prince/data/netjson.json create mode 100755 prince/data/topolgy.json create mode 100644 prince/src/test/data/prince_0_16_3.json create mode 100644 prince/src/test/data/prova.log diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..6213d45 --- /dev/null +++ b/.clang-format @@ -0,0 +1,20 @@ +BasedOnStyle: LLVM +Language: Cpp +IndentWidth: 8 +UseTab: Always +BreakBeforeBraces: Linux +AlwaysBreakBeforeMultilineStrings: true +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AllowShortFunctionsOnASingleLine: false +IndentCaseLabels: false +AlignEscapedNewlinesLeft: false +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: false +AlignAfterOpenBracket: true +SpaceAfterCStyleCast: false +MaxEmptyLinesToKeep: 2 +BreakBeforeBinaryOperators: NonAssignment +BreakStringLiterals: false +SortIncludes: false +ContinuationIndentWidth: 8 diff --git a/.gitignore b/.gitignore index 9c43459..d2bfad2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ .project .settings/ nbproject/ +build/ .idea/ #files being edited diff --git a/graph-parser_c/include/biconnected.h b/graph-parser_c/include/biconnected.h index 55e0413..f14f5d2 100644 --- a/graph-parser_c/include/biconnected.h +++ b/graph-parser_c/include/biconnected.h @@ -11,60 +11,52 @@ #include "graph/graph.h" #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif - struct graph; +struct graph; - struct connected_component - { - struct graph g; +struct connected_component { + struct graph g; - int * mapping; - int * weights; + int *mapping; + int *weights; - struct node_graph *cutpoint; + struct node_graph *cutpoint; - int cutpoint_index; - }; + int cutpoint_index; +}; - struct sub_graph - { - struct list connected_components; +struct sub_graph { + struct list connected_components; - int size; - }; + int size; +}; - // These function returns the list of list of connected components - // i.e. a list of connected components for each connected subgraph - struct list *tarjan_rec_undir(struct graph *g, - bool * is_articulation_point, - int * component_indexes); +// These function returns the list of list of connected components +// i.e. a list of connected components for each connected subgraph +struct list *tarjan_rec_undir(struct graph *g, bool *is_articulation_point, + int *component_indexes); - struct list *tarjan_iter_undir(struct graph *g, - bool * is_articulation_point, - int * component_indexes); +struct list *tarjan_iter_undir(struct graph *g, bool *is_articulation_point, + int *component_indexes); - // not employed and completed yet (missing art_poit and - // division in subgraph with component indexing) - struct list *tarjan_rec_dir(struct graph *g, - bool * is_articulation_point, - int * component_indexes); +// not employed and completed yet (missing art_poit and +// division in subgraph with component indexing) +struct list *tarjan_rec_dir(struct graph *g, bool *is_articulation_point, + int *component_indexes); - struct list *tarjan_iter_dir(struct graph *g, - bool * is_articulation_point, - int * component_indexes); +struct list *tarjan_iter_dir(struct graph *g, bool *is_articulation_point, + int *component_indexes); #ifdef __cplusplus } #endif #endif /* BICONNECTED_H */ - diff --git a/graph-parser_c/include/brandes.h b/graph-parser_c/include/brandes.h index c991c5f..65f8697 100644 --- a/graph-parser_c/include/brandes.h +++ b/graph-parser_c/include/brandes.h @@ -12,31 +12,27 @@ #include "biconnected.h" #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif - /** - * This is the algorithm described in the linked papers. - * Given a network, in the form of a weighted graph, it computes the - * centrality of each node based on the amount of routes that are based on it. - * http://www.algo.uni-konstanz.de/publications/b-fabc-01.pdf - * http://algo.uni-konstanz.de/publications/b-vspbc-08.pdf - */ - double * betweeness_brandes(struct graph *g, - bool endpoints, - int * articulation_point_val, - bool normalized); +/** + * This is the algorithm described in the linked papers. + * Given a network, in the form of a weighted graph, it computes the + * centrality of each node based on the amount of routes that are based on it. + * http://www.algo.uni-konstanz.de/publications/b-fabc-01.pdf + * http://algo.uni-konstanz.de/publications/b-vspbc-08.pdf + */ +double *betweeness_brandes(struct graph *g, bool endpoints, + int *articulation_point_val, bool normalized); - /** - * This is the algorithm described in the linked paper. - * It reaches the exact results of the previous one, but it is implemented - * using a divide et impera mechanism based on biconnected components in order - * to speed up computation. - * http://algo.uni-konstanz.de/publications/pzedb-hsbcc-12.pdf - */ - double * betwenness_heuristic(struct graph *g, - bool recursive); +/** + * This is the algorithm described in the linked paper. + * It reaches the exact results of the previous one, but it is implemented + * using a divide et impera mechanism based on biconnected components in order + * to speed up computation. + * http://algo.uni-konstanz.de/publications/pzedb-hsbcc-12.pdf + */ +double *betwenness_heuristic(struct graph *g, bool recursive); #ifdef __cplusplus } diff --git a/graph-parser_c/include/graph/graph.h b/graph-parser_c/include/graph/graph.h index ead1a3f..5403bea 100644 --- a/graph-parser_c/include/graph/graph.h +++ b/graph-parser_c/include/graph/graph.h @@ -14,54 +14,48 @@ #define INVALID_NODE_GRAPH NULL #define INVALID_EDGE_GRAPH NULL -#define INVALID_GRAPH NULL -#define NODE_GRAPH_SIZE sizeof(struct node_graph) -#define EDGE_GRAPH_SIZE sizeof(struct edge_graph) -#define GRAPH_SIZE sizeof(struct graph) +#define INVALID_GRAPH NULL +#define NODE_GRAPH_SIZE sizeof(struct node_graph) +#define EDGE_GRAPH_SIZE sizeof(struct edge_graph) +#define GRAPH_SIZE sizeof(struct graph) /** * Structs and functions used to represent a graph. Further detail in grap.c */ struct node_graph { - struct list neighbours; - char * name; - int node_graph_id; + struct list neighbours; + char *name; + int node_graph_id; }; -typedef struct node_graph * node_graph_t; +typedef struct node_graph *node_graph_t; struct edge_graph { - struct node_graph *to; - double value; + struct node_graph *to; + double value; }; -typedef struct edge_graph * edge_graph_t; +typedef struct edge_graph *edge_graph_t; struct graph { - struct list nodes; - bool directed; + struct list nodes; + bool directed; }; -typedef struct graph * graph_t; +typedef struct graph *graph_t; void init_graph(graph_t g); -void add_edge_graph(graph_t g, - const char * name_from, - const char * name_to, - double value, - bool directed); - -void add_edge_graph_return_node_indexes(graph_t g, - const char * name_from, - const char * name_to, - double value, - bool directed, - int * nodefrom, - int * nodeto); +void add_edge_graph(graph_t g, const char *name_from, const char *name_to, + double value, bool directed); + +void add_edge_graph_return_node_indexes(graph_t g, const char *name_from, + const char *name_to, double value, + bool directed, int *nodefrom, + int *nodeto); void free_graph(graph_t g); diff --git a/graph-parser_c/include/graph/list.h b/graph-parser_c/include/graph/list.h index 78cf477..5e3c36b 100644 --- a/graph-parser_c/include/graph/list.h +++ b/graph-parser_c/include/graph/list.h @@ -12,105 +12,95 @@ #include #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif - /** - * Structs and functions used to represent a generic list and a priority - * queue. - * The list works both with LIFO and FIFO policies. - * The priority queue offers all operation needed for dijkstra algorithm. - * The underlying implementation for both is the double linked list. - * Further detail in list.c - */ - struct node_list - { - void * content; +/** + * Structs and functions used to represent a generic list and a priority + * queue. + * The list works both with LIFO and FIFO policies. + * The priority queue offers all operation needed for dijkstra algorithm. + * The underlying implementation for both is the double linked list. + * Further detail in list.c + */ +struct node_list { + void *content; - struct node_list *next; + struct node_list *next; - struct node_list *prev; - }; + struct node_list *prev; +}; - struct list - { - struct node_list *head; +struct list { + struct node_list *head; - struct node_list *tail; + struct node_list *tail; - int size; - }; + int size; +}; - struct node_priority_queue - { - void * content; +struct node_priority_queue { + void *content; - struct node_priority_queue *next; + struct node_priority_queue *next; - struct node_priority_queue *prev; + struct node_priority_queue *prev; - double value; - }; + double value; +}; - struct priority_queue - { - struct node_priority_queue *head; +struct priority_queue { + struct node_priority_queue *head; - struct node_priority_queue *tail; + struct node_priority_queue *tail; - int size; - }; + int size; +}; - void init_list(struct list *q); +void init_list(struct list *q); - void enqueue_list(struct list *q, - void * item); +void enqueue_list(struct list *q, void *item); - void * dequeue_list(struct list *q); +void *dequeue_list(struct list *q); - void * peek_last_list(struct list *q); +void *peek_last_list(struct list *q); - void * peek_first_list(struct list *q); +void *peek_first_list(struct list *q); - void * pop_list(struct list *q); +void *pop_list(struct list *q); - void print_list(struct list *q); +void print_list(struct list *q); - void clear_list(struct list *q); +void clear_list(struct list *q); - int is_empty_list(struct list *q); +int is_empty_list(struct list *q); - void init_priority_queue(struct priority_queue *q); +void init_priority_queue(struct priority_queue *q); - void insert_priority_queue(struct priority_queue *q, - void * item, - double val); +void insert_priority_queue(struct priority_queue *q, void *item, double val); - void insert_or_update_priority_queue(struct priority_queue *q, - void * item, - double val); +void insert_or_update_priority_queue(struct priority_queue *q, void *item, + double val); - void * dequeue_priority_queue(struct priority_queue *q); +void *dequeue_priority_queue(struct priority_queue *q); - void print_priority_queue(struct priority_queue *q); +void print_priority_queue(struct priority_queue *q); - int is_empty_priority_queue(struct priority_queue *q); +int is_empty_priority_queue(struct priority_queue *q); #ifdef __cplusplus } #endif #endif /* LIST_H */ - diff --git a/graph-parser_c/include/graph_parser.h b/graph-parser_c/include/graph_parser.h index f49fde6..7596d61 100644 --- a/graph-parser_c/include/graph_parser.h +++ b/graph-parser_c/include/graph_parser.h @@ -24,20 +24,21 @@ extern bool multithread; * We define a base measure of change, and will be further studied. * Default is true. */ -extern bool stop_computing_if_unchanged; +extern bool stop_computing_if_unchanged; typedef void c_graph_parser; struct graph_parser { - struct graph g; - bool heuristic_b; - double * bc; + struct graph g; + bool heuristic_b; + double *bc; }; c_graph_parser *new_graph_parser(int weight, int heuristic); -void free_graph_parser(c_graph_parser * v); +void free_graph_parser(c_graph_parser *v); -void graph_parser_parse_simplegraph(c_graph_parser * v, topology_t topo); -void graph_parser_calculate_bc(c_graph_parser * v); -int graph_parser_compose_degree_bc_map(c_graph_parser * v, map_id_degree_bc *map); +void graph_parser_parse_simplegraph(c_graph_parser *v, topology_t topo); +void graph_parser_calculate_bc(c_graph_parser *v); +int graph_parser_compose_degree_bc_map(c_graph_parser *v, + map_id_degree_bc *map); #endif /* GRAPH_PARSER_H_ */ diff --git a/graph-parser_c/include/network_change.h b/graph-parser_c/include/network_change.h index 0bd091e..15feb13 100644 --- a/graph-parser_c/include/network_change.h +++ b/graph-parser_c/include/network_change.h @@ -14,49 +14,38 @@ #include #ifdef __cplusplus -extern "C" -{ +extern "C" { #endif - /** - * These functions aims to infer if the network is changed, and if it is so, - * they measure this modification. If it is lower than a predefined - * threshold, they return the old values. - * - */ +/** + * These functions aims to infer if the network is changed, and if it is so, + * they measure this modification. If it is lower than a predefined + * threshold, they return the old values. + * + */ - /** - * !!! NOTE !!! - * This is the function that decides whether to recompute. - * Further details in network_change.c. - * If you want to change this aspect, you have to replace this function. - * - */ - bool recompute(double deviation, - double deviation_old, - int biconnected_num, - int old_biconnected_num); +/** + * !!! NOTE !!! + * This is the function that decides whether to recompute. + * Further details in network_change.c. + * If you want to change this aspect, you have to replace this function. + * + */ +bool recompute(double deviation, double deviation_old, int biconnected_num, + int old_biconnected_num); - double * is_network_changed(struct list *biconnected_components_subgraph, - int node_num, - int * biconnected_num, - float * standard_deviation_bic, - float * standard_deviation_edge, - int * result_size, - char *** node_names); +double *is_network_changed(struct list *biconnected_components_subgraph, + int node_num, int *biconnected_num, + float *standard_deviation_bic, + float *standard_deviation_edge, int *result_size, + char ***node_names); - void write_file(int connected_comp_num, - float standard_deviation_bic, - float standard_deviation_edge, - int size, - double * ret_vals, - struct list *list_of_nodes); +void write_file(int connected_comp_num, float standard_deviation_bic, + float standard_deviation_edge, int size, double *ret_vals, + struct list *list_of_nodes); - void copy_old_values(double * old_vals, - double * vals, - char ** names, - int names_count, - struct list *list_of_nodes); +void copy_old_values(double *old_vals, double *vals, char **names, + int names_count, struct list *list_of_nodes); #ifdef __cplusplus diff --git a/graph-parser_c/src/biconnected.c b/graph-parser_c/src/biconnected.c index 3be457d..941ce48 100644 --- a/graph-parser_c/src/biconnected.c +++ b/graph-parser_c/src/biconnected.c @@ -1,51 +1,26 @@ #include "biconnected.h" -// pseudocode from https://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm -void strongconnect(struct node_graph *v, - int * current_index, - struct list *s, - int * index, - int * low_link, - bool * on_stack, - bool * added, - struct sub_graph *sg, - bool * is_articulation_point, - int * component_indexes, - int * component_index, - struct node_graph *from, - int * bcc_indexes, - int * bcc_num); - -void DFS_iter(struct node_graph *u, - int * current_index, - struct list *s, - struct node_graph **caller, - struct node_list **iterator, - int * index, - int * low_link, - bool * on_stack, - bool * added, - struct sub_graph *sg, - bool * is_articulation_point, - int * component_indexes, - int * component_index, - int * bcc_indexes, - int * bcc_num); - -void DFS_visit(struct node_graph *u, - struct list *s, - int * d, - int * low, - bool * visited, - struct node_graph **parent, - int * count, - bool * added, - bool * is_articulation_point, - int node_num, - struct sub_graph *sg, - int * component_indexes, - int component_index); +// pseudocode from +// https://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm +void strongconnect(struct node_graph *v, int *current_index, struct list *s, + int *index, int *low_link, bool *on_stack, bool *added, + struct sub_graph *sg, bool *is_articulation_point, + int *component_indexes, int *component_index, + struct node_graph *from, int *bcc_indexes, int *bcc_num); + +void DFS_iter(struct node_graph *u, int *current_index, struct list *s, + struct node_graph **caller, struct node_list **iterator, + int *index, int *low_link, bool *on_stack, bool *added, + struct sub_graph *sg, bool *is_articulation_point, + int *component_indexes, int *component_index, int *bcc_indexes, + int *bcc_num); + +void DFS_visit(struct node_graph *u, struct list *s, int *d, int *low, + bool *visited, struct node_graph **parent, int *count, + bool *added, bool *is_articulation_point, int node_num, + struct sub_graph *sg, int *component_indexes, + int component_index); /** * Returns the min of two numbers. It is inline, so it simplyifies code withouth @@ -55,15 +30,13 @@ void DFS_visit(struct node_graph *u, * @param b An integer * @return the minimum value of @a and @b. */ -static inline int min(int a, - int b) +static inline int min(int a, int b) { - if (a < b) - { - return a; - } + if (a < b) { + return a; + } - return b; + return b; } /** @@ -75,25 +48,25 @@ static inline int min(int a, */ struct sub_graph *init_sub_graph() { - struct sub_graph *sg = (struct sub_graph*) malloc(sizeof(struct sub_graph)); + struct sub_graph *sg = + (struct sub_graph *)malloc(sizeof(struct sub_graph)); - init_list(&sg -> connected_components); + init_list(&sg->connected_components); - sg -> size = 0; + sg->size = 0; - return sg; + return sg; } -struct edge_repr -{ - struct node_graph *from; +struct edge_repr { + struct node_graph *from; - struct node_graph *to; + struct node_graph *to; - double value; + double value; }; @@ -105,18 +78,18 @@ struct edge_repr * @param value Weight of the edge * @return The newly created edge */ -struct edge_repr *init_edge_repr(struct node_graph *from, - struct node_graph *to, - double value) +struct edge_repr *init_edge_repr(struct node_graph *from, struct node_graph *to, + double value) { - struct edge_repr *er = (struct edge_repr*) malloc(sizeof(struct edge_repr)); + struct edge_repr *er = + (struct edge_repr *)malloc(sizeof(struct edge_repr)); - er -> from = from; - er -> to = to; - er -> value = value; + er->from = from; + er->to = to; + er->value = value; - return er; + return er; } /** @@ -127,92 +100,80 @@ struct edge_repr *init_edge_repr(struct node_graph *from, * @param g A graph * @return A list of biconnected components */ -struct list *tarjan_rec_dir(struct graph *g, - bool * is_articulation_point, - int * component_indexes) +struct list *tarjan_rec_dir(struct graph *g, bool *is_articulation_point, + int *component_indexes) { - struct list *connected_components_subgraph = - (struct list*) malloc(sizeof(struct list)); + struct list *connected_components_subgraph = + (struct list *)malloc(sizeof(struct list)); - init_list(connected_components_subgraph); - struct list s; + init_list(connected_components_subgraph); + struct list s; - init_list(&s); + init_list(&s); - int current_index = 0; + int current_index = 0; - struct node_list *n = g -> nodes.head; + struct node_list *n = g->nodes.head; - int node_num = g -> nodes.size; - int * index = (int *) malloc(sizeof(int) * node_num); - int * low_link = (int *) malloc(sizeof(int) * node_num); - int * bcc_indexes = (int *) malloc(sizeof(int) * node_num); - bool * on_stack = (bool *) malloc(sizeof(bool) * node_num); - bool * added = (bool *) malloc(sizeof(bool) * node_num); - int i; - int component_index = 0; - int bcc_index = 0; + int node_num = g->nodes.size; + int *index = (int *)malloc(sizeof(int) * node_num); + int *low_link = (int *)malloc(sizeof(int) * node_num); + int *bcc_indexes = (int *)malloc(sizeof(int) * node_num); + bool *on_stack = (bool *)malloc(sizeof(bool) * node_num); + bool *added = (bool *)malloc(sizeof(bool) * node_num); + int i; + int component_index = 0; + int bcc_index = 0; - for (i = 0; i < node_num; i++) - { - index[i] = -1; - low_link[i] = -1; - on_stack[i] = false; - is_articulation_point[i] = false; - component_indexes[i] = -1; - bcc_indexes[i] = -1; - added[i] = false; - } + for (i = 0; i < node_num; i++) { + index[i] = -1; + low_link[i] = -1; + on_stack[i] = false; + is_articulation_point[i] = false; + component_indexes[i] = -1; + bcc_indexes[i] = -1; + added[i] = false; + } - while (n != 0) - { - struct node_graph *ng = (struct node_graph*) n -> content; + while (n != 0) { + struct node_graph *ng = (struct node_graph *)n->content; - struct sub_graph *sg = init_sub_graph(); + struct sub_graph *sg = init_sub_graph(); - if (index[ng -> node_graph_id] < 0) - { - strongconnect(ng, - ¤t_index, - &s, - index, - low_link, - on_stack, - added, - sg, - is_articulation_point, - component_indexes, - &component_index, - 0, - bcc_indexes, - &bcc_index); - } + if (index[ng->node_graph_id] < 0) { + strongconnect(ng, ¤t_index, &s, index, low_link, + on_stack, added, sg, + is_articulation_point, component_indexes, + &component_index, 0, bcc_indexes, + &bcc_index); + } - component_index++; - enqueue_list(connected_components_subgraph, sg); + component_index++; + enqueue_list(connected_components_subgraph, sg); - n = n -> next; - } + n = n->next; + } - free(index); - free(low_link); - free(on_stack); - free(added); - free(bcc_indexes); + free(index); + free(low_link); + free(on_stack); + free(added); + free(bcc_indexes); - return connected_components_subgraph; + return connected_components_subgraph; } /** Given a node, it returns all the biconnected components in the connected * graph. - * - * Based on https://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm - * + * + * Based on + * https://en.wikipedia.org/wiki/Tarjan's_strongly_connected_components_algorithm + * * @param v A node * @param current_index current index value, used for detecting component * @param s list working as a stack of nodes @@ -222,184 +183,161 @@ struct list *tarjan_rec_dir(struct graph *g, * @param added array of boolean, used to check the if we are in biconnected * component * @param sg subgraph of current biconnected component - * @param is_articulation_point boolean array, tells whether a node is an + * @param is_articulation_point boolean array, tells whether a node is an * articulation point * @param component_indexes integer array, tells the id of the component a node * belongs to * @param component_index integer value. It denotes the connected component - * @param from Node that defines the source node from which the + * @param from Node that defines the source node from which the * function is called. (it is the param v in previous call). * @param bcc_indexes integer array, tells the id of the biconnected component * a node belongs to * @param bcc_num integer value. It denotes the biconnected component */ -void strongconnect(struct node_graph *v, - int * current_index, - struct list *s, - int * index, - int * low_link, - bool * on_stack, - bool * added, - struct sub_graph *sg, - bool * is_articulation_point, - int * component_indexes, - int * component_index, - struct node_graph *from, - int * bcc_indexes, - int * bcc_num) +void strongconnect(struct node_graph *v, int *current_index, struct list *s, + int *index, int *low_link, bool *on_stack, bool *added, + struct sub_graph *sg, bool *is_articulation_point, + int *component_indexes, int *component_index, + struct node_graph *from, int *bcc_indexes, int *bcc_num) { - index[v -> node_graph_id] = *current_index; - low_link[v -> node_graph_id] = *current_index; + index[v->node_graph_id] = *current_index; + low_link[v->node_graph_id] = *current_index; - (*current_index)++; - enqueue_list(s, (void *) v); + (*current_index)++; + enqueue_list(s, (void *)v); - on_stack[v -> node_graph_id] = true; - struct node_list *nq = v -> neighbours.head; + on_stack[v->node_graph_id] = true; + struct node_list *nq = v->neighbours.head; - for (nq = v -> neighbours.head; nq != 0; nq = nq -> next) - { - struct node_graph *w = ((struct edge_graph*) nq -> content) -> to; + for (nq = v->neighbours.head; nq != 0; nq = nq->next) { + struct node_graph *w = ((struct edge_graph *)nq->content)->to; - if (index[w -> node_graph_id] < 0) - { - strongconnect(w, - current_index, - s, - index, - low_link, - on_stack, - added, - sg, - is_articulation_point, - component_indexes, - component_index, - v, - bcc_indexes, - bcc_num); + if (index[w->node_graph_id] < 0) { + strongconnect(w, current_index, s, index, low_link, + on_stack, added, sg, + is_articulation_point, component_indexes, + component_index, v, bcc_indexes, bcc_num); - low_link[v -> node_graph_id] = - min(low_link[v -> node_graph_id], low_link[w -> node_graph_id]); - } - else if (on_stack[w -> node_graph_id]) - { - low_link[v -> node_graph_id] = - min(low_link[v -> node_graph_id], index[w -> node_graph_id]); - } - } + low_link[v->node_graph_id] = + min(low_link[v->node_graph_id], + low_link[w->node_graph_id]); + } else if (on_stack[w->node_graph_id]) { + low_link[v->node_graph_id] = + min(low_link[v->node_graph_id], + index[w->node_graph_id]); + } + } - if (low_link[v -> node_graph_id] == index[v -> node_graph_id]) - { - struct node_graph *w = 0; + if (low_link[v->node_graph_id] == index[v->node_graph_id]) { + struct node_graph *w = 0; - struct list *nodes_in_cc = (struct list*) malloc(sizeof(struct list)); + struct list *nodes_in_cc = + (struct list *)malloc(sizeof(struct list)); - init_list(nodes_in_cc); + init_list(nodes_in_cc); - if (from != 0) - { - enqueue_list(nodes_in_cc, from); + if (from != 0) { + enqueue_list(nodes_in_cc, from); - if ((bcc_indexes[from -> node_graph_id] != *bcc_num) && - (bcc_indexes[from -> node_graph_id] >= 0)) - { - is_articulation_point[from -> node_graph_id] = true; - } + if ((bcc_indexes[from->node_graph_id] != *bcc_num) + && (bcc_indexes[from->node_graph_id] >= 0)) { + is_articulation_point[from->node_graph_id] = + true; + } - bcc_indexes[from -> node_graph_id] = *bcc_num; - } + bcc_indexes[from->node_graph_id] = *bcc_num; + } - do - { - w = - (struct node_graph*) pop_list(s); - component_indexes[w -> node_graph_id] = *component_index; + do { + w = (struct node_graph *)pop_list(s); + component_indexes[w->node_graph_id] = *component_index; - enqueue_list(nodes_in_cc, w); + enqueue_list(nodes_in_cc, w); - added[w -> node_graph_id] = true; - on_stack[w -> node_graph_id] = false; - } - while (w != v); + added[w->node_graph_id] = true; + on_stack[w->node_graph_id] = false; + } while (w != v); - int added_count = nodes_in_cc -> size; + int added_count = nodes_in_cc->size; - if (added_count > 1) - { - struct connected_component *cc = - (struct connected_component*) - malloc(sizeof(struct connected_component)); + if (added_count > 1) { + struct connected_component *cc = + (struct connected_component *)malloc( + sizeof(struct connected_component)); - init_graph(&(cc -> g)); + init_graph(&(cc->g)); - cc -> mapping = (int *) malloc(sizeof(int) * added_count); - cc -> weights = (int *) malloc(sizeof(int) * added_count); + cc->mapping = (int *)malloc(sizeof(int) * added_count); + cc->weights = (int *)malloc(sizeof(int) * added_count); - int i; + int i; - for (i = 0; i < added_count; i++) - { - cc -> weights[i] = -1; - } + for (i = 0; i < added_count; i++) { + cc->weights[i] = -1; + } - while (!is_empty_list(nodes_in_cc)) - { - struct node_graph *node = pop_list(nodes_in_cc); + while (!is_empty_list(nodes_in_cc)) { + struct node_graph *node = pop_list(nodes_in_cc); - struct node_list *nq = node -> neighbours.head; + struct node_list *nq = node->neighbours.head; - for (nq = node -> neighbours.head; nq != 0; nq = nq -> next) - { - struct node_graph *to = - ((struct edge_graph*) nq -> content) -> to; + for (nq = node->neighbours.head; nq != 0; + nq = nq->next) { + struct node_graph *to = + ((struct edge_graph *) + nq->content) + ->to; - if (added[to -> node_graph_id]) - { - if ((bcc_indexes[to -> node_graph_id] != *bcc_num) && - (bcc_indexes[to -> node_graph_id] >= 0)) - { - is_articulation_point[to -> node_graph_id] = true; - } + if (added[to->node_graph_id]) { + if ((bcc_indexes + [to->node_graph_id] + != *bcc_num) + && (bcc_indexes + [to->node_graph_id] + >= 0)) { + is_articulation_point + [to->node_graph_id] = + true; + } - int f = 0, - t = 0; + int f = 0, t = 0; - add_edge_graph_return_node_indexes(&(cc -> g), - node -> name, - to -> name, - ((struct edge_graph*) - nq -> content) -> - value, - 1, - &f, - &t); + add_edge_graph_return_node_indexes( + &(cc->g), node->name, + to->name, + ((struct edge_graph *) + nq->content) + ->value, + 1, &f, &t); - // add mapping and translation - cc -> mapping[f] = node -> node_graph_id; - cc -> mapping[t] = to -> node_graph_id; - } + // add mapping and translation + cc->mapping[f] = + node->node_graph_id; + cc->mapping[t] = + to->node_graph_id; + } - added[to -> node_graph_id] = false; - } - } + added[to->node_graph_id] = false; + } + } - enqueue_list(&(sg -> connected_components), cc); - } + enqueue_list(&(sg->connected_components), cc); + } - clear_list(nodes_in_cc); - free(nodes_in_cc); - (*bcc_num)++; + clear_list(nodes_in_cc); + free(nodes_in_cc); + (*bcc_num)++; - added[v -> node_graph_id] = false; - } + added[v->node_graph_id] = false; + } } /** @@ -410,95 +348,80 @@ void strongconnect(struct node_graph *v, * @param g A graph * @return A list of biconnected components */ -struct list *tarjan_iter_dir(struct graph *g, - bool * is_articulation_point, - int * component_indexes) +struct list *tarjan_iter_dir(struct graph *g, bool *is_articulation_point, + int *component_indexes) { - struct list *connected_components_subgraph = - (struct list*) malloc(sizeof(struct list)); + struct list *connected_components_subgraph = + (struct list *)malloc(sizeof(struct list)); - init_list(connected_components_subgraph); - struct node_list *n = g -> nodes.head; + init_list(connected_components_subgraph); + struct node_list *n = g->nodes.head; - int current_index = 0; - int component_index = 0; - int bcc_index = 0; + int current_index = 0; + int component_index = 0; + int bcc_index = 0; - struct list s; + struct list s; - init_list(&s); - struct node_graph **caller = (struct node_graph**) - malloc(sizeof(struct node_graph*) * g -> nodes.size); + init_list(&s); + struct node_graph **caller = (struct node_graph **)malloc( + sizeof(struct node_graph *) * g->nodes.size); - struct node_list **iterator = (struct node_list**) - malloc(sizeof(struct node_list*) * g -> nodes.size); + struct node_list **iterator = (struct node_list **)malloc( + sizeof(struct node_list *) * g->nodes.size); - int node_num = g -> nodes.size; - int * index = (int *) malloc(sizeof(int) * node_num); - int * low_link = (int *) malloc(sizeof(int) * node_num); - bool * on_stack = (bool *) malloc(sizeof(bool) * node_num); - bool * added = (bool *) malloc(sizeof(bool) * node_num); - int * bcc_indexes = (int *) malloc(sizeof(int) * node_num); - int i; + int node_num = g->nodes.size; + int *index = (int *)malloc(sizeof(int) * node_num); + int *low_link = (int *)malloc(sizeof(int) * node_num); + bool *on_stack = (bool *)malloc(sizeof(bool) * node_num); + bool *added = (bool *)malloc(sizeof(bool) * node_num); + int *bcc_indexes = (int *)malloc(sizeof(int) * node_num); + int i; - for (i = 0; i < node_num; i++) - { - index[i] = -1; - low_link[i] = -1; - on_stack[i] = false; - is_articulation_point[i] = false; - component_indexes[i] = -1; - bcc_indexes[i] = -1; - added[i] = false; - } + for (i = 0; i < node_num; i++) { + index[i] = -1; + low_link[i] = -1; + on_stack[i] = false; + is_articulation_point[i] = false; + component_indexes[i] = -1; + bcc_indexes[i] = -1; + added[i] = false; + } - while (n != 0) - { - struct node_graph *ng = (struct node_graph*) n -> content; + while (n != 0) { + struct node_graph *ng = (struct node_graph *)n->content; - struct sub_graph *sg = init_sub_graph(); + struct sub_graph *sg = init_sub_graph(); - if (index[ng -> node_graph_id] < 0) - { - DFS_iter(ng, - ¤t_index, - &s, - caller, - iterator, - index, - low_link, - on_stack, - added, - sg, - is_articulation_point, - component_indexes, - &component_index, - bcc_indexes, - &bcc_index); - component_index++; - } + if (index[ng->node_graph_id] < 0) { + DFS_iter(ng, ¤t_index, &s, caller, iterator, + index, low_link, on_stack, added, sg, + is_articulation_point, component_indexes, + &component_index, bcc_indexes, &bcc_index); + component_index++; + } - enqueue_list(connected_components_subgraph, sg); + enqueue_list(connected_components_subgraph, sg); - n = n -> next; - } + n = n->next; + } - free(caller); - free(iterator); - free(index); - free(low_link); - free(on_stack); - free(added); - free(bcc_indexes); + free(caller); + free(iterator); + free(index); + free(low_link); + free(on_stack); + free(added); + free(bcc_indexes); - return connected_components_subgraph; + return connected_components_subgraph; } // https://www.researchgate.net/profile/Oscar_Karnalim/publication/303959022_Improving_Scalability_of_Java_Archive_Search_Engine_through_Recursion_Conversion_And_Multithreading/links/57c929ed08aefc4af350b37d.pdf?origin=publication_detail @@ -519,7 +442,7 @@ struct list *tarjan_iter_dir(struct graph *g, * @param added array of boolean, used to check the if we are in biconnected * component * @param sg subgraph of current biconnected component - * @param is_articulation_point boolean array, tells whether a node is an + * @param is_articulation_point boolean array, tells whether a node is an * articulation point * @param component_indexes integer array, tells the id of the component a node * belongs to @@ -528,221 +451,239 @@ struct list *tarjan_iter_dir(struct graph *g, * a node belongs to * @param bcc_num integer value. It denotes the biconnected component */ -void DFS_iter(struct node_graph *u, - int * current_index, - struct list *s, - struct node_graph **caller, - struct node_list **iterator, - int * index, - int * low_link, - bool * on_stack, - bool * added, - struct sub_graph *sg, - bool * is_articulation_point, - int * component_indexes, - int * component_index, - int * bcc_indexes, - int * bcc_num) +void DFS_iter(struct node_graph *u, int *current_index, struct list *s, + struct node_graph **caller, struct node_list **iterator, + int *index, int *low_link, bool *on_stack, bool *added, + struct sub_graph *sg, bool *is_articulation_point, + int *component_indexes, int *component_index, int *bcc_indexes, + int *bcc_num) { - index[u -> node_graph_id] = *current_index; - low_link[u -> node_graph_id] = *current_index; - - (*current_index)++; - - iterator[u -> node_graph_id] = u -> neighbours.head; - caller[u -> node_graph_id] = 0; - on_stack[u -> node_graph_id] = true, - - enqueue_list(s, (void *) u); - struct node_graph *last = u; - - - while (true) - { - if (iterator[last -> node_graph_id] != 0) - { - struct node_graph *w = - ((struct edge_graph*) - iterator[last -> node_graph_id] -> content) -> to; - - - iterator[last -> node_graph_id] = - iterator[last -> node_graph_id] -> next; - - if (index[w -> node_graph_id] < 0) - { - caller[w -> node_graph_id] = last; - index[w -> node_graph_id] = *current_index; - low_link[w -> node_graph_id] = *current_index; - - (*current_index)++; - enqueue_list(s, (void *) w); - - on_stack[w -> node_graph_id] = true; - iterator[w -> node_graph_id] = w -> neighbours.head; - last = w; - } - else if (on_stack[w -> node_graph_id]) - { - low_link[last -> node_graph_id] = - min(low_link[last -> node_graph_id], - index[w -> node_graph_id]); - } - } - else - { - if (index[last -> node_graph_id] == low_link[last -> node_graph_id]) - { - struct list *nodes_in_cc = (struct list*) malloc(sizeof(struct list)); - - - init_list(nodes_in_cc); - struct node_graph *top = 0; - - - do - { - top = (struct node_graph*) pop_list(s); - - if (top != 0) - { - enqueue_list(nodes_in_cc, top); - - added[top -> node_graph_id] = true; - component_indexes[top -> node_graph_id] = *component_index; - on_stack[top -> node_graph_id] = false; - - if ((bcc_indexes[top -> node_graph_id] != *bcc_num) && - (bcc_indexes[top -> node_graph_id] >= 0)) - { - is_articulation_point[top -> node_graph_id] = true; - } - - bcc_indexes[top -> node_graph_id] = *bcc_num; - } - } - while ((top != 0) && (index[top -> node_graph_id] != - index[last -> node_graph_id])); - - if (nodes_in_cc -> size == 1) - { - struct node_graph *tmp = - caller[((struct node_graph*) nodes_in_cc -> - head -> content) -> node_graph_id]; - - - if ((tmp != 0) &&!added[tmp -> node_graph_id]) - { - if ((bcc_indexes[tmp -> node_graph_id] != *bcc_num) && - (bcc_indexes[tmp -> node_graph_id] >= 0)) - { - is_articulation_point[tmp -> node_graph_id] = true; - } - - bcc_indexes[tmp -> node_graph_id] = *bcc_num; - - enqueue_list(nodes_in_cc, tmp); - } - else - { - is_articulation_point[top -> node_graph_id] = false; - } - } - - int added_count = nodes_in_cc -> size; - - if (added_count > 1) - { - struct connected_component *cc = - (struct connected_component*) - malloc(sizeof(struct connected_component)); - - - init_graph(&(cc -> g)); - - cc -> mapping = (int *) malloc(sizeof(int) * added_count); - cc -> weights = (int *) malloc(sizeof(int) * added_count); - - int i; - - for (i = 0; i < added_count; i++) - { - cc -> weights[i] = -1; - } - - while (!is_empty_list(nodes_in_cc)) - { - struct node_graph *node = pop_list(nodes_in_cc); - - - struct node_list *nq = node -> neighbours.head; - - - for (nq = node -> neighbours.head; nq != 0; nq = nq -> next) - { - struct node_graph *to = - ((struct edge_graph*) nq -> content) -> to; - - - if (added[to -> node_graph_id]) - { - if ((bcc_indexes[to -> node_graph_id] - != *bcc_num) - && (bcc_indexes[to -> node_graph_id] - >= 0)) - { - is_articulation_point[to -> node_graph_id] - = true; - } - - int f = 0, - t = 0; - - add_edge_graph_return_node_indexes(&(cc -> g), - node -> name, - to -> name, - ((struct - edge_graph*) - nq -> - content) - -> value, - 1, - &f, - &t); - - // add mapping and translation - cc -> mapping[f] = node -> node_graph_id; - cc -> mapping[t] = to -> node_graph_id; - } - - added[to -> node_graph_id] = false; - } - } - - enqueue_list(&(sg -> connected_components), cc); - (*bcc_num)++; - } - - clear_list(nodes_in_cc); - free(nodes_in_cc); - } - - struct node_graph *new_last = caller[last -> node_graph_id]; - - - if (new_last != 0) - { - low_link[new_last -> node_graph_id] = min(low_link - [new_last -> node_graph_id], - low_link[last -> node_graph_id]); - last = new_last; - } - else - { - break; - } - } - } + index[u->node_graph_id] = *current_index; + low_link[u->node_graph_id] = *current_index; + + (*current_index)++; + + iterator[u->node_graph_id] = u->neighbours.head; + caller[u->node_graph_id] = 0; + on_stack[u->node_graph_id] = true, + + enqueue_list(s, (void *)u); + struct node_graph *last = u; + + + while (true) { + if (iterator[last->node_graph_id] != 0) { + struct node_graph *w = + ((struct edge_graph *) + iterator[last->node_graph_id] + ->content) + ->to; + + + iterator[last->node_graph_id] = + iterator[last->node_graph_id]->next; + + if (index[w->node_graph_id] < 0) { + caller[w->node_graph_id] = last; + index[w->node_graph_id] = *current_index; + low_link[w->node_graph_id] = *current_index; + + (*current_index)++; + enqueue_list(s, (void *)w); + + on_stack[w->node_graph_id] = true; + iterator[w->node_graph_id] = w->neighbours.head; + last = w; + } else if (on_stack[w->node_graph_id]) { + low_link[last->node_graph_id] = + min(low_link[last->node_graph_id], + index[w->node_graph_id]); + } + } else { + if (index[last->node_graph_id] + == low_link[last->node_graph_id]) { + struct list *nodes_in_cc = + (struct list *)malloc( + sizeof(struct list)); + + + init_list(nodes_in_cc); + struct node_graph *top = 0; + + + do { + top = (struct node_graph *)pop_list(s); + + if (top != 0) { + enqueue_list(nodes_in_cc, top); + + added[top->node_graph_id] = + true; + component_indexes + [top->node_graph_id] = + *component_index; + on_stack[top->node_graph_id] = + false; + + if ((bcc_indexes + [top->node_graph_id] + != *bcc_num) + && (bcc_indexes + [top->node_graph_id] + >= 0)) { + is_articulation_point + [top->node_graph_id] = + true; + } + + bcc_indexes + [top->node_graph_id] = + *bcc_num; + } + } while ((top != 0) + && (index[top->node_graph_id] + != index[last->node_graph_id])); + + if (nodes_in_cc->size == 1) { + struct node_graph *tmp = + caller[((struct node_graph + *)nodes_in_cc + ->head->content) + ->node_graph_id]; + + + if ((tmp != 0) + && !added[tmp->node_graph_id]) { + if ((bcc_indexes + [tmp->node_graph_id] + != *bcc_num) + && (bcc_indexes + [tmp->node_graph_id] + >= 0)) { + is_articulation_point + [tmp->node_graph_id] = + true; + } + + bcc_indexes + [tmp->node_graph_id] = + *bcc_num; + + enqueue_list(nodes_in_cc, tmp); + } else { + is_articulation_point + [top->node_graph_id] = + false; + } + } + + int added_count = nodes_in_cc->size; + + if (added_count > 1) { + struct connected_component *cc = + (struct connected_component *) + malloc(sizeof( + struct + connected_component)); + + + init_graph(&(cc->g)); + + cc->mapping = (int *)malloc( + sizeof(int) * added_count); + cc->weights = (int *)malloc( + sizeof(int) * added_count); + + int i; + + for (i = 0; i < added_count; i++) { + cc->weights[i] = -1; + } + + while (!is_empty_list(nodes_in_cc)) { + struct node_graph *node = + pop_list(nodes_in_cc); + + + struct node_list *nq = + node->neighbours.head; + + + for (nq = node->neighbours.head; + nq != 0; nq = nq->next) { + struct node_graph *to = + ((struct + edge_graph + *)nq + ->content) + ->to; + + + if (added[to->node_graph_id]) { + if ((bcc_indexes + [to->node_graph_id] + != *bcc_num) + && (bcc_indexes + [to->node_graph_id] + >= 0)) { + is_articulation_point + [to->node_graph_id] = + true; + } + + int f = 0, + t = 0; + + add_edge_graph_return_node_indexes( + &(cc->g), + node->name, + to->name, + ((struct + edge_graph + *)nq + ->content) + ->value, + 1, &f, + &t); + + // add mapping + // and + // translation + cc->mapping[f] = + node->node_graph_id; + cc->mapping[t] = + to->node_graph_id; + } + + added[to->node_graph_id] = + false; + } + } + + enqueue_list( + &(sg->connected_components), + cc); + (*bcc_num)++; + } + + clear_list(nodes_in_cc); + free(nodes_in_cc); + } + + struct node_graph *new_last = + caller[last->node_graph_id]; + + + if (new_last != 0) { + low_link[new_last->node_graph_id] = + min(low_link[new_last->node_graph_id], + low_link[last->node_graph_id]); + last = new_last; + } else { + break; + } + } + } } // From http://www.cs.umd.edu/class/fall2005/cmsc451/biconcomps.pdf @@ -755,99 +696,85 @@ void DFS_iter(struct node_graph *u, * @param g A graph * @param is_articulation_point An array representing whether a node is an * articulation point. Used as return value - * @param component_indexes An integer array telling to which connected component a + * @param component_indexes An integer array telling to which connected + * component a * node belongs to. * @return a list of pair, one for each connected component, of biconnected * components and node number in that connected component */ -struct list *tarjan_rec_undir(struct graph *g, - bool * is_articulation_point, - int * component_indexes) +struct list *tarjan_rec_undir(struct graph *g, bool *is_articulation_point, + int *component_indexes) { - struct list *connected_components_subgraph = - (struct list*) malloc(sizeof(struct list)); + struct list *connected_components_subgraph = + (struct list *)malloc(sizeof(struct list)); - init_list(connected_components_subgraph); + init_list(connected_components_subgraph); - int node_num = g -> nodes.size; - int count = 0; - bool * visited = (bool *) malloc(node_num * sizeof(bool)); - bool * added = (bool *) malloc(node_num * sizeof(bool)); + int node_num = g->nodes.size; + int count = 0; + bool *visited = (bool *)malloc(node_num * sizeof(bool)); + bool *added = (bool *)malloc(node_num * sizeof(bool)); - struct node_graph **parent = (struct node_graph**) - malloc(sizeof(struct node_graph*) * g -> nodes.size); + struct node_graph **parent = (struct node_graph **)malloc( + sizeof(struct node_graph *) * g->nodes.size); - struct list s; + struct list s; - init_list(&s); + init_list(&s); - int * d = (int *) malloc(node_num * sizeof(int)); - int * low = (int *) malloc(node_num * sizeof(int)); - int i; + int *d = (int *)malloc(node_num * sizeof(int)); + int *low = (int *)malloc(node_num * sizeof(int)); + int i; - for (i = 0; i < node_num; i++) - { - visited[i] = false; - added[i] = false; - parent[i] = 0; - d[i] = -1; - low[i] = -1; - is_articulation_point[i] = false; - } + for (i = 0; i < node_num; i++) { + visited[i] = false; + added[i] = false; + parent[i] = 0; + d[i] = -1; + low[i] = -1; + is_articulation_point[i] = false; + } - struct node_list *n; + struct node_list *n; - i = 0; + i = 0; - int component_index = 0; + int component_index = 0; - for (n = g -> nodes.head; n != 0; n = n -> next) - { - struct node_graph *ng = (struct node_graph*) n -> content; + for (n = g->nodes.head; n != 0; n = n->next) { + struct node_graph *ng = (struct node_graph *)n->content; - if (!visited[i]) - { - struct sub_graph *sg = init_sub_graph(); + if (!visited[i]) { + struct sub_graph *sg = init_sub_graph(); - DFS_visit(ng, - &s, - d, - low, - visited, - parent, - &count, - added, - is_articulation_point, - node_num, - sg, - component_indexes, - component_index); + DFS_visit(ng, &s, d, low, visited, parent, &count, + added, is_articulation_point, node_num, sg, + component_indexes, component_index); - if (ng -> neighbours.size > 1) - { - is_articulation_point[ng -> node_graph_id] = true; - } + if (ng->neighbours.size > 1) { + is_articulation_point[ng->node_graph_id] = true; + } - component_index++; - enqueue_list(connected_components_subgraph, sg); - } + component_index++; + enqueue_list(connected_components_subgraph, sg); + } - i++; - } + i++; + } - free(added); - free(visited); - free(parent); - free(d); - free(low); + free(added); + free(visited); + free(parent); + free(d); + free(low); - return connected_components_subgraph; + return connected_components_subgraph; } @@ -875,197 +802,199 @@ struct list *tarjan_rec_undir(struct graph *g, * @param component_index Int value that tells current connected components * index. */ -void DFS_visit(struct node_graph *u, - struct list *s, - int * d, - int * low, - bool * visited, - struct node_graph **parent, - int * count, - bool * added, - bool * is_articulation_point, - int node_num, - struct sub_graph *sg, - int * component_indexes, - int component_index) +void DFS_visit(struct node_graph *u, struct list *s, int *d, int *low, + bool *visited, struct node_graph **parent, int *count, + bool *added, bool *is_articulation_point, int node_num, + struct sub_graph *sg, int *component_indexes, + int component_index) { - visited[u -> node_graph_id] = true; - d[u -> node_graph_id] = (*count); - low[u -> node_graph_id] = (*count); - (*count) = (*count) + 1; - - if (u -> neighbours.size > 0) - { - struct node_list *edge_iterator; - - - for (edge_iterator = u -> neighbours.head; - edge_iterator != 0; edge_iterator = edge_iterator -> next) - { - struct edge_graph *edge = - (struct edge_graph*) edge_iterator -> content; - - - struct node_graph *v = edge -> to; - - - if (!visited[v -> node_graph_id]) - { - visited[v -> node_graph_id] = true; - struct edge_repr *er = init_edge_repr(u, v, edge -> value); - - - enqueue_list(s, er); - - parent[v -> node_graph_id] = u; + visited[u->node_graph_id] = true; + d[u->node_graph_id] = (*count); + low[u->node_graph_id] = (*count); + (*count) = (*count) + 1; - DFS_visit(v, - s, - d, - low, - visited, - parent, - count, - added, - is_articulation_point, - node_num, - sg, - component_indexes, - component_index); + if (u->neighbours.size > 0) { + struct node_list *edge_iterator; - if (low[v -> node_graph_id] >= d[u -> node_graph_id]) - { - struct edge_repr *er_i = 0; + for (edge_iterator = u->neighbours.head; edge_iterator != 0; + edge_iterator = edge_iterator->next) { + struct edge_graph *edge = + (struct edge_graph *)edge_iterator->content; - struct list l; + struct node_graph *v = edge->to; - init_list(&l); - do - { - er_i = (struct edge_repr*) pop_list(s); - added[er_i -> from -> node_graph_id] = true; - added[er_i -> to -> node_graph_id] = true; + if (!visited[v->node_graph_id]) { + visited[v->node_graph_id] = true; + struct edge_repr *er = + init_edge_repr(u, v, edge->value); - if (component_indexes[er_i -> from -> node_graph_id] - == -1) - { - component_indexes[er_i -> from -> node_graph_id] - = component_index; - sg -> size++; - } + enqueue_list(s, er); - if (component_indexes[er_i -> to -> node_graph_id] - == -1) - { - component_indexes[er_i -> to -> node_graph_id] - = component_index; + parent[v->node_graph_id] = u; - sg -> size++; - } + DFS_visit(v, s, d, low, visited, parent, count, + added, is_articulation_point, + node_num, sg, component_indexes, + component_index); - enqueue_list(&l, er_i); - } - while (!is_empty_list(s) &&!((er_i -> to == er -> to) && - (er_i -> from == er -> from))); + if (low[v->node_graph_id] + >= d[u->node_graph_id]) { + struct edge_repr *er_i = 0; - int added_count = 0; - int i; - for (i = 0; i < node_num; i++) - { - if (added[i]) - { - added_count++; + struct list l; - added[i] = false; - } - } - if (!is_empty_list(&l)) - { - struct connected_component *cc = - (struct connected_component*) - malloc(sizeof(struct connected_component)); + init_list(&l); + do { + er_i = (struct edge_repr *) + pop_list(s); + added[er_i->from + ->node_graph_id] = + true; + added[er_i->to->node_graph_id] = + true; - init_graph(&(cc -> g)); + if (component_indexes + [er_i->from + ->node_graph_id] + == -1) { + component_indexes + [er_i->from + ->node_graph_id] = + component_index; - cc -> mapping = (int *) malloc(sizeof(int) * added_count); - cc -> weights = (int *) malloc(sizeof(int) * added_count); + sg->size++; + } - for (i = 0; i < added_count; i++) - { - cc -> weights[i] = -1; - } + if (component_indexes + [er_i->to->node_graph_id] + == -1) { + component_indexes + [er_i->to->node_graph_id] = + component_index; - cc -> cutpoint = 0; + sg->size++; + } - while (!is_empty_list(&l)) - { - struct edge_repr *er_i = (struct edge_repr*) pop_list(&l); + enqueue_list(&l, er_i); + } while (!is_empty_list(s) + && !((er_i->to == er->to) + && (er_i->from + == er->from))); + int added_count = 0; + int i; - int f = 0, - t = 0; + for (i = 0; i < node_num; i++) { + if (added[i]) { + added_count++; - add_edge_graph_return_node_indexes(&(cc -> g), - er_i -> from -> name, - er_i -> to -> name, - er_i -> value, - 0, - &f, - &t); - - cc -> mapping[f] = er_i -> from -> node_graph_id; - cc -> mapping[t] = er_i -> to -> node_graph_id; + added[i] = false; + } + } - free(er_i); - } + if (!is_empty_list(&l)) { + struct connected_component *cc = + (struct + connected_component *) + malloc(sizeof( + struct + connected_component)); - enqueue_list(&(sg -> connected_components), cc); - } - clear_list(&l); - } + init_graph(&(cc->g)); - if ((u -> neighbours.size > 1) - && ((parent[u -> node_graph_id] != 0) && - (low[v -> node_graph_id] >= d[u -> node_graph_id]))) - { - is_articulation_point[u -> node_graph_id] = true; - } + cc->mapping = (int *)malloc( + sizeof(int) + * added_count); + cc->weights = (int *)malloc( + sizeof(int) + * added_count); - low[u -> node_graph_id] = min(low[u -> node_graph_id], - low[v -> node_graph_id]); - } - else if ((parent[u -> node_graph_id] != v) && (d[v -> node_graph_id] - < d[u -> node_graph_id])) - { - struct edge_repr *er = init_edge_repr(u, v, edge -> value); + for (i = 0; i < added_count; + i++) { + cc->weights[i] = -1; + } - - enqueue_list(s, er); - - low[u -> node_graph_id] = min(low[u -> node_graph_id], - d[v -> node_graph_id]); - } - } - } + cc->cutpoint = 0; + + while (!is_empty_list(&l)) { + struct edge_repr *er_i = + (struct + edge_repr *) + pop_list( + &l); + + + int f = 0, t = 0; + + add_edge_graph_return_node_indexes( + &(cc->g), + er_i->from + ->name, + er_i->to->name, + er_i->value, 0, + &f, &t); + + cc->mapping[f] = + er_i->from + ->node_graph_id; + cc->mapping[t] = + er_i->to->node_graph_id; + + free(er_i); + } + + enqueue_list( + &(sg->connected_components), + cc); + } + + clear_list(&l); + } + + if ((u->neighbours.size > 1) + && ((parent[u->node_graph_id] != 0) + && (low[v->node_graph_id] + >= d[u->node_graph_id]))) { + is_articulation_point + [u->node_graph_id] = true; + } + + low[u->node_graph_id] = + min(low[u->node_graph_id], + low[v->node_graph_id]); + } else if ((parent[u->node_graph_id] != v) + && (d[v->node_graph_id] + < d[u->node_graph_id])) { + struct edge_repr *er = + init_edge_repr(u, v, edge->value); + + + enqueue_list(s, er); + + low[u->node_graph_id] = + min(low[u->node_graph_id], + d[v->node_graph_id]); + } + } + } } -struct cc_edge_stack -{ - struct node_graph *grandparent; +struct cc_edge_stack { + struct node_graph *grandparent; - struct node_graph *parent; + struct node_graph *parent; - struct node_list *iterator; + struct node_list *iterator; }; @@ -1079,17 +1008,17 @@ struct cc_edge_stack * @return Newly created edge stack. */ struct cc_edge_stack *init_cc_edge_stack(struct node_graph *grandparent, - struct node_graph *parent) + struct node_graph *parent) { - struct cc_edge_stack *ced = - (struct cc_edge_stack*) malloc(sizeof(struct cc_edge_stack)); + struct cc_edge_stack *ced = + (struct cc_edge_stack *)malloc(sizeof(struct cc_edge_stack)); - ced -> parent = parent; - ced -> grandparent = grandparent; - ced -> iterator = parent -> neighbours.head; + ced->parent = parent; + ced->grandparent = grandparent; + ced->iterator = parent->neighbours.head; - return ced; + return ced; } // https://github.com/networkx/networkx/blob/master/networkx/algorithms/components/biconnected.py#L427 @@ -1102,401 +1031,463 @@ struct cc_edge_stack *init_cc_edge_stack(struct node_graph *grandparent, * @param g A graph * @param is_articulation_point An array representing whether a node is an * articulation point. Used as return value - * @param component_indexes An integer array telling to which connected component a + * @param component_indexes An integer array telling to which connected + * component a * node belongs to. * @return a list of pair, one for each connected component, of biconnected * components and node number in that connected component */ -struct list *tarjan_iter_undir(struct graph *g, - bool * is_articulation_point, - int * component_indexes) +struct list *tarjan_iter_undir(struct graph *g, bool *is_articulation_point, + int *component_indexes) { - struct list *connected_components_subgraph = - (struct list*) malloc(sizeof(struct list)); - - - init_list(connected_components_subgraph); - struct sub_graph *sg = init_sub_graph(); - - - int component_index = 0; - int node_num = g -> nodes.size; - bool * visited = (bool *) malloc(sizeof(bool) * node_num); - bool * added = (bool *) malloc(sizeof(bool) * node_num); - int * discovery = (int *) malloc(sizeof(int) * node_num); - int * low = (int *) malloc(sizeof(int) * node_num); - int i; - - for (i = 0; i < node_num; i++) - { - is_articulation_point[i] = false; - visited[i] = false; - added[i] = false; - } - - struct node_list *n; - - - for (n = g -> nodes.head; n != 0; n = n -> next) - { - struct node_graph *ng = (struct node_graph*) n -> content; - - - if (!visited[ng -> node_graph_id]) - { - visited[ng -> node_graph_id] = true; - - int discovery_len = 1; - int root_children = 0; - - for (i = 0; i < node_num; i++) - { - discovery[i] = 0; - low[i] = 0; - } - - struct list edge_stack; - - - init_list(&edge_stack); - struct list s; - - - init_list(&s); - enqueue_list(&s, init_cc_edge_stack(ng, ng)); - - while (!is_empty_list(&s)) - { - struct cc_edge_stack *ced = - (struct cc_edge_stack*) peek_last_list(&s); - - - if (ced -> iterator != 0) - { - struct edge_graph *edge = - (struct edge_graph*) ced -> iterator -> content; + struct list *connected_components_subgraph = + (struct list *)malloc(sizeof(struct list)); - struct node_graph *child = edge -> to; + init_list(connected_components_subgraph); + struct sub_graph *sg = init_sub_graph(); - ced -> iterator = ced -> iterator -> next; + int component_index = 0; + int node_num = g->nodes.size; + bool *visited = (bool *)malloc(sizeof(bool) * node_num); + bool *added = (bool *)malloc(sizeof(bool) * node_num); + int *discovery = (int *)malloc(sizeof(int) * node_num); + int *low = (int *)malloc(sizeof(int) * node_num); + int i; - if (child == ced -> grandparent) - { - continue; - } + for (i = 0; i < node_num; i++) { + is_articulation_point[i] = false; + visited[i] = false; + added[i] = false; + } - if (visited[child -> node_graph_id]) - { - if (discovery[child -> node_graph_id] <= - discovery[ced -> parent -> node_graph_id]) - { - low[ced -> parent -> node_graph_id] = - min(low[ced -> parent -> node_graph_id], - discovery[child -> node_graph_id]); + struct node_list *n; - enqueue_list(&edge_stack, - init_edge_repr(ced -> parent, child, - edge -> value)); - } - } - else - { - low[child -> node_graph_id] = discovery_len; - discovery[child -> node_graph_id] = discovery_len; - discovery_len++; + for (n = g->nodes.head; n != 0; n = n->next) { + struct node_graph *ng = (struct node_graph *)n->content; - visited[child -> node_graph_id] = true; - enqueue_list(&s, - init_cc_edge_stack(ced -> parent, child)); - enqueue_list(&edge_stack, init_edge_repr(ced -> parent, - child, edge -> value)); - } - } - else - { - struct cc_edge_stack *ced_to_clear = - (struct cc_edge_stack*) pop_list(&s); + if (!visited[ng->node_graph_id]) { + visited[ng->node_graph_id] = true; + int discovery_len = 1; + int root_children = 0; - if (s.size > 1) - { - if (low[ced -> parent -> node_graph_id] >= - discovery[ced -> grandparent -> node_graph_id]) - { - struct edge_repr *er = (struct edge_repr*) - peek_last_list(&edge_stack); + for (i = 0; i < node_num; i++) { + discovery[i] = 0; + low[i] = 0; + } + struct list edge_stack; - struct list l; + init_list(&edge_stack); + struct list s; - init_list(&l); - int added_count = 0; + init_list(&s); + enqueue_list(&s, init_cc_edge_stack(ng, ng)); - while (er != 0) - { - enqueue_list(&l, er); + while (!is_empty_list(&s)) { + struct cc_edge_stack *ced = + (struct cc_edge_stack *)peek_last_list( + &s); - if (component_indexes[er -> from - -> node_graph_id] == -1) - { - component_indexes[er -> from - -> node_graph_id] = component_index; - sg -> size++; - } + if (ced->iterator != 0) { + struct edge_graph *edge = + (struct edge_graph *) + ced->iterator->content; - if (component_indexes[er -> to - -> node_graph_id] == -1) - { - component_indexes[er -> to - -> node_graph_id] = component_index; - sg -> size++; - } + struct node_graph *child = edge->to; - if (!added[er -> from -> node_graph_id]) - { - added_count++; - } - if (!added[er -> to -> node_graph_id]) - { - added_count++; - } + ced->iterator = ced->iterator->next; - added[er -> from -> node_graph_id] = true; - added[er -> to -> node_graph_id] = true; + if (child == ced->grandparent) { + continue; + } - pop_list(&edge_stack); + if (visited[child->node_graph_id]) { + if (discovery + [child->node_graph_id] + <= discovery + [ced->parent + ->node_graph_id]) { + low[ced->parent->node_graph_id] = min( + low[ced->parent + ->node_graph_id], + discovery + [child->node_graph_id]); - if (((er -> from == ced -> grandparent) && - (er -> to == ced -> parent))) - { - break; - } + enqueue_list( + &edge_stack, + init_edge_repr( + ced->parent, + child, + edge->value)); + } + } else { + low[child->node_graph_id] = + discovery_len; + discovery + [child->node_graph_id] = + discovery_len; - er = (struct edge_repr*) - peek_last_list(&edge_stack); - } + discovery_len++; - if (!is_empty_list(&l)) - { - struct connected_component *cc = - (struct connected_component*) - malloc(sizeof(struct connected_component)); - - - init_graph(&(cc -> g)); - - cc -> mapping = - (int *) malloc(sizeof(int) * added_count); - cc -> weights = - (int *) malloc(sizeof(int) * added_count); - - for (i = 0; i < added_count; i++) - { - cc -> weights[i] = -1; - } - - cc -> cutpoint = 0; - - while (!is_empty_list(&l)) - { - struct edge_repr *er_i = - (struct edge_repr*) pop_list(&l); - - - added[er_i -> from -> node_graph_id] = false; - added[er_i -> to -> node_graph_id] = false; - - int f = 0, - t = 0; - - add_edge_graph_return_node_indexes(&(cc -> g), - er_i - -> from - -> name, - er_i - -> to - -> name, - er_i - -> value, - 0, - &f, - &t); - - cc -> mapping[f] = er_i -> from -> node_graph_id; - cc -> mapping[t] = er_i -> to -> node_graph_id; - - free(er_i); - } - - enqueue_list(&(sg -> connected_components), cc); - } - - is_articulation_point - [ced -> grandparent -> node_graph_id] = true; - } - - low[ced -> grandparent -> node_graph_id] = - min(low[ced -> parent -> node_graph_id], - low[ced -> grandparent -> node_graph_id]); - } - else if (s.size == 1) - { - root_children++; - struct node_list *nl = edge_stack.tail; - - - struct edge_repr *er = (struct edge_repr*) nl -> content; - - - struct list l; - - - init_list(&l); - - int added_count = 0; - - while (er != 0) - { - enqueue_list(&l, er); - - if (component_indexes[er -> from -> node_graph_id] - == -1) - { - component_indexes[er -> from -> node_graph_id] - = component_index; - - sg -> size++; - } - - if (component_indexes[er -> to -> node_graph_id] - == -1) - { - component_indexes[er -> to -> node_graph_id] - = component_index; - - sg -> size++; - } - - if (!added[er -> from -> node_graph_id]) - { - added_count++; - } - - if (!added[er -> to -> node_graph_id]) - { - added_count++; - } - - nl = nl -> prev; - - if ((er -> from == ced -> grandparent) && - (er -> to == ced -> parent)) - { - break; - } - - if (nl != 0) - { - er = (struct edge_repr*) nl -> content; - } - else - { - er = 0; - } - } - - if (!is_empty_list(&l)) - { - struct connected_component *cc = - (struct connected_component*) - malloc(sizeof(struct connected_component)); - - - init_graph(&(cc -> g)); - - cc -> mapping = - (int *) malloc(sizeof(int) * added_count); - cc -> weights = - (int *) malloc(sizeof(int) * added_count); - - for (i = 0; i < added_count; i++) - { - cc -> weights[i] = -1; - } - - cc -> cutpoint = 0; - - while (!is_empty_list(&l)) - { - struct edge_repr *er_i = - (struct edge_repr*) pop_list(&l); - - - added[er_i -> from -> node_graph_id] = false; - added[er_i -> to -> node_graph_id] = false; - - int f = 0, - t = 0; - - add_edge_graph_return_node_indexes(&(cc -> g), - er_i -> - from -> name, - er_i -> - to -> name, - er_i -> - value, - 0, - &f, - &t); - - cc -> mapping[f] = er_i -> from -> node_graph_id; - cc -> mapping[t] = er_i -> to -> node_graph_id; - - free(er_i); - } - - enqueue_list(&(sg -> connected_components), cc); - } - } - - free(ced_to_clear); - } - } - - if (root_children > 1) - { - is_articulation_point[ng -> node_graph_id] = true; - } - - enqueue_list(connected_components_subgraph, sg); - - sg = init_sub_graph(); - - component_index++; - clear_list(&edge_stack); - clear_list(&s); - } - } - - if (sg -> size == 0) - { - free(sg); - } - - free(added); - free(low); - free(discovery); - free(visited); - - return connected_components_subgraph; + visited[child->node_graph_id] = + true; + + enqueue_list( + &s, init_cc_edge_stack( + ced->parent, + child)); + enqueue_list( + &edge_stack, + init_edge_repr( + ced->parent, + child, + edge->value)); + } + } else { + struct cc_edge_stack *ced_to_clear = + (struct cc_edge_stack *) + pop_list(&s); + + + if (s.size > 1) { + if (low[ced->parent + ->node_graph_id] + >= discovery + [ced->grandparent + ->node_graph_id]) { + struct edge_repr *er = (struct + edge_repr + *) + peek_last_list( + &edge_stack); + + + struct list l; + + + init_list(&l); + + int added_count = 0; + + while (er != 0) { + enqueue_list( + &l, er); + + if (component_indexes + [er->from->node_graph_id] + == -1) { + component_indexes + [er->from->node_graph_id] = + component_index; + + sg->size++; + } + + if (component_indexes + [er->to->node_graph_id] + == -1) { + component_indexes + [er->to->node_graph_id] = + component_index; + + sg->size++; + } + + if (!added[er->from->node_graph_id]) { + added_count++; + } + + if (!added[er->to->node_graph_id]) { + added_count++; + } + + added[er->from->node_graph_id] = + true; + added[er->to->node_graph_id] = + true; + + pop_list( + &edge_stack); + + if (((er->from + == ced->grandparent) + && (er->to + == ced->parent))) { + break; + } + + er = (struct + edge_repr + *) + peek_last_list( + &edge_stack); + } + + if (!is_empty_list( + &l)) { + struct connected_component + *cc = (struct + connected_component + *) + malloc(sizeof( + struct + connected_component)); + + + init_graph(&( + cc->g)); + + cc->mapping = (int *)malloc( + sizeof(int) + * added_count); + cc->weights = (int *)malloc( + sizeof(int) + * added_count); + + for (i = 0; + i + < added_count; + i++) { + cc->weights + [i] = + -1; + } + + cc->cutpoint = + 0; + + while (!is_empty_list( + &l)) { + struct edge_repr *er_i = + (struct + edge_repr + *) + pop_list( + &l); + + + added[er_i->from + ->node_graph_id] = + false; + added[er_i->to->node_graph_id] = + false; + + int f = 0, + t = 0; + + add_edge_graph_return_node_indexes( + &(cc->g), + er_i->from + ->name, + er_i->to->name, + er_i->value, + 0, + &f, + &t); + + cc->mapping + [f] = + er_i->from + ->node_graph_id; + cc->mapping + [t] = + er_i->to->node_graph_id; + + free(er_i); + } + + enqueue_list( + &(sg->connected_components), + cc); + } + + is_articulation_point + [ced->grandparent + ->node_graph_id] = + true; + } + + low[ced->grandparent + ->node_graph_id] = + min(low[ced->parent + ->node_graph_id], + low[ced->grandparent + ->node_graph_id]); + } else if (s.size == 1) { + root_children++; + struct node_list *nl = + edge_stack.tail; + + + struct edge_repr *er = + (struct edge_repr *) + nl->content; + + + struct list l; + + + init_list(&l); + + int added_count = 0; + + while (er != 0) { + enqueue_list(&l, er); + + if (component_indexes + [er->from->node_graph_id] + == -1) { + component_indexes + [er->from->node_graph_id] = + component_index; + + sg->size++; + } + + if (component_indexes + [er->to->node_graph_id] + == -1) { + component_indexes + [er->to->node_graph_id] = + component_index; + + sg->size++; + } + + if (!added[er->from->node_graph_id]) { + added_count++; + } + + if (!added[er->to->node_graph_id]) { + added_count++; + } + + nl = nl->prev; + + if ((er->from + == ced->grandparent) + && (er->to + == ced->parent)) { + break; + } + + if (nl != 0) { + er = (struct + edge_repr + *)nl + ->content; + } else { + er = 0; + } + } + + if (!is_empty_list(&l)) { + struct connected_component + *cc = (struct + connected_component + *) + malloc(sizeof( + struct + connected_component)); + + + init_graph(&(cc->g)); + + cc->mapping = (int *)malloc( + sizeof(int) + * added_count); + cc->weights = (int *)malloc( + sizeof(int) + * added_count); + + for (i = 0; + i < added_count; + i++) { + cc->weights[i] = + -1; + } + + cc->cutpoint = 0; + + while (!is_empty_list( + &l)) { + struct edge_repr *er_i = + (struct + edge_repr + *) + pop_list( + &l); + + + added[er_i->from + ->node_graph_id] = + false; + added[er_i->to->node_graph_id] = + false; + + int f = 0, + t = 0; + + add_edge_graph_return_node_indexes( + &(cc->g), + er_i->from + ->name, + er_i->to->name, + er_i->value, + 0, &f, + &t); + + cc->mapping[f] = + er_i->from + ->node_graph_id; + cc->mapping[t] = + er_i->to->node_graph_id; + + free(er_i); + } + + enqueue_list( + &(sg->connected_components), + cc); + } + } + + free(ced_to_clear); + } + } + + if (root_children > 1) { + is_articulation_point[ng->node_graph_id] = true; + } + + enqueue_list(connected_components_subgraph, sg); + + sg = init_sub_graph(); + + component_index++; + clear_list(&edge_stack); + clear_list(&s); + } + } + + if (sg->size == 0) { + free(sg); + } + + free(added); + free(low); + free(discovery); + free(visited); + + return connected_components_subgraph; } - diff --git a/graph-parser_c/src/brandes.c b/graph-parser_c/src/brandes.c index e9d020c..79dd417 100644 --- a/graph-parser_c/src/brandes.c +++ b/graph-parser_c/src/brandes.c @@ -14,7 +14,7 @@ #include "network_change.h" // parameter initialization -bool multithread = false; +bool multithread = false; bool stop_computing_if_unchanged = false; /** @@ -37,13 +37,14 @@ bool use_heu_on_single_biconnected = true; float decimal_places = 1000000000; -static inline double round_decimal(double d){ - return roundf(d*decimal_places)/decimal_places; +static inline double round_decimal(double d) +{ + return roundf(d * decimal_places) / decimal_places; } const int INFINITY_DIST = INT_MAX; -double scale=1; +double scale = 1; /** * This function implements brandes algorithm. Given a weighted graph, @@ -58,295 +59,317 @@ double scale=1; * of nodes. * @param endpoints Whether we want to include endpoints in final value. It is * always used as true when calling this function with next parameter null. - * @param articulation_point_val It is either 0 or an array of integers. In first case, + * @param articulation_point_val It is either 0 or an array of integers. In + * first case, * normal brandes algorithm run, in second the heuristic one, considering * intra-component and inter-component traffic * @return An array with betwenness centrality for each node */ -double * betweeness_brandes(struct graph *g, - bool endpoints, - int * articulation_point_val, - bool normalized) +double *betweeness_brandes(struct graph *g, bool endpoints, + int *articulation_point_val, bool normalized) { - struct priority_queue q; - - - struct list S; - - - init_priority_queue(&q); - init_list(&S); - - int i; - int node_num = g->nodes.size; - double *dist = (double *) calloc(node_num, sizeof(double)); - if (dist == NULL) { - perror("brandes"); - exit(EXIT_FAILURE); - } - struct list *pred = (struct list*) calloc(node_num, sizeof(struct list)); - if (pred == NULL) { - perror("brandes"); - exit(EXIT_FAILURE); - } - int *sigma = (int *) calloc(node_num, sizeof(int)); - if (sigma == NULL) { - perror("brandes"); - exit(EXIT_FAILURE); - } - double *delta = (double *) calloc(node_num, sizeof(double)); - if (delta == NULL) { - perror("brandes"); - exit(EXIT_FAILURE); - } - - //result - double *ret_val = (double *) calloc(node_num, sizeof(double)); - if (ret_val == NULL) { - perror("brandes"); - exit(EXIT_FAILURE); - } - - for (i = 0; i < node_num; i++) - { - ret_val[i] = 0; - - init_list(pred + i); - } - - struct node_list *n = 0; - - - for (n = g -> nodes.head; n != 0; n = n -> next) - { - struct node_graph *s = (struct node_graph*) n -> content; - - - for (i = 0; i < node_num; i++) - { - clear_list(pred + i); - - dist[i] = INFINITY_DIST; - sigma[i] = 0; - delta[i] = 0; - } - - dist[s -> node_graph_id] = 0; - sigma[s -> node_graph_id] = 1; - - insert_priority_queue(&q, (void *) s, 0); - - /** - * weighted shortest path (dijkstra) - */ - while (!is_empty_priority_queue(&q)) - { - struct node_graph *v = (struct node_graph*) dequeue_priority_queue(&q); - - - enqueue_list(&S, v); - - if (v -> neighbours.size > 0) - { - struct node_list *edge_iterator; - - - for (edge_iterator = v -> neighbours.head; edge_iterator != 0; edge_iterator = edge_iterator -> next) - { - struct edge_graph *edge = (struct edge_graph*) edge_iterator -> content; - - - struct node_graph *w = edge -> to; - - - double weight = edge -> value; - - if (dist[w -> node_graph_id] > (dist[v -> node_graph_id] + weight)) - { - dist[w -> node_graph_id] = dist[v -> node_graph_id] + weight; - - insert_or_update_priority_queue(&q, w, dist[w -> node_graph_id]); - - sigma[w -> node_graph_id] = 0; - - clear_list(pred + w -> node_graph_id); - } - - if (dist[w -> node_graph_id] == (dist[v -> node_graph_id] + weight)) - { - sigma[w -> node_graph_id] += sigma[v -> node_graph_id]; - - enqueue_list(pred + w -> node_graph_id, v); - } - } - } - } - - // accumulation - if (articulation_point_val != 0) - { // traffic_matrix!=0){ - // endpoints included by default - while (!is_empty_list(&S)) - { - struct node_graph *w = (struct node_graph*) pop_list(&S); + struct priority_queue q; - // double communication_intensity=(double)traffic_matrix[w->node_graph_id][s->node_graph_id]; - // new - int new_val = 0; + struct list S; - if (w -> node_graph_id != s -> node_graph_id) - { - int w_val = articulation_point_val[w -> node_graph_id], - s_val = articulation_point_val[s -> node_graph_id]; - if ((w_val == 0) && (s_val == 0)) - { - new_val = 1; - } - else if ((w_val > 0) && (s_val > 0)) - { - new_val = w_val * s_val; - } - else if (w_val > 0) - { - new_val = w_val; - } - else - { - new_val = s_val; - } + init_priority_queue(&q); + init_list(&S); - } + int i; + int node_num = g->nodes.size; + double *dist = (double *)calloc(node_num, sizeof(double)); + if (dist == NULL) { + perror("brandes"); + exit(EXIT_FAILURE); + } + struct list *pred = + (struct list *)calloc(node_num, sizeof(struct list)); + if (pred == NULL) { + perror("brandes"); + exit(EXIT_FAILURE); + } + int *sigma = (int *)calloc(node_num, sizeof(int)); + if (sigma == NULL) { + perror("brandes"); + exit(EXIT_FAILURE); + } + double *delta = (double *)calloc(node_num, sizeof(double)); + if (delta == NULL) { + perror("brandes"); + exit(EXIT_FAILURE); + } - double communication_intensity = (double) new_val; + // result + double *ret_val = (double *)calloc(node_num, sizeof(double)); + if (ret_val == NULL) { + perror("brandes"); + exit(EXIT_FAILURE); + } - ret_val[s -> node_graph_id] += communication_intensity; - struct node_list *node_iterator; + for (i = 0; i < node_num; i++) { + ret_val[i] = 0; + init_list(pred + i); + } - for (node_iterator = pred[w -> node_graph_id].head; node_iterator != 0; - node_iterator = node_iterator -> next) - { - struct node_graph *v = (struct node_graph*) node_iterator -> content; + struct node_list *n = 0; - delta[v -> node_graph_id] += ((delta[w -> node_graph_id] + communication_intensity) - * (((double) sigma[v -> node_graph_id]) - / ((double) sigma[w -> node_graph_id]))); - } + for (n = g->nodes.head; n != 0; n = n->next) { + struct node_graph *s = (struct node_graph *)n->content; + + + for (i = 0; i < node_num; i++) { + clear_list(pred + i); + + dist[i] = INFINITY_DIST; + sigma[i] = 0; + delta[i] = 0; + } + dist[s->node_graph_id] = 0; + sigma[s->node_graph_id] = 1; - if (w != s) - { - ret_val[w -> node_graph_id] += delta[w -> node_graph_id] + communication_intensity; - } - } - } - else if (endpoints) - { - ret_val[s -> node_graph_id] += (S.size - 1); + insert_priority_queue(&q, (void *)s, 0); - while (!is_empty_list(&S)) - { - struct node_graph *w = (struct node_graph*) pop_list(&S); - struct node_list *node_iterator; + /** + * weighted shortest path (dijkstra) + */ + while (!is_empty_priority_queue(&q)) { + struct node_graph *v = + (struct node_graph *)dequeue_priority_queue(&q); - double coeff = (1 + delta[w -> node_graph_id]) / ((double) sigma[w -> node_graph_id]); + enqueue_list(&S, v); + + if (v->neighbours.size > 0) { + struct node_list *edge_iterator; + + + for (edge_iterator = v->neighbours.head; + edge_iterator != 0; + edge_iterator = edge_iterator->next) { + struct edge_graph *edge = + (struct edge_graph *) + edge_iterator->content; + + + struct node_graph *w = edge->to; + + + double weight = edge->value; + + if (dist[w->node_graph_id] + > (dist[v->node_graph_id] + + weight)) { + dist[w->node_graph_id] = + dist[v->node_graph_id] + + weight; + + insert_or_update_priority_queue( + &q, w, + dist[w->node_graph_id]); + + sigma[w->node_graph_id] = 0; + + clear_list(pred + + w->node_graph_id); + } + + if (dist[w->node_graph_id] + == (dist[v->node_graph_id] + + weight)) { + sigma[w->node_graph_id] += + sigma[v->node_graph_id]; + + enqueue_list( + pred + w->node_graph_id, + v); + } + } + } + } + + // accumulation + if (articulation_point_val != 0) { // traffic_matrix!=0){ + // endpoints included by default + while (!is_empty_list(&S)) { + struct node_graph *w = + (struct node_graph *)pop_list(&S); + + + // double + // communication_intensity=(double)traffic_matrix[w->node_graph_id][s->node_graph_id]; + // new + int new_val = 0; - for (node_iterator = pred[w -> node_graph_id].head; node_iterator != 0; - node_iterator = node_iterator -> next) - { - struct node_graph *v = (struct node_graph*) node_iterator -> content; - delta[v -> node_graph_id] += ((double) sigma[v -> node_graph_id]) * coeff; - } - - if (w != s) - { - ret_val[w -> node_graph_id] += delta[w -> node_graph_id] + 1; - } - } - } - else - { - while (!is_empty_list(&S)) - { - struct node_graph *w = (struct node_graph*) pop_list(&S); - - - struct node_list *node_iterator; - - - for (node_iterator = pred[w -> node_graph_id].head; node_iterator != 0; - node_iterator = node_iterator -> next) - { - struct node_graph *v = (struct node_graph*) node_iterator -> content; - - - delta[v -> node_graph_id] = delta[v -> node_graph_id] - + ((((double) sigma[v -> node_graph_id]) - / ((double) sigma[w -> node_graph_id])) * (1 - + delta[w -> node_graph_id])); - } - - if (w != s) - { - ret_val[w -> node_graph_id] = ret_val[w -> node_graph_id] + delta[w -> node_graph_id]; - } - } - } - } - - free(dist); - - for (i = 0; i < node_num; i++) - { - clear_list(&pred[i]); - } - - // clear_list(pred); - free(pred); - free(sigma); - free(delta); - struct node_list *nl = g -> nodes.head; - - if((normalized==true)&&(endpoints==true)) - scale = 1 / (((double) (node_num - 1)) * ((double) (node_num - 2))); - - else if((normalized==true)&&(endpoints==false)) - scale = 1 / (((double) (node_num)) * ((double) (node_num - 1))); - - else - scale = 0.5; - - if ((node_num > 2) && (articulation_point_val == 0)) - { - for (i = 0; i < node_num; i++) - { - struct node_graph *ng = (struct node_graph*) nl -> content; - - - ret_val[ng -> node_graph_id] *= scale; - nl = nl -> next; - } - } - - return ret_val; + if (w->node_graph_id != s->node_graph_id) { + int w_val = articulation_point_val + [w->node_graph_id], + s_val = articulation_point_val + [s->node_graph_id]; + + if ((w_val == 0) && (s_val == 0)) { + new_val = 1; + } else if ((w_val > 0) && (s_val > 0)) { + new_val = w_val * s_val; + } else if (w_val > 0) { + new_val = w_val; + } else { + new_val = s_val; + } + } + + double communication_intensity = + (double)new_val; + + ret_val[s->node_graph_id] += + communication_intensity; + struct node_list *node_iterator; + + + for (node_iterator = + pred[w->node_graph_id].head; + node_iterator != 0; + node_iterator = node_iterator->next) { + struct node_graph *v = + (struct node_graph *) + node_iterator->content; + + + delta[v->node_graph_id] += + ((delta[w->node_graph_id] + + communication_intensity) + * (((double)sigma + [v->node_graph_id]) + / ((double)sigma + [w->node_graph_id]))); + } + + + if (w != s) { + ret_val[w->node_graph_id] += + delta[w->node_graph_id] + + communication_intensity; + } + } + } else if (endpoints) { + ret_val[s->node_graph_id] += (S.size - 1); + + while (!is_empty_list(&S)) { + struct node_graph *w = + (struct node_graph *)pop_list(&S); + struct node_list *node_iterator; + + + double coeff = + (1 + delta[w->node_graph_id]) + / ((double)sigma[w->node_graph_id]); + + for (node_iterator = + pred[w->node_graph_id].head; + node_iterator != 0; + node_iterator = node_iterator->next) { + struct node_graph *v = + (struct node_graph *) + node_iterator->content; + delta[v->node_graph_id] += + ((double)sigma + [v->node_graph_id]) + * coeff; + } + + if (w != s) { + ret_val[w->node_graph_id] += + delta[w->node_graph_id] + 1; + } + } + } else { + while (!is_empty_list(&S)) { + struct node_graph *w = + (struct node_graph *)pop_list(&S); + + + struct node_list *node_iterator; + + + for (node_iterator = + pred[w->node_graph_id].head; + node_iterator != 0; + node_iterator = node_iterator->next) { + struct node_graph *v = + (struct node_graph *) + node_iterator->content; + + + delta[v->node_graph_id] = + delta[v->node_graph_id] + + ((((double)sigma + [v->node_graph_id]) + / ((double)sigma + [w->node_graph_id])) + * (1 + + delta[w->node_graph_id])); + } + + if (w != s) { + ret_val[w->node_graph_id] = + ret_val[w->node_graph_id] + + delta[w->node_graph_id]; + } + } + } + } + + free(dist); + + for (i = 0; i < node_num; i++) { + clear_list(&pred[i]); + } + + // clear_list(pred); + free(pred); + free(sigma); + free(delta); + struct node_list *nl = g->nodes.head; + + if ((normalized == true) && (endpoints == true)) + scale = 1 + / (((double)(node_num - 1)) * ((double)(node_num - 2))); + + else if ((normalized == true) && (endpoints == false)) + scale = 1 / (((double)(node_num)) * ((double)(node_num - 1))); + + else + scale = 0.5; + + if ((node_num > 2) && (articulation_point_val == 0)) { + for (i = 0; i < node_num; i++) { + struct node_graph *ng = + (struct node_graph *)nl->content; + + + ret_val[ng->node_graph_id] *= scale; + nl = nl->next; + } + } + + return ret_val; } // normal defines whether the pair is (B,v) (if false, (v,B)). Since ordering // would be a major computational effort, a boolean ("normal") indicates that. -struct cc_node_edge -{ - struct connected_component *from; +struct cc_node_edge { + struct connected_component *from; - struct node_graph *to; + struct node_graph *to; - int * weight; - bool normal; + int *weight; + bool normal; }; @@ -358,23 +381,23 @@ struct cc_node_edge * @param to The node (in the connected component) originating the link * @param weight The edge weight * @param normal Whether in the computation represent a normal couple - * node-component or a component-node one. Used in compute_component_tree_weights + * node-component or a component-node one. Used in + * compute_component_tree_weights * @return the newly created link */ struct cc_node_edge *init_cc_node_edge(struct connected_component *from, - struct node_graph *to, - int * weight, - bool normal) + struct node_graph *to, int *weight, + bool normal) { - struct cc_node_edge *cne = (struct cc_node_edge*) - malloc(sizeof(struct cc_node_edge)); - cne -> from = from; - cne -> to = to; - cne -> weight = weight; - cne -> normal = normal; + struct cc_node_edge *cne = + (struct cc_node_edge *)malloc(sizeof(struct cc_node_edge)); + cne->from = from; + cne->to = to; + cne->weight = weight; + cne->normal = normal; - return cne; + return cne; } /** @@ -388,14 +411,14 @@ struct cc_node_edge *init_cc_node_edge(struct connected_component *from, */ struct cc_node_edge *clone_cc_node_edge(struct cc_node_edge *cne) { - struct cc_node_edge *cne_n = (struct cc_node_edge*) - malloc(sizeof(struct cc_node_edge)); - cne_n -> from = cne -> from; - cne_n -> to = cne -> to; - cne_n -> weight = cne -> weight; - cne_n -> normal = cne -> normal; - - return cne_n; + struct cc_node_edge *cne_n = + (struct cc_node_edge *)malloc(sizeof(struct cc_node_edge)); + cne_n->from = cne->from; + cne_n->to = cne->to; + cne_n->weight = cne->weight; + cne_n->normal = cne->normal; + + return cne_n; } /** @@ -413,88 +436,79 @@ struct cc_node_edge *clone_cc_node_edge(struct cc_node_edge *cne) * @return A tree representing the flow of routes */ struct list *connected_components_to_tree(struct graph *g, - struct list *connected_components, - bool * is_articulation_point) + struct list *connected_components, + bool *is_articulation_point) { - struct list *tree_edges = (struct list*) malloc(sizeof(struct list)); + struct list *tree_edges = (struct list *)malloc(sizeof(struct list)); - init_list(tree_edges); - struct node_list *ccs_iterator; + init_list(tree_edges); + struct node_list *ccs_iterator; - struct node_graph **nodes = - (struct node_graph**) - malloc(sizeof(struct node_graph*) * g -> nodes.size); + struct node_graph **nodes = (struct node_graph **)malloc( + sizeof(struct node_graph *) * g->nodes.size); - int i; + int i; - struct node_list *node_iterator = g -> nodes.head; + struct node_list *node_iterator = g->nodes.head; - for (i = 0; i < g -> nodes.size; i++) - { - nodes[i] = (struct node_graph*) node_iterator -> content; - node_iterator = node_iterator -> next; - } + for (i = 0; i < g->nodes.size; i++) { + nodes[i] = (struct node_graph *)node_iterator->content; + node_iterator = node_iterator->next; + } - for (ccs_iterator = connected_components -> head; ccs_iterator != 0; - ccs_iterator = ccs_iterator -> next) - { - struct connected_component *cc = - (struct connected_component*) - ccs_iterator -> content; + for (ccs_iterator = connected_components->head; ccs_iterator != 0; + ccs_iterator = ccs_iterator->next) { + struct connected_component *cc = + (struct connected_component *)ccs_iterator->content; - int art_points = 0; + int art_points = 0; - struct node_graph *ng_cutpoint = 0; + struct node_graph *ng_cutpoint = 0; - int index = -1; + int index = -1; - struct node_list *n; + struct node_list *n; - i = 0; + i = 0; - for (n = cc -> g.nodes.head; n != 0; n = n -> next) - { - if (is_articulation_point[cc -> mapping[i]]) - { - int new_index = cc -> mapping[i]; + for (n = cc->g.nodes.head; n != 0; n = n->next) { + if (is_articulation_point[cc->mapping[i]]) { + int new_index = cc->mapping[i]; - ng_cutpoint = nodes[new_index]; + ng_cutpoint = nodes[new_index]; - art_points++; + art_points++; - index = i; + index = i; - struct cc_node_edge *cne = - init_cc_node_edge - (cc, nodes[new_index], cc -> weights + i, true); + struct cc_node_edge *cne = init_cc_node_edge( + cc, nodes[new_index], cc->weights + i, + true); - enqueue_list(tree_edges, cne); - } + enqueue_list(tree_edges, cne); + } - i++; - } + i++; + } - if (art_points == 1) - { - cc -> cutpoint = ng_cutpoint; - cc -> cutpoint_index = index; - } - else - { - cc -> cutpoint = 0; - cc -> cutpoint_index = -1; - } - } + if (art_points == 1) { + cc->cutpoint = ng_cutpoint; + cc->cutpoint_index = index; + } else { + cc->cutpoint = 0; + cc->cutpoint_index = -1; + } + } - free(nodes); + free(nodes); - return tree_edges; + return tree_edges; } // From http://algo.uni-konstanz.de/publications/pzedb-hsbcc-12.pdf @@ -519,141 +533,138 @@ struct list *connected_components_to_tree(struct graph *g, * connected_components_to_tree * @param v_num number of vertex in the given graph (or connected subgraph) */ -void compute_component_tree_weights(struct graph *g, - struct list *tree_edges, - int v_num) +void compute_component_tree_weights(struct graph *g, struct list *tree_edges, + int v_num) { - struct list q; + struct list q; - init_list(&q); - struct node_list *edge_iterator; + init_list(&q); + struct node_list *edge_iterator; - for (edge_iterator = tree_edges -> head; edge_iterator != 0; - edge_iterator = edge_iterator -> next) - { - struct cc_node_edge *cne = - (struct cc_node_edge*) edge_iterator -> content; + for (edge_iterator = tree_edges->head; edge_iterator != 0; + edge_iterator = edge_iterator->next) { + struct cc_node_edge *cne = + (struct cc_node_edge *)edge_iterator->content; - if (cne -> from -> cutpoint != 0) - { - cne = clone_cc_node_edge(cne); + if (cne->from->cutpoint != 0) { + cne = clone_cc_node_edge(cne); - enqueue_list(&q, cne); - } - } + enqueue_list(&q, cne); + } + } - while (!is_empty_list(&q)) - { - struct cc_node_edge *cne = (struct cc_node_edge*) dequeue_list(&q); + while (!is_empty_list(&q)) { + struct cc_node_edge *cne = + (struct cc_node_edge *)dequeue_list(&q); - if (cne -> normal) - { - int size = cne -> from -> g.nodes.size - 1; + if (cne->normal) { + int size = cne->from->g.nodes.size - 1; - struct node_list *edge_iterator; + struct node_list *edge_iterator; - for (edge_iterator = tree_edges -> head; edge_iterator != 0; - edge_iterator = edge_iterator -> next) - { - struct cc_node_edge *cne_i = - (struct cc_node_edge*) edge_iterator -> content; + for (edge_iterator = tree_edges->head; + edge_iterator != 0; + edge_iterator = edge_iterator->next) { + struct cc_node_edge *cne_i = + (struct cc_node_edge *) + edge_iterator->content; - if ((cne_i -> from == cne -> from) && (*cne_i -> weight) != -1 - && (cne_i -> to != cne -> to)) - { - size += (v_num - (*cne_i -> weight) - 1); - } - } + if ((cne_i->from == cne->from) + && (*cne_i->weight) != -1 + && (cne_i->to != cne->to)) { + size += (v_num - (*cne_i->weight) - 1); + } + } - (*cne -> weight) = size; + (*cne->weight) = size; - int count = 0; + int count = 0; - struct cc_node_edge *t; + struct cc_node_edge *t; - for (edge_iterator = tree_edges -> head; edge_iterator != 0; - edge_iterator = edge_iterator -> next) - { - struct cc_node_edge *cne_i = - (struct cc_node_edge*) edge_iterator -> content; + for (edge_iterator = tree_edges->head; + edge_iterator != 0; + edge_iterator = edge_iterator->next) { + struct cc_node_edge *cne_i = + (struct cc_node_edge *) + edge_iterator->content; - if ((strcmp(cne_i -> to -> name, cne -> to -> name) == 0) - && (*cne_i -> weight) == -1) - { - count++; + if ((strcmp(cne_i->to->name, cne->to->name) + == 0) + && (*cne_i->weight) == -1) { + count++; - t = cne_i; - } - } + t = cne_i; + } + } - if (count == 1) - { - t = clone_cc_node_edge(t); - t -> normal = false; + if (count == 1) { + t = clone_cc_node_edge(t); + t->normal = false; - enqueue_list(&q, t); - } - } - else - { - int size = 0; + enqueue_list(&q, t); + } + } else { + int size = 0; - struct node_list *edge_iterator; + struct node_list *edge_iterator; - for (edge_iterator = tree_edges -> head; edge_iterator != 0; - edge_iterator = edge_iterator -> next) - { - struct cc_node_edge *cne_i = - (struct cc_node_edge*) edge_iterator -> content; + for (edge_iterator = tree_edges->head; + edge_iterator != 0; + edge_iterator = edge_iterator->next) { + struct cc_node_edge *cne_i = + (struct cc_node_edge *) + edge_iterator->content; - if ((strcmp(cne_i -> to -> name, cne -> to -> name) == 0) && - (*cne_i -> weight) != -1 - && (cne_i -> from != cne -> from)) - { - size += (*cne_i -> weight); - } - } + if ((strcmp(cne_i->to->name, cne->to->name) + == 0) + && (*cne_i->weight) != -1 + && (cne_i->from != cne->from)) { + size += (*cne_i->weight); + } + } - (*cne -> weight) = v_num - 1 - size; + (*cne->weight) = v_num - 1 - size; - int count = 0; + int count = 0; - struct cc_node_edge *t; + struct cc_node_edge *t; - for (edge_iterator = tree_edges -> head; edge_iterator != 0; - edge_iterator = edge_iterator -> next) - { - struct cc_node_edge *cne_i = (struct cc_node_edge*) edge_iterator -> content; + for (edge_iterator = tree_edges->head; + edge_iterator != 0; + edge_iterator = edge_iterator->next) { + struct cc_node_edge *cne_i = + (struct cc_node_edge *) + edge_iterator->content; - if ((cne_i -> from == cne -> from) && (*cne_i -> weight) == -1) - { - count++; + if ((cne_i->from == cne->from) + && (*cne_i->weight) == -1) { + count++; - t = cne_i; - } - } + t = cne_i; + } + } - if (count == 1) - { - t = clone_cc_node_edge(t); - t -> normal = true; + if (count == 1) { + t = clone_cc_node_edge(t); + t->normal = true; - enqueue_list(&q, t); - } - } + enqueue_list(&q, t); + } + } - free(cne); - } + free(cne); + } } @@ -671,44 +682,39 @@ void compute_component_tree_weights(struct graph *g, * id is an articulation point * @return The gross centrality of the biconnected component. */ -double * compute_traffic_matrix_and_centrality( - struct connected_component *cc, - int node_num, - bool * is_articulation_point) +double *compute_traffic_matrix_and_centrality(struct connected_component *cc, + int node_num, + bool *is_articulation_point) { - int cc_node_num = cc -> g.nodes.size; - int i; - int * art_point_val = (int *) malloc(sizeof(int) * cc_node_num); - - for (i = 0; i < cc_node_num; i++) - { - int new_i = cc -> mapping[i]; - bool is_i_ap = is_articulation_point[new_i]; - - if (is_i_ap) - { - art_point_val[i] = (node_num - (cc -> weights[i])); - } - else - { - art_point_val[i] = 0; - } - } - - double * ret_val = betweeness_brandes(&(cc -> g), true, art_point_val,false); - - free(art_point_val); - - return ret_val; + int cc_node_num = cc->g.nodes.size; + int i; + int *art_point_val = (int *)malloc(sizeof(int) * cc_node_num); + + for (i = 0; i < cc_node_num; i++) { + int new_i = cc->mapping[i]; + bool is_i_ap = is_articulation_point[new_i]; + + if (is_i_ap) { + art_point_val[i] = (node_num - (cc->weights[i])); + } else { + art_point_val[i] = 0; + } + } + + double *ret_val = + betweeness_brandes(&(cc->g), true, art_point_val, false); + + free(art_point_val); + + return ret_val; } -struct heuristic_cc_args_struct -{ - struct connected_component *cc; - int * node_num; - bool * is_articulation_point; - double * ret_val; - pthread_t t; +struct heuristic_cc_args_struct { + struct connected_component *cc; + int *node_num; + bool *is_articulation_point; + double *ret_val; + pthread_t t; }; @@ -721,16 +727,15 @@ struct heuristic_cc_args_struct * array for betwenness value, reference thread * @return nothing, it respects the typing for a pthread thread */ -void * run_brandes_heu(void * arguments) +void *run_brandes_heu(void *arguments) { - struct heuristic_cc_args_struct *args = - (struct heuristic_cc_args_struct*) arguments; + struct heuristic_cc_args_struct *args = + (struct heuristic_cc_args_struct *)arguments; - args -> ret_val = compute_traffic_matrix_and_centrality(args -> cc, - *args -> node_num, - args -> is_articulation_point); + args->ret_val = compute_traffic_matrix_and_centrality( + args->cc, *args->node_num, args->is_articulation_point); - return 0; + return 0; } /** @@ -754,204 +759,197 @@ void * run_brandes_heu(void * arguments) * @param cc_index an identifier (index) of the connected subgraph */ void compute_heuristic_wo_scale(struct graph *g, - struct list *connected_components, - bool * is_articulation_point, - double * bc, - int * connected_component_index, - int cc_node_num, - int cc_index) + struct list *connected_components, + bool *is_articulation_point, double *bc, + int *connected_component_index, int cc_node_num, + int cc_index) { - int node_num = g -> nodes.size; - int i; - - node_num = cc_node_num; - struct list *tree_edges = - connected_components_to_tree(g, connected_components, is_articulation_point); - - - compute_component_tree_weights(g, tree_edges, node_num); - - i = 0; - struct node_list *graph_iterator; - - - for (graph_iterator = g -> nodes.head; graph_iterator != 0; - graph_iterator = graph_iterator -> next) - { - struct node_graph *n = (struct node_graph*) graph_iterator -> content; - - - if (connected_component_index[i] == cc_index) - { - if (is_articulation_point[i]) - { - struct node_list *tree_edge_iterator; - - - double weight_sum = 0; // -1; - - for (tree_edge_iterator = tree_edges -> head; - tree_edge_iterator != 0; - tree_edge_iterator = tree_edge_iterator -> next) - { - struct cc_node_edge *cne = (struct cc_node_edge*) - tree_edge_iterator -> content; - - - if (cne -> to == n) - { - // give that DBV)+1+DBV(=|V|, BC^inter = (|V|-1+DBV( )*DBV( - // weight_sum+=tree_weights[cne->index]*(node_num-1-tree_weights[cne->index]); - // weight_sum+=(*cne->weight)*(node_num-1-(*cne->weight)); - weight_sum += (*cne -> weight) * - (node_num - 1 - (*cne -> weight)); - - } - } - bc[i] -= weight_sum; - } - } - - i++; - } - - struct node_list *ccs_iterator; - - int bcc_num = connected_components -> size; - - if (multithread && (bcc_num > 1)) - { - struct list meaningful_CC; - init_list(&meaningful_CC); - - for(ccs_iterator=connected_components->head;ccs_iterator!=0; - ccs_iterator=ccs_iterator->next) - { - struct connected_component * cc= - ( struct connected_component *)ccs_iterator->content; - - if(cc->g.nodes.size>2) - { - struct heuristic_cc_args_struct * args= - (struct heuristic_cc_args_struct *) - malloc(sizeof(struct heuristic_cc_args_struct )); - struct connected_component * cc= - ( struct connected_component *)ccs_iterator->content; - args->cc=cc; - args->is_articulation_point=is_articulation_point; - args->node_num=&node_num; - args->ret_val=0; - enqueue_list(&meaningful_CC,(void*)args); - } - - } - - struct node_list * great_cc_iterator=meaningful_CC.head; - - for(;great_cc_iterator!=0;great_cc_iterator=great_cc_iterator->next) - { - struct heuristic_cc_args_struct * args= - (struct heuristic_cc_args_struct *)great_cc_iterator->content; - pthread_create - (&(args->t), NULL, &run_brandes_heu, (void *)(args+i)); - } - - for(ccs_iterator=connected_components->head;ccs_iterator!=0; - ccs_iterator=ccs_iterator->next) - { - struct connected_component * cc= - ( struct connected_component *)ccs_iterator->content; - if(cc->g.nodes.size<=2) - { - double * ret_val= - compute_traffic_matrix_and_centrality - (cc,node_num,is_articulation_point); - int j; - - for(j=0;jg.nodes.size;j++) - { - bc[cc->mapping[j]] += ret_val[j]; - } - - free(ret_val); - } - } - - while(! is_empty_list(&meaningful_CC)) - { - struct heuristic_cc_args_struct * args= - (struct heuristic_cc_args_struct *) - pop_list(&meaningful_CC); - pthread_join(args->t, NULL); - int j; - - for(j=0;jcc->g.nodes.size;j++) - { - bc[args->cc->mapping[j]] += args->ret_val[j]; - } - - free(args->ret_val); - } - } - else - { - for (ccs_iterator = connected_components -> head; ccs_iterator != 0; - ccs_iterator = ccs_iterator -> next) - { - struct connected_component *cc = - (struct connected_component*) ccs_iterator -> content; - - double * partial = compute_traffic_matrix_and_centrality(cc, - node_num, is_articulation_point); - int i; - for (i = 0; i < cc -> g.nodes.size; i++) - { - bc[cc -> mapping[i]] += partial[i]; - } - - free(partial); - } - } - - while (!is_empty_list(tree_edges)) - { - struct cc_node_edge *cne = (struct cc_node_edge*) dequeue_list(tree_edges); - - - free(cne); - } - - free(tree_edges); - - while (!is_empty_list(connected_components)) - { - struct connected_component *cc = - (struct connected_component*) dequeue_list(connected_components); - - - free_graph(&(cc -> g)); - free(cc -> mapping); - free(cc -> weights); - free(cc); - } - - free(connected_components); + int node_num = g->nodes.size; + int i; + + node_num = cc_node_num; + struct list *tree_edges = connected_components_to_tree( + g, connected_components, is_articulation_point); + + + compute_component_tree_weights(g, tree_edges, node_num); + + i = 0; + struct node_list *graph_iterator; + + + for (graph_iterator = g->nodes.head; graph_iterator != 0; + graph_iterator = graph_iterator->next) { + struct node_graph *n = + (struct node_graph *)graph_iterator->content; + + + if (connected_component_index[i] == cc_index) { + if (is_articulation_point[i]) { + struct node_list *tree_edge_iterator; + + + double weight_sum = 0; // -1; + + for (tree_edge_iterator = tree_edges->head; + tree_edge_iterator != 0; + tree_edge_iterator = + tree_edge_iterator->next) { + struct cc_node_edge *cne = + (struct cc_node_edge *) + tree_edge_iterator + ->content; + + + if (cne->to == n) { + // give that DBV)+1+DBV(=|V|, + // BC^inter = (|V|-1+DBV( )*DBV( + // weight_sum+=tree_weights[cne->index]*(node_num-1-tree_weights[cne->index]); + // weight_sum+=(*cne->weight)*(node_num-1-(*cne->weight)); + weight_sum += + (*cne->weight) + * (node_num - 1 + - (*cne->weight)); + } + } + bc[i] -= weight_sum; + } + } + + i++; + } + + struct node_list *ccs_iterator; + + int bcc_num = connected_components->size; + + if (multithread && (bcc_num > 1)) { + struct list meaningful_CC; + init_list(&meaningful_CC); + + for (ccs_iterator = connected_components->head; + ccs_iterator != 0; ccs_iterator = ccs_iterator->next) { + struct connected_component *cc = + (struct connected_component *) + ccs_iterator->content; + + if (cc->g.nodes.size > 2) { + struct heuristic_cc_args_struct *args = + (struct heuristic_cc_args_struct *)malloc( + sizeof(struct + heuristic_cc_args_struct)); + struct connected_component *cc = + (struct connected_component *) + ccs_iterator->content; + args->cc = cc; + args->is_articulation_point = + is_articulation_point; + args->node_num = &node_num; + args->ret_val = 0; + enqueue_list(&meaningful_CC, (void *)args); + } + } + + struct node_list *great_cc_iterator = meaningful_CC.head; + + for (; great_cc_iterator != 0; + great_cc_iterator = great_cc_iterator->next) { + struct heuristic_cc_args_struct *args = + (struct heuristic_cc_args_struct *) + great_cc_iterator->content; + pthread_create(&(args->t), NULL, &run_brandes_heu, + (void *)(args + i)); + } + + for (ccs_iterator = connected_components->head; + ccs_iterator != 0; ccs_iterator = ccs_iterator->next) { + struct connected_component *cc = + (struct connected_component *) + ccs_iterator->content; + if (cc->g.nodes.size <= 2) { + double *ret_val = + compute_traffic_matrix_and_centrality( + cc, node_num, + is_articulation_point); + int j; + + for (j = 0; j < cc->g.nodes.size; j++) { + bc[cc->mapping[j]] += ret_val[j]; + } + + free(ret_val); + } + } + + while (!is_empty_list(&meaningful_CC)) { + struct heuristic_cc_args_struct *args = + (struct heuristic_cc_args_struct *)pop_list( + &meaningful_CC); + pthread_join(args->t, NULL); + int j; + + for (j = 0; j < args->cc->g.nodes.size; j++) { + bc[args->cc->mapping[j]] += args->ret_val[j]; + } + + free(args->ret_val); + } + } else { + for (ccs_iterator = connected_components->head; + ccs_iterator != 0; ccs_iterator = ccs_iterator->next) { + struct connected_component *cc = + (struct connected_component *) + ccs_iterator->content; + + double *partial = compute_traffic_matrix_and_centrality( + cc, node_num, is_articulation_point); + int i; + for (i = 0; i < cc->g.nodes.size; i++) { + bc[cc->mapping[i]] += partial[i]; + } + + free(partial); + } + } + + while (!is_empty_list(tree_edges)) { + struct cc_node_edge *cne = + (struct cc_node_edge *)dequeue_list(tree_edges); + + + free(cne); + } + + free(tree_edges); + + while (!is_empty_list(connected_components)) { + struct connected_component *cc = + (struct connected_component *)dequeue_list( + connected_components); + + + free_graph(&(cc->g)); + free(cc->mapping); + free(cc->weights); + free(cc); + } + + free(connected_components); } -struct multithread_subgraph_struct -{ - struct graph *g; +struct multithread_subgraph_struct { + struct graph *g; - struct list *ccs; + struct list *ccs; - bool * art_point; - double * bc; - int * indexes; - int * size; - int cc_index; - pthread_t t; + bool *art_point; + double *bc; + int *indexes; + int *size; + int cc_index; + pthread_t t; }; @@ -965,20 +963,17 @@ struct multithread_subgraph_struct * current subgraph, the index of it and the current thread. * @return nothing, it respects the typing for a pthread thread */ -void * run_subgraph(void * arguments) +void *run_subgraph(void *arguments) { - struct multithread_subgraph_struct *args = (struct multithread_subgraph_struct*) arguments; + struct multithread_subgraph_struct *args = + (struct multithread_subgraph_struct *)arguments; - compute_heuristic_wo_scale(args -> g, - args -> ccs, - args -> art_point, - args -> bc, - args -> indexes, - (*args -> size), - args -> cc_index); + compute_heuristic_wo_scale(args->g, args->ccs, args->art_point, + args->bc, args->indexes, (*args->size), + args->cc_index); - return 0; + return 0; } /** @@ -990,260 +985,235 @@ void * run_subgraph(void * arguments) * @param recursive whether we want to use the recursive or iterative approach * @return An array with betwenness centrality for each node */ -double * betwenness_heuristic(struct graph *g, - bool recursive) +double *betwenness_heuristic(struct graph *g, bool recursive) { - int node_num = g -> nodes.size; - bool * is_articulation_point = (bool *) malloc(sizeof(bool) * node_num); - int * connected_component_indexes = (int *) malloc(sizeof(int) * node_num); - double * ret_val = (double *) malloc(sizeof(double) * node_num); - int i; - - for (i = 0; i < node_num; i++) - { - ret_val[i] = 0; - connected_component_indexes[i] = -1; - } - - struct list *connected_components_subgraphs; - - - if (!g -> directed) - { - if (recursive) - { - connected_components_subgraphs = - tarjan_rec_undir(g, is_articulation_point, - connected_component_indexes); - } - else - { - connected_components_subgraphs = - tarjan_iter_undir(g, is_articulation_point, - connected_component_indexes); - } - } - else - { - if (recursive) - { - connected_components_subgraphs = - tarjan_rec_dir(g, is_articulation_point, - connected_component_indexes); - } - else - { - connected_components_subgraphs = - tarjan_iter_dir(g, is_articulation_point, - connected_component_indexes); - } - } - - int biconnected_component_num = -1, - result_size = -1; - float standard_deviation_bic = -1; - float standard_deviation_edge = 0; - - if (stop_computing_if_unchanged) - { - int edge_num = 0; - - struct node_list *nl = g -> nodes.head; - - - for (; nl != 0; nl = nl -> next) - { - struct node_graph *ng = (struct node_graph*) nl -> content; - - - struct node_list *nl2 = ng -> neighbours.head; - - - for (; nl2 != 0; nl2 = nl2 -> next) - { - struct edge_graph *eg = (struct edge_graph*) nl2 -> content; - - - standard_deviation_edge += eg -> value; - - edge_num++; - } - } - - standard_deviation_edge /= edge_num; - - // if we rely on old values when network - // is not changed - char ** old_names = 0; - double * old_ret_val = is_network_changed(connected_components_subgraphs, - g -> nodes.size, - &biconnected_component_num, - &standard_deviation_bic, - &standard_deviation_edge, - &result_size, - &old_names); - - if (old_ret_val != 0) - { - // free everything is behind - while (!is_empty_list(connected_components_subgraphs)) - { - struct list *tmp = - (struct list*) dequeue_list(connected_components_subgraphs); - - - while (!is_empty_list(tmp)) - { - struct connected_component *cc = - (struct connected_component*) dequeue_list(tmp); - - - free_graph(&cc -> g); - free(cc -> mapping); - free(cc -> weights); - free(cc); - } - - free(tmp); - } - - clear_list(connected_components_subgraphs); - free(connected_components_subgraphs); - free(is_articulation_point); - free(connected_component_indexes); - - if (node_num == result_size) - { - copy_old_values(old_ret_val, ret_val, old_names, result_size, &g -> nodes); - } - - free(old_ret_val); - - if (old_names != 0) - { - for (i = 0; i < result_size; i++) - { - free(old_names[i]); - } - - free(old_names); - } - - return ret_val; - } + int node_num = g->nodes.size; + bool *is_articulation_point = (bool *)malloc(sizeof(bool) * node_num); + int *connected_component_indexes = + (int *)malloc(sizeof(int) * node_num); + double *ret_val = (double *)malloc(sizeof(double) * node_num); + int i; + + for (i = 0; i < node_num; i++) { + ret_val[i] = 0; + connected_component_indexes[i] = -1; + } + + struct list *connected_components_subgraphs; + + + if (!g->directed) { + if (recursive) { + connected_components_subgraphs = + tarjan_rec_undir(g, is_articulation_point, + connected_component_indexes); + } else { + connected_components_subgraphs = + tarjan_iter_undir(g, is_articulation_point, + connected_component_indexes); + } + } else { + if (recursive) { + connected_components_subgraphs = + tarjan_rec_dir(g, is_articulation_point, + connected_component_indexes); + } else { + connected_components_subgraphs = + tarjan_iter_dir(g, is_articulation_point, + connected_component_indexes); + } + } + + int biconnected_component_num = -1, result_size = -1; + float standard_deviation_bic = -1; + float standard_deviation_edge = 0; + + if (stop_computing_if_unchanged) { + int edge_num = 0; + + struct node_list *nl = g->nodes.head; + + + for (; nl != 0; nl = nl->next) { + struct node_graph *ng = + (struct node_graph *)nl->content; + + + struct node_list *nl2 = ng->neighbours.head; + + + for (; nl2 != 0; nl2 = nl2->next) { + struct edge_graph *eg = + (struct edge_graph *)nl2->content; + + + standard_deviation_edge += eg->value; + + edge_num++; + } + } + + standard_deviation_edge /= edge_num; + + // if we rely on old values when network + // is not changed + char **old_names = 0; + double *old_ret_val = is_network_changed( + connected_components_subgraphs, g->nodes.size, + &biconnected_component_num, &standard_deviation_bic, + &standard_deviation_edge, &result_size, &old_names); + + if (old_ret_val != 0) { + // free everything is behind + while (!is_empty_list(connected_components_subgraphs)) { + struct list *tmp = (struct list *)dequeue_list( + connected_components_subgraphs); + + + while (!is_empty_list(tmp)) { + struct connected_component *cc = + (struct connected_component *) + dequeue_list(tmp); + + + free_graph(&cc->g); + free(cc->mapping); + free(cc->weights); + free(cc); + } + + free(tmp); + } + + clear_list(connected_components_subgraphs); + free(connected_components_subgraphs); + free(is_articulation_point); + free(connected_component_indexes); + + if (node_num == result_size) { + copy_old_values(old_ret_val, ret_val, old_names, + result_size, &g->nodes); + } + + free(old_ret_val); + + if (old_names != 0) { + for (i = 0; i < result_size; i++) { + free(old_names[i]); + } + + free(old_names); + } + + return ret_val; + } + + if (old_names != 0) { + for (i = 0; i < result_size; i++) { + free(old_names[i]); + } + + free(old_names); + } + } - if (old_names != 0) - { - for (i = 0; i < result_size; i++) - { - free(old_names[i]); - } + int connected_component_index = 0; + int cc_num = connected_components_subgraphs->size; - free(old_names); - } - } + if (multithread && (cc_num > 1)) { + i = 0; + struct multithread_subgraph_struct *args = + (struct multithread_subgraph_struct *)malloc( + sizeof(struct multithread_subgraph_struct) + * cc_num); - int connected_component_index = 0; - int cc_num = connected_components_subgraphs -> size; - if (multithread && (cc_num > 1)) - { - i = 0; - struct multithread_subgraph_struct *args = - (struct multithread_subgraph_struct*) - malloc(sizeof(struct multithread_subgraph_struct) * cc_num); + struct node_list *subgraph_iterator = + connected_components_subgraphs->head; - struct node_list *subgraph_iterator = connected_components_subgraphs -> head; + for (; subgraph_iterator != 0; + subgraph_iterator = subgraph_iterator->next) { + struct sub_graph *sg = + (struct sub_graph *)subgraph_iterator->content; + args[i].g = g; + args[i].ccs = &sg->connected_components; + args[i].art_point = is_articulation_point; + args[i].bc = ret_val; + args[i].indexes = connected_component_indexes; + args[i].size = &sg->size; + args[i].cc_index = i; + i++; + } - - for (; subgraph_iterator != 0; subgraph_iterator = subgraph_iterator -> next) - { - struct sub_graph *sg = (struct sub_graph*) subgraph_iterator -> content; - args[i].g = g; - args[i].ccs = &sg -> connected_components; - args[i].art_point = is_articulation_point; - args[i].bc = ret_val; - args[i].indexes = connected_component_indexes; - args[i].size = &sg -> size; - args[i].cc_index = i; - i++; - } - - for (i = 0; i < cc_num; i++) - { - pthread_create(&args[i].t, NULL, &run_subgraph, (void *) (args + i)); - } - - for (i = 0; i < cc_num; i++) - { - pthread_join(args[i].t, NULL); - } - - free(args); - - } - else - { - int bcc_num = ((struct sub_graph *)connected_components_subgraphs->head->content) + for (i = 0; i < cc_num; i++) { + pthread_create(&args[i].t, NULL, &run_subgraph, + (void *)(args + i)); + } + + for (i = 0; i < cc_num; i++) { + pthread_join(args[i].t, NULL); + } + + free(args); + + } else { + int bcc_num = + ((struct sub_graph *) + connected_components_subgraphs->head->content) ->connected_components.size; - if( cc_num>1|| use_heu_on_single_biconnected || bcc_num > 1) - { - struct node_list *subgraph_iterator = connected_components_subgraphs -> head; - - - for (; subgraph_iterator != 0; subgraph_iterator = subgraph_iterator -> next) - { - struct sub_graph *sg = (struct sub_graph*) subgraph_iterator -> content; - - - compute_heuristic_wo_scale(g, - &(sg -> connected_components), - is_articulation_point, - ret_val, - connected_component_indexes, - sg -> size, - connected_component_index++); - } - } - else - { - clear_list(connected_components_subgraphs); - free(connected_components_subgraphs); - free(is_articulation_point); - free(connected_component_indexes); - free(ret_val); - - return betweeness_brandes(g, true, 0,false); - } - } - - if (node_num > 2) - { - //double scale = 1 / (((double) (node_num - 1)) * ((double) (node_num - 2))); - - for (i = 0; i < node_num; i++) - { - ret_val[i] *= scale; - - // ret_val[i]=round_decimal(ret_val[i]); - } - } - - // if we are storing values for next computation - if (stop_computing_if_unchanged) { - write_file(biconnected_component_num, - standard_deviation_bic, - standard_deviation_edge, - node_num, - ret_val, - &g -> nodes); - } - - clear_list(connected_components_subgraphs); - free(connected_components_subgraphs); - free(is_articulation_point); - free(connected_component_indexes); - - return ret_val; + if (cc_num > 1 || use_heu_on_single_biconnected + || bcc_num > 1) { + struct node_list *subgraph_iterator = + connected_components_subgraphs->head; + + + for (; subgraph_iterator != 0; + subgraph_iterator = subgraph_iterator->next) { + struct sub_graph *sg = + (struct sub_graph *) + subgraph_iterator->content; + + + compute_heuristic_wo_scale( + g, &(sg->connected_components), + is_articulation_point, ret_val, + connected_component_indexes, sg->size, + connected_component_index++); + } + } else { + clear_list(connected_components_subgraphs); + free(connected_components_subgraphs); + free(is_articulation_point); + free(connected_component_indexes); + free(ret_val); + + return betweeness_brandes(g, true, 0, false); + } + } + + if (node_num > 2) { + // double scale = 1 / (((double) (node_num - 1)) * ((double) + // (node_num - 2))); + + for (i = 0; i < node_num; i++) { + ret_val[i] *= scale; + + // ret_val[i]=round_decimal(ret_val[i]); + } + } + + // if we are storing values for next computation + if (stop_computing_if_unchanged) { + write_file(biconnected_component_num, standard_deviation_bic, + standard_deviation_edge, node_num, ret_val, + &g->nodes); + } + + clear_list(connected_components_subgraphs); + free(connected_components_subgraphs); + free(is_articulation_point); + free(connected_component_indexes); + + return ret_val; } diff --git a/graph-parser_c/src/graph/graph.c b/graph-parser_c/src/graph/graph.c index 0ddd75d..c510d45 100644 --- a/graph-parser_c/src/graph/graph.c +++ b/graph-parser_c/src/graph/graph.c @@ -1,15 +1,11 @@ #include "graph/graph.h" -void init_node_graph(node_graph_t n, - const char * name, - int node_graph_id); +void init_node_graph(node_graph_t n, const char *name, int node_graph_id); void init_edge_graph(edge_graph_t e); -void init_edge_graph_params(edge_graph_t e, - node_graph_t to, - double value); +void init_edge_graph_params(edge_graph_t e, node_graph_t to, double value); /** * Constructor of a graph. Given a graph, it initialities its parameter. @@ -20,9 +16,9 @@ void init_edge_graph_params(edge_graph_t e, */ void init_graph(graph_t g) { - init_list(&(g -> nodes)); + init_list(&(g->nodes)); - g -> directed = true; + g->directed = true; } /** @@ -35,15 +31,16 @@ void init_graph(graph_t g) * @param name The name of the node that will be added * @return The created and added node */ -node_graph_t add_node_graph(graph_t g, const char * name) { - // uniqueness check not performed - node_graph_t n = (node_graph_t) malloc(NODE_GRAPH_SIZE); +node_graph_t add_node_graph(graph_t g, const char *name) +{ + // uniqueness check not performed + node_graph_t n = (node_graph_t)malloc(NODE_GRAPH_SIZE); - init_node_graph(n, name, g->nodes.size); - enqueue_list(&(g -> nodes), (void *) n); + init_node_graph(n, name, g->nodes.size); + enqueue_list(&(g->nodes), (void *)n); - return n; + return n; } /** @@ -62,61 +59,64 @@ node_graph_t add_node_graph(graph_t g, const char * name) { * @param value The edge weight * @param directed Whether the edge is directed or not */ -void add_edge_graph(graph_t g, - const char * name_from, - const char * name_to, - double value, - bool directed) { - g -> directed = g -> directed && directed; - node_graph_t from = INVALID_NODE_GRAPH, - to = INVALID_NODE_GRAPH, - current = INVALID_NODE_GRAPH; +void add_edge_graph(graph_t g, const char *name_from, const char *name_to, + double value, bool directed) +{ + g->directed = g->directed && directed; + node_graph_t from = INVALID_NODE_GRAPH, to = INVALID_NODE_GRAPH, + current = INVALID_NODE_GRAPH; - struct node_list *n = g -> nodes.head; + struct node_list *n = g->nodes.head; - while ((n != NULL) && ((from == INVALID_NODE_GRAPH) || (to == INVALID_NODE_GRAPH))) - { // if there are no more nodes or we have found both edge ends - current = (node_graph_t) n->content; + while ((n != NULL) + && ((from == INVALID_NODE_GRAPH) + || (to == INVALID_NODE_GRAPH))) { // if there are no more + // nodes or we have found + // both edge ends + current = (node_graph_t)n->content; - if ((from == INVALID_NODE_GRAPH) && (strcmp(current->name, name_from) == 0)) { - from = current; - } + if ((from == INVALID_NODE_GRAPH) + && (strcmp(current->name, name_from) == 0)) { + from = current; + } - if ((to == INVALID_NODE_GRAPH) && (strcmp(current->name, name_to) == 0)) { - to = current; - } + if ((to == INVALID_NODE_GRAPH) + && (strcmp(current->name, name_to) == 0)) { + to = current; + } - n = n->next; - } + n = n->next; + } - if (from == INVALID_NODE_GRAPH) { - from = add_node_graph(g, name_from); + if (from == INVALID_NODE_GRAPH) { + from = add_node_graph(g, name_from); - if (strcmp(name_from, name_to) == 0) { - to = from; - } - } + if (strcmp(name_from, name_to) == 0) { + to = from; + } + } - if (to == INVALID_NODE_GRAPH) { - to = add_node_graph(g, name_to); - } + if (to == INVALID_NODE_GRAPH) { + to = add_node_graph(g, name_to); + } - if ((from != INVALID_NODE_GRAPH) && (to != INVALID_NODE_GRAPH)) { - edge_graph_t e = (edge_graph_t) malloc(EDGE_GRAPH_SIZE); + if ((from != INVALID_NODE_GRAPH) && (to != INVALID_NODE_GRAPH)) { + edge_graph_t e = (edge_graph_t)malloc(EDGE_GRAPH_SIZE); - init_edge_graph_params(e, to, value); - enqueue_list(&(from -> neighbours), (void *) e); + init_edge_graph_params(e, to, value); + enqueue_list(&(from->neighbours), (void *)e); - if (!directed) { - edge_graph_t e_r = (edge_graph_t) malloc(EDGE_GRAPH_SIZE); + if (!directed) { + edge_graph_t e_r = + (edge_graph_t)malloc(EDGE_GRAPH_SIZE); - init_edge_graph_params(e_r, from, value); - enqueue_list(&(to -> neighbours), (void *) e_r); - } - } + init_edge_graph_params(e_r, from, value); + enqueue_list(&(to->neighbours), (void *)e_r); + } + } } /** @@ -132,70 +132,70 @@ void add_edge_graph(graph_t g, * @param nodefrom An integer pointer used to return the id of @name_from node * @param nodeto An integer pointer used to return the id of @name_to node */ -void add_edge_graph_return_node_indexes(graph_t g, - const char * name_from, - const char * name_to, - double value, - bool directed, - int * nodefrom, - int * nodeto) +void add_edge_graph_return_node_indexes(graph_t g, const char *name_from, + const char *name_to, double value, + bool directed, int *nodefrom, + int *nodeto) { - node_graph_t from = INVALID_NODE_GRAPH, - to = INVALID_NODE_GRAPH, - current = INVALID_NODE_GRAPH; + node_graph_t from = INVALID_NODE_GRAPH, to = INVALID_NODE_GRAPH, + current = INVALID_NODE_GRAPH; - struct node_list *n = g -> nodes.head; + struct node_list *n = g->nodes.head; - while ((n != 0) && ((from == INVALID_NODE_GRAPH) || (to == INVALID_NODE_GRAPH))) { - // if there are no more nodes or we have found both edge ends - current = (node_graph_t) n -> content; + while ((n != 0) && ((from == INVALID_NODE_GRAPH) + || (to == INVALID_NODE_GRAPH))) { + // if there are no more nodes or we have found both edge ends + current = (node_graph_t)n->content; - if ((from == INVALID_NODE_GRAPH) && (strcmp(current -> name, name_from) == 0)) { - from = current; - } + if ((from == INVALID_NODE_GRAPH) + && (strcmp(current->name, name_from) == 0)) { + from = current; + } - if ((to == INVALID_NODE_GRAPH) && (strcmp(current -> name, name_to) == 0)) { - to = current; - } + if ((to == INVALID_NODE_GRAPH) + && (strcmp(current->name, name_to) == 0)) { + to = current; + } - n = n -> next; - } + n = n->next; + } - if (from == INVALID_NODE_GRAPH) { - from = add_node_graph(g, name_from); + if (from == INVALID_NODE_GRAPH) { + from = add_node_graph(g, name_from); - if (strcmp(name_from, name_to) == 0) { - to = from; - } - } + if (strcmp(name_from, name_to) == 0) { + to = from; + } + } - if (to == INVALID_NODE_GRAPH) { - to = add_node_graph(g, name_to); - } + if (to == INVALID_NODE_GRAPH) { + to = add_node_graph(g, name_to); + } - if ((from != INVALID_NODE_GRAPH) && (to != INVALID_NODE_GRAPH)) { - if (nodefrom != 0) { - (*nodefrom) = from->node_graph_id; - } + if ((from != INVALID_NODE_GRAPH) && (to != INVALID_NODE_GRAPH)) { + if (nodefrom != 0) { + (*nodefrom) = from->node_graph_id; + } - if (nodeto != INVALID_NODE_GRAPH) { - (*nodeto) = to->node_graph_id; - } + if (nodeto != INVALID_NODE_GRAPH) { + (*nodeto) = to->node_graph_id; + } - edge_graph_t e = (edge_graph_t) malloc(EDGE_GRAPH_SIZE); + edge_graph_t e = (edge_graph_t)malloc(EDGE_GRAPH_SIZE); - init_edge_graph_params(e, to, value); - enqueue_list(&(from -> neighbours), (void *) e); + init_edge_graph_params(e, to, value); + enqueue_list(&(from->neighbours), (void *)e); - if (!directed) { - edge_graph_t e_r = (edge_graph_t) malloc(EDGE_GRAPH_SIZE); - init_edge_graph_params(e_r, from, value); - enqueue_list(&(to -> neighbours), (void *) e_r); - } - } + if (!directed) { + edge_graph_t e_r = + (edge_graph_t)malloc(EDGE_GRAPH_SIZE); + init_edge_graph_params(e_r, from, value); + enqueue_list(&(to->neighbours), (void *)e_r); + } + } } /** @@ -204,31 +204,32 @@ void add_edge_graph_return_node_indexes(graph_t g, * * @param g A graph */ -void print_graph(graph_t g) { - struct node_list *nq = g -> nodes.head; +void print_graph(graph_t g) +{ + struct node_list *nq = g->nodes.head; - while (nq != 0) { - node_graph_t ng = (node_graph_t) nq->content; + while (nq != 0) { + node_graph_t ng = (node_graph_t)nq->content; - struct node_list *nqi = ng->neighbours.head; + struct node_list *nqi = ng->neighbours.head; - fprintf(stdout, "%s (%d) [", ng->name, ng->node_graph_id); + fprintf(stdout, "%s (%d) [", ng->name, ng->node_graph_id); - while (nqi != 0) { - edge_graph_t eg = (edge_graph_t) nqi->content; + while (nqi != 0) { + edge_graph_t eg = (edge_graph_t)nqi->content; - fprintf(stdout, " (%s , %f) ", eg->to->name, eg->value); + fprintf(stdout, " (%s , %f) ", eg->to->name, eg->value); - nqi = nqi->next; - } + nqi = nqi->next; + } - printf("]\n"); + printf("]\n"); - nq = nq->next; - } + nq = nq->next; + } } /** @@ -240,15 +241,13 @@ void print_graph(graph_t g) { * @param name The name that identifies a node. Must be unique * @param node_graph_id The numeric id of the new node. */ -void init_node_graph(node_graph_t n, - const char * name, - int node_graph_id) +void init_node_graph(node_graph_t n, const char *name, int node_graph_id) { - n -> name = strdup((const char *) name); + n->name = strdup((const char *)name); - init_list(&(n->neighbours)); + init_list(&(n->neighbours)); - n->node_graph_id = node_graph_id; + n->node_graph_id = node_graph_id; } /** @@ -258,8 +257,9 @@ void init_node_graph(node_graph_t n, * * @param e An edge */ -void init_edge_graph(edge_graph_t e) { - memset(e, 0, EDGE_GRAPH_SIZE); +void init_edge_graph(edge_graph_t e) +{ + memset(e, 0, EDGE_GRAPH_SIZE); } /** @@ -271,12 +271,10 @@ void init_edge_graph(edge_graph_t e) { * @param to The node that is the vertex of the edge * @param value The value of the link */ -void init_edge_graph_params(edge_graph_t e, - node_graph_t to, - double value) +void init_edge_graph_params(edge_graph_t e, node_graph_t to, double value) { - e->to = to; - e->value = value; + e->to = to; + e->value = value; } /** @@ -286,27 +284,28 @@ void init_edge_graph_params(edge_graph_t e, * @param g A graph to be deallocated. * */ -void free_graph(graph_t g) { - struct node_list *nq = g -> nodes.head; - - while (nq != 0) { - struct node_list *nq_tmp = nq; - - - node_graph_t ng = (node_graph_t) nq->content; - struct node_list *eq = (struct node_list*) ng->neighbours.head; - struct node_list *eq_tmp; - - while (eq != 0) { - eq_tmp = eq; - edge_graph_t e = (edge_graph_t) eq->content; - eq = eq->next; - free(eq_tmp); - free(e); - } - nq = nq->next; - free(ng->name); - free(ng); - free(nq_tmp); - } +void free_graph(graph_t g) +{ + struct node_list *nq = g->nodes.head; + + while (nq != 0) { + struct node_list *nq_tmp = nq; + + + node_graph_t ng = (node_graph_t)nq->content; + struct node_list *eq = (struct node_list *)ng->neighbours.head; + struct node_list *eq_tmp; + + while (eq != 0) { + eq_tmp = eq; + edge_graph_t e = (edge_graph_t)eq->content; + eq = eq->next; + free(eq_tmp); + free(e); + } + nq = nq->next; + free(ng->name); + free(ng); + free(nq_tmp); + } } diff --git a/graph-parser_c/src/graph/list.c b/graph-parser_c/src/graph/list.c index d465a8c..7d5882c 100644 --- a/graph-parser_c/src/graph/list.c +++ b/graph-parser_c/src/graph/list.c @@ -9,9 +9,9 @@ */ void init_list(struct list *q) { - q -> head = 0; - q -> tail = 0; - q -> size = 0; + q->head = 0; + q->tail = 0; + q->size = 0; } /** @@ -20,31 +20,28 @@ void init_list(struct list *q) * @param q The list or queue we want to expand with a new element * @param item The new item that will be inserted at the end. */ -void enqueue_list(struct list *q, - void * item) +void enqueue_list(struct list *q, void *item) { - struct node_list *n = (struct node_list*) malloc(sizeof(struct node_list)); - - - n -> content = item; - - if (q -> head == 0) - { - n -> prev = 0; - n -> next = 0; - q -> head = n; - q -> size = 1; - q -> tail = n; - } - else - { - n -> prev = q -> tail; - q -> tail -> next = n; - n -> next = 0; - q -> tail = n; - - q -> size++; - } + struct node_list *n = + (struct node_list *)malloc(sizeof(struct node_list)); + + + n->content = item; + + if (q->head == 0) { + n->prev = 0; + n->next = 0; + q->head = n; + q->size = 1; + q->tail = n; + } else { + n->prev = q->tail; + q->tail->next = n; + n->next = 0; + q->tail = n; + + q->size++; + } } /** @@ -55,33 +52,29 @@ void enqueue_list(struct list *q, * @return The first element in the queue, if present. * Null pointer if no one present. */ -void * dequeue_list(struct list *q) +void *dequeue_list(struct list *q) { - void * ret_val = 0; + void *ret_val = 0; - if (q -> head != 0) - { - struct node_list *to_remove = q -> head; + if (q->head != 0) { + struct node_list *to_remove = q->head; - ret_val = q -> head -> content; + ret_val = q->head->content; - if (q -> head -> next == 0) - { - q -> head = 0; - q -> tail = 0; - } - else - { - q -> head = q -> head -> next; - q -> head -> prev = 0; - } + if (q->head->next == 0) { + q->head = 0; + q->tail = 0; + } else { + q->head = q->head->next; + q->head->prev = 0; + } - free(to_remove); - q -> size--; - } + free(to_remove); + q->size--; + } - return ret_val; + return ret_val; } /** @@ -92,16 +85,15 @@ void * dequeue_list(struct list *q) * @return The last element, without removing it, if present. * Null pointer if no one present.. */ -void * peek_last_list(struct list *q) +void *peek_last_list(struct list *q) { - void * ret_val = 0; + void *ret_val = 0; - if (q -> tail != 0) - { - ret_val = q -> tail -> content; - } + if (q->tail != 0) { + ret_val = q->tail->content; + } - return ret_val; + return ret_val; } /** @@ -111,16 +103,15 @@ void * peek_last_list(struct list *q) * @return The first element, without removing it, if present. * Null pointer if no one present. */ -void * peek_first_list(struct list *q) +void *peek_first_list(struct list *q) { - void * ret_val = 0; + void *ret_val = 0; - if (q -> head != 0) - { - ret_val = q -> head -> content; - } + if (q->head != 0) { + ret_val = q->head->content; + } - return ret_val; + return ret_val; } /** @@ -131,32 +122,28 @@ void * peek_first_list(struct list *q) * @return The last element in the queue, if present. * Null pointer if no one present. */ -void * pop_list(struct list *q) +void *pop_list(struct list *q) { - void * ret_val = 0; + void *ret_val = 0; - if (q -> tail != 0) - { - struct node_list *to_remove = q -> tail; + if (q->tail != 0) { + struct node_list *to_remove = q->tail; - ret_val = to_remove -> content; - q -> tail = to_remove -> prev; + ret_val = to_remove->content; + q->tail = to_remove->prev; - if (q -> tail != 0) - { - q -> tail -> next = 0; - } - else - { - q -> head = 0; - } + if (q->tail != 0) { + q->tail->next = 0; + } else { + q->head = 0; + } - free(to_remove); - q -> size--; - } + free(to_remove); + q->size--; + } - return ret_val; + return ret_val; } /** @@ -166,19 +153,18 @@ void * pop_list(struct list *q) */ void print_list(struct list *q) { - struct node_list *n = q -> head; + struct node_list *n = q->head; - printf("["); + printf("["); - while (n != 0) - { - printf("%p ", n); + while (n != 0) { + printf("%p ", n); - n = n -> next; - } + n = n->next; + } - printf("]\n"); + printf("]\n"); } /** @@ -188,22 +174,21 @@ void print_list(struct list *q) */ void clear_list(struct list *q) { - struct node_list *n = q -> head; + struct node_list *n = q->head; - while (n != 0) - { - struct node_list *tmp = n; + while (n != 0) { + struct node_list *tmp = n; - n = n -> next; + n = n->next; - free(tmp); - } + free(tmp); + } - q -> head = 0; - q -> tail = 0; - q -> size = 0; + q->head = 0; + q->tail = 0; + q->size = 0; } /** @@ -214,7 +199,7 @@ void clear_list(struct list *q) */ int is_empty_list(struct list *q) { - return q -> head == 0; + return q->head == 0; } /** @@ -225,9 +210,9 @@ int is_empty_list(struct list *q) */ void init_priority_queue(struct priority_queue *q) { - q -> head = 0; - q -> tail = 0; - q -> size = 0; + q->head = 0; + q->tail = 0; + q->size = 0; } /** @@ -240,61 +225,53 @@ void init_priority_queue(struct priority_queue *q) * @param item An item to be added * @param val The value that defines the position (the priority) of it */ -void insert_priority_queue(struct priority_queue *q, - void * item, - double val) +void insert_priority_queue(struct priority_queue *q, void *item, double val) { - struct node_priority_queue *n = (struct node_priority_queue*) malloc(sizeof(struct node_priority_queue)); - - - n -> content = item; - n -> value = val; - - if (q -> head == 0) - { // if priority list is empty - n -> prev = 0; - n -> next = 0; - q -> head = n; - q -> size = 1; - q -> tail = n; - } - else - { - struct node_priority_queue *n_current = q -> head; - - - while ((n_current != 0) && (n_current -> value < val)) - { // find the node we have to replace - n_current = n_current -> next; - } - - if (n_current == 0) - { // if it is the last, we put the node as last - n -> prev = q -> tail; - q -> tail -> next = n; - n -> next = 0; - q -> tail = n; - } - else - { // if it is not the last - if (n_current -> prev != 0) - { // if it is not the first - n_current -> prev -> next = n; - n -> prev = n_current -> prev; - n_current -> prev = n; - n -> next = n_current; - } - else - { // if it is the first - q -> head -> prev = n; - q -> head = n; - n -> prev = 0; - n -> next = n_current; - } - } - - q -> size++; - } + struct node_priority_queue *n = (struct node_priority_queue *)malloc( + sizeof(struct node_priority_queue)); + + + n->content = item; + n->value = val; + + if (q->head == 0) { // if priority list is empty + n->prev = 0; + n->next = 0; + q->head = n; + q->size = 1; + q->tail = n; + } else { + struct node_priority_queue *n_current = q->head; + + + while ((n_current != 0) + && (n_current->value + < val)) { // find the node we have to replace + n_current = n_current->next; + } + + if (n_current + == 0) { // if it is the last, we put the node as last + n->prev = q->tail; + q->tail->next = n; + n->next = 0; + q->tail = n; + } else { // if it is not the last + if (n_current->prev != 0) { // if it is not the first + n_current->prev->next = n; + n->prev = n_current->prev; + n_current->prev = n; + n->next = n_current; + } else { // if it is the first + q->head->prev = n; + q->head = n; + n->prev = 0; + n->next = n_current; + } + } + + q->size++; + } } /** @@ -307,116 +284,100 @@ void insert_priority_queue(struct priority_queue *q, * @param item An item to be added or present but update in value * @param val The value that defines the position (the priority) of it */ -void insert_or_update_priority_queue(struct priority_queue *q, - void * item, - double val) +void insert_or_update_priority_queue(struct priority_queue *q, void *item, + double val) { - if (q -> head == 0) - { // if priority list is empty - struct node_priority_queue *n = (struct node_priority_queue*) malloc(sizeof(struct node_priority_queue)); - - - n -> content = item; - n -> value = val; - n -> prev = 0; - n -> next = 0; - q -> head = n; - q -> size = 1; - q -> tail = n; - } - else - { - struct node_priority_queue *n_current = q -> head; - - - struct node_priority_queue *to_replace = 0; - - - struct node_priority_queue *actual_pos = 0; - - - while ((n_current != 0) && ((to_replace == 0) || (actual_pos == 0))) - { // stop if we end queue - if (n_current -> content == item) - { - actual_pos = n_current; - } - - if ((n_current -> value > val) && (to_replace == 0)) - { - to_replace = n_current; - } - - n_current = n_current -> next; - } - - struct node_priority_queue *n = 0; - - - if (actual_pos != 0) - { // if node is already in queue - if (actual_pos == to_replace) - { - actual_pos -> value = val; - - return; - } - - n = actual_pos; - actual_pos -> value = val; - - if (actual_pos -> prev != 0) - { - actual_pos -> prev -> next = actual_pos -> next; - } - else - { - q -> head = actual_pos -> next; - } - - if (actual_pos -> next != 0) - { - actual_pos -> next -> prev = actual_pos -> prev; - } - else - { - q -> tail = actual_pos -> prev; - } - } - else - { // if node is new, do the same as insert, except we already have address - n = (struct node_priority_queue*) malloc(sizeof(struct node_priority_queue)); - n -> content = item; - n -> value = val; - - q -> size++; - } - - if (to_replace == 0) - { // if it is the last, we put the node as last - n -> prev = q -> tail; - q -> tail -> next = n; - n -> next = 0; - q -> tail = n; - } - else - { // if it is not the last - if (to_replace -> prev != 0) - { // if it is not the first - to_replace -> prev -> next = n; - n -> prev = to_replace -> prev; - to_replace -> prev = n; - n -> next = to_replace; - } - else - { // if it is the first - q -> head -> prev = n; - q -> head = n; - n -> prev = 0; - n -> next = to_replace; - } - } - } + if (q->head == 0) { // if priority list is empty + struct node_priority_queue *n = + (struct node_priority_queue *)malloc( + sizeof(struct node_priority_queue)); + + + n->content = item; + n->value = val; + n->prev = 0; + n->next = 0; + q->head = n; + q->size = 1; + q->tail = n; + } else { + struct node_priority_queue *n_current = q->head; + + + struct node_priority_queue *to_replace = 0; + + + struct node_priority_queue *actual_pos = 0; + + + while ((n_current != 0) + && ((to_replace == 0) + || (actual_pos == 0))) { // stop if we end queue + if (n_current->content == item) { + actual_pos = n_current; + } + + if ((n_current->value > val) && (to_replace == 0)) { + to_replace = n_current; + } + + n_current = n_current->next; + } + + struct node_priority_queue *n = 0; + + + if (actual_pos != 0) { // if node is already in queue + if (actual_pos == to_replace) { + actual_pos->value = val; + + return; + } + + n = actual_pos; + actual_pos->value = val; + + if (actual_pos->prev != 0) { + actual_pos->prev->next = actual_pos->next; + } else { + q->head = actual_pos->next; + } + + if (actual_pos->next != 0) { + actual_pos->next->prev = actual_pos->prev; + } else { + q->tail = actual_pos->prev; + } + } else { // if node is new, do the same as insert, except we + // already have address + n = (struct node_priority_queue *)malloc( + sizeof(struct node_priority_queue)); + n->content = item; + n->value = val; + + q->size++; + } + + if (to_replace + == 0) { // if it is the last, we put the node as last + n->prev = q->tail; + q->tail->next = n; + n->next = 0; + q->tail = n; + } else { // if it is not the last + if (to_replace->prev != 0) { // if it is not the first + to_replace->prev->next = n; + n->prev = to_replace->prev; + to_replace->prev = n; + n->next = to_replace; + } else { // if it is the first + q->head->prev = n; + q->head = n; + n->prev = 0; + n->next = to_replace; + } + } + } } /** @@ -429,33 +390,29 @@ void insert_or_update_priority_queue(struct priority_queue *q, * @param q A priority queue * @return The first element of the priority queue, or 0 if it is empty. */ -void * dequeue_priority_queue(struct priority_queue *q) +void *dequeue_priority_queue(struct priority_queue *q) { - void * ret_val = 0; + void *ret_val = 0; - if (q -> head != 0) - { - struct node_priority_queue *to_remove = q -> head; + if (q->head != 0) { + struct node_priority_queue *to_remove = q->head; - ret_val = q -> head -> content; + ret_val = q->head->content; - if (q -> head -> next == 0) - { - q -> head = 0; - q -> tail = 0; - } - else - { - q -> head = q -> head -> next; - q -> head -> prev = 0; - } + if (q->head->next == 0) { + q->head = 0; + q->tail = 0; + } else { + q->head = q->head->next; + q->head->prev = 0; + } - q -> size--; - free(to_remove); - } + q->size--; + free(to_remove); + } - return ret_val; + return ret_val; } /** @@ -465,19 +422,18 @@ void * dequeue_priority_queue(struct priority_queue *q) */ void print_priority_queue(struct priority_queue *q) { - struct node_priority_queue *n = q -> head; + struct node_priority_queue *n = q->head; - printf("["); + printf("["); - while (n != 0) - { - printf("%f ", n -> value); + while (n != 0) { + printf("%f ", n->value); - n = n -> next; - } + n = n->next; + } - printf("]\n"); + printf("]\n"); } /** @@ -488,6 +444,5 @@ void print_priority_queue(struct priority_queue *q) */ int is_empty_priority_queue(struct priority_queue *q) { - return q -> head == 0; + return q->head == 0; } - diff --git a/graph-parser_c/src/graph_parser.c b/graph-parser_c/src/graph_parser.c index 20a6d00..734929a 100644 --- a/graph-parser_c/src/graph_parser.c +++ b/graph-parser_c/src/graph_parser.c @@ -12,21 +12,21 @@ bool recursive = true; * @return a struct of type c_graph_parser that is capable of computing * Brandes Betweenness centrality */ -c_graph_parser * new_graph_parser(int weight, - int heuristic) +c_graph_parser *new_graph_parser(int weight, int heuristic) { - struct graph_parser *gp = (struct graph_parser*) malloc(sizeof(struct graph_parser)); - if (gp == NULL) { - perror("graph-parser-new"); - return NULL; - } + struct graph_parser *gp = + (struct graph_parser *)malloc(sizeof(struct graph_parser)); + if (gp == NULL) { + perror("graph-parser-new"); + return NULL; + } - gp -> heuristic_b = heuristic == 1; - gp -> bc = 0; + gp->heuristic_b = heuristic == 1; + gp->bc = 0; - init_graph(&(gp -> g)); + init_graph(&(gp->g)); - return (c_graph_parser *) gp; + return (c_graph_parser *)gp; } /** @@ -37,21 +37,23 @@ c_graph_parser * new_graph_parser(int weight, * @param topo A topology struct that defines the structure of a graph, * in particular the neighbors list */ -void graph_parser_parse_simplegraph(c_graph_parser * v, struct topology *topo) { - struct graph_parser *gp = (struct graph_parser*) v; - struct node *punt; - free_graph(&(gp -> g)); - init_graph(&(gp -> g)); - - for (punt = topo -> first; punt != 0; punt = punt -> next) { - struct neighbor *neigh; - for (neigh = punt -> neighbor_list; neigh != 0; neigh = neigh -> next) { - const char * source = punt -> id; - const char * target = neigh -> id -> id; - double cost = neigh -> weight; - add_edge_graph(&(gp -> g), source, target, cost, false); - } - } +void graph_parser_parse_simplegraph(c_graph_parser *v, struct topology *topo) +{ + struct graph_parser *gp = (struct graph_parser *)v; + struct node *punt; + free_graph(&(gp->g)); + init_graph(&(gp->g)); + + for (punt = topo->first; punt != 0; punt = punt->next) { + struct neighbor *neigh; + for (neigh = punt->neighbor_list; neigh != 0; + neigh = neigh->next) { + const char *source = punt->id; + const char *target = neigh->id->id; + double cost = neigh->weight; + add_edge_graph(&(gp->g), source, target, cost, false); + } + } } /** @@ -59,55 +61,59 @@ void graph_parser_parse_simplegraph(c_graph_parser * v, struct topology *topo) { * @param v c_graph_parser struct that contains data to run the centrality * algorithm */ -void graph_parser_calculate_bc(c_graph_parser * v) { - struct graph_parser *gp = (struct graph_parser*) v; - // empirical results show that in smaller graphs the - // original algorithm is faster - if (gp -> heuristic_b && (gp -> g.nodes.size > 80)) { - gp -> bc = (double *) betwenness_heuristic(&(gp -> g), recursive); - } else { - gp -> bc = betweeness_brandes(&(gp -> g), true, 0, false); - } +void graph_parser_calculate_bc(c_graph_parser *v) +{ + struct graph_parser *gp = (struct graph_parser *)v; + // empirical results show that in smaller graphs the + // original algorithm is faster + if (gp->heuristic_b && (gp->g.nodes.size > 80)) { + gp->bc = (double *)betwenness_heuristic(&(gp->g), recursive); + } else { + gp->bc = betweeness_brandes(&(gp->g), true, 0, false); + } } /** - * Wrapper function that returns in a custom struct the centrality and other data. + * Wrapper function that returns in a custom struct the centrality and other + * data. * @param v c_graph_parser struct that contains data with results * @param map A custom struct that contains data relative to the centrality and * @return A fixed value */ -int graph_parser_compose_degree_bc_map(c_graph_parser * v, - map_id_degree_bc * map) { - struct graph_parser *gp = (struct graph_parser*) v; +int graph_parser_compose_degree_bc_map(c_graph_parser *v, map_id_degree_bc *map) +{ + struct graph_parser *gp = (struct graph_parser *)v; - map -> size = gp -> g.nodes.size; - map -> map = (struct _id_degree_bc*) malloc(sizeof(struct _id_degree_bc) * gp -> g.nodes.size); - map -> n_edges = 0; - struct node_list *nl; + map->size = gp->g.nodes.size; + map->map = (struct _id_degree_bc *)malloc(sizeof(struct _id_degree_bc) + * gp->g.nodes.size); + map->n_edges = 0; + struct node_list *nl; - int i = 0; + int i = 0; - for (nl = gp -> g.nodes.head; nl != 0; nl = nl -> next) { - struct node_graph *ng = (struct node_graph*) nl -> content; + for (nl = gp->g.nodes.head; nl != 0; nl = nl->next) { + struct node_graph *ng = (struct node_graph *)nl->content; - map -> map[i].id = strdup(ng -> name); - map -> map[i].bc = gp -> bc[ng -> node_graph_id]; - map -> n_edges += ng -> neighbours.size; - map -> map[i].degree = ng -> neighbours.size; - i++; - } - return 1; + map->map[i].id = strdup(ng->name); + map->map[i].bc = gp->bc[ng->node_graph_id]; + map->n_edges += ng->neighbours.size; + map->map[i].degree = ng->neighbours.size; + i++; + } + return 1; } /** * Function that deallocates and frees memory used for centrality computation * @param v c_graph_parser struct that has to be deleted */ -void free_graph_parser(void * v) { - struct graph_parser *gp = (struct graph_parser*) v; - if (gp -> bc != NULL) { - free(gp -> bc); - } - free(gp); +void free_graph_parser(void *v) +{ + struct graph_parser *gp = (struct graph_parser *)v; + if (gp->bc != NULL) { + free(gp->bc); + } + free(gp); } diff --git a/graph-parser_c/src/network_change.c b/graph-parser_c/src/network_change.c index ab2b6d0..524fe36 100644 --- a/graph-parser_c/src/network_change.c +++ b/graph-parser_c/src/network_change.c @@ -10,9 +10,11 @@ * completes, instead of using already computed value, you have to change this * function. * - * @param deviation_bic The standard deviation of the biconnected components sizes in + * @param deviation_bic The standard deviation of the biconnected components + * sizes in * current graph - * @param old_deviation_bic The standard deviation of the biconnected components sizes + * @param old_deviation_bic The standard deviation of the biconnected components + * sizes * of the last computed graph * @param biconnected_num The number of biconnected component of the * current graph @@ -20,41 +22,38 @@ * computed graph * @param old_node_num The number of nodes in the last computed graph * @param node_num The number of nodes in the present graph - * @param deviation_edge The standard deviation of the current graph edges weights + * @param deviation_edge The standard deviation of the current graph edges + * weights * @param old_deviation_edge The standard deviation of the last computed * graph edges weights * * @return whether we have to recompute the graph */ -bool is_to_recompute(double deviation_bic, - double old_deviation_bic, - int biconnected_num, - int old_biconnected_num, - int node_num, - int old_node_num, - double deviation_edge, - double old_deviation_edge) +bool is_to_recompute(double deviation_bic, double old_deviation_bic, + int biconnected_num, int old_biconnected_num, int node_num, + int old_node_num, double deviation_edge, + double old_deviation_edge) { - float stdd = 1; + float stdd = 1; - if (old_deviation_bic > 0) - { - stdd = abs(deviation_bic - old_deviation_bic) / old_deviation_bic; - } + if (old_deviation_bic > 0) { + stdd = abs(deviation_bic - old_deviation_bic) + / old_deviation_bic; + } - int nn = abs(biconnected_num - old_biconnected_num); - float stde = 1; + int nn = abs(biconnected_num - old_biconnected_num); + float stde = 1; - if (old_deviation_edge > 0) - { - stde = abs(deviation_edge - old_deviation_edge) / old_deviation_edge; - } + if (old_deviation_edge > 0) { + stde = abs(deviation_edge - old_deviation_edge) + / old_deviation_edge; + } - int nb = abs(node_num - old_node_num); + int nb = abs(node_num - old_node_num); - // parameters are chosen from test on real and simulated networks, see - // py scripts for details - return (stdd >= 0.05) && (nn >= 4) && (stde >= 0.005) && (nb >= 0); + // parameters are chosen from test on real and simulated networks, see + // py scripts for details + return (stdd >= 0.05) && (nn >= 4) && (stde >= 0.005) && (nb >= 0); } /** @@ -72,57 +71,49 @@ bool is_to_recompute(double deviation_bic, * @param ret_vals a pointer values of the result (could be 0, null pointing) * @param list_of_nodes list of nodes in the graph, used for storing id */ -void write_file(int connected_comp_num, - float standard_deviation_bic, - float standard_deviation_edge, - int size, - double * ret_vals, - struct list *list_of_nodes) +void write_file(int connected_comp_num, float standard_deviation_bic, + float standard_deviation_edge, int size, double *ret_vals, + struct list *list_of_nodes) { - FILE * fp; + FILE *fp; - fp = fopen("network.dat", "w+"); + fp = fopen("network.dat", "w+"); - if (fp == NULL) - { - printf("I couldn't open results.dat for writing.\n"); - exit(0); - } + if (fp == NULL) { + printf("I couldn't open results.dat for writing.\n"); + exit(0); + } - struct node_list *nl; + struct node_list *nl; - int str_len = 0; + int str_len = 0; - for (nl = list_of_nodes -> head; nl != 0; nl = nl -> next) - { - struct node_graph *ng = (struct node_graph*) nl -> content; + for (nl = list_of_nodes->head; nl != 0; nl = nl->next) { + struct node_graph *ng = (struct node_graph *)nl->content; - int str_len_tmp = strlen(ng -> name); + int str_len_tmp = strlen(ng->name); - str_len = ((str_len > str_len_tmp) ? str_len : str_len_tmp); - } + str_len = ((str_len > str_len_tmp) ? str_len : str_len_tmp); + } - fprintf(fp, - "%d, %f, %f, %d\n", - connected_comp_num, - (float) standard_deviation_bic, - (float) standard_deviation_edge, - list_of_nodes -> size); - fprintf(fp, "%d, %d\n", size, str_len + 1); + fprintf(fp, "%d, %f, %f, %d\n", connected_comp_num, + (float)standard_deviation_bic, (float)standard_deviation_edge, + list_of_nodes->size); + fprintf(fp, "%d, %d\n", size, str_len + 1); - int i = 0; + int i = 0; - for (nl = list_of_nodes -> head; nl != 0; nl = nl -> next) - { - struct node_graph *ng = (struct node_graph*) nl -> content; + for (nl = list_of_nodes->head; nl != 0; nl = nl->next) { + struct node_graph *ng = (struct node_graph *)nl->content; - fprintf(fp, "%f,%s\n", (float) ret_vals[ng -> node_graph_id], ng -> name); - } + fprintf(fp, "%f,%s\n", (float)ret_vals[ng->node_graph_id], + ng->name); + } - fclose(fp); + fclose(fp); } ; @@ -144,48 +135,45 @@ void write_file(int connected_comp_num, * @param node_names Pointer to char * array, will retrieve the list of names * @return Whether everything was fine */ -bool read_file(int * connected_comp_num, - float * standard_deviation_bic, - float * standard_deviation_edge, - int * node_num, - int * size, - double ** ret_vals, - char *** node_names) +bool read_file(int *connected_comp_num, float *standard_deviation_bic, + float *standard_deviation_edge, int *node_num, int *size, + double **ret_vals, char ***node_names) { - FILE * fp; + FILE *fp; - fp = fopen("network.dat", "r"); + fp = fopen("network.dat", "r"); - if (fp == NULL) - { - return false; - } + if (fp == NULL) { + return false; + } - bool ret_val = (fscanf(fp, "%d, %f, %f, %d\n", connected_comp_num, standard_deviation_bic, standard_deviation_edge, - node_num) == 4); - int string_max_len = -1; + bool ret_val = (fscanf(fp, "%d, %f, %f, %d\n", connected_comp_num, + standard_deviation_bic, standard_deviation_edge, + node_num) + == 4); + int string_max_len = -1; - ret_val = ret_val && (fscanf(fp, "%d, %d\n", size, &string_max_len) == 2); + ret_val = + ret_val && (fscanf(fp, "%d, %d\n", size, &string_max_len) == 2); - char * buffer = malloc(sizeof(char) * string_max_len); - int i = 0; + char *buffer = malloc(sizeof(char) * string_max_len); + int i = 0; - (*ret_vals) = malloc(sizeof(double) * (*size)); - (*node_names) = malloc(sizeof(char *) * (*size)); + (*ret_vals) = malloc(sizeof(double) * (*size)); + (*node_names) = malloc(sizeof(char *) * (*size)); - for (; ret_val && (i < *size); i++) - { - float tmp; + for (; ret_val && (i < *size); i++) { + float tmp; - ret_val = ret_val && (fscanf(fp, "%f,%s\n", &tmp, buffer) == 2); - (*node_names)[i] = strdup(buffer); - (*ret_vals)[i] = (double) tmp; - } + ret_val = ret_val && (fscanf(fp, "%f,%s\n", &tmp, buffer) == 2); + (*node_names)[i] = strdup(buffer); + (*ret_vals)[i] = (double)tmp; + } - fclose(fp); - free(buffer); + fclose(fp); + free(buffer); - return ret_val; + return ret_val; } ; @@ -202,58 +190,57 @@ bool read_file(int * connected_comp_num, * standard deviation of biconnected components size. */ void compute_mean_number(struct list *biconnected_components_subgraph, - int * connected_num, - float * standard_deviation) + int *connected_num, float *standard_deviation) { - struct node_list *nl; + struct node_list *nl; - float mean = 0; - int biconnected_comp_count = 0; + float mean = 0; + int biconnected_comp_count = 0; - for (nl = biconnected_components_subgraph -> head; nl != 0; nl = nl -> next) - { - struct list *tmp = (struct list*) nl -> content; + for (nl = biconnected_components_subgraph->head; nl != 0; + nl = nl->next) { + struct list *tmp = (struct list *)nl->content; - struct node_list *nl2 = tmp -> head; + struct node_list *nl2 = tmp->head; - for (; nl2 != 0; nl2 = nl2 -> next) - { - struct connected_component *cc = (struct connected_component*) nl2 -> content; + for (; nl2 != 0; nl2 = nl2->next) { + struct connected_component *cc = + (struct connected_component *)nl2->content; - biconnected_comp_count++; + biconnected_comp_count++; - mean += cc -> g.nodes.size; - } - } + mean += cc->g.nodes.size; + } + } - mean /= biconnected_comp_count; + mean /= biconnected_comp_count; - float std = 0; + float std = 0; - for (nl = biconnected_components_subgraph -> head; nl != 0; nl = nl -> next) - { - struct list *tmp = (struct list*) nl -> content; + for (nl = biconnected_components_subgraph->head; nl != 0; + nl = nl->next) { + struct list *tmp = (struct list *)nl->content; - struct node_list *nl2 = tmp -> head; + struct node_list *nl2 = tmp->head; - for (; nl2 != 0; nl2 = nl2 -> next) - { - struct connected_component *cc = (struct connected_component*) nl2 -> content; + for (; nl2 != 0; nl2 = nl2->next) { + struct connected_component *cc = + (struct connected_component *)nl2->content; - std += pow((((float) cc -> g.nodes.size) - mean), 2); - } - } + std += pow((((float)cc->g.nodes.size) - mean), 2); + } + } - std /= biconnected_comp_count; - (*connected_num) = biconnected_comp_count; - (*standard_deviation) = std; + std /= biconnected_comp_count; + (*connected_num) = biconnected_comp_count; + (*standard_deviation) = std; } /** @@ -277,82 +264,66 @@ void compute_mean_number(struct list *biconnected_components_subgraph, * @param node_names Pointer to char * array, will retrieve the list of names * @return the last results if the network is not changed, o.w. a zero pointer. */ -double * is_network_changed(struct list *biconnected_components_subgraph, - int node_num, - int * biconnected_num, - float * standard_deviation_bic, - float * standard_deviation_edge, - int * result_size, - char *** node_names) +double *is_network_changed(struct list *biconnected_components_subgraph, + int node_num, int *biconnected_num, + float *standard_deviation_bic, + float *standard_deviation_edge, int *result_size, + char ***node_names) { - int biconnected_num_old = -1; - float standard_deviation_bic_old = -1; - int node_num_old = -1; - float standard_deviation_edge_old = -1; - - compute_mean_number(biconnected_components_subgraph, biconnected_num, standard_deviation_bic); - - double * ret_val; - - if (!read_file(&biconnected_num_old, &standard_deviation_bic_old, &standard_deviation_edge_old, &node_num_old, - result_size, &ret_val, node_names)) - { - return 0; - } - - bool recompute = is_to_recompute(*standard_deviation_bic, - standard_deviation_bic_old, - *biconnected_num, - biconnected_num_old, - node_num, - node_num_old, - *standard_deviation_edge, - standard_deviation_edge_old); - - if (recompute) - { - free(ret_val); - - return 0; - } - else - { - return ret_val; - } + int biconnected_num_old = -1; + float standard_deviation_bic_old = -1; + int node_num_old = -1; + float standard_deviation_edge_old = -1; + + compute_mean_number(biconnected_components_subgraph, biconnected_num, + standard_deviation_bic); + + double *ret_val; + + if (!read_file(&biconnected_num_old, &standard_deviation_bic_old, + &standard_deviation_edge_old, &node_num_old, result_size, + &ret_val, node_names)) { + return 0; + } + + bool recompute = is_to_recompute( + *standard_deviation_bic, standard_deviation_bic_old, + *biconnected_num, biconnected_num_old, node_num, node_num_old, + *standard_deviation_edge, standard_deviation_edge_old); + + if (recompute) { + free(ret_val); + + return 0; + } else { + return ret_val; + } } -void copy_old_values(double * old_vals, - double * vals, - char ** names, - int names_count, - struct list *list_of_nodes) +void copy_old_values(double *old_vals, double *vals, char **names, + int names_count, struct list *list_of_nodes) { - int i; + int i; - struct node_list *nl; + struct node_list *nl; - struct node_graph *ng = 0; + struct node_graph *ng = 0; - for (i = 0; i < names_count; i++) - { - for (nl = list_of_nodes -> head; nl != 0; nl = nl -> next) - { - ng = (struct node_graph*) nl -> content; + for (i = 0; i < names_count; i++) { + for (nl = list_of_nodes->head; nl != 0; nl = nl->next) { + ng = (struct node_graph *)nl->content; - if (strcmp(ng -> name, names[i]) == 0) - { - break; - } - } + if (strcmp(ng->name, names[i]) == 0) { + break; + } + } - if (ng != 0) - { - vals[ng -> node_graph_id] = old_vals[i]; - } + if (ng != 0) { + vals[ng->node_graph_id] = old_vals[i]; + } - ng = 0; - } + ng = 0; + } } - diff --git a/graph-parser_c/src/test.c b/graph-parser_c/src/test.c index d0e06a7..3601900 100644 --- a/graph-parser_c/src/test.c +++ b/graph-parser_c/src/test.c @@ -17,52 +17,49 @@ #include "graph_parser.h" -char * read_file_content(char * filename) +char *read_file_content(char *filename) { - char * buffer = NULL; - int string_size, read_size; - FILE * handler = fopen(filename, "r"); + char *buffer = NULL; + int string_size, read_size; + FILE *handler = fopen(filename, "r"); - if (handler) - { - // Seek the last byte of the file - fseek(handler, 0, SEEK_END); + if (handler) { + // Seek the last byte of the file + fseek(handler, 0, SEEK_END); - // Offset from the first to the last byte, or in other words, filesize - string_size = ftell(handler); + // Offset from the first to the last byte, or in other words, + // filesize + string_size = ftell(handler); - // go back to the start of the file - rewind(handler); + // go back to the start of the file + rewind(handler); - // Allocate a string that can hold it all - buffer = (char *) malloc(sizeof(char) * (string_size + 1)); + // Allocate a string that can hold it all + buffer = (char *)malloc(sizeof(char) * (string_size + 1)); - // Read it all in one operation - read_size = fread(buffer, sizeof(char), string_size, handler); + // Read it all in one operation + read_size = fread(buffer, sizeof(char), string_size, handler); - // fread doesn't set it so put a \0 in the last position - // and buffer is now officially a string - buffer[string_size] = '\0'; + // fread doesn't set it so put a \0 in the last position + // and buffer is now officially a string + buffer[string_size] = '\0'; - if (string_size != read_size) - { - // Something went wrong, throw away the memory and set - // the buffer to NULL - free(buffer); + if (string_size != read_size) { + // Something went wrong, throw away the memory and set + // the buffer to NULL + free(buffer); - buffer = NULL; - } + buffer = NULL; + } - // Always remember to close the file. - fclose(handler); - } - else - { - printf("Input file not found!\n"); - exit(9); - } + // Always remember to close the file. + fclose(handler); + } else { + printf("Input file not found!\n"); + exit(9); + } - return buffer; + return buffer; } /* int main() @@ -84,7 +81,8 @@ int main() bool * is_articulation_point = (bool *) malloc(sizeof(bool) * g.nodes.size); int * component_indexes = (int *) malloc(sizeof(int) * g.nodes.size); - struct list *l = tarjan_iter_dir(&g, is_articulation_point, component_indexes); + struct list *l = tarjan_iter_dir(&g, is_articulation_point, +component_indexes); struct node_list *nl = l -> head; @@ -92,31 +90,31 @@ int main() for (; nl != 0; nl = nl -> next) { - struct sub_graph *sg = (struct sub_graph*) nl -> content; + struct sub_graph *sg = (struct sub_graph*) nl -> content; - struct node_list *nl2 = sg -> connected_components.head; + struct node_list *nl2 = sg -> connected_components.head; - for (; nl2 != 0; nl2 = nl2 -> next) - { - struct connected_component *cc = - (struct connected_component*) nl2 -> content; + for (; nl2 != 0; nl2 = nl2 -> next) + { + struct connected_component *cc = + (struct connected_component*) nl2 -> content; - struct node_list *nl4 = cc -> g.nodes.head; + struct node_list *nl4 = cc -> g.nodes.head; - for (; nl4 != 0; nl4 = nl4 -> next) - { - struct node_graph *ng = (struct node_graph*) nl4 -> content; + for (; nl4 != 0; nl4 = nl4 -> next) + { + struct node_graph *ng = (struct node_graph*) nl4 -> content; - printf("%s ", ng -> name); - } + printf("%s ", ng -> name); + } - printf("\n"); - } + printf("\n"); + } } struct node_list *nl4 = g.nodes.head; @@ -126,10 +124,11 @@ int main() for (; nl4 != 0; nl4 = nl4 -> next) { - struct node_graph *ng = (struct node_graph*) nl4 -> content; + struct node_graph *ng = (struct node_graph*) nl4 -> content; - printf("%s %d\n", ng -> name, is_articulation_point[ng -> node_graph_id]); + printf("%s %d\n", ng -> name, is_articulation_point[ng -> +node_graph_id]); } free_graph(&g); @@ -140,34 +139,34 @@ int main() for (; nl != 0; nl = nl -> next) { - struct sub_graph *sg = (struct sub_graph*) nl -> content; + struct sub_graph *sg = (struct sub_graph*) nl -> content; - struct node_list *nl2 = sg -> connected_components.head; + struct node_list *nl2 = sg -> connected_components.head; - for (; nl2 != 0; nl2 = nl2 -> next) - { - struct connected_component *cc = - (struct connected_component*) nl2 -> content; + for (; nl2 != 0; nl2 = nl2 -> next) + { + struct connected_component *cc = + (struct connected_component*) nl2 -> content; - struct node_list *nl4 = cc -> g.nodes.head; + struct node_list *nl4 = cc -> g.nodes.head; - for (; nl4 != 0; nl4 = nl4 -> next) - { - struct node_graph *ng = (struct node_graph*) nl4 -> content; - } + for (; nl4 != 0; nl4 = nl4 -> next) + { + struct node_graph *ng = (struct node_graph*) nl4 -> content; + } - free_graph(&(cc -> g)); - free(cc -> mapping); - free(cc -> weights); - free(cc); - } + free_graph(&(cc -> g)); + free(cc -> mapping); + free(cc -> weights); + free(cc); + } - clear_list(&(sg -> connected_components)); - free(sg); + clear_list(&(sg -> connected_components)); + free(sg); } clear_list(l); @@ -176,38 +175,39 @@ int main() return 0; } */ -int main(int argc, char** argv) { -if(argc==1) -return 0; -int heuristic=atoi(argv[1]); -//to remove in case of different test -multithread=false; -if(argc==3){ -stop_computing_if_unchanged=atoi(argv[2])==1; -} -c_graph_parser* cgp=new_graph_parser(1, heuristic); -char * file_content=read_file_content("input.json"); -struct topology *topo=parse_netjson(file_content); -free(file_content); -graph_parser_parse_simplegraph(cgp,topo); -graph_parser_calculate_bc(cgp); -map_id_degree_bc * bc_degree_map = (map_id_degree_bc *) -malloc(sizeof(map_id_degree_bc)); -graph_parser_compose_degree_bc_map(cgp,bc_degree_map); -int i; -// printf("[\n"); -printf("{\n"); -for(i=0; isize; i++){ -//if(strcmp(bc_degree_map->map[i].id, node_name)==0){ -printf("%s:%1.50f",bc_degree_map->map[i].id,bc_degree_map->map[i].bc); -// break; -// } -if(isize-1) -printf(","); -printf("\n"); -} -printf("}"); -delete_graph_parser(cgp); -return (EXIT_SUCCESS); +int main(int argc, char **argv) +{ + if (argc == 1) + return 0; + int heuristic = atoi(argv[1]); + // to remove in case of different test + multithread = false; + if (argc == 3) { + stop_computing_if_unchanged = atoi(argv[2]) == 1; + } + c_graph_parser *cgp = new_graph_parser(1, heuristic); + char *file_content = read_file_content("input.json"); + struct topology *topo = parse_netjson(file_content); + free(file_content); + graph_parser_parse_simplegraph(cgp, topo); + graph_parser_calculate_bc(cgp); + map_id_degree_bc *bc_degree_map = + (map_id_degree_bc *)malloc(sizeof(map_id_degree_bc)); + graph_parser_compose_degree_bc_map(cgp, bc_degree_map); + int i; + // printf("[\n"); + printf("{\n"); + for (i = 0; i < bc_degree_map->size; i++) { + // if(strcmp(bc_degree_map->map[i].id, node_name)==0){ + printf("%s:%1.50f", bc_degree_map->map[i].id, + bc_degree_map->map[i].bc); + // break; + // } + if (i < bc_degree_map->size - 1) + printf(","); + printf("\n"); + } + printf("}"); + delete_graph_parser(cgp); + return (EXIT_SUCCESS); } - diff --git a/prince/data/jsoninfo.json b/prince/data/jsoninfo.json new file mode 100644 index 0000000..dbec3ab --- /dev/null +++ b/prince/data/jsoninfo.json @@ -0,0 +1,1416 @@ +{ + "pid": 24810, + "systemTime": 1506424836, + "timeSinceStartup": 148307, + "configurationChecksum": "c168eae9", + "topology": [ + { + "lastHopIP": "10.0.0.1", + "pathCost": 24, + "validityTime": 93164, + "refCount": 3, + "msgSeq": 39574, + "msgHops": 23, + "hops": 24, + "ansn": 18, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.1.1", + "tcEdgeCost": 1, + "ansnEdge": 18, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.1.1", + "pathCost": 23, + "validityTime": 65625, + "refCount": 5, + "msgSeq": 62020, + "msgHops": 22, + "hops": 23, + "ansn": 26, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.0.1", + "tcEdgeCost": 1, + "ansnEdge": 26, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.1.1", + "pathCost": 23, + "validityTime": 65625, + "refCount": 5, + "msgSeq": 62020, + "msgHops": 22, + "hops": 23, + "ansn": 26, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.2.1", + "tcEdgeCost": 1, + "ansnEdge": 26, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.2.1", + "pathCost": 22, + "validityTime": 62264, + "refCount": 5, + "msgSeq": 57849, + "msgHops": 21, + "hops": 22, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.1.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.2.1", + "pathCost": 22, + "validityTime": 62264, + "refCount": 5, + "msgSeq": 57849, + "msgHops": 21, + "hops": 22, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.3.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.3.1", + "pathCost": 21, + "validityTime": 56206, + "refCount": 5, + "msgSeq": 46915, + "msgHops": 20, + "hops": 21, + "ansn": 35, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.2.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.3.1", + "pathCost": 21, + "validityTime": 56206, + "refCount": 5, + "msgSeq": 46915, + "msgHops": 20, + "hops": 21, + "ansn": 35, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.4.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.4.1", + "pathCost": 20, + "validityTime": 54668, + "refCount": 5, + "msgSeq": 30955, + "msgHops": 19, + "hops": 20, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.3.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.4.1", + "pathCost": 20, + "validityTime": 54668, + "refCount": 5, + "msgSeq": 30955, + "msgHops": 19, + "hops": 20, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.5.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.5.1", + "pathCost": 19, + "validityTime": 43424, + "refCount": 5, + "msgSeq": 10234, + "msgHops": 18, + "hops": 19, + "ansn": 35, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.4.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.5.1", + "pathCost": 19, + "validityTime": 43424, + "refCount": 5, + "msgSeq": 10234, + "msgHops": 18, + "hops": 19, + "ansn": 35, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.6.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.6.1", + "pathCost": 18, + "validityTime": 46749, + "refCount": 5, + "msgSeq": 31996, + "msgHops": 17, + "hops": 18, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.5.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.6.1", + "pathCost": 18, + "validityTime": 46749, + "refCount": 5, + "msgSeq": 31996, + "msgHops": 17, + "hops": 18, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.7.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.7.1", + "pathCost": 17, + "validityTime": 40066, + "refCount": 5, + "msgSeq": 5572, + "msgHops": 16, + "hops": 17, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.6.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.7.1", + "pathCost": 17, + "validityTime": 40066, + "refCount": 5, + "msgSeq": 5572, + "msgHops": 16, + "hops": 17, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.8.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.8.1", + "pathCost": 16, + "validityTime": 40487, + "refCount": 5, + "msgSeq": 43513, + "msgHops": 15, + "hops": 16, + "ansn": 32, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.7.1", + "tcEdgeCost": 1, + "ansnEdge": 32, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.8.1", + "pathCost": 16, + "validityTime": 40487, + "refCount": 5, + "msgSeq": 43513, + "msgHops": 15, + "hops": 16, + "ansn": 32, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.9.1", + "tcEdgeCost": 1, + "ansnEdge": 32, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.9.1", + "pathCost": 15, + "validityTime": 38127, + "refCount": 5, + "msgSeq": 57319, + "msgHops": 14, + "hops": 15, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.8.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.9.1", + "pathCost": 15, + "validityTime": 38127, + "refCount": 5, + "msgSeq": 57319, + "msgHops": 14, + "hops": 15, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.10.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.10.1", + "pathCost": 14, + "validityTime": 38426, + "refCount": 5, + "msgSeq": 41965, + "msgHops": 13, + "hops": 14, + "ansn": 24, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.9.1", + "tcEdgeCost": 1, + "ansnEdge": 24, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.10.1", + "pathCost": 14, + "validityTime": 38426, + "refCount": 5, + "msgSeq": 41965, + "msgHops": 13, + "hops": 14, + "ansn": 24, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.11.1", + "tcEdgeCost": 1, + "ansnEdge": 24, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.11.1", + "pathCost": 13, + "validityTime": 37208, + "refCount": 5, + "msgSeq": 50474, + "msgHops": 12, + "hops": 13, + "ansn": 35, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.10.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.11.1", + "pathCost": 13, + "validityTime": 37208, + "refCount": 5, + "msgSeq": 50474, + "msgHops": 12, + "hops": 13, + "ansn": 35, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.12.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.12.1", + "pathCost": 12, + "validityTime": 37539, + "refCount": 5, + "msgSeq": 40190, + "msgHops": 11, + "hops": 12, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.11.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.12.1", + "pathCost": 12, + "validityTime": 37539, + "refCount": 5, + "msgSeq": 40190, + "msgHops": 11, + "hops": 12, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.13.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.13.1", + "pathCost": 11, + "validityTime": 36689, + "refCount": 5, + "msgSeq": 40801, + "msgHops": 10, + "hops": 11, + "ansn": 24, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.12.1", + "tcEdgeCost": 1, + "ansnEdge": 24, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.13.1", + "pathCost": 11, + "validityTime": 36689, + "refCount": 5, + "msgSeq": 40801, + "msgHops": 10, + "hops": 11, + "ansn": 24, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.14.1", + "tcEdgeCost": 1, + "ansnEdge": 24, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.14.1", + "pathCost": 10, + "validityTime": 34848, + "refCount": 5, + "msgSeq": 63301, + "msgHops": 9, + "hops": 10, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.13.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.14.1", + "pathCost": 10, + "validityTime": 34848, + "refCount": 5, + "msgSeq": 63301, + "msgHops": 9, + "hops": 10, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.15.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.15.1", + "pathCost": 9, + "validityTime": 37722, + "refCount": 5, + "msgSeq": 47642, + "msgHops": 8, + "hops": 9, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.14.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.15.1", + "pathCost": 9, + "validityTime": 37722, + "refCount": 5, + "msgSeq": 47642, + "msgHops": 8, + "hops": 9, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.16.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.16.1", + "pathCost": 8, + "validityTime": 34293, + "refCount": 5, + "msgSeq": 63960, + "msgHops": 7, + "hops": 8, + "ansn": 35, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.15.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.16.1", + "pathCost": 8, + "validityTime": 34293, + "refCount": 5, + "msgSeq": 63960, + "msgHops": 7, + "hops": 8, + "ansn": 35, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.17.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.17.1", + "pathCost": 7, + "validityTime": 33077, + "refCount": 5, + "msgSeq": 36565, + "msgHops": 6, + "hops": 7, + "ansn": 29, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.16.1", + "tcEdgeCost": 1, + "ansnEdge": 29, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.17.1", + "pathCost": 7, + "validityTime": 33077, + "refCount": 5, + "msgSeq": 36565, + "msgHops": 6, + "hops": 7, + "ansn": 29, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.18.1", + "tcEdgeCost": 1, + "ansnEdge": 29, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.18.1", + "pathCost": 6, + "validityTime": 38440, + "refCount": 5, + "msgSeq": 15678, + "msgHops": 5, + "hops": 6, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.17.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.18.1", + "pathCost": 6, + "validityTime": 38440, + "refCount": 5, + "msgSeq": 15678, + "msgHops": 5, + "hops": 6, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.19.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.19.1", + "pathCost": 5, + "validityTime": 35014, + "refCount": 5, + "msgSeq": 35125, + "msgHops": 4, + "hops": 5, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.18.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.19.1", + "pathCost": 5, + "validityTime": 35014, + "refCount": 5, + "msgSeq": 35125, + "msgHops": 4, + "hops": 5, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.20.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.20.1", + "pathCost": 4, + "validityTime": 37091, + "refCount": 5, + "msgSeq": 44059, + "msgHops": 3, + "hops": 4, + "ansn": 37, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.19.1", + "tcEdgeCost": 1, + "ansnEdge": 37, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.20.1", + "pathCost": 4, + "validityTime": 37091, + "refCount": 5, + "msgSeq": 44059, + "msgHops": 3, + "hops": 4, + "ansn": 37, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.21.1", + "tcEdgeCost": 1, + "ansnEdge": 37, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.21.1", + "pathCost": 3, + "validityTime": 37061, + "refCount": 5, + "msgSeq": 22786, + "msgHops": 2, + "hops": 3, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.20.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.21.1", + "pathCost": 3, + "validityTime": 37061, + "refCount": 5, + "msgSeq": 22786, + "msgHops": 2, + "hops": 3, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.22.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.22.1", + "pathCost": 2, + "validityTime": 37305, + "refCount": 5, + "msgSeq": 45625, + "msgHops": 1, + "hops": 2, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.21.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.22.1", + "pathCost": 2, + "validityTime": 37305, + "refCount": 5, + "msgSeq": 45625, + "msgHops": 1, + "hops": 2, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.23.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.23.1", + "pathCost": 1, + "validityTime": 37681, + "refCount": 5, + "msgSeq": 52110, + "msgHops": 0, + "hops": 1, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.22.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.23.1", + "pathCost": 1, + "validityTime": 37681, + "refCount": 5, + "msgSeq": 52110, + "msgHops": 0, + "hops": 1, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.24.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.24.1", + "pathCost": 0, + "validityTime": 0, + "refCount": 4, + "msgSeq": 0, + "msgHops": 0, + "hops": 0, + "ansn": 0, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.23.1", + "tcEdgeCost": 1, + "ansnEdge": 0, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.24.1", + "pathCost": 0, + "validityTime": 0, + "refCount": 4, + "msgSeq": 0, + "msgHops": 0, + "hops": 0, + "ansn": 0, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.25.1", + "tcEdgeCost": 1, + "ansnEdge": 0, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.25.1", + "pathCost": 1, + "validityTime": 36995, + "refCount": 5, + "msgSeq": 24963, + "msgHops": 0, + "hops": 1, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.24.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.25.1", + "pathCost": 1, + "validityTime": 36995, + "refCount": 5, + "msgSeq": 24963, + "msgHops": 0, + "hops": 1, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.26.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.26.1", + "pathCost": 2, + "validityTime": 39244, + "refCount": 5, + "msgSeq": 60363, + "msgHops": 1, + "hops": 2, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.25.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.26.1", + "pathCost": 2, + "validityTime": 39244, + "refCount": 5, + "msgSeq": 60363, + "msgHops": 1, + "hops": 2, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.27.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.27.1", + "pathCost": 3, + "validityTime": 39305, + "refCount": 5, + "msgSeq": 10216, + "msgHops": 2, + "hops": 3, + "ansn": 26, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.26.1", + "tcEdgeCost": 1, + "ansnEdge": 26, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.27.1", + "pathCost": 3, + "validityTime": 39305, + "refCount": 5, + "msgSeq": 10216, + "msgHops": 2, + "hops": 3, + "ansn": 26, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.28.1", + "tcEdgeCost": 1, + "ansnEdge": 26, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.28.1", + "pathCost": 4, + "validityTime": 43161, + "refCount": 5, + "msgSeq": 2421, + "msgHops": 3, + "hops": 4, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.27.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.28.1", + "pathCost": 4, + "validityTime": 43161, + "refCount": 5, + "msgSeq": 2421, + "msgHops": 3, + "hops": 4, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.29.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.29.1", + "pathCost": 5, + "validityTime": 46465, + "refCount": 5, + "msgSeq": 1786, + "msgHops": 4, + "hops": 5, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.28.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.29.1", + "pathCost": 5, + "validityTime": 46465, + "refCount": 5, + "msgSeq": 1786, + "msgHops": 4, + "hops": 5, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.30.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.30.1", + "pathCost": 6, + "validityTime": 47119, + "refCount": 5, + "msgSeq": 15555, + "msgHops": 5, + "hops": 6, + "ansn": 37, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.29.1", + "tcEdgeCost": 1, + "ansnEdge": 37, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.30.1", + "pathCost": 6, + "validityTime": 47119, + "refCount": 5, + "msgSeq": 15555, + "msgHops": 5, + "hops": 6, + "ansn": 37, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.31.1", + "tcEdgeCost": 1, + "ansnEdge": 37, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.31.1", + "pathCost": 7, + "validityTime": 56263, + "refCount": 5, + "msgSeq": 7596, + "msgHops": 6, + "hops": 7, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.30.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.31.1", + "pathCost": 7, + "validityTime": 56263, + "refCount": 5, + "msgSeq": 7596, + "msgHops": 6, + "hops": 7, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.32.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.32.1", + "pathCost": 8, + "validityTime": 59743, + "refCount": 5, + "msgSeq": 34726, + "msgHops": 7, + "hops": 8, + "ansn": 32, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.31.1", + "tcEdgeCost": 1, + "ansnEdge": 32, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.32.1", + "pathCost": 8, + "validityTime": 59743, + "refCount": 5, + "msgSeq": 34726, + "msgHops": 7, + "hops": 8, + "ansn": 32, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.33.1", + "tcEdgeCost": 1, + "ansnEdge": 32, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.33.1", + "pathCost": 9, + "validityTime": 65911, + "refCount": 5, + "msgSeq": 42345, + "msgHops": 8, + "hops": 9, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.32.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.33.1", + "pathCost": 9, + "validityTime": 65911, + "refCount": 5, + "msgSeq": 42345, + "msgHops": 8, + "hops": 9, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.34.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.34.1", + "pathCost": 10, + "validityTime": 80048, + "refCount": 5, + "msgSeq": 40048, + "msgHops": 9, + "hops": 10, + "ansn": 37, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.33.1", + "tcEdgeCost": 1, + "ansnEdge": 37, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.34.1", + "pathCost": 10, + "validityTime": 80048, + "refCount": 5, + "msgSeq": 40048, + "msgHops": 9, + "hops": 10, + "ansn": 37, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.35.1", + "tcEdgeCost": 1, + "ansnEdge": 37, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.35.1", + "pathCost": 11, + "validityTime": 86187, + "refCount": 3, + "msgSeq": 16808, + "msgHops": 10, + "hops": 11, + "ansn": 17, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.34.1", + "tcEdgeCost": 1, + "ansnEdge": 17, + "linkQuality": 1, + "neighborLinkQuality": 1 + } + ], + "config": { + "configurationChecksum": "c168eae9", + "cli": [ + "../olsrd/olsrd", + "-f", + "/home/gabriel/git/wcn_emulator/LINEAR36_LINEAR36_1506424682_250/h24_24_olsr.conf" + ], + "configurationFile": "/home/gabriel/git/wcn_emulator/LINEAR36_LINEAR36_1506424682_250/h24_24_olsr.conf", + "olsrPort": 698, + "debugLevel": 1, + "noFork": false, + "pidFile": "", + "hostEmulation": false, + "ipVersion": 4, + "allowNoInt": true, + "tosValue": 192, + "rtProto": 3, + "rtTable": { + "main": 254, + "default": 254, + "tunnel": 254, + "priority": -1, + "tunnelPriority": -1, + "defaultOlsrPriority": -1, + "defaultPriority": -1 + }, + "willingness": { + "willingness": 3, + "auto": false, + "updateInterval": 20 + }, + "fib": { + "metric": "flat", + "metricDefault": "approx" + }, + "hysteresis": { + "enabled": false, + "scaling": 0.5, + "thresholdLow": 0.3, + "thresholdHigh": 0.8 + }, + "hna": [], + "pollrate": 0.05, + "nicChgsPollInt": 2.5, + "clearScreen": true, + "tcRedundancy": 2, + "mprCoverage": 7, + "linkQuality": { + "level": 2, + "fishEye": false, + "aging": 0.05, + "algorithm": "" + }, + "minTCVTime": 0, + "setIpForward": true, + "lockFile": "/var/run/h24_241506424687.8.lock", + "useNiit": false, + "smartGateway": { + "enabled": false, + "alwaysRemoveServerTunnel": false, + "allowNAT": true, + "uplinkNAT": true, + "useCount": 1, + "takeDownPercentage": 25, + "instanceId": "", + "policyRoutingScript": "", + "egress": { + "interfaces": [], + "interfacesCount": 0, + "file": "", + "filePeriod": 5000 + }, + "statusFile": "", + "tablesOffset": 90, + "rulesOffset": 0, + "period": 10000, + "stableCount": 6, + "threshold": 0, + "costsCalculation": { + "exitLinkUp": 1, + "exitLinkDown": 1, + "etx": 1, + "dividerEtx": 0 + }, + "maxCostMaxEtx": 2560, + "uplink": "both", + "bandwidth": { + "uplinkKbps": 128, + "downlinkKbps": 1024 + }, + "prefix": { + "prefix": "0.0.0.0", + "length": 0 + } + }, + "mainIp": "10.0.24.1", + "unicastSourceIpAddress": "10.0.24.1", + "srcIpRoutes": false, + "maxPrefixLength": 32, + "ipSize": 4, + "delgw": false, + "maxSendMessageJitter": 2, + "exitValue": 0, + "maxTcValidTime": 5, + "niit4to6InterfaceIndex": 0, + "niit6to4InterfaceIndex": 0, + "hasIpv4Gateway": false, + "hasIpv6Gateway": false, + "ioctlSocket": 3, + "routeNetlinkSocket": 4, + "routeMonitorSocket": 5, + "linkQualityNatThreshold": 1, + "brokenLinkCost": 4194304, + "brokenRouteCost": 4294967295, + "ipcConnectMaxConnections": 0, + "ipcConnectAllowed": [], + "interfaceDefaults": { + "ipv4Broadcast": "0.0.0.0", + "ipv6Multicast": "255.2.0.0", + "ipv4Source": "0.0.0.0", + "ipv6Source": "0.0.0.0", + "ipv6SourcePrefixLength": 0, + "mode": "mesh", + "weightValue": 0, + "weightFixed": false, + "hello": { + "emissionInterval": 2, + "validityTime": 20 + }, + "tc": { + "emissionInterval": 5, + "validityTime": 500 + }, + "mid": { + "emissionInterval": 5, + "validityTime": 300 + }, + "hna": { + "emissionInterval": 5, + "validityTime": 300 + }, + "linkQualityMultipliers": [], + "linkQualityMultipliersCount": 0, + "autoDetectChanges": true + }, + "os": "GNU/Linux", + "startTime": 1506424687 + } +} diff --git a/prince/data/jsoninfo2.json b/prince/data/jsoninfo2.json new file mode 100644 index 0000000..7b8ac06 --- /dev/null +++ b/prince/data/jsoninfo2.json @@ -0,0 +1,1416 @@ +{ + "pid": 15218, + "systemTime": 1506524789, + "timeSinceStartup": 303000, + "configurationChecksum": "2e4c422b", + "topology": [ + { + "lastHopIP": "10.0.0.1", + "pathCost": 5, + "validityTime": 486720, + "refCount": 3, + "msgSeq": 62428, + "msgHops": 4, + "hops": 5, + "ansn": 17, + "tcIgnored": 1, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.1.1", + "tcEdgeCost": 1, + "ansnEdge": 17, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.1.1", + "pathCost": 4, + "validityTime": 477580, + "refCount": 5, + "msgSeq": 4927, + "msgHops": 3, + "hops": 4, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.0.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.1.1", + "pathCost": 4, + "validityTime": 477580, + "refCount": 5, + "msgSeq": 4927, + "msgHops": 3, + "hops": 4, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.2.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.2.1", + "pathCost": 3, + "validityTime": 481493, + "refCount": 5, + "msgSeq": 6772, + "msgHops": 2, + "hops": 3, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.1.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.2.1", + "pathCost": 3, + "validityTime": 481493, + "refCount": 5, + "msgSeq": 6772, + "msgHops": 2, + "hops": 3, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.3.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.3.1", + "pathCost": 2, + "validityTime": 487250, + "refCount": 5, + "msgSeq": 8343, + "msgHops": 1, + "hops": 2, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.2.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.3.1", + "pathCost": 2, + "validityTime": 487250, + "refCount": 5, + "msgSeq": 8343, + "msgHops": 1, + "hops": 2, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.4.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.4.1", + "pathCost": 1, + "validityTime": 472429, + "refCount": 5, + "msgSeq": 22923, + "msgHops": 0, + "hops": 1, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.3.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.4.1", + "pathCost": 1, + "validityTime": 472429, + "refCount": 5, + "msgSeq": 22923, + "msgHops": 0, + "hops": 1, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.5.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.5.1", + "pathCost": 0, + "validityTime": 0, + "refCount": 4, + "msgSeq": 0, + "msgHops": 0, + "hops": 0, + "ansn": 0, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.4.1", + "tcEdgeCost": 1, + "ansnEdge": 0, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.5.1", + "pathCost": 0, + "validityTime": 0, + "refCount": 4, + "msgSeq": 0, + "msgHops": 0, + "hops": 0, + "ansn": 0, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.6.1", + "tcEdgeCost": 1, + "ansnEdge": 0, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.6.1", + "pathCost": 1, + "validityTime": 474153, + "refCount": 5, + "msgSeq": 46068, + "msgHops": 0, + "hops": 1, + "ansn": 30, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.5.1", + "tcEdgeCost": 1, + "ansnEdge": 30, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.6.1", + "pathCost": 1, + "validityTime": 474153, + "refCount": 5, + "msgSeq": 46068, + "msgHops": 0, + "hops": 1, + "ansn": 30, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.7.1", + "tcEdgeCost": 1, + "ansnEdge": 30, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.7.1", + "pathCost": 2, + "validityTime": 478118, + "refCount": 5, + "msgSeq": 35156, + "msgHops": 1, + "hops": 2, + "ansn": 40, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.6.1", + "tcEdgeCost": 1, + "ansnEdge": 40, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.7.1", + "pathCost": 2, + "validityTime": 478118, + "refCount": 5, + "msgSeq": 35156, + "msgHops": 1, + "hops": 2, + "ansn": 40, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.8.1", + "tcEdgeCost": 1, + "ansnEdge": 40, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.8.1", + "pathCost": 3, + "validityTime": 472881, + "refCount": 5, + "msgSeq": 29385, + "msgHops": 2, + "hops": 3, + "ansn": 33, + "tcIgnored": 1, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.7.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.8.1", + "pathCost": 3, + "validityTime": 472881, + "refCount": 5, + "msgSeq": 29385, + "msgHops": 2, + "hops": 3, + "ansn": 33, + "tcIgnored": 1, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.9.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.9.1", + "pathCost": 4, + "validityTime": 481188, + "refCount": 5, + "msgSeq": 64513, + "msgHops": 3, + "hops": 4, + "ansn": 36, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.8.1", + "tcEdgeCost": 1, + "ansnEdge": 36, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.9.1", + "pathCost": 4, + "validityTime": 481188, + "refCount": 5, + "msgSeq": 64513, + "msgHops": 3, + "hops": 4, + "ansn": 36, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.10.1", + "tcEdgeCost": 1, + "ansnEdge": 36, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.10.1", + "pathCost": 5, + "validityTime": 492469, + "refCount": 5, + "msgSeq": 53290, + "msgHops": 4, + "hops": 5, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.9.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.10.1", + "pathCost": 5, + "validityTime": 492469, + "refCount": 5, + "msgSeq": 53290, + "msgHops": 4, + "hops": 5, + "ansn": 28, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.11.1", + "tcEdgeCost": 1, + "ansnEdge": 28, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.11.1", + "pathCost": 6, + "validityTime": 494640, + "refCount": 5, + "msgSeq": 4636, + "msgHops": 5, + "hops": 6, + "ansn": 36, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.10.1", + "tcEdgeCost": 1, + "ansnEdge": 36, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.11.1", + "pathCost": 6, + "validityTime": 494640, + "refCount": 5, + "msgSeq": 4636, + "msgHops": 5, + "hops": 6, + "ansn": 36, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.12.1", + "tcEdgeCost": 1, + "ansnEdge": 36, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.12.1", + "pathCost": 7, + "validityTime": 481023, + "refCount": 5, + "msgSeq": 57813, + "msgHops": 6, + "hops": 7, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.11.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.12.1", + "pathCost": 7, + "validityTime": 481023, + "refCount": 5, + "msgSeq": 57813, + "msgHops": 6, + "hops": 7, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.13.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.13.1", + "pathCost": 8, + "validityTime": 471678, + "refCount": 5, + "msgSeq": 2026, + "msgHops": 7, + "hops": 8, + "ansn": 32, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.12.1", + "tcEdgeCost": 1, + "ansnEdge": 32, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.13.1", + "pathCost": 8, + "validityTime": 471678, + "refCount": 5, + "msgSeq": 2026, + "msgHops": 7, + "hops": 8, + "ansn": 32, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.14.1", + "tcEdgeCost": 1, + "ansnEdge": 32, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.14.1", + "pathCost": 9, + "validityTime": 478639, + "refCount": 5, + "msgSeq": 21572, + "msgHops": 8, + "hops": 9, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.13.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.14.1", + "pathCost": 9, + "validityTime": 478639, + "refCount": 5, + "msgSeq": 21572, + "msgHops": 8, + "hops": 9, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.15.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.15.1", + "pathCost": 10, + "validityTime": 467640, + "refCount": 5, + "msgSeq": 62282, + "msgHops": 9, + "hops": 10, + "ansn": 32, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.14.1", + "tcEdgeCost": 1, + "ansnEdge": 32, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.15.1", + "pathCost": 10, + "validityTime": 467640, + "refCount": 5, + "msgSeq": 62282, + "msgHops": 9, + "hops": 10, + "ansn": 32, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.16.1", + "tcEdgeCost": 1, + "ansnEdge": 32, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.16.1", + "pathCost": 11, + "validityTime": 471231, + "refCount": 5, + "msgSeq": 11816, + "msgHops": 10, + "hops": 11, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.15.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.16.1", + "pathCost": 11, + "validityTime": 471231, + "refCount": 5, + "msgSeq": 11816, + "msgHops": 10, + "hops": 11, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.17.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.17.1", + "pathCost": 12, + "validityTime": 490984, + "refCount": 5, + "msgSeq": 33172, + "msgHops": 11, + "hops": 12, + "ansn": 29, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.16.1", + "tcEdgeCost": 1, + "ansnEdge": 29, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.17.1", + "pathCost": 12, + "validityTime": 490984, + "refCount": 5, + "msgSeq": 33172, + "msgHops": 11, + "hops": 12, + "ansn": 29, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.18.1", + "tcEdgeCost": 1, + "ansnEdge": 29, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.18.1", + "pathCost": 13, + "validityTime": 487690, + "refCount": 5, + "msgSeq": 31724, + "msgHops": 12, + "hops": 13, + "ansn": 30, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.17.1", + "tcEdgeCost": 1, + "ansnEdge": 30, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.18.1", + "pathCost": 13, + "validityTime": 487690, + "refCount": 5, + "msgSeq": 31724, + "msgHops": 12, + "hops": 13, + "ansn": 30, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.19.1", + "tcEdgeCost": 1, + "ansnEdge": 30, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.19.1", + "pathCost": 14, + "validityTime": 479889, + "refCount": 5, + "msgSeq": 60346, + "msgHops": 13, + "hops": 14, + "ansn": 29, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.18.1", + "tcEdgeCost": 1, + "ansnEdge": 29, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.19.1", + "pathCost": 14, + "validityTime": 479889, + "refCount": 5, + "msgSeq": 60346, + "msgHops": 13, + "hops": 14, + "ansn": 29, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.20.1", + "tcEdgeCost": 1, + "ansnEdge": 29, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.20.1", + "pathCost": 15, + "validityTime": 481825, + "refCount": 5, + "msgSeq": 54734, + "msgHops": 14, + "hops": 15, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.19.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.20.1", + "pathCost": 15, + "validityTime": 481825, + "refCount": 5, + "msgSeq": 54734, + "msgHops": 14, + "hops": 15, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.21.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.21.1", + "pathCost": 16, + "validityTime": 483412, + "refCount": 5, + "msgSeq": 17605, + "msgHops": 15, + "hops": 16, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.20.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.21.1", + "pathCost": 16, + "validityTime": 483412, + "refCount": 5, + "msgSeq": 17605, + "msgHops": 15, + "hops": 16, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.22.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.22.1", + "pathCost": 17, + "validityTime": 477314, + "refCount": 5, + "msgSeq": 49175, + "msgHops": 16, + "hops": 17, + "ansn": 25, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.21.1", + "tcEdgeCost": 1, + "ansnEdge": 25, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.22.1", + "pathCost": 17, + "validityTime": 477314, + "refCount": 5, + "msgSeq": 49175, + "msgHops": 16, + "hops": 17, + "ansn": 25, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.23.1", + "tcEdgeCost": 1, + "ansnEdge": 25, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.23.1", + "pathCost": 18, + "validityTime": 470546, + "refCount": 5, + "msgSeq": 17401, + "msgHops": 17, + "hops": 18, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.22.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.23.1", + "pathCost": 18, + "validityTime": 470546, + "refCount": 5, + "msgSeq": 17401, + "msgHops": 17, + "hops": 18, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.24.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.24.1", + "pathCost": 19, + "validityTime": 475150, + "refCount": 5, + "msgSeq": 47398, + "msgHops": 18, + "hops": 19, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.23.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.24.1", + "pathCost": 19, + "validityTime": 475150, + "refCount": 5, + "msgSeq": 47398, + "msgHops": 18, + "hops": 19, + "ansn": 34, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.25.1", + "tcEdgeCost": 1, + "ansnEdge": 34, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.25.1", + "pathCost": 20, + "validityTime": 475419, + "refCount": 5, + "msgSeq": 18476, + "msgHops": 19, + "hops": 20, + "ansn": 40, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.24.1", + "tcEdgeCost": 1, + "ansnEdge": 40, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.25.1", + "pathCost": 20, + "validityTime": 475419, + "refCount": 5, + "msgSeq": 18476, + "msgHops": 19, + "hops": 20, + "ansn": 40, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.26.1", + "tcEdgeCost": 1, + "ansnEdge": 40, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.26.1", + "pathCost": 21, + "validityTime": 469267, + "refCount": 5, + "msgSeq": 29338, + "msgHops": 20, + "hops": 21, + "ansn": 35, + "tcIgnored": 1, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.25.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.26.1", + "pathCost": 21, + "validityTime": 469267, + "refCount": 5, + "msgSeq": 29338, + "msgHops": 20, + "hops": 21, + "ansn": 35, + "tcIgnored": 1, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.27.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.27.1", + "pathCost": 22, + "validityTime": 475881, + "refCount": 5, + "msgSeq": 24314, + "msgHops": 21, + "hops": 22, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.26.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.27.1", + "pathCost": 22, + "validityTime": 475881, + "refCount": 5, + "msgSeq": 24314, + "msgHops": 21, + "hops": 22, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.28.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.28.1", + "pathCost": 23, + "validityTime": 479290, + "refCount": 5, + "msgSeq": 55116, + "msgHops": 22, + "hops": 23, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.27.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.28.1", + "pathCost": 23, + "validityTime": 479290, + "refCount": 5, + "msgSeq": 55116, + "msgHops": 22, + "hops": 23, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.29.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.29.1", + "pathCost": 24, + "validityTime": 468770, + "refCount": 5, + "msgSeq": 39285, + "msgHops": 23, + "hops": 24, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.28.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.29.1", + "pathCost": 24, + "validityTime": 468770, + "refCount": 5, + "msgSeq": 39285, + "msgHops": 23, + "hops": 24, + "ansn": 31, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.30.1", + "tcEdgeCost": 1, + "ansnEdge": 31, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.30.1", + "pathCost": 25, + "validityTime": 483904, + "refCount": 5, + "msgSeq": 55286, + "msgHops": 24, + "hops": 25, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.29.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.30.1", + "pathCost": 25, + "validityTime": 483904, + "refCount": 5, + "msgSeq": 55286, + "msgHops": 24, + "hops": 25, + "ansn": 39, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.31.1", + "tcEdgeCost": 1, + "ansnEdge": 39, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.31.1", + "pathCost": 26, + "validityTime": 482764, + "refCount": 5, + "msgSeq": 34415, + "msgHops": 25, + "hops": 26, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.30.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.31.1", + "pathCost": 26, + "validityTime": 482764, + "refCount": 5, + "msgSeq": 34415, + "msgHops": 25, + "hops": 26, + "ansn": 33, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.32.1", + "tcEdgeCost": 1, + "ansnEdge": 33, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.32.1", + "pathCost": 27, + "validityTime": 479424, + "refCount": 5, + "msgSeq": 35325, + "msgHops": 26, + "hops": 27, + "ansn": 30, + "tcIgnored": 1, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.31.1", + "tcEdgeCost": 1, + "ansnEdge": 30, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.32.1", + "pathCost": 27, + "validityTime": 479424, + "refCount": 5, + "msgSeq": 35325, + "msgHops": 26, + "hops": 27, + "ansn": 30, + "tcIgnored": 1, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.33.1", + "tcEdgeCost": 1, + "ansnEdge": 30, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.33.1", + "pathCost": 28, + "validityTime": 486199, + "refCount": 5, + "msgSeq": 56422, + "msgHops": 27, + "hops": 28, + "ansn": 35, + "tcIgnored": 1, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.32.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.33.1", + "pathCost": 28, + "validityTime": 486199, + "refCount": 5, + "msgSeq": 56422, + "msgHops": 27, + "hops": 28, + "ansn": 35, + "tcIgnored": 1, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.34.1", + "tcEdgeCost": 1, + "ansnEdge": 35, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.34.1", + "pathCost": 29, + "validityTime": 470736, + "refCount": 5, + "msgSeq": 31038, + "msgHops": 28, + "hops": 29, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.33.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.34.1", + "pathCost": 29, + "validityTime": 470736, + "refCount": 5, + "msgSeq": 31038, + "msgHops": 28, + "hops": 29, + "ansn": 38, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.35.1", + "tcEdgeCost": 1, + "ansnEdge": 38, + "linkQuality": 1, + "neighborLinkQuality": 1 + }, + { + "lastHopIP": "10.0.35.1", + "pathCost": 30, + "validityTime": 488865, + "refCount": 3, + "msgSeq": 13934, + "msgHops": 29, + "hops": 30, + "ansn": 21, + "tcIgnored": 0, + "errSeq": 0, + "errSeqValid": false, + "destinationIP": "10.0.34.1", + "tcEdgeCost": 1, + "ansnEdge": 21, + "linkQuality": 1, + "neighborLinkQuality": 1 + } + ], + "config": { + "configurationChecksum": "2e4c422b", + "cli": [ + "../olsrd/olsrd", + "-f", + "/home/gabriel/git/wcn_emulator/LINEAR36_LINEAR36_1506524464_250/h5_5_olsr.conf" + ], + "configurationFile": "/home/gabriel/git/wcn_emulator/LINEAR36_LINEAR36_1506524464_250/h5_5_olsr.conf", + "olsrPort": 698, + "debugLevel": 1, + "noFork": false, + "pidFile": "", + "hostEmulation": false, + "ipVersion": 4, + "allowNoInt": true, + "tosValue": 192, + "rtProto": 3, + "rtTable": { + "main": 254, + "default": 254, + "tunnel": 254, + "priority": -1, + "tunnelPriority": -1, + "defaultOlsrPriority": -1, + "defaultPriority": -1 + }, + "willingness": { + "willingness": 3, + "auto": false, + "updateInterval": 20 + }, + "fib": { + "metric": "flat", + "metricDefault": "approx" + }, + "hysteresis": { + "enabled": false, + "scaling": 0.5, + "thresholdLow": 0.3, + "thresholdHigh": 0.8 + }, + "hna": [], + "pollrate": 0.05, + "nicChgsPollInt": 2.5, + "clearScreen": true, + "tcRedundancy": 2, + "mprCoverage": 7, + "linkQuality": { + "level": 2, + "fishEye": false, + "aging": 0.05, + "algorithm": "" + }, + "minTCVTime": 0, + "setIpForward": true, + "lockFile": "/var/run/h5_51506524486.7.lock", + "useNiit": false, + "smartGateway": { + "enabled": false, + "alwaysRemoveServerTunnel": false, + "allowNAT": true, + "uplinkNAT": true, + "useCount": 1, + "takeDownPercentage": 25, + "instanceId": "", + "policyRoutingScript": "", + "egress": { + "interfaces": [], + "interfacesCount": 0, + "file": "", + "filePeriod": 5000 + }, + "statusFile": "", + "tablesOffset": 90, + "rulesOffset": 0, + "period": 10000, + "stableCount": 6, + "threshold": 0, + "costsCalculation": { + "exitLinkUp": 1, + "exitLinkDown": 1, + "etx": 1, + "dividerEtx": 0 + }, + "maxCostMaxEtx": 2560, + "uplink": "both", + "bandwidth": { + "uplinkKbps": 128, + "downlinkKbps": 1024 + }, + "prefix": { + "prefix": "0.0.0.0", + "length": 0 + } + }, + "mainIp": "10.0.5.1", + "unicastSourceIpAddress": "10.0.5.1", + "srcIpRoutes": false, + "maxPrefixLength": 32, + "ipSize": 4, + "delgw": false, + "maxSendMessageJitter": 2, + "exitValue": 0, + "maxTcValidTime": 5, + "niit4to6InterfaceIndex": 0, + "niit6to4InterfaceIndex": 0, + "hasIpv4Gateway": false, + "hasIpv6Gateway": false, + "ioctlSocket": 3, + "routeNetlinkSocket": 4, + "routeMonitorSocket": 5, + "linkQualityNatThreshold": 1, + "brokenLinkCost": 4194304, + "brokenRouteCost": 4294967295, + "ipcConnectMaxConnections": 0, + "ipcConnectAllowed": [], + "interfaceDefaults": { + "ipv4Broadcast": "0.0.0.0", + "ipv6Multicast": "255.2.0.0", + "ipv4Source": "0.0.0.0", + "ipv6Source": "0.0.0.0", + "ipv6SourcePrefixLength": 0, + "mode": "mesh", + "weightValue": 0, + "weightFixed": false, + "hello": { + "emissionInterval": 2, + "validityTime": 20 + }, + "tc": { + "emissionInterval": 5, + "validityTime": 500 + }, + "mid": { + "emissionInterval": 5, + "validityTime": 300 + }, + "hna": { + "emissionInterval": 5, + "validityTime": 300 + }, + "linkQualityMultipliers": [], + "linkQualityMultipliersCount": 0, + "autoDetectChanges": true + }, + "os": "GNU/Linux", + "startTime": 1506524486 + } +} diff --git a/prince/data/netjson.json b/prince/data/netjson.json new file mode 100644 index 0000000..973d5cd --- /dev/null +++ b/prince/data/netjson.json @@ -0,0 +1,654 @@ +{ + "type": "NetworkGraph", + "protocol": "olsrv1", + "version": "pre-0.9.7", + "metric": "", + "revision": "olsr.org - pre-0.9.7-git_3126ae15-hash_b723fbce0dad64eddbe85573f39bd5a0", + "router_id": "10.0.4.1", + "nodes": [ + { + "id": "10.0.0.1" + }, + { + "id": "10.0.1.1", + "local_addresses": [ + "10.0.1.2" + ] + }, + { + "id": "10.0.2.1", + "local_addresses": [ + "10.0.2.2" + ] + }, + { + "id": "10.0.3.1", + "local_addresses": [ + "10.0.3.2" + ] + }, + { + "id": "10.0.4.1", + "local_addresses": [ + "10.0.4.2" + ] + }, + { + "id": "10.0.5.1", + "local_addresses": [ + "10.0.5.2" + ] + }, + { + "id": "10.0.6.1", + "local_addresses": [ + "10.0.6.2" + ] + }, + { + "id": "10.0.7.1", + "local_addresses": [ + "10.0.7.2" + ] + }, + { + "id": "10.0.8.1", + "local_addresses": [ + "10.0.8.2" + ] + }, + { + "id": "10.0.9.1", + "local_addresses": [ + "10.0.9.2" + ] + }, + { + "id": "10.0.10.1", + "local_addresses": [ + "10.0.10.2" + ] + }, + { + "id": "10.0.11.1", + "local_addresses": [ + "10.0.11.2" + ] + }, + { + "id": "10.0.12.1", + "local_addresses": [ + "10.0.12.2" + ] + }, + { + "id": "10.0.13.1", + "local_addresses": [ + "10.0.13.2" + ] + }, + { + "id": "10.0.14.1", + "local_addresses": [ + "10.0.14.2" + ] + }, + { + "id": "10.0.15.1", + "local_addresses": [ + "10.0.15.2" + ] + }, + { + "id": "10.0.16.1", + "local_addresses": [ + "10.0.16.2" + ] + }, + { + "id": "10.0.17.1", + "local_addresses": [ + "10.0.17.2" + ] + }, + { + "id": "10.0.18.1", + "local_addresses": [ + "10.0.18.2" + ] + }, + { + "id": "10.0.19.1", + "local_addresses": [ + "10.0.19.2" + ] + }, + { + "id": "10.0.20.1", + "local_addresses": [ + "10.0.20.2" + ] + }, + { + "id": "10.0.21.1", + "local_addresses": [ + "10.0.21.2" + ] + }, + { + "id": "10.0.22.1", + "local_addresses": [ + "10.0.22.2" + ] + }, + { + "id": "10.0.23.1", + "local_addresses": [ + "10.0.23.2" + ] + }, + { + "id": "10.0.24.1", + "local_addresses": [ + "10.0.24.2" + ] + }, + { + "id": "10.0.25.1", + "local_addresses": [ + "10.0.25.2" + ] + }, + { + "id": "10.0.26.1", + "local_addresses": [ + "10.0.26.2" + ] + }, + { + "id": "10.0.27.1", + "local_addresses": [ + "10.0.27.2" + ] + }, + { + "id": "10.0.28.1", + "local_addresses": [ + "10.0.28.2" + ] + }, + { + "id": "10.0.29.1", + "local_addresses": [ + "10.0.29.2" + ] + }, + { + "id": "10.0.30.1", + "local_addresses": [ + "10.0.30.2" + ] + }, + { + "id": "10.0.31.1", + "local_addresses": [ + "10.0.31.2" + ] + }, + { + "id": "10.0.32.1", + "local_addresses": [ + "10.0.32.2" + ] + }, + { + "id": "10.0.33.1", + "local_addresses": [ + "10.0.33.2" + ] + }, + { + "id": "10.0.34.1", + "local_addresses": [ + "10.0.34.2" + ] + }, + { + "id": "10.0.35.1" + } + ], + "links": [ + { + "source": "10.0.4.1", + "target": "10.0.3.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.3.1", + "target": "10.0.4.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.4.1", + "target": "10.0.5.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.5.1", + "target": "10.0.4.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.0.1", + "target": "10.0.1.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.1.1", + "target": "10.0.0.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.1.1", + "target": "10.0.2.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.2.1", + "target": "10.0.1.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.2.1", + "target": "10.0.3.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.3.1", + "target": "10.0.2.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.3.1", + "target": "10.0.4.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.5.1", + "target": "10.0.4.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.5.1", + "target": "10.0.6.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.6.1", + "target": "10.0.5.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.6.1", + "target": "10.0.7.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.7.1", + "target": "10.0.6.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.7.1", + "target": "10.0.8.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.8.1", + "target": "10.0.7.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.8.1", + "target": "10.0.9.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.9.1", + "target": "10.0.8.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.9.1", + "target": "10.0.10.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.10.1", + "target": "10.0.9.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.10.1", + "target": "10.0.11.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.11.1", + "target": "10.0.10.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.11.1", + "target": "10.0.12.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.12.1", + "target": "10.0.11.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.12.1", + "target": "10.0.13.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.13.1", + "target": "10.0.12.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.13.1", + "target": "10.0.14.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.14.1", + "target": "10.0.13.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.14.1", + "target": "10.0.15.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.15.1", + "target": "10.0.14.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.15.1", + "target": "10.0.16.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.16.1", + "target": "10.0.15.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.16.1", + "target": "10.0.17.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.17.1", + "target": "10.0.16.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.17.1", + "target": "10.0.18.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.18.1", + "target": "10.0.17.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.18.1", + "target": "10.0.19.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.19.1", + "target": "10.0.18.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.19.1", + "target": "10.0.20.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.20.1", + "target": "10.0.19.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.20.1", + "target": "10.0.21.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.21.1", + "target": "10.0.20.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.21.1", + "target": "10.0.22.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.22.1", + "target": "10.0.21.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.22.1", + "target": "10.0.23.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.23.1", + "target": "10.0.22.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.23.1", + "target": "10.0.24.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.24.1", + "target": "10.0.23.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.24.1", + "target": "10.0.25.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.25.1", + "target": "10.0.24.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.25.1", + "target": "10.0.26.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.26.1", + "target": "10.0.25.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.26.1", + "target": "10.0.27.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.27.1", + "target": "10.0.26.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.27.1", + "target": "10.0.28.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.28.1", + "target": "10.0.27.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.28.1", + "target": "10.0.29.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.29.1", + "target": "10.0.28.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.29.1", + "target": "10.0.30.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.30.1", + "target": "10.0.29.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.30.1", + "target": "10.0.31.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.31.1", + "target": "10.0.30.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.31.1", + "target": "10.0.32.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.32.1", + "target": "10.0.31.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.32.1", + "target": "10.0.33.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.33.1", + "target": "10.0.32.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.33.1", + "target": "10.0.34.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.34.1", + "target": "10.0.33.1", + "cost": 1, + "cost_text": "1.000" + }, + { + "source": "10.0.34.1", + "target": "10.0.35.1", + "cost": 1, + "cost_text": "6.396" + }, + { + "source": "10.0.35.1", + "target": "10.0.34.1", + "cost": 1, + "cost_text": "10.160" + } + ] +} diff --git a/prince/data/topolgy.json b/prince/data/topolgy.json new file mode 100755 index 0000000..1929205 --- /dev/null +++ b/prince/data/topolgy.json @@ -0,0 +1 @@ +{"type": "NetworkGraph", "protocol": "olsrv2", "version": "poprouting custom", "revision": "0.11.3", "metric": "ff_dat_metric", "router_id": "h9_9", "nodes": [{"id": "h4_4"}, {"id": "h28_28"}, {"id": "h24_24"}, {"id": "h25_25"}, {"id": "h3_3"}, {"id": "h33_33"}, {"id": "h30_30"}, {"id": "h5_5"}, {"id": "h29_29"}, {"id": "h0_0"}, {"id": "h23_23"}, {"id": "h7_7"}, {"id": "h14_14"}, {"id": "h6_6"}, {"id": "h2_2"}, {"id": "h1_1"}, {"id": "h21_21"}, {"id": "h11_11"}, {"id": "h34_34"}, {"id": "h18_18"}, {"id": "h32_32"}, {"id": "h31_31"}, {"id": "h20_20"}, {"id": "h15_15"}, {"id": "h16_16"}, {"id": "h22_22"}, {"id": "h8_8"}, {"id": "h12_12"}, {"id": "h26_26"}, {"id": "h17_17"}, {"id": "h9_9"}, {"id": "h27_27"}, {"id": "h19_19"}, {"id": "h10_10"}, {"id": "h13_13"}, {"id": "h35_35"}], "links": [{"source": "h4_4", "cost": 1.0, "target": "h5_5"}, {"source": "h4_4", "cost": 1.0, "target": "h3_3"}, {"source": "h28_28", "cost": 1.0, "target": "h29_29"}, {"source": "h28_28", "cost": 1.0, "target": "h27_27"}, {"source": "h24_24", "cost": 1.0, "target": "h25_25"}, {"source": "h24_24", "cost": 1.0, "target": "h23_23"}, {"source": "h25_25", "cost": 1.0, "target": "h26_26"}, {"source": "h3_3", "cost": 1.0, "target": "h2_2"}, {"source": "h33_33", "cost": 1.0, "target": "h34_34"}, {"source": "h33_33", "cost": 1.0, "target": "h32_32"}, {"source": "h30_30", "cost": 1.0, "target": "h29_29"}, {"source": "h30_30", "cost": 1.0, "target": "h31_31"}, {"source": "h5_5", "cost": 1.0, "target": "h6_6"}, {"source": "h0_0", "cost": 1.0, "target": "h1_1"}, {"source": "h23_23", "cost": 1.0, "target": "h22_22"}, {"source": "h7_7", "cost": 1.0, "target": "h8_8"}, {"source": "h7_7", "cost": 1.0, "target": "h6_6"}, {"source": "h14_14", "cost": 1.0, "target": "h13_13"}, {"source": "h14_14", "cost": 1.0, "target": "h15_15"}, {"source": "h2_2", "cost": 1.0, "target": "h1_1"}, {"source": "h21_21", "cost": 1.0, "target": "h20_20"}, {"source": "h21_21", "cost": 1.0, "target": "h22_22"}, {"source": "h11_11", "cost": 1.0, "target": "h12_12"}, {"source": "h11_11", "cost": 1.0, "target": "h10_10"}, {"source": "h34_34", "cost": 1.0, "target": "h35_35"}, {"source": "h18_18", "cost": 1.0, "target": "h17_17"}, {"source": "h18_18", "cost": 1.0, "target": "h19_19"}, {"source": "h32_32", "cost": 1.0, "target": "h31_31"}, {"source": "h20_20", "cost": 1.0, "target": "h19_19"}, {"source": "h15_15", "cost": 1.0, "target": "h16_16"}, {"source": "h16_16", "cost": 1.0, "target": "h17_17"}, {"source": "h8_8", "cost": 1.0, "target": "h9_9"}, {"source": "h12_12", "cost": 1.0, "target": "h13_13"}, {"source": "h26_26", "cost": 1.0, "target": "h27_27"}, {"source": "h9_9", "cost": 1.0, "target": "h10_10"}]} \ No newline at end of file diff --git a/prince/include/common.h b/prince/include/common.h index 926a73c..7163af4 100644 --- a/prince/include/common.h +++ b/prince/include/common.h @@ -14,7 +14,7 @@ #define LINE_SIZE 64 -struct timers{ +struct timers { double h_timer; double tc_timer; double exec_time; @@ -22,8 +22,7 @@ struct timers{ }; -typedef struct -routing_plugin_{ +typedef struct routing_plugin_ { char *recv_buffer; char *self_id; char *host; @@ -36,11 +35,12 @@ routing_plugin_{ } routing_plugin; -routing_plugin* (*new_plugin_p)(char* host, int port, int json_type, int timer_port); +routing_plugin *(*new_plugin_p)(char *host, int port, int json_type, + int timer_port); -int (*get_initial_timers_p) (routing_plugin *o, struct timers *t); -int (*get_topology_p) (routing_plugin *o); -int (*push_timers_p) (routing_plugin *o, struct timers t); -void (*delete_plugin_p) (routing_plugin *o); +int (*get_initial_timers_p)(routing_plugin *o, struct timers *t); +int (*get_topology_p)(routing_plugin *o); +int (*push_timers_p)(routing_plugin *o, struct timers t); +void (*delete_plugin_p)(routing_plugin *o); #endif /* SRC_COMMON_H_ */ diff --git a/prince/include/config.h b/prince/include/config.h index 0192286..eea27c6 100644 --- a/prince/include/config.h +++ b/prince/include/config.h @@ -5,8 +5,8 @@ #include "prince_handler.h" -int parse_json_config(const char *filepath, prince_handler_t ph); -char * read_file_content(const char *filename); -int read_config_file(prince_handler_t ph, const char *filepath); +int parse_json_config(const char *filepath, prince_handler_t ph); +char *read_file_content(const char *filename); +int read_config_file(prince_handler_t ph, const char *filepath); #endif /* SRC_CONFIG_H_ */ diff --git a/prince/include/config_graph.h b/prince/include/config_graph.h index fe24e47..9b57c52 100644 --- a/prince/include/config_graph.h +++ b/prince/include/config_graph.h @@ -3,28 +3,28 @@ #include -typedef struct graph_config * graph_config_t; +typedef struct graph_config *graph_config_t; #include "config.h" #include "prince.h" #include "prince_handler.h" -#define GRAPH_CONFIG_SIZE sizeof(struct proto_config) +#define GRAPH_CONFIG_SIZE sizeof(struct proto_config) #define INVALID_GRAPH_CONFIG NULL struct graph_config { - bool heuristic; - bool weights; - bool recursive; - bool stop_unchanged; - bool multithreaded; + bool heuristic; + bool weights; + bool recursive; + bool stop_unchanged; + bool multithreaded; }; graph_config_t new_graph_config(void); -void free_graph_config(graph_config_t conf); -int load_graph_config(const char *filepath, graph_config_t conf); +void free_graph_config(graph_config_t conf); +int load_graph_config(const char *filepath, graph_config_t conf); #endif /* SRC_CONFIG_GRAPH_H_ */ diff --git a/prince/include/config_proto.h b/prince/include/config_proto.h index d47f924..78776a7 100644 --- a/prince/include/config_proto.h +++ b/prince/include/config_proto.h @@ -3,24 +3,24 @@ #include -typedef struct proto_config * proto_config_t; +typedef struct proto_config *proto_config_t; #include "config.h" -#define PROTO_CONFIG_SIZE sizeof(struct proto_config) +#define PROTO_CONFIG_SIZE sizeof(struct proto_config) #define INVALID_PROTO_CONFIG NULL struct proto_config { - char *proto; - int json_type; - int port; - int refresh; - int sleep_onfail; - int timer_port; + char *proto; + int json_type; + int port; + int refresh; + int sleep_onfail; + int timer_port; }; proto_config_t new_proto_config(void); -void free_proto_config(proto_config_t conf); -int load_proto_config(const char *filepath, proto_config_t conf); +void free_proto_config(proto_config_t conf); +int load_proto_config(const char *filepath, proto_config_t conf); #endif /* SRC_CONFIG_PROTO_H */ diff --git a/prince/include/load_plugin.h b/prince/include/load_plugin.h index 4a9e15a..a4049f1 100644 --- a/prince/include/load_plugin.h +++ b/prince/include/load_plugin.h @@ -8,5 +8,6 @@ int load_routing_plugin(prince_handler_t result); int free_routing_plugin(prince_handler_t result); -int load_routing_plugin_symbol(prince_handler_t result, const char* symbol_name); +int load_routing_plugin_symbol(prince_handler_t result, + const char *symbol_name); #endif /* LOAD_PLUGIN_H_ */ diff --git a/prince/include/plugin_interface.h b/prince/include/plugin_interface.h index ebc4309..5903b02 100644 --- a/prince/include/plugin_interface.h +++ b/prince/include/plugin_interface.h @@ -4,10 +4,10 @@ #include "common.h" -routing_plugin* new_plugin(char* host, int port, int json_type, int timer_port); -int get_topology(routing_plugin *o); -int get_initial_timers(routing_plugin *o, struct timers *t); -int push_timers(routing_plugin *o, struct timers t); -void delete_plugin(routing_plugin* o); +routing_plugin *new_plugin(char *host, int port, int json_type, int timer_port); +int get_topology(routing_plugin *o); +int get_initial_timers(routing_plugin *o, struct timers *t); +int push_timers(routing_plugin *o, struct timers t); +void delete_plugin(routing_plugin *o); #endif diff --git a/prince/include/prince.h b/prince/include/prince.h index 478f8c6..e627a85 100644 --- a/prince/include/prince.h +++ b/prince/include/prince.h @@ -16,9 +16,9 @@ #include "topology_parser.h" -int compute_constants(prince_handler_t ph); -int compute_timers(prince_handler_t ph); -double get_self_bc(prince_handler_t ph); -void log_line(char *text, prince_handler_t ph); -void signal_callback_handler(int signum); +int compute_constants(prince_handler_t ph); +int compute_timers(prince_handler_t ph); +double get_self_bc(prince_handler_t ph); +void log_line(char *text, prince_handler_t ph); +void signal_callback_handler(int signum); #endif /* SRC_PRINCE_H_ */ diff --git a/prince/include/prince_handler.h b/prince/include/prince_handler.h index 7316b15..5deb2c3 100644 --- a/prince/include/prince_handler.h +++ b/prince/include/prince_handler.h @@ -4,7 +4,7 @@ #include #include -typedef struct prince_handler * prince_handler_t; +typedef struct prince_handler *prince_handler_t; #include "common.h" #include "config.h" @@ -21,22 +21,22 @@ struct constants { struct prince_handler { - bool heuristic, weights, recursive, stop_unchanged, multithreaded; - c_graph_parser *gp; - char *command, *proto, *log_file; - char *self_id, *host; - int port, refresh, json_type, sleep_onfail, timer_port; + bool heuristic, weights, recursive, stop_unchanged, multithreaded; + c_graph_parser *gp; + char *command, *proto, *log_file; + char *self_id, *host; + int port, refresh, json_type, sleep_onfail, timer_port; map_id_degree_bc *bc_degree_map; - routing_plugin *rp; - struct timers def_t, opt_t; + routing_plugin *rp; + struct timers def_t, opt_t; struct constants c; - void *plugin_handle; - proto_config_t proto_config; - graph_config_t graph_config; + void *plugin_handle; + proto_config_t proto_config; + graph_config_t graph_config; }; -prince_handler_t new_prince_handler(const char * conf_file); -int free_prince_handler(prince_handler_t ph); -int update_prince_id(prince_handler_t ph); +prince_handler_t new_prince_handler(const char *conf_file); +int free_prince_handler(prince_handler_t ph); +int update_prince_id(prince_handler_t ph); #endif /*SRC_PRINCE_HANDLER_H_*/ diff --git a/prince/include/socket.h b/prince/include/socket.h index 9c54bf4..d02365d 100644 --- a/prince/include/socket.h +++ b/prince/include/socket.h @@ -18,7 +18,7 @@ /*FUNCTIONS DECLARATION*/ -int _create_socket(char* destinazione, int porta, int ignore); +int _create_socket(char *destinazione, int porta, int ignore); int _http_receive(int sd, char **buffer); int _telnet_receive(int sd, char **finalBuffer); int _receive_data(int sd, char **finalBuffer); diff --git a/prince/include/topology.h b/prince/include/topology.h index 68bb5ea..574b2e0 100644 --- a/prince/include/topology.h +++ b/prince/include/topology.h @@ -11,17 +11,17 @@ typedef struct node * node_t; struct node { - char *id; - struct neighbor *neighbor_list; - node_t next; - struct local_address *addresses; + char *id; + struct neighbor *neighbor_list; + node_t next; + struct local_address *addresses; }; struct topology { - int id_lenght; - char *protocol; - char *self_id; - node_t first; + int id_lenght; + char *protocol; + char *self_id; + node_t first; }; topology_t new_topo(int type); diff --git a/prince/include/topology_parser.h b/prince/include/topology_parser.h index bc265ac..17927ca 100644 --- a/prince/include/topology_parser.h +++ b/prince/include/topology_parser.h @@ -9,29 +9,29 @@ /* bc degree structures definition*/ typedef struct _id_degree_bc{ - char* id; - double bc; - int degree; + char* id; + double bc; + int degree; } id_degree_bc; typedef struct _map_id_degree_bc{ - id_degree_bc *map; - size_t size; - int n_edges; + id_degree_bc *map; + size_t size; + int n_edges; } map_id_degree_bc; struct neighbor { - node_t id; - float weight; - int validity; - struct neighbor *next; + node_t id; + float weight; + int validity; + struct neighbor *next; }; struct local_address { - const char *id; - struct local_address *next; + const char *id; + struct local_address *next; }; struct neighbor* find_neigh(node_t source, node_t target); diff --git a/prince/lib/olsrd/olsr.c b/prince/lib/olsrd/olsr.c index 6bbd52c..9480ad8 100644 --- a/prince/lib/olsrd/olsr.c +++ b/prince/lib/olsrd/olsr.c @@ -7,22 +7,24 @@ * @param proto type of the remote plugin (0->netjson 1->jsoninfo) * @return pointer to olsr plugin handler */ -routing_plugin* new_plugin(char* host, int port, int json_type, int timer_port) { - routing_plugin *o = (routing_plugin *) malloc(sizeof(routing_plugin)); - if (o == NULL) { - perror("olsr-new"); - return NULL; - } - o->port=port; - o->host=strdup(host); - o->recv_buffer=0; - o->self_id=0; - o->json_type=json_type; +routing_plugin *new_plugin(char *host, int port, int json_type, int timer_port) +{ + routing_plugin *o = (routing_plugin *)malloc(sizeof(routing_plugin)); + if (o == NULL) { + perror("olsr-new"); + return NULL; + } + o->port = port; + o->host = strdup(host); + o->recv_buffer = 0; + o->self_id = 0; + o->json_type = json_type; o->timer_port = timer_port; return o; } -int get_initial_timers(routing_plugin *o, struct timers *t){ +int get_initial_timers(routing_plugin *o, struct timers *t) +{ t->h_timer = get_initial_timer(o, "/HelloTimer"); t->tc_timer = get_initial_timer(o, "/TcTimer"); if (t->h_timer == -1) { @@ -38,25 +40,26 @@ int get_initial_timers(routing_plugin *o, struct timers *t){ return 0; } -float get_initial_timer(routing_plugin* o, char* cmd){ - o->sd =_create_socket(o->host, o->timer_port, ECONNREFUSED); +float get_initial_timer(routing_plugin *o, char *cmd) +{ + o->sd = _create_socket(o->host, o->timer_port, ECONNREFUSED); char *page; char *token; - float value=0; - page = (char*) malloc(sizeof(char) * 24); - if (page == NULL) { - perror("olsr"); - return -1; - } + float value = 0; + page = (char *)malloc(sizeof(char) * 24); + if (page == NULL) { + perror("olsr"); + return -1; + } write(o->sd, cmd, strlen(cmd)); - if(recv(o->sd, page, strlen(cmd), 0)>0){ + if (recv(o->sd, page, strlen(cmd), 0) > 0) { token = strtok(page, ":"); token = strtok(NULL, ":"); value = atof(token); } else { - fprintf(stderr, "Could not recieve timer %s\n", cmd); - return -1; - } + fprintf(stderr, "Could not recieve timer %s\n", cmd); + return -1; + } close(o->sd); free(page); return value; @@ -70,72 +73,70 @@ float get_initial_timer(routing_plugin* o, char* cmd){ int get_topology(routing_plugin *o) /*netjson & jsoninfo*/ { int sent; - if((o->sd= _create_socket(o->host, o->port, 0))==0){ + if ((o->sd = _create_socket(o->host, o->port, 0)) == 0) { printf("Cannot connect to %s:%d", o->host, o->port); return -1; } - switch(o->json_type){ - case 0:{ - /*olsrd jsoninfo*/ - char *req = "/topology/config"; - if((sent = send(o->sd,req,strlen(req),MSG_NOSIGNAL))==-1){ - printf("Cannot send to %s:%d", o->host, o->port); - close(o->sd); - return -1; - } - if(o->recv_buffer!=0){ - free(o->recv_buffer); - o->recv_buffer=0; - } - if(!_telnet_receive(o->sd, &(o->recv_buffer))){ - printf("cannot receive \n"); - close(o->sd); - return -1; - } - o->t = parse_jsoninfo(o->recv_buffer); - if(!o->t){ - printf("can't parse jsoninfo\n %s \n", o->recv_buffer); - close(o->sd); - return -1; - } + switch (o->json_type) { + case 0: { + /*olsrd jsoninfo*/ + char *req = "/topology/config"; + if ((sent = send(o->sd, req, strlen(req), MSG_NOSIGNAL)) + == -1) { + printf("Cannot send to %s:%d", o->host, o->port); + close(o->sd); + return -1; + } + if (o->recv_buffer != 0) { + free(o->recv_buffer); + o->recv_buffer = 0; + } + if (!_telnet_receive(o->sd, &(o->recv_buffer))) { + printf("cannot receive \n"); + close(o->sd); + return -1; + } + o->t = parse_jsoninfo(o->recv_buffer); + if (!o->t) { + printf("can't parse jsoninfo\n %s \n", o->recv_buffer); + close(o->sd); + return -1; + } + } break; + case 1: { + /*olsrd netjson*/ + char *req = "/NetworkGraph"; + if ((sent = send(o->sd, req, strlen(req), MSG_NOSIGNAL)) + == -1) { + printf("Cannot send to %s:%d\n", o->host, o->port); + close(o->sd); + return -1; + } + if (o->recv_buffer != 0) { + free(o->recv_buffer); + o->recv_buffer = 0; } - break; - case 1:{ - /*olsrd netjson*/ - char *req = "/NetworkGraph"; - if( (sent = send(o->sd,req,strlen(req),MSG_NOSIGNAL))==-1){ - printf("Cannot send to %s:%d\n", o->host, o->port); - close(o->sd); - return -1; - } - if(o->recv_buffer!=0){ - free(o->recv_buffer); - o->recv_buffer=0; - } - if(!_telnet_receive(o->sd, &(o->recv_buffer))){ - printf("cannot receive \n"); - close(o->sd); - return -1; - } - o->t = parse_netjson(o->recv_buffer); - if(!o->t){ - printf("can't parse netjson\n %s \n", o->recv_buffer); - close(o->sd); - return -1; - } - } - break; - default: + if (!_telnet_receive(o->sd, &(o->recv_buffer))) { + printf("cannot receive \n"); close(o->sd); return -1; } + o->t = parse_netjson(o->recv_buffer); + if (!o->t) { + printf("can't parse netjson\n %s \n", o->recv_buffer); + close(o->sd); + return -1; + } + } break; + default: close(o->sd); - return 0; + return -1; + } + close(o->sd); + return 0; } - - /** * Push the timers value to the routing daemon * @param olsr plugin handler object @@ -143,15 +144,16 @@ int get_topology(routing_plugin *o) /*netjson & jsoninfo*/ */ int push_timers(routing_plugin *o, struct timers t) { - o->sd =_create_socket(o->host, o->timer_port, 0); + o->sd = _create_socket(o->host, o->timer_port, 0); char cmd[25]; sprintf(cmd, "/HelloTimer=%4.4f", t.h_timer); write(o->sd, cmd, strlen(cmd)); close(o->sd); - o->sd =_create_socket(o->host, o->timer_port, 0); + o->sd = _create_socket(o->host, o->timer_port, 0); sprintf(cmd, "/TcTimer=%4.4f", t.tc_timer); write(o->sd, cmd, strlen(cmd)); - printf("%4.4f\t%4.4f\t%4.4f\t%4.4f\n", t.tc_timer, t.h_timer, t.exec_time, t.centrality); + printf("%4.4f\t%4.4f\t%4.4f\t%4.4f\n", t.tc_timer, t.h_timer, + t.exec_time, t.centrality); close(o->sd); return 1; } @@ -160,11 +162,11 @@ int push_timers(routing_plugin *o, struct timers t) * Delete the olsr plugin handler * @param olsr plugin handler object */ -void delete_plugin(routing_plugin* o) +void delete_plugin(routing_plugin *o) { free(o->host); - if(o->recv_buffer!=0) - free(o->recv_buffer); + if (o->recv_buffer != 0) + free(o->recv_buffer); free(o->self_id); free(o); } diff --git a/prince/lib/olsrd/olsr.h b/prince/lib/olsrd/olsr.h index 0f97136..d5b4e91 100644 --- a/prince/lib/olsrd/olsr.h +++ b/prince/lib/olsrd/olsr.h @@ -7,6 +7,6 @@ /*inehrit methods from here */ #include "plugin_interface.h" -float get_initial_timer(routing_plugin* o, char* cmd); +float get_initial_timer(routing_plugin *o, char *cmd); #endif /* SRC_OLSR_H_ */ diff --git a/prince/lib/oonf/oonf.c b/prince/lib/oonf/oonf.c index 65465ef..be28f02 100644 --- a/prince/lib/oonf/oonf.c +++ b/prince/lib/oonf/oonf.c @@ -6,42 +6,44 @@ * @param host host address as a string * @return pointer to oonf plugin handler */ -routing_plugin* new_plugin(char* host, int port, int json_type, int timer_port){ - routing_plugin *o = (routing_plugin*) malloc(sizeof(routing_plugin)); - if (o == NULL) { - perror("oonf"); - exit(EXIT_FAILURE); - } - o->port = port; - o->host = strdup(host); - o->json_type = json_type; - o->recv_buffer = NULL; - o->self_id = NULL; - o->timer_port = timer_port; // the port is the same for netjson topolgy and timer update - return o; +routing_plugin *new_plugin(char *host, int port, int json_type, int timer_port) +{ + routing_plugin *o = (routing_plugin *)malloc(sizeof(routing_plugin)); + if (o == NULL) { + perror("oonf"); + exit(EXIT_FAILURE); + } + o->port = port; + o->host = strdup(host); + o->json_type = json_type; + o->recv_buffer = NULL; + o->self_id = NULL; + o->timer_port = timer_port; // the port is the same for netjson topolgy + // and timer update + return o; } /** * Delete the oonf plugin handler * @param oonf plugin handler object */ -void delete_plugin(routing_plugin* o) { - if (o != NULL) { - if (o->host != NULL) { - free(o->host); - } - if (o->recv_buffer != NULL) { - free(o->recv_buffer); - } - if (o->self_id != NULL) { - free(o->self_id); - } - if (o->t != NULL) { - free(o->t); - } - free(o); - - } +void delete_plugin(routing_plugin *o) +{ + if (o != NULL) { + if (o->host != NULL) { + free(o->host); + } + if (o->recv_buffer != NULL) { + free(o->recv_buffer); + } + if (o->self_id != NULL) { + free(o->self_id); + } + if (o->t != NULL) { + free(o->t); + } + free(o); + } } /** @@ -49,21 +51,22 @@ void delete_plugin(routing_plugin* o) { * @param oonf plugin handler object * @return 0 if success, -1 otherwise */ -int get_topology(routing_plugin *o) { +int get_topology(routing_plugin *o) +{ int sent; - if((o->sd = _create_socket(o->host, o->port, 0))==0){ + if ((o->sd = _create_socket(o->host, o->port, 0)) == 0) { printf("Cannot connect to %s:%d", o->host, o->port); return -1; } char *req = "/netjsoninfo filter graph ipv6_0/quit\n"; - if( (sent = send(o->sd,req,strlen(req),MSG_NOSIGNAL))==-1){ + if ((sent = send(o->sd, req, strlen(req), MSG_NOSIGNAL)) == -1) { printf("Cannot send to %s:%d", o->host, o->port); close(o->sd); return -1; } if (o->recv_buffer != NULL) { free(o->recv_buffer); - o->recv_buffer=0; + o->recv_buffer = 0; } if (!_telnet_receive(o->sd, &(o->recv_buffer))) { printf("cannot receive \n"); @@ -73,7 +76,7 @@ int get_topology(routing_plugin *o) { o->t = parse_netjson(o->recv_buffer); if (o->t == INVALID_TOPOLOGY) { fprintf(stderr, "Can't parse netjson\n"); - fprintf(stderr, "%s\n", o->recv_buffer); + fprintf(stderr, "%s\n", o->recv_buffer); close(o->sd); return -1; } @@ -89,9 +92,11 @@ int get_topology(routing_plugin *o) { */ int push_timers(routing_plugin *o, struct timers t) { - o->sd =_create_socket(o->host, o->timer_port, 0); + o->sd = _create_socket(o->host, o->timer_port, 0); char cmd[111]; - sprintf(cmd, "/config set olsrv2.tc_interval=%4.2f/config set interface.hello_interval=%4.2f/config commit/quit", t.tc_timer, t.h_timer); + sprintf(cmd, + "/config set olsrv2.tc_interval=%4.2f/config set interface.hello_interval=%4.2f/config commit/quit", + t.tc_timer, t.h_timer); write(o->sd, cmd, strlen(cmd)); printf("Pushed Timers %4.2f %4.2f\n", t.tc_timer, t.h_timer); close(o->sd); @@ -99,11 +104,12 @@ int push_timers(routing_plugin *o, struct timers t) } -#define HELLO_TIMER_MESSAGE "/HelloTimer" +#define HELLO_TIMER_MESSAGE "/HelloTimer" #define TC_TIMER_MESSAGE "/TcTimer" -int get_initial_timers(routing_plugin *o, struct timers *t) { - t->h_timer = parse_initial_timer(o, HELLO_TIMER_MESSAGE); - t->tc_timer = parse_initial_timer(o, TC_TIMER_MESSAGE); +int get_initial_timers(routing_plugin *o, struct timers *t) +{ + t->h_timer = parse_initial_timer(o, HELLO_TIMER_MESSAGE); + t->tc_timer = parse_initial_timer(o, TC_TIMER_MESSAGE); if (t->h_timer == -1) { fprintf(stderr, "Could not initialise h_timer\n"); fprintf(stdout, "Setting h_timer to 2\n"); @@ -114,16 +120,17 @@ int get_initial_timers(routing_plugin *o, struct timers *t) { fprintf(stdout, "Setting tc_timer to 5\n"); t->tc_timer = 5; } - return 0; + return 0; } #define RESPONSE_SIZE (sizeof(char) * 24) -float parse_initial_timer(routing_plugin* o, const char* cmd) { - o->sd =_create_socket(o->host, o->timer_port, ECONNREFUSED); +float parse_initial_timer(routing_plugin *o, const char *cmd) +{ + o->sd = _create_socket(o->host, o->timer_port, ECONNREFUSED); char *page; char *token; float value = 0; - page = (char*) malloc(RESPONSE_SIZE); + page = (char *)malloc(RESPONSE_SIZE); write(o->sd, cmd, strlen(cmd)); if (recv(o->sd, page, strlen(cmd), 0) > 0) { token = strtok(page, ":"); diff --git a/prince/lib/oonf/oonf.h b/prince/lib/oonf/oonf.h index 85b2d37..81c4d10 100644 --- a/prince/lib/oonf/oonf.h +++ b/prince/lib/oonf/oonf.h @@ -7,6 +7,6 @@ /*inehrit methods from here */ #include "plugin_interface.h" -int push_timers(routing_plugin *o, struct timers t); -float parse_initial_timer(routing_plugin *o, const char *cmd); +int push_timers(routing_plugin *o, struct timers t); +float parse_initial_timer(routing_plugin *o, const char *cmd); #endif /* SRC_OONF_H_ */ diff --git a/prince/lib/ospf/ospf.c b/prince/lib/ospf/ospf.c index b35b961..fb6da99 100644 --- a/prince/lib/ospf/ospf.c +++ b/prince/lib/ospf/ospf.c @@ -5,13 +5,14 @@ * @param host host address as a string * @return pointer to oonf plugin handler */ -routing_plugin* new_plugin(char* host, int port, int json_type, int timer_port){ - routing_plugin *o =(routing_plugin*) malloc(sizeof(routing_plugin)); - o->port=port; - o->host=strdup(host); - o->json_type=json_type; - o->recv_buffer=0; - o->self_id=0; +routing_plugin *new_plugin(char *host, int port, int json_type, int timer_port) +{ + routing_plugin *o = (routing_plugin *)malloc(sizeof(routing_plugin)); + o->port = port; + o->host = strdup(host); + o->json_type = json_type; + o->recv_buffer = 0; + o->self_id = 0; o->timer_port = port; return o; } @@ -26,51 +27,52 @@ int get_topology(routing_plugin *o) char cmd[] = "show ospf topology netjson\n"; int c; bird_connect(o); - c = write(o->sd, cmd, strlen(cmd)+1); - if(c<0){ + c = write(o->sd, cmd, strlen(cmd) + 1); + if (c < 0) { printf("Write error"); return false; } sleep(1); receive_netjson(o); o->t = parse_netjson(o->recv_buffer); - if(!o->t){ - printf("can't parse netjson\n %s \n", o->recv_buffer ); + if (!o->t) { + printf("can't parse netjson\n %s \n", o->recv_buffer); return false; } return true; } -int receive_netjson(routing_plugin *o){ +int receive_netjson(routing_plugin *o) +{ char *lines[20], *token, *netjson; char code[5]; - int i=0,n_lines=0,lenght=0, line_len; + int i = 0, n_lines = 0, lenght = 0, line_len; - if(!_telnet_receive(o->sd , &(o->recv_buffer))){ + if (!_telnet_receive(o->sd, &(o->recv_buffer))) { return false; } - //split the message in lines + // split the message in lines token = strtok(o->recv_buffer, "\n"); - while(token != NULL) - { - lines[i]=token; + while (token != NULL) { + lines[i] = token; token = strtok(NULL, "\n"); i++; } - n_lines=i; - //select the lines with the netjson code, remove the code and merge them - for(i=0;irecv_buffer); - o->recv_buffer=netjson; + o->recv_buffer = netjson; return true; } @@ -81,7 +83,7 @@ int receive_netjson(routing_plugin *o){ */ int push_timers(routing_plugin *o, struct timers t) { - //TODO + // TODO return true; } @@ -89,25 +91,27 @@ int push_timers(routing_plugin *o, struct timers t) * Delete the oonf plugin handler * @param oonf plugin handler object */ -void delete_plugin(routing_plugin* o) +void delete_plugin(routing_plugin *o) { free(o->host); - if(o->recv_buffer!=0) - free(o->recv_buffer); + if (o->recv_buffer != 0) + free(o->recv_buffer); free(o->self_id); free(o); } -void bird_connect(routing_plugin *o){ - struct sockaddr_un sa; - o->sd = socket(AF_UNIX, SOCK_STREAM, 0); - if (o->sd < 0) - printf("Cannot create socket"); - bzero(&sa, sizeof(sa)); - sa.sun_family = AF_UNIX; - strcpy(sa.sun_path, o->host); - if (connect(o->sd, (struct sockaddr *) &sa, SUN_LEN(&sa)) < 0) - printf("Unable to connect to server control socket (%s)", o->host); - if (fcntl(o->sd, F_SETFL, O_NONBLOCK) < 0) - printf("fcntl"); +void bird_connect(routing_plugin *o) +{ + struct sockaddr_un sa; + o->sd = socket(AF_UNIX, SOCK_STREAM, 0); + if (o->sd < 0) + printf("Cannot create socket"); + bzero(&sa, sizeof(sa)); + sa.sun_family = AF_UNIX; + strcpy(sa.sun_path, o->host); + if (connect(o->sd, (struct sockaddr *)&sa, SUN_LEN(&sa)) < 0) + printf("Unable to connect to server control socket (%s)", + o->host); + if (fcntl(o->sd, F_SETFL, O_NONBLOCK) < 0) + printf("fcntl"); } diff --git a/prince/lib/testlib/test.c b/prince/lib/testlib/test.c index 44ff2c1..a7c5fe4 100644 --- a/prince/lib/testlib/test.c +++ b/prince/lib/testlib/test.c @@ -6,14 +6,16 @@ * @param host host address as a string * @return pointer to oonf plugin handler */ -routing_plugin* new_plugin(char* host, int port, int json_type, int timer_port){ - routing_plugin *o =(routing_plugin*) malloc(sizeof(routing_plugin)); - o->port=port; - o->host=strdup(host); - o->json_type=0; - o->recv_buffer=0; - o->self_id=0; - o->timer_port = timer_port; // the port is the same for netjson and poprouting +routing_plugin *new_plugin(char *host, int port, int json_type, int timer_port) +{ + routing_plugin *o = (routing_plugin *)malloc(sizeof(routing_plugin)); + o->port = port; + o->host = strdup(host); + o->json_type = 0; + o->recv_buffer = 0; + o->self_id = 0; + o->timer_port = + timer_port; // the port is the same for netjson and poprouting return o; } @@ -25,80 +27,81 @@ routing_plugin* new_plugin(char* host, int port, int json_type, int timer_port){ int get_topology(routing_plugin *o) { int sent; - if((o->sd= _create_socket(o->host, o->port, 0))==0){ + if ((o->sd = _create_socket(o->host, o->port, 0)) == 0) { printf("Cannot connect to %s:%d", o->host, o->port); return -1; } - switch(o->json_type){ - case 1:{ + switch (o->json_type) { + case 1: { /*olsrd netjson*/ char *req = "/NetworkGraph"; - if( (sent = send(o->sd,req,strlen(req),MSG_NOSIGNAL))==-1){ + if ((sent = send(o->sd, req, strlen(req), MSG_NOSIGNAL)) + == -1) { printf("Cannot send to %s:%d\n", o->host, o->port); close(o->sd); return -1; } - if(o->recv_buffer!=0){ + if (o->recv_buffer != 0) { free(o->recv_buffer); - o->recv_buffer=0; + o->recv_buffer = 0; } - if(!_telnet_receive(o->sd, &(o->recv_buffer))){ + if (!_telnet_receive(o->sd, &(o->recv_buffer))) { printf("cannot receive \n"); close(o->sd); return -1; } o->t = parse_netjson(o->recv_buffer); - if(!o->t){ + if (!o->t) { printf("can't parse netjson\n %s \n", o->recv_buffer); close(o->sd); return -1; } - } - break; + } break; - case 0:{ + case 0: { /*olsrd jsoninfo*/ char *req = "/topology/config"; - if((sent = send(o->sd,req,strlen(req),MSG_NOSIGNAL))==-1){ + if ((sent = send(o->sd, req, strlen(req), MSG_NOSIGNAL)) + == -1) { printf("Cannot send to %s:%d", o->host, o->port); close(o->sd); return -1; } - if(o->recv_buffer!=0){ + if (o->recv_buffer != 0) { free(o->recv_buffer); - o->recv_buffer=0; + o->recv_buffer = 0; } - if(!_telnet_receive(o->sd, &(o->recv_buffer))){ + if (!_telnet_receive(o->sd, &(o->recv_buffer))) { printf("cannot receive \n"); close(o->sd); return -1; } o->t = parse_jsoninfo(o->recv_buffer); - if(!o->t){ + if (!o->t) { printf("can't parse jsoninfo\n %s \n", o->recv_buffer); close(o->sd); return -1; } - } - break; + } break; case 2: { char *req = "/netjsoninfo filter graph ipv6_0/quit\n"; - if( (sent = send(o->sd,req,strlen(req),MSG_NOSIGNAL))==-1){ + if ((sent = send(o->sd, req, strlen(req), MSG_NOSIGNAL)) + == -1) { printf("Cannot send to %s:%d", o->host, o->port); close(o->sd); return -1; } - if(o->recv_buffer!=0){ + if (o->recv_buffer != 0) { free(o->recv_buffer); - o->recv_buffer=0; + o->recv_buffer = 0; } - if(!_telnet_receive(o->sd, &(o->recv_buffer))){ + if (!_telnet_receive(o->sd, &(o->recv_buffer))) { printf("cannot receive \n"); close(o->sd); return -1; } o->t = parse_netjson(o->recv_buffer); - if(!o->t){ + if (!o->t) { printf("can't parse netjson\n %s \n", o->recv_buffer); close(o->sd); return -1; @@ -120,16 +123,19 @@ int get_topology(routing_plugin *o) */ int push_timers(routing_plugin *o, struct timers t) { - o->sd =_create_socket(o->host, o->timer_port, 0); + o->sd = _create_socket(o->host, o->timer_port, 0); char cmd[25]; - //sprintf(cmd, "/HelloTimer=%4.4f/TcTimer=%4.4f/exec_time=%4.4f/centrality=%4.4f", t.tc_timer, t.h_timer, t.exec_time, t.centrality); + // sprintf(cmd, + // "/HelloTimer=%4.4f/TcTimer=%4.4f/exec_time=%4.4f/centrality=%4.4f", + // t.tc_timer, t.h_timer, t.exec_time, t.centrality); sprintf(cmd, "/HelloTimer=%4.4f", t.h_timer); write(o->sd, cmd, strlen(cmd)); close(o->sd); - o->sd =_create_socket(o->host, o->timer_port, 0); + o->sd = _create_socket(o->host, o->timer_port, 0); sprintf(cmd, "/TcTimer=%4.4f", t.tc_timer); write(o->sd, cmd, strlen(cmd)); - printf("%4.4f\t%4.4f\t%4.4f\t%4.4f\n", t.tc_timer, t.h_timer, t.exec_time, t.centrality); + printf("%4.4f\t%4.4f\t%4.4f\t%4.4f\n", t.tc_timer, t.h_timer, + t.exec_time, t.centrality); close(o->sd); return 1; } @@ -138,10 +144,10 @@ int push_timers(routing_plugin *o, struct timers t) * Delete the oonf plugin handler * @param oonf plugin handler object */ -void delete_plugin(routing_plugin* o) +void delete_plugin(routing_plugin *o) { free(o->host); - if(o->recv_buffer!=0) + if (o->recv_buffer != 0) free(o->recv_buffer); free(o->self_id); free(o); diff --git a/prince/src/config.c b/prince/src/config.c index 19d075f..94fa79b 100644 --- a/prince/src/config.c +++ b/prince/src/config.c @@ -1,24 +1,25 @@ #include "config.h" -char* read_file_content(const char *filename) { +char *read_file_content(const char *filename) +{ char *buffer = NULL; - int string_size, read_size; + int string_size, read_size; FILE *handler = fopen(filename, "r"); if (handler == NULL) { - perror("prince-config"); - exit(EXIT_FAILURE); + perror("prince-config"); + exit(EXIT_FAILURE); } else { - fprintf(stdout, "Reading %s\n", filename); + fprintf(stdout, "Reading %s\n", filename); fseek(handler, 0, SEEK_END); string_size = ftell(handler); rewind(handler); - buffer = (char*) malloc(sizeof(char) * (string_size + 1) ); - if (buffer == NULL) { - perror("prince-config"); - return NULL; - } + buffer = (char *)malloc(sizeof(char) * (string_size + 1)); + if (buffer == NULL) { + perror("prince-config"); + return NULL; + } read_size = fread(buffer, sizeof(char), string_size, handler); @@ -29,10 +30,10 @@ char* read_file_content(const char *filename) { buffer = NULL; } if (fclose(handler)) { - perror("prince-config"); - exit(EXIT_FAILURE); - } - } + perror("prince-config"); + exit(EXIT_FAILURE); + } + } return buffer; } @@ -42,14 +43,17 @@ char* read_file_content(const char *filename) { * @param *filepath path to the configuration file * @return 0 if success, -1 if fail */ -int read_config_file(prince_handler_t ph, const char *filepath) { - if (parse_json_config(filepath, ph)) { - fprintf(stderr, "Cannot read configuration file '%s' (Either format or content not compliant or complete). Exiting.\n", filepath); - return -1; - } +int read_config_file(prince_handler_t ph, const char *filepath) +{ + if (parse_json_config(filepath, ph)) { + fprintf(stderr, + "Cannot read configuration file '%s' (Either format or content not compliant or complete). Exiting.\n", + filepath); + return -1; + } - fprintf(stdout, "Config loaded from %s\n", filepath); - return 0; + fprintf(stdout, "Config loaded from %s\n", filepath); + return 0; } @@ -60,7 +64,7 @@ int read_config_file(prince_handler_t ph, const char *filepath) { * "protocol":"oonf", * "host":"127.0.0.1", * "port":2009, -* "timer_port": 1234, +* "timer_port": 1234, * "refresh":1 * }, * "graph-parser":{ @@ -76,155 +80,254 @@ int read_config_file(prince_handler_t ph, const char *filepath) { * @param ph The prince_handler, for saving the configuration * @return 0 on success, -1 on failure */ -int parse_json_config(const char *filepath, prince_handler_t ph) { - char * buffer = read_file_content(filepath); +int parse_json_config(const char *filepath, prince_handler_t ph) +{ + char *buffer = read_file_content(filepath); if (buffer == NULL) { - return -1; - } + return -1; + } int completed = 0; - bool heuristic_set = false, - weights_set = false, - recursive_set = false, - stop_unchanged_set = false, - multithreaded_set = false; + bool heuristic_set = false, weights_set = false, recursive_set = false, + stop_unchanged_set = false, multithreaded_set = false; struct json_object *jobj = json_tokener_parse(buffer); - if (jobj == NULL) { - fprintf(stderr, "prince-config: Received null json pointer"); - exit(EXIT_FAILURE); - } + if (jobj == NULL) { + fprintf(stderr, "prince-config: Received null json pointer"); + exit(EXIT_FAILURE); + } - json_object_object_foreach(jobj, key, val) { + json_object_object_foreach(jobj, key, val) + { if (strcmp(key, "proto") == 0) { if (json_object_get_type(val) == json_type_object) { - json_object_object_foreach(val, key_i, val_i) { + json_object_object_foreach(val, key_i, val_i) + { if (strcmp(key_i, "protocol") == 0) { - if (json_object_get_type(val_i) == json_type_string) { - const char * content = json_object_get_string(val_i); - ph->proto = strdup(content); + if (json_object_get_type(val_i) + == json_type_string) { + const char *content = + json_object_get_string( + val_i); + ph->proto = + strdup(content); completed++; } else { - fprintf(stderr, "\"protocol\" value is not a string\n"); - exit(EXIT_FAILURE); - } + fprintf(stderr, + "\"protocol\" value is not a string\n"); + exit(EXIT_FAILURE); + } - } else if (ph->host == 0 && strcmp(key_i, "host") == 0) { - if (json_object_get_type(val_i) == json_type_string) { - const char * content = json_object_get_string(val_i); - ph->host = strdup(content); + } else if (ph->host == 0 + && strcmp(key_i, "host") + == 0) { + if (json_object_get_type(val_i) + == json_type_string) { + const char *content = + json_object_get_string( + val_i); + ph->host = + strdup(content); completed++; } else { - fprintf(stderr, "\"host\" value is not a string\n"); - exit(EXIT_FAILURE); - } - } else if (ph->port < 0 && strcmp(key_i, "port") == 0) { - if (json_object_get_type(val_i) == json_type_int) { - ph->port = json_object_get_int(val_i); + fprintf(stderr, + "\"host\" value is not a string\n"); + exit(EXIT_FAILURE); + } + } else if (ph->port < 0 + && strcmp(key_i, "port") + == 0) { + if (json_object_get_type(val_i) + == json_type_int) { + ph->port = + json_object_get_int( + val_i); completed++; } else { - fprintf(stderr, "\"port\" value is not an int\n"); - exit(EXIT_FAILURE); - } - } else if (ph->port < 0 && strcmp(key_i, "json_type") == 0) { - if (json_object_get_type(val_i) == json_type_string) { - const char * content = json_object_get_string(val_i); - if (strcmp(content, "netjson") == 0) { - ph->json_type = 1; - } else if (strcmp(content, "jsoninfo") == 0) { - ph->json_type = 0; + fprintf(stderr, + "\"port\" value is not an int\n"); + exit(EXIT_FAILURE); + } + } else if (ph->port < 0 + && strcmp(key_i, "json_type") + == 0) { + if (json_object_get_type(val_i) + == json_type_string) { + const char *content = + json_object_get_string( + val_i); + if (strcmp(content, + "netjson") + == 0) { + ph->json_type = + 1; + } else if ( + strcmp(content, + "jsoninfo") + == 0) { + ph->json_type = + 0; } else { - fprintf(stderr, "\"json_type\" value is not specified, will default to netjson"); - ph->json_type = 0; - } - } else if (json_object_get_type(val_i) == json_type_int) { - ph->json_type= json_object_get_int(val_i); + fprintf(stderr, + "\"json_type\" value is not specified, will default to netjson"); + ph->json_type = + 0; + } + } else if (json_object_get_type( + val_i) + == json_type_int) { + ph->json_type = + json_object_get_int( + val_i); completed++; } else { - fprintf(stderr, "\"json_type\" value is not accepted\n"); - exit(EXIT_FAILURE); - } - } - else if (strcmp(key_i, "timer_port") == 0) { - if (json_object_get_type(val_i) == json_type_int) { - ph->timer_port = json_object_get_int(val_i); + fprintf(stderr, + "\"json_type\" value is not accepted\n"); + exit(EXIT_FAILURE); + } + } else if (strcmp(key_i, "timer_port") + == 0) { + if (json_object_get_type(val_i) + == json_type_int) { + ph->timer_port = + json_object_get_int( + val_i); completed++; } else { - fprintf(stderr, "\"timer_port\" value is not an int\n"); - exit(EXIT_FAILURE); - } - } else if(strcmp(key_i, "refresh") == 0) { - if (ph->refresh < 0 && json_object_get_type(val_i) == json_type_int) { - ph->refresh = json_object_get_int(val_i); + fprintf(stderr, + "\"timer_port\" value is not an int\n"); + exit(EXIT_FAILURE); + } + } else if (strcmp(key_i, "refresh") + == 0) { + if (ph->refresh < 0 + && json_object_get_type( + val_i) + == json_type_int) { + ph->refresh = + json_object_get_int( + val_i); completed++; } else { - fprintf(stderr, "\"refresh\" value is not an int\n"); - exit(EXIT_FAILURE); - } - } else if(strcmp(key_i, "log_file") == 0) { - if (json_object_get_type(val_i) == json_type_string) { - const char * content = json_object_get_string(val_i); - ph->log_file = strdup(content); + fprintf(stderr, + "\"refresh\" value is not an int\n"); + exit(EXIT_FAILURE); + } + } else if (strcmp(key_i, "log_file") + == 0) { + if (json_object_get_type(val_i) + == json_type_string) { + const char *content = + json_object_get_string( + val_i); + ph->log_file = + strdup(content); } else { - fprintf(stderr, "\"log_file\" value is not a string\n"); - exit(EXIT_FAILURE); - } + fprintf(stderr, + "\"log_file\" value is not a string\n"); + exit(EXIT_FAILURE); + } } } } } else if (strcmp(key, "graph-parser") == 0) { if (json_object_get_type(val) == json_type_object) { - json_object_object_foreach(val, key_i, val_i) { - if (!heuristic_set && strcmp(key_i, "heuristic") == 0) { - if (json_object_get_type(val_i) == json_type_int) { - ph->heuristic = json_object_get_int(val_i) == 1; + json_object_object_foreach(val, key_i, val_i) + { + if (!heuristic_set + && strcmp(key_i, "heuristic") + == 0) { + if (json_object_get_type(val_i) + == json_type_int) { + ph->heuristic = + json_object_get_int( + val_i) + == 1; heuristic_set = true; } else { - fprintf(stderr, "\"heuristic\" value not an int\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(key_i, "weights") == 0) { - if (!weights_set&&json_object_get_type(val_i) == json_type_int) { - ph->weights = json_object_get_int(val_i) == 1; + fprintf(stderr, + "\"heuristic\" value not an int\n"); + exit(EXIT_FAILURE); + } + } else if (strcmp(key_i, "weights") + == 0) { + if (!weights_set + && json_object_get_type( + val_i) + == json_type_int) { + ph->weights = + json_object_get_int( + val_i) + == 1; weights_set = true; } else { - fprintf(stderr, "\"weights\" value not an int\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(key_i, "recursive") == 0) { - if (!recursive_set && json_object_get_type(val_i) == json_type_int) { - ph->recursive=json_object_get_int(val_i) == 1; + fprintf(stderr, + "\"weights\" value not an int\n"); + exit(EXIT_FAILURE); + } + } else if (strcmp(key_i, "recursive") + == 0) { + if (!recursive_set + && json_object_get_type( + val_i) + == json_type_int) { + ph->recursive = + json_object_get_int( + val_i) + == 1; recursive_set = true; } else { - fprintf(stderr, "\"recursive\" value not an int\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(key_i, "stop_unchanged") == 0) { - if (!stop_unchanged_set && json_object_get_type(val_i) == json_type_int) { - ph->stop_unchanged = json_object_get_int(val_i); - stop_unchanged_set = true; + fprintf(stderr, + "\"recursive\" value not an int\n"); + exit(EXIT_FAILURE); + } + } else if (strcmp(key_i, + "stop_unchanged") + == 0) { + if (!stop_unchanged_set + && json_object_get_type( + val_i) + == json_type_int) { + ph->stop_unchanged = + json_object_get_int( + val_i); + stop_unchanged_set = + true; } else { - fprintf(stderr, "\"stop_unchanged\" value not an int\n"); - exit(EXIT_FAILURE); - } - } else if (strcmp(key_i, "multithreaded") == 0) { - if (!multithreaded_set && json_object_get_type(val_i) == json_type_int) { - ph->multithreaded = json_object_get_int(val_i); - multithreaded_set = true; + fprintf(stderr, + "\"stop_unchanged\" value not an int\n"); + exit(EXIT_FAILURE); + } + } else if (strcmp(key_i, + "multithreaded") + == 0) { + if (!multithreaded_set + && json_object_get_type( + val_i) + == json_type_int) { + ph->multithreaded = + json_object_get_int( + val_i); + multithreaded_set = + true; } else { - fprintf(stderr, "\"multithreaded\" value not an int\n"); - exit(EXIT_FAILURE); - } + fprintf(stderr, + "\"multithreaded\" value not an int\n"); + exit(EXIT_FAILURE); + } } else { - fprintf(stderr, "unknowk key \"%s\"\n", key_i); - } + fprintf(stderr, + "unknowk key \"%s\"\n", + key_i); + } } } else { - fprintf(stderr, "\"graph-parser\" value not an object\n"); - exit(EXIT_FAILURE); - } + fprintf(stderr, + "\"graph-parser\" value not an object\n"); + exit(EXIT_FAILURE); + } } else { - fprintf(stderr, "unknown key \"%s\"", key); - } + fprintf(stderr, "unknown key \"%s\"", key); + } } if (jobj != NULL) { json_object_put(jobj); @@ -232,9 +335,8 @@ int parse_json_config(const char *filepath, prince_handler_t ph) { } free(buffer); if (completed == 5 && heuristic_set && weights_set && recursive_set - && stop_unchanged_set && multithreaded_set) { + && stop_unchanged_set && multithreaded_set) { return 0; - } + } return -1; } - diff --git a/prince/src/config_graph.c b/prince/src/config_graph.c index 5731c79..541efe4 100644 --- a/prince/src/config_graph.c +++ b/prince/src/config_graph.c @@ -1,32 +1,35 @@ #include "config_graph.h" -int load_graph_config(const char *filepath, graph_config_t conf) { - if (conf == INVALID_GRAPH_CONFIG) { - return -1; - } - prince_handler_t temp = new_prince_handler(filepath); - if (temp == INVALID_PRINCE_HANDLER) { - return -1; - } - conf->heuristic = temp->heuristic; - conf->weights = temp->weights; - conf->recursive = temp->recursive; - conf->stop_unchanged = temp->stop_unchanged; - conf->multithreaded = temp->multithreaded; - free_prince_handler(temp); - return 0; +int load_graph_config(const char *filepath, graph_config_t conf) +{ + if (conf == INVALID_GRAPH_CONFIG) { + return -1; + } + prince_handler_t temp = new_prince_handler(filepath); + if (temp == INVALID_PRINCE_HANDLER) { + return -1; + } + conf->heuristic = temp->heuristic; + conf->weights = temp->weights; + conf->recursive = temp->recursive; + conf->stop_unchanged = temp->stop_unchanged; + conf->multithreaded = temp->multithreaded; + free_prince_handler(temp); + return 0; } -graph_config_t new_graph_config(void) { - graph_config_t result = (graph_config_t) malloc(GRAPH_CONFIG_SIZE); - if (result == INVALID_GRAPH_CONFIG) { - perror(NULL); - return INVALID_GRAPH_CONFIG; - } - memset(result, 0, GRAPH_CONFIG_SIZE); - return result; +graph_config_t new_graph_config(void) +{ + graph_config_t result = (graph_config_t)malloc(GRAPH_CONFIG_SIZE); + if (result == INVALID_GRAPH_CONFIG) { + perror(NULL); + return INVALID_GRAPH_CONFIG; + } + memset(result, 0, GRAPH_CONFIG_SIZE); + return result; } -void free_graph_config(graph_config_t conf) { - free(conf); +void free_graph_config(graph_config_t conf) +{ + free(conf); } diff --git a/prince/src/config_proto.c b/prince/src/config_proto.c index 6c5d993..e3710f1 100644 --- a/prince/src/config_proto.c +++ b/prince/src/config_proto.c @@ -2,38 +2,41 @@ #include "prince.h" -int load_proto_config(const char *filepath, proto_config_t conf) { - if (conf == INVALID_PROTO_CONFIG) { - return -1; - } - prince_handler_t temp = new_prince_handler(filepath); - if (temp == INVALID_PRINCE_HANDLER) { - return -1; - } - conf->json_type = temp->json_type; - conf->port = temp->port; - conf->proto = temp->proto; - conf->refresh = temp->refresh; - conf->sleep_onfail = temp->sleep_onfail; - conf->timer_port = temp->timer_port; - free_prince_handler(temp); - return 0; +int load_proto_config(const char *filepath, proto_config_t conf) +{ + if (conf == INVALID_PROTO_CONFIG) { + return -1; + } + prince_handler_t temp = new_prince_handler(filepath); + if (temp == INVALID_PRINCE_HANDLER) { + return -1; + } + conf->json_type = temp->json_type; + conf->port = temp->port; + conf->proto = temp->proto; + conf->refresh = temp->refresh; + conf->sleep_onfail = temp->sleep_onfail; + conf->timer_port = temp->timer_port; + free_prince_handler(temp); + return 0; } -proto_config_t new_proto_config(void) { - proto_config_t result = (proto_config_t) malloc(PROTO_CONFIG_SIZE); - if (result == INVALID_PROTO_CONFIG) { - perror(NULL); - return INVALID_PROTO_CONFIG; - } - memset(result, 0, PROTO_CONFIG_SIZE); - result->port = -1; - result->refresh = -1; - result->sleep_onfail = -1; - return result; +proto_config_t new_proto_config(void) +{ + proto_config_t result = (proto_config_t)malloc(PROTO_CONFIG_SIZE); + if (result == INVALID_PROTO_CONFIG) { + perror(NULL); + return INVALID_PROTO_CONFIG; + } + memset(result, 0, PROTO_CONFIG_SIZE); + result->port = -1; + result->refresh = -1; + result->sleep_onfail = -1; + return result; } -void free_proto_config(proto_config_t conf) { - free(conf->proto); - free(conf); +void free_proto_config(proto_config_t conf) +{ + free(conf->proto); + free(conf); } diff --git a/prince/src/load_plugin.c b/prince/src/load_plugin.c index 6c1ec0b..687933e 100644 --- a/prince/src/load_plugin.c +++ b/prince/src/load_plugin.c @@ -2,90 +2,95 @@ /** - * Load a plugin library and check if the operation is successful - */ -int load_routing_plugin(prince_handler_t result) { +* Load a plugin library and check if the operation is successful +*/ +int load_routing_plugin(prince_handler_t result) +{ char libname[20] = "libprince_"; - char *shared_library_error; - + char *shared_library_error; strcat(libname, result->proto); strcat(libname, ".so"); - // clear dlerror - dlerror(); + // clear dlerror + dlerror(); result->plugin_handle = dlopen(libname, RTLD_LAZY); - shared_library_error = dlerror(); + shared_library_error = dlerror(); if (shared_library_error != NULL) { - fprintf(stderr, "%s\n", shared_library_error); - errno = ELIBACC; - return -1; - } - return 0; + fprintf(stderr, "%s\n", shared_library_error); + errno = ELIBACC; + return -1; + } + return 0; } -int free_routing_plugin(prince_handler_t result) { - char * shared_library_error; - dlerror(); - // free routing plugin from shared library - // with function from shared library +int free_routing_plugin(prince_handler_t result) +{ + char *shared_library_error; + dlerror(); + // free routing plugin from shared library + // with function from shared library delete_plugin_p(result->rp); - // TODO: needed or symbol exists and - // there cannot be an error? - shared_library_error = dlerror(); - if (shared_library_error != NULL) { - fprintf(stderr, "%s\n", shared_library_error); - errno = ELIBACC; - return -1; - } - // clear - dlerror(); - // end TODO + // TODO: needed or symbol exists and + // there cannot be an error? + shared_library_error = dlerror(); + if (shared_library_error != NULL) { + fprintf(stderr, "%s\n", shared_library_error); + errno = ELIBACC; + return -1; + } + // clear + dlerror(); + // end TODO dlclose(result->plugin_handle); - shared_library_error = dlerror(); - if (shared_library_error != NULL) { - fprintf(stderr, "%s\n", shared_library_error); - errno = ELIBACC; - return -1; - } - return 0; + shared_library_error = dlerror(); + if (shared_library_error != NULL) { + fprintf(stderr, "%s\n", shared_library_error); + errno = ELIBACC; + return -1; + } + return 0; } -/** - * Load a symbol from the plugin library and check if the operation is successful - */ -int load_routing_plugin_symbol(prince_handler_t result, const char* symbol_name) { - char *shared_library_error; - // clear previous dlerror - dlerror(); - // find the required symbol - // and assign it to the correct variable - if (strcmp(symbol_name, "new_plugin") == 0) { - new_plugin_p = (routing_plugin* (*)(char* host, int port, int json_type, int timer_port)) - dlsym(result->plugin_handle, symbol_name); - } else if (strcmp(symbol_name, "delete_plugin") == 0) { - delete_plugin_p = (void (*) (routing_plugin *o)) - dlsym(result->plugin_handle, symbol_name); - } else if (strcmp(symbol_name, "get_initial_timers") == 0) { - get_initial_timers_p = (int (*) (routing_plugin *o, struct timers *t)) - dlsym(result->plugin_handle, symbol_name); - } else if (strcmp(symbol_name, "push_timers") == 0) { - push_timers_p = (int (*) (routing_plugin *o, struct timers t)) - dlsym(result->plugin_handle, symbol_name); - } else if (strcmp(symbol_name, "get_topology") == 0) { - get_topology_p = (int (*) (routing_plugin *o)) - dlsym(result->plugin_handle, symbol_name); - } else { - fprintf(stderr, "Symbol %s not permitted\n", symbol_name); - errno = ELIBACC; - return -1; - } - // check if there has been any error when - // resolving the symbol - shared_library_error = dlerror(); - if (shared_library_error != NULL) { - fprintf(stderr, "%s\n", shared_library_error); - errno = ELIBACC; - return -1; - } - return 0; +/** +* Load a symbol from the plugin library and check if the operation is successful +*/ +int load_routing_plugin_symbol(prince_handler_t result, const char *symbol_name) +{ + char *shared_library_error; + // clear previous dlerror + dlerror(); + // find the required symbol + // and assign it to the correct variable + if (strcmp(symbol_name, "new_plugin") == 0) { + new_plugin_p = + (routing_plugin * (*)(char *host, int port, + int json_type, int timer_port)) + dlsym(result->plugin_handle, symbol_name); + } else if (strcmp(symbol_name, "delete_plugin") == 0) { + delete_plugin_p = (void (*)(routing_plugin * o)) + dlsym(result->plugin_handle, symbol_name); + } else if (strcmp(symbol_name, "get_initial_timers") == 0) { + get_initial_timers_p = + (int (*)(routing_plugin * o, struct timers * t)) + dlsym(result->plugin_handle, symbol_name); + } else if (strcmp(symbol_name, "push_timers") == 0) { + push_timers_p = (int (*)(routing_plugin * o, struct timers t)) + dlsym(result->plugin_handle, symbol_name); + } else if (strcmp(symbol_name, "get_topology") == 0) { + get_topology_p = (int (*)(routing_plugin * o)) + dlsym(result->plugin_handle, symbol_name); + } else { + fprintf(stderr, "Symbol %s not permitted\n", symbol_name); + errno = ELIBACC; + return -1; + } + // check if there has been any error when + // resolving the symbol + shared_library_error = dlerror(); + if (shared_library_error != NULL) { + fprintf(stderr, "%s\n", shared_library_error); + errno = ELIBACC; + return -1; + } + return 0; } diff --git a/prince/src/prince.c b/prince/src/prince.c index 6fc473b..acc70e6 100644 --- a/prince/src/prince.c +++ b/prince/src/prince.c @@ -1,49 +1,54 @@ #include "prince.h" /** -* Main routine of Prince. Collect topology, parse it, calculate bc and timers, push them back. +* Main routine of Prince. Collect topology, parse it, calculate bc and timers, +* push them back. * @param argv[1] <- config filename * @return 0 on success */ -int main(int argc, char* argv[]) { +int main(int argc, char *argv[]) +{ FILE *log = NULL; if (argc < 2) { fprintf(stderr, "No conf file specified. Exiting.\n"); - exit(1); + exit(1); } fprintf(stdout, "Prince Started\n"); prince_handler_t ph = new_prince_handler(argv[1]); - if (ph == INVALID_PRINCE_HANDLER) { - fprintf(stderr, "Could not create prince handler\n"); - exit(EXIT_FAILURE); - } - - //if (load_proto_config(argv[1], ph->proto_config)) { - // fprintf(stderr, "Could not load section 'proto' from config\n"); - //} - //if (load_graph_config(argv[1], ph->graph_config)) { - // fprintf(stderr, "Could not load section 'graph' from config\n"); - //} + if (ph == INVALID_PRINCE_HANDLER) { + fprintf(stderr, "Could not create prince handler\n"); + exit(EXIT_FAILURE); + } + + // if (load_proto_config(argv[1], ph->proto_config)) { + // fprintf(stderr, "Could not load section 'proto' from + // config\n"); + //} + // if (load_graph_config(argv[1], ph->graph_config)) { + // fprintf(stderr, "Could not load section 'graph' from + // config\n"); + //} if (ph->log_file) { log = fopen(ph->log_file, "a+"); if (log == NULL) { - printf("Could not open log file <%s>; Continuing without\n", ph->log_file); + printf("Could not open log file <%s>; Continuing without\n", + ph->log_file); free(ph->log_file); ph->log_file = NULL; } else { printf("Logging to <%s>\n", ph->log_file); - } + } } - recursive = ph->recursive; - multithread = ph->multithreaded; + recursive = ph->recursive; + multithread = ph->multithreaded; stop_computing_if_unchanged = ph->stop_unchanged; signal(SIGPIPE, signal_callback_handler); - struct graph_parser * gp_p = (struct graph_parser *) ph->gp; + struct graph_parser *gp_p = (struct graph_parser *)ph->gp; do { sleep(ph->refresh); @@ -59,34 +64,38 @@ int main(int argc, char* argv[]) { continue; } - if (valid_topo(ph->rp->t)) { - fprintf(stderr, "Invalid topology data\n"); - exit(EXIT_FAILURE); - } + if (valid_topo(ph->rp->t)) { + fprintf(stderr, "Invalid topology data\n"); + exit(EXIT_FAILURE); + } - update_prince_id(ph); + update_prince_id(ph); graph_parser_parse_simplegraph(ph->gp, ph->rp->t); free_topo(ph->rp->t); clock_t start = clock(); graph_parser_calculate_bc(ph->gp); clock_t end = clock(); - ph->bc_degree_map = (map_id_degree_bc *) malloc(sizeof(map_id_degree_bc)); - if (ph->bc_degree_map == NULL) { - perror("prince"); - exit(EXIT_FAILURE); - } + ph->bc_degree_map = + (map_id_degree_bc *)malloc(sizeof(map_id_degree_bc)); + if (ph->bc_degree_map == NULL) { + perror("prince"); + exit(EXIT_FAILURE); + } ph->bc_degree_map->size = gp_p->g.nodes.size; ph->bc_degree_map->map = 0; graph_parser_compose_degree_bc_map(ph->gp, ph->bc_degree_map); - ph->opt_t.exec_time = (double) (end - start) / CLOCKS_PER_SEC; + ph->opt_t.exec_time = (double)(end - start) / CLOCKS_PER_SEC; ph->opt_t.centrality = get_self_bc(ph); if (log) { log = fopen(ph->log_file, "a+"); struct timeval tv; gettimeofday(&tv, NULL); - fprintf(log, "%i\t%4.4f\t%4.4f\t%4.4f\t%4.4f\n", tv.tv_sec, ph->opt_t.tc_timer, ph->opt_t.h_timer, ph->opt_t.exec_time, ph->opt_t.centrality); + fprintf(log, "%i\t%4.4f\t%4.4f\t%4.4f\t%4.4f\n", + tv.tv_sec, ph->opt_t.tc_timer, + ph->opt_t.h_timer, ph->opt_t.exec_time, + ph->opt_t.centrality); fclose(log); } if (compute_timers(ph)) { @@ -107,8 +116,8 @@ int main(int argc, char* argv[]) { } while (go); if (free_prince_handler(ph)) { - perror("prince"); - } + perror("prince"); + } fprintf(stdout, "Prince Exited\n"); if (log) { log = fopen(ph->log_file, "a+"); @@ -125,23 +134,26 @@ int main(int argc, char* argv[]) { * @param pointer to the prince_handler object * @return 0 if success, -1 if fail */ -int compute_constants(prince_handler_t ph) { +int compute_constants(prince_handler_t ph) +{ map_id_degree_bc *m_degree_bc = ph->bc_degree_map; struct timers t = ph->def_t; int degrees = 0, i; for (i = 0; i < m_degree_bc->size; i++) { degrees += m_degree_bc->map[i].degree; - /*printf("%s %f\n", m_degree_bc->map[i].id, m_degree_bc->map[i].bc);*/ + /*printf("%s %f\n", m_degree_bc->map[i].id, + * m_degree_bc->map[i].bc);*/ } - ph->c.R = m_degree_bc->n_edges; - ph->c.O_H = degrees / t.h_timer; + ph->c.R = m_degree_bc->n_edges; + ph->c.O_H = degrees / t.h_timer; ph->c.O_TC = m_degree_bc->size * ph->c.R / t.tc_timer; double sqrt_sum1 = 0, sqrt_sum2 = 0; for (i = 0; i < m_degree_bc->size; i++) { - sqrt_sum1 += sqrt(m_degree_bc->map[i].degree * m_degree_bc->map[i].bc); - sqrt_sum2 += sqrt(ph->c.R*m_degree_bc->map[i].bc); + sqrt_sum1 += sqrt(m_degree_bc->map[i].degree + * m_degree_bc->map[i].bc); + sqrt_sum2 += sqrt(ph->c.R * m_degree_bc->map[i].bc); } - ph->c.sq_lambda_H = sqrt_sum1 / ph->c.O_H; + ph->c.sq_lambda_H = sqrt_sum1 / ph->c.O_H; ph->c.sq_lambda_TC = sqrt_sum2 / ph->c.O_TC; return 0; } @@ -152,37 +164,43 @@ int compute_constants(prince_handler_t ph) { * @param pointer to the prince_handler object * @return 0 if success, -1 if fail */ -int compute_timers(prince_handler_t ph) { +int compute_timers(prince_handler_t ph) +{ int t = compute_constants(ph); - if (t == -1) { - fprintf(stderr, "Could not compute constants\n"); - exit(EXIT_FAILURE); - } + if (t == -1) { + fprintf(stderr, "Could not compute constants\n"); + exit(EXIT_FAILURE); + } int my_index = -1, i; - for (i = 0; i< ph->bc_degree_map->size; i++) { + for (i = 0; i < ph->bc_degree_map->size; i++) { if (strcmp(ph->bc_degree_map->map[i].id, ph->self_id) == 0) { my_index = i; } } if (my_index == -1) { - return -1; - } - ph->opt_t.h_timer = sqrt(ph->bc_degree_map->map[my_index].degree / ph->bc_degree_map->map[my_index].bc) * ph->c.sq_lambda_H; - ph->opt_t.tc_timer = sqrt(ph->c.R/ph->bc_degree_map->map[my_index].bc) * ph->c.sq_lambda_TC; + return -1; + } + ph->opt_t.h_timer = sqrt(ph->bc_degree_map->map[my_index].degree + / ph->bc_degree_map->map[my_index].bc) + * ph->c.sq_lambda_H; + ph->opt_t.tc_timer = sqrt(ph->c.R / ph->bc_degree_map->map[my_index].bc) + * ph->c.sq_lambda_TC; return 0; } -double get_self_bc(prince_handler_t ph) { +double get_self_bc(prince_handler_t ph) +{ map_id_degree_bc *m_degree_bc = ph->bc_degree_map; int i; - for(i = 0; i < m_degree_bc->size; i++) { + for (i = 0; i < m_degree_bc->size; i++) { if (strcmp(ph->self_id, m_degree_bc->map[i].id) == 0) { return m_degree_bc->map[i].bc; - } + } } return 0; } -void signal_callback_handler(int signum){ - printf("Caught signal SIGPIPE %d\n",signum); +void signal_callback_handler(int signum) +{ + printf("Caught signal SIGPIPE %d\n", signum); } diff --git a/prince/src/prince_handler.c b/prince/src/prince_handler.c index add33f9..16edaa3 100644 --- a/prince/src/prince_handler.c +++ b/prince/src/prince_handler.c @@ -5,73 +5,76 @@ * @param host host address as a string * @return pointer to prince handler */ -prince_handler_t new_prince_handler(const char * conf_file) { - prince_handler_t result = (prince_handler_t) malloc(PRINCE_HANDLER_SIZE); - if (result == INVALID_PRINCE_HANDLER) { - perror(NULL); - return INVALID_PRINCE_HANDLER; - } - memset(result, 0, PRINCE_HANDLER_SIZE); - /* ph->bc_degree_map = (map_id_degree_bc *) malloc(sizeof(map_id_degree_bc));*/ +prince_handler_t new_prince_handler(const char *conf_file) +{ + prince_handler_t result = (prince_handler_t)malloc(PRINCE_HANDLER_SIZE); + if (result == INVALID_PRINCE_HANDLER) { + perror(NULL); + return INVALID_PRINCE_HANDLER; + } + memset(result, 0, PRINCE_HANDLER_SIZE); + /* ph->bc_degree_map = (map_id_degree_bc *) + * malloc(sizeof(map_id_degree_bc));*/ - result->proto_config = new_proto_config(); - if (result->proto_config == INVALID_PROTO_CONFIG) { - free_prince_handler(result); - return INVALID_PRINCE_HANDLER; - } + result->proto_config = new_proto_config(); + if (result->proto_config == INVALID_PROTO_CONFIG) { + free_prince_handler(result); + return INVALID_PRINCE_HANDLER; + } - result->graph_config = new_graph_config(); - if (result->graph_config == INVALID_PROTO_CONFIG) { - free_prince_handler(result); - return INVALID_PRINCE_HANDLER; - } + result->graph_config = new_graph_config(); + if (result->graph_config == INVALID_PROTO_CONFIG) { + free_prince_handler(result); + return INVALID_PRINCE_HANDLER; + } - /*setting to undefined all params*/ + /*setting to undefined all params*/ result->port = -1; result->refresh = -1; result->sleep_onfail = 1; if (read_config_file(result, conf_file)) { return INVALID_PRINCE_HANDLER; - } + } result->gp = new_graph_parser(result->weights, result->heuristic); - if (result->gp == NULL) { - fprintf(stderr, "Could not create graph_parser\n"); - free_prince_handler(result); - exit(EXIT_FAILURE); - } + if (result->gp == NULL) { + fprintf(stderr, "Could not create graph_parser\n"); + free_prince_handler(result); + exit(EXIT_FAILURE); + } - if (load_routing_plugin(result)) { - perror("prince-plugin"); - return INVALID_PRINCE_HANDLER; - } - if (load_routing_plugin_symbol(result, "new_plugin")) { - perror("prince-plugin"); - return INVALID_PRINCE_HANDLER; - } - if (load_routing_plugin_symbol(result, "delete_plugin")) { - perror("prince-plugin"); - return INVALID_PRINCE_HANDLER; - } - if (load_routing_plugin_symbol(result, "get_initial_timers") != 0) { - perror("prince-plugin"); - return INVALID_PRINCE_HANDLER; - } - if (load_routing_plugin_symbol(result, "push_timers")) { - perror("prince-plugin"); - return INVALID_PRINCE_HANDLER; - } - if (load_routing_plugin_symbol(result, "get_topology")) { - perror("prince-plugin"); - return INVALID_PRINCE_HANDLER; - } + if (load_routing_plugin(result)) { + perror("prince-plugin"); + return INVALID_PRINCE_HANDLER; + } + if (load_routing_plugin_symbol(result, "new_plugin")) { + perror("prince-plugin"); + return INVALID_PRINCE_HANDLER; + } + if (load_routing_plugin_symbol(result, "delete_plugin")) { + perror("prince-plugin"); + return INVALID_PRINCE_HANDLER; + } + if (load_routing_plugin_symbol(result, "get_initial_timers") != 0) { + perror("prince-plugin"); + return INVALID_PRINCE_HANDLER; + } + if (load_routing_plugin_symbol(result, "push_timers")) { + perror("prince-plugin"); + return INVALID_PRINCE_HANDLER; + } + if (load_routing_plugin_symbol(result, "get_topology")) { + perror("prince-plugin"); + return INVALID_PRINCE_HANDLER; + } - result->rp = new_plugin_p(result->host, result->port, result->json_type, result->timer_port); - if (result->rp == NULL) { - fprintf(stderr, "Could not create plugin\n"); - exit(EXIT_FAILURE); - } + result->rp = new_plugin_p(result->host, result->port, result->json_type, + result->timer_port); + if (result->rp == NULL) { + fprintf(stderr, "Could not create plugin\n"); + exit(EXIT_FAILURE); + } return result; } @@ -79,37 +82,39 @@ prince_handler_t new_prince_handler(const char * conf_file) { * Delete a Prince handler and free all the memory * @param struct prince_handler* pointer to the prince_handler struct. */ -int free_prince_handler(prince_handler_t ph) { +int free_prince_handler(prince_handler_t ph) +{ free_graph_parser(ph->gp); - free_graph_config(ph->graph_config); - free_proto_config(ph->proto_config); - if (free_routing_plugin(ph)) { - perror("prince-handler"); - errno = ELIBACC; - return -1; - } + free_graph_config(ph->graph_config); + free_proto_config(ph->proto_config); + if (free_routing_plugin(ph)) { + perror("prince-handler"); + errno = ELIBACC; + return -1; + } /*bc_degree_map_delete(ph->bc_degree_map);*/ free(ph->self_id); free(ph->host); free(ph); - return 0; + return 0; } -int update_prince_id(prince_handler_t ph) { - if (ph->self_id != NULL) { - fprintf(stderr, "I already had an id\n"); - free(ph->self_id); - } - if (ph->rp->t->self_id != NULL) { - ph->self_id = strdup(ph->rp->t->self_id); - if (ph->self_id == NULL) { - perror("prince_handler"); - fprintf(stderr, "Could not set my id from topology\n"); - return 1; - } - } else { - fprintf(stderr, "Plugin does not have an id for me"); - return 1; - } - return 0; +int update_prince_id(prince_handler_t ph) +{ + if (ph->self_id != NULL) { + fprintf(stderr, "I already had an id\n"); + free(ph->self_id); + } + if (ph->rp->t->self_id != NULL) { + ph->self_id = strdup(ph->rp->t->self_id); + if (ph->self_id == NULL) { + perror("prince_handler"); + fprintf(stderr, "Could not set my id from topology\n"); + return 1; + } + } else { + fprintf(stderr, "Plugin does not have an id for me"); + return 1; + } + return 0; } diff --git a/prince/src/socket.c b/prince/src/socket.c index e43490b..e448049 100644 --- a/prince/src/socket.c +++ b/prince/src/socket.c @@ -8,37 +8,39 @@ * @param port remote port of the server * @return socket descriptor */ -int _create_socket(char* hostname, int port, int ignore) { +int _create_socket(char *hostname, int port, int ignore) +{ struct sockaddr_in temp; struct hostent *host; int sock; int rc; - unsigned int timeout = DEFAULT_TIMEOUT; + unsigned int timeout = DEFAULT_TIMEOUT; temp.sin_family = AF_INET; temp.sin_port = htons(port); host = gethostbyname(hostname); if (host == NULL) { - perror("prince-socket"); + perror("prince-socket"); exit(EXIT_FAILURE); } bcopy(host->h_addr, &temp.sin_addr, host->h_length); sock = socket(AF_INET, SOCK_STREAM, 0); - if (sock == -1) { - perror("socket"); - exit(EXIT_FAILURE); - } - while (connect(sock, (struct sockaddr*) &temp, sizeof(temp))) { - if (errno == errno & ignore) { - break; - } - perror("connect"); - fprintf(stderr, "Could not connect to socket, retry\n"); - fprintf(stderr, "Wait %d seconds before reconnecting\n", timeout); - sleep(timeout); - timeout = timeout >= MAX_TIMEOUT ? MAX_TIMEOUT : timeout * 2; - } - timeout = DEFAULT_TIMEOUT; + if (sock == -1) { + perror("socket"); + exit(EXIT_FAILURE); + } + while (connect(sock, (struct sockaddr *)&temp, sizeof(temp))) { + if (errno == errno & ignore) { + break; + } + perror("connect"); + fprintf(stderr, "Could not connect to socket, retry\n"); + fprintf(stderr, "Wait %d seconds before reconnecting\n", + timeout); + sleep(timeout); + timeout = timeout >= MAX_TIMEOUT ? MAX_TIMEOUT : timeout * 2; + } + timeout = DEFAULT_TIMEOUT; return sock; } @@ -53,19 +55,19 @@ int _telnet_receive(int sd, char **buffer) /*ALLOC finalBuffer ->> MUST FREE IT */ int i = 0; int amntRecvd = 0; - char * page = (char*) malloc(SIZE_TO_READ); - while ((amntRecvd = recv(sd, page+i, SIZE_TO_READ, 0)) > 0) { + char *page = (char *)malloc(SIZE_TO_READ); + while ((amntRecvd = recv(sd, page + i, SIZE_TO_READ, 0)) > 0) { i += amntRecvd; - if (!(page=realloc(page, SIZE_TO_READ + i))) { + if (!(page = realloc(page, SIZE_TO_READ + i))) { free(page); return false; } } - if (!i) return false; + if (!i) + return false; page[i] = '\0'; *buffer = page; return true; - } @@ -79,44 +81,47 @@ int _receive_data(int sd, char **buffer) { /*ALLOC finalBuffer ->> MUST FREE IT */ - int i=0; + int i = 0; int amntRecvd = 0; - char * page = (char*) malloc(SIZE_TO_READ); - while ((amntRecvd = recv(sd, page+i, SIZE_TO_READ, 0)) > 0) { + char *page = (char *)malloc(SIZE_TO_READ); + while ((amntRecvd = recv(sd, page + i, SIZE_TO_READ, 0)) > 0) { i += amntRecvd; - if (!(page=realloc(page, i+SIZE_TO_READ))) { + if (!(page = realloc(page, i + SIZE_TO_READ))) { free(page); return 0; } } - if (!i) return 0; + if (!i) + return 0; *buffer = page; char *body = strstr(page, "\r\n\r\n"); - if (body) body += 4; + if (body) + body += 4; /*check if we have received the full topology */ int r = check_header_clen(page, body); - if(!r) fprintf(stderr, "Lenght of buffer don't match\n"); + if (!r) + fprintf(stderr, "Lenght of buffer don't match\n"); *buffer = strdup(body); free(page); return r; } /** * Check if the size of the HTTP content is the same as the header -* @param *header header buffer -* @param *body http body buffer +* @param *header header buffer +* @param *body http body buffer * @return 1 if equal, 0 otherwise */ int check_header_clen(char *header, char *body) { char *buffer = strstr(header, "Content-Length:"); char *endbuf = strstr(buffer, "\r\n"); - char *len = (char*) malloc(endbuf-buffer); - if (len == NULL) { - perror("prince-socket"); - exit(EXIT_FAILURE); - } - memcpy(len, buffer + 15, endbuf-buffer); + char *len = (char *)malloc(endbuf - buffer); + if (len == NULL) { + perror("prince-socket"); + exit(EXIT_FAILURE); + } + memcpy(len, buffer + 15, endbuf - buffer); unsigned long size = atol(len); if (strlen(body) == size) return 1; diff --git a/prince/src/test/data/prince_0_16_3.json b/prince/src/test/data/prince_0_16_3.json new file mode 100644 index 0000000..8495674 --- /dev/null +++ b/prince/src/test/data/prince_0_16_3.json @@ -0,0 +1,19 @@ + +{ + "proto": { + "protocol": "oonf", + "host": "127.0.0.1", + "port": 2009, + "timer_port": 2009, + "refresh": 0, + "log_file": "data/prova.log" + }, + "graph-parser": { + "heuristic": 0, + "weights": 1, + "recursive": 0, + "stop_unchanged": 0, + "multithreaded": 0 + } +} + diff --git a/prince/src/test/data/prova.log b/prince/src/test/data/prova.log new file mode 100644 index 0000000..1c6864f --- /dev/null +++ b/prince/src/test/data/prova.log @@ -0,0 +1,60 @@ +1502811878 0.0000 0.0000 0.0001 0.3944 +1502811878 4.0240 1.8799 0.0001 0.1627 +1502811878 6.2656 2.0698 0.0001 0.3944 +1502811878 4.0240 1.8799 0.0001 0.1627 +1502811878 6.2656 2.0698 0.0001 0.1627 +1502811878 6.2656 2.0698 0.0001 0.3944 +1502811878 4.0240 1.8799 0.0001 0.2452 +1502811878 5.1034 2.0647 0.0001 0.3944 +1502811878 4.0240 1.8799 0.0001 0.1627 +1502811878 6.2656 2.0698 0.0001 0.2452 +1502811878 5.1034 2.0647 0.0001 0.1627 +1502811878 6.2656 2.0698 0.0001 0.2452 +1502811878 5.1034 2.0647 0.0001 0.1627 +1502811878 6.2656 2.0698 0.0001 0.3944 +1502811878 4.0240 1.8799 0.0001 0.3944 +1502811878 4.0240 1.8799 0.0001 0.1627 +1502811878 6.2656 2.0698 0.0001 0.1627 +1502811878 6.2656 2.0698 0.0001 0.3944 +1502811878 4.0240 1.8799 0.0001 0.3944 +1502811878 4.0240 1.8799 0.0001 0.3944 +1502884444 0.0000 0.0000 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.3944 +1502884444 4.0240 1.8799 0.0001 0.3944 +1502884444 4.0240 1.8799 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.3944 +1502884444 4.0240 1.8799 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.3944 +1502884444 4.0240 1.8799 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.1627 +1502884444 6.2656 2.0698 0.0001 0.3944 +1502884444 4.0240 1.8799 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.3944 +1502884444 4.0240 1.8799 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.3944 +1502884444 4.0240 1.8799 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.2452 +1502884444 5.1034 2.0647 0.0001 0.3944 +1502884458 0.0000 0.0000 0.0001 0.2452 +1502884458 5.1034 2.0647 0.0001 0.1627 +1502884458 6.2656 2.0698 0.0001 0.3944 +1502884458 4.0240 1.8799 0.0001 0.1627 +1502884458 6.2656 2.0698 0.0001 0.2452 +1502884458 5.1034 2.0647 0.0001 0.3944 +1502884458 4.0240 1.8799 0.0001 0.3944 +1502884458 4.0240 1.8799 0.0001 0.2452 +1502884458 5.1034 2.0647 0.0001 0.2452 +1502884458 5.1034 2.0647 0.0001 0.2452 +1502884458 5.1034 2.0647 0.0001 0.1627 +1502884458 6.2656 2.0698 0.0001 0.2452 +1502884458 5.1034 2.0647 0.0001 0.3944 +1502884458 4.0240 1.8799 0.0001 0.2452 +1502884458 5.1034 2.0647 0.0001 0.2452 +1502884458 5.1034 2.0647 0.0001 0.1627 +1502884458 6.2656 2.0698 0.0001 0.2452 +1502884458 5.1034 2.0647 0.0001 0.1627 +1502884458 6.2656 2.0698 0.0001 0.1627 +1502884458 6.2656 2.0698 0.0001 0.1627 diff --git a/prince/src/topology.c b/prince/src/topology.c index c8d9d1b..47e4a79 100644 --- a/prince/src/topology.c +++ b/prince/src/topology.c @@ -11,17 +11,20 @@ * @param const char* string containing the id of the new node * @return 1 on success, 0 otherwise */ -int add_node(topology_t topo, const char *id) { +int add_node(topology_t topo, const char *id) +{ node_t head = topo->first; - topo->first = (node_t) malloc(NODE_SIZE); - if (topo->first == INVALID_NODE) { - fprintf(stderr, "prince-topology: could not add node '%s' to topology '%s'", id, topo->self_id); - return 0; - } - topo->first->addresses = 0; - topo->first->id = strdup(id); + topo->first = (node_t)malloc(NODE_SIZE); + if (topo->first == INVALID_NODE) { + fprintf(stderr, + "prince-topology: could not add node '%s' to topology '%s'", + id, topo->self_id); + return 0; + } + topo->first->addresses = 0; + topo->first->id = strdup(id); topo->first->neighbor_list = 0; - topo->first->next = head; + topo->first->next = head; return 1; } @@ -31,14 +34,16 @@ int add_node(topology_t topo, const char *id) { * @param const char* string containing the id of the searched node * @return pointer to the node on success, 0 otherwise */ -node_t find_node(topology_t topo, const char *id) { +node_t find_node(topology_t topo, const char *id) +{ node_t punt; for (punt = topo->first; punt != INVALID_NODE; punt = punt->next) { if (strcmp(punt->id, id) == 0) { return punt; } struct local_address *address; - for (address = punt->addresses; address != NULL; address = address->next) { + for (address = punt->addresses; address != NULL; + address = address->next) { if (strcmp(address->id, id) == 0) { return punt; } @@ -55,31 +60,34 @@ node_t find_node(topology_t topo, const char *id) { * @param const double cost of the edge * @return 0 on success, 1 otherwise */ -int add_neigh(topology_t topo, const char *source, const char *id, const double weight, int validity) { +int add_neigh(topology_t topo, const char *source, const char *id, + const double weight, int validity) +{ struct neighbor *temp, *found; node_t s, t; if ((s = find_node(topo, source)) == NULL) return 1; // check if source node exists if ((t = find_node(topo, id)) == NULL) - return 1; //check if target node exists + return 1; // check if target node exists found = find_neigh(s, t); if (found) { if (found->validity > validity) { - found->weight = weight; //if the link found is older, i update the weight - } - return 0; //The link is already present + found->weight = weight; // if the link found is older, i + // update the weight + } + return 0; // The link is already present } temp = s->neighbor_list; - s->neighbor_list = (struct neighbor*) malloc(sizeof(struct neighbor)); - if (s->neighbor_list == NULL) { - perror("topology"); - return 1; - } - s->neighbor_list->id = t; // add node to source neighbor list - s->neighbor_list->next = temp; + s->neighbor_list = (struct neighbor *)malloc(sizeof(struct neighbor)); + if (s->neighbor_list == NULL) { + perror("topology"); + return 1; + } + s->neighbor_list->id = t; // add node to source neighbor list + s->neighbor_list->next = temp; s->neighbor_list->validity = validity; - s->neighbor_list->weight = weight; + s->neighbor_list->weight = weight; return 0; } @@ -88,26 +96,28 @@ int add_neigh(topology_t topo, const char *source, const char *id, const double * @param int number of chars of the id (0 ipv6, 1 ipv4) * @return pointer to the topology */ -topology_t new_topo(int topology_type) { - topology_t topo = (topology_t) malloc(TOPOLOGY_SIZE); - if (topo == INVALID_TOPOLOGY) { - perror("topology"); - return NULL; - } - switch (topology_type) { - case 0: - topo->id_lenght = 39; - break; - case 1: - topo->id_lenght = 15; - break; - default: - fprintf(stderr, "Received an unknown topology type <%d>\n", topology_type); - free_topo(topo); - return NULL; - } - - topo->first = 0; +topology_t new_topo(int topology_type) +{ + topology_t topo = (topology_t)malloc(TOPOLOGY_SIZE); + if (topo == INVALID_TOPOLOGY) { + perror("topology"); + return NULL; + } + switch (topology_type) { + case 0: + topo->id_lenght = 39; + break; + case 1: + topo->id_lenght = 15; + break; + default: + fprintf(stderr, "Received an unknown topology type <%d>\n", + topology_type); + free_topo(topo); + return NULL; + } + + topo->first = 0; topo->protocol = 0; return topo; } @@ -117,8 +127,9 @@ topology_t new_topo(int topology_type) { * Free topology and dealloc * @param struct topology * pointer to the structure **/ -void free_topo(topology_t topo) { - node_t n_temp, punt=topo->first; +void free_topo(topology_t topo) +{ + node_t n_temp, punt = topo->first; while (punt) { struct neighbor *n = punt->neighbor_list; while (n) { @@ -136,18 +147,19 @@ void free_topo(topology_t topo) { free(topo); } -int valid_topo(const topology_t topo) { - if (topo->self_id == NULL) { - fprintf(stderr, "Topology does not have valid id\n"); - return 1; - } - if (topo->protocol == NULL) { - fprintf(stderr, "Topology does not have valid protocol\n"); - return 1; - } - if (topo->first == NULL) { - fprintf(stderr, "Topology does not have valid node list\n"); - return 1; - } - return 0; +int valid_topo(const topology_t topo) +{ + if (topo->self_id == NULL) { + fprintf(stderr, "Topology does not have valid id\n"); + return 1; + } + if (topo->protocol == NULL) { + fprintf(stderr, "Topology does not have valid protocol\n"); + return 1; + } + if (topo->first == NULL) { + fprintf(stderr, "Topology does not have valid node list\n"); + return 1; + } + return 0; } diff --git a/prince/src/topology_parser.c b/prince/src/topology_parser.c index 32740a5..7c2e113 100644 --- a/prince/src/topology_parser.c +++ b/prince/src/topology_parser.c @@ -6,14 +6,15 @@ * Free a bc_degree map data structure * @param map_id_degree_bc* pointer to the data structure */ -void free_bc_degree_map(map_id_degree_bc * map) +void free_bc_degree_map(map_id_degree_bc *map) { if (map != 0) { int i; for (i = 0; i < map->size; i++) { free(map->map[i].id); } - if (map->map != 0) free(map->map); + if (map->map != 0) + free(map->map); free(map); } } @@ -22,25 +23,30 @@ void free_bc_degree_map(map_id_degree_bc * map) * Parse jsoninfo format * @param char* buffer containing the serialized json */ -topology_t parse_jsoninfo(char *buffer) { +topology_t parse_jsoninfo(char *buffer) +{ topology_t result = new_topo(0); - if (result == INVALID_TOPOLOGY) { - fprintf(stderr, "Could not create recieving topology\n"); - return INVALID_TOPOLOGY; - } + if (result == INVALID_TOPOLOGY) { + fprintf(stderr, "Could not create recieving topology\n"); + return INVALID_TOPOLOGY; + } json_object *topo = json_tokener_parse(buffer); if (topo == NULL) { - fprintf(stderr, "Could not tokenize buffer\n"); - return INVALID_TOPOLOGY; - } - json_object_object_foreach(topo, key, val) { + fprintf(stderr, "Could not tokenize buffer\n"); + return INVALID_TOPOLOGY; + } + json_object_object_foreach(topo, key, val) + { if (strcmp(key, "config") == 0) { json_object *config; json_object_object_get_ex(topo, key, &config); - json_object_object_foreach(config, key, val) - if (strcmp(key, "mainIp") == 0) { - result->self_id = strdup(json_object_get_string(val)); - } + json_object_object_foreach( + config, key, + val) if (strcmp(key, "mainIp") == 0) + { + result->self_id = + strdup(json_object_get_string(val)); + } } else if (strcmp(key, "topology") == 0) { int i; json_object *jarray; @@ -48,71 +54,94 @@ topology_t parse_jsoninfo(char *buffer) { int arraylen = json_object_array_length(jarray); if (arraylen == 0) { return INVALID_TOPOLOGY; - } + } for (i = 0; i < arraylen; i++) { - const char *source=0, *target=0; + const char *source = 0, *target = 0; double cost = 0; int validity = 0; - json_object *elem = json_object_array_get_idx(jarray, i); - json_object_object_foreach(elem, key, val) { + json_object *elem = + json_object_array_get_idx(jarray, i); + json_object_object_foreach(elem, key, val) + { if (strcmp(key, "lastHopIP") == 0) { - source = json_object_get_string(val); - } else if (strcmp(key, "destinationIP") == 0) { - target = json_object_get_string(val); - } else if (strcmp(key, "tcEdgeCost") == 0) { - cost = json_object_get_double(val); - } else if (strcmp(key, "validityTime") == 0) { - validity = json_object_get_int(val); - } else if (source && target && cost && validity) { - if (!find_node(result, source)) { - add_node(result, source); - } - if (!find_node(result, target)) { - add_node(result, target); - } - //printf("%s\t%s\t%f\n", source, target, cost); - if (add_neigh(result, source, target, cost, validity)) { + source = json_object_get_string( + val); + } else if (strcmp(key, "destinationIP") + == 0) { + target = json_object_get_string( + val); + } else if (strcmp(key, "tcEdgeCost") + == 0) { + cost = json_object_get_double( + val); + } else if (strcmp(key, "validityTime") + == 0) { + validity = json_object_get_int( + val); + } else if (source && target && cost + && validity) { + if (!find_node(result, + source)) { + add_node(result, + source); + } + if (!find_node(result, + target)) { + add_node(result, + target); + } + // printf("%s\t%s\t%f\n", + // source, target, cost); + if (add_neigh(result, source, + target, cost, + validity)) { printf("error\n"); return 0; } source = target = 0; cost = 0; } else { - fprintf(stderr, "Recieved unknown key '%s'\n", key); - } + fprintf(stderr, + "Recieved unknown key '%s'\n", + key); + } } } } else { - fprintf(stderr, "Recieved unknown key '%s' when parsing jsoninfo\n", key); - } + fprintf(stderr, + "Recieved unknown key '%s' when parsing jsoninfo\n", + key); + } } - result->protocol = strdup("olsrv1"); + result->protocol = strdup("olsrv1"); json_object_put(topo); return result; } -struct neighbor* find_neigh(node_t source, node_t target){ +struct neighbor *find_neigh(node_t source, node_t target) +{ struct neighbor *punt; for (punt = source->neighbor_list; punt != 0; punt = punt->next) { - if (punt->id==target) { + if (punt->id == target) { return punt; - } + } } - for(punt = target->neighbor_list; punt != 0; punt = punt->next) { + for (punt = target->neighbor_list; punt != 0; punt = punt->next) { if (punt->id == source) { return punt; - } + } } return NULL; } - -int add_local_address(node_t node, const char* address) { +int add_local_address(node_t node, const char *address) +{ struct local_address *la_temp; la_temp = node->addresses; - node->addresses = (struct local_address*) malloc(sizeof(struct local_address)); + node->addresses = + (struct local_address *)malloc(sizeof(struct local_address)); node->addresses->id = address; node->addresses->next = la_temp; return 1; @@ -124,22 +153,24 @@ int add_local_address(node_t node, const char* address) { * @param char* buffer containing the serialized json * @return representation of the graph as "struct topology*" type */ -topology_t parse_netjson(char* buffer) { +topology_t parse_netjson(char *buffer) +{ topology_t c_topo = new_topo(0); - if (c_topo == INVALID_TOPOLOGY) { - fprintf(stderr, "Could not create recieving topology\n"); - return c_topo; - } + if (c_topo == INVALID_TOPOLOGY) { + fprintf(stderr, "Could not create recieving topology\n"); + return c_topo; + } json_object *topo = json_tokener_parse(buffer); if (topo == NULL) { - fprintf(stderr, "Could not tokenize buffer\n"); - return INVALID_TOPOLOGY; - } - json_object_object_foreach(topo, key, val) { + fprintf(stderr, "Could not tokenize buffer\n"); + return INVALID_TOPOLOGY; + } + json_object_object_foreach(topo, key, val) + { if (strcmp(key, "protocol") == 0) { - c_topo->protocol = strdup(json_object_get_string(val)); - } else if (strcmp(key,"router_id") == 0) { - c_topo->self_id=strdup(json_object_get_string(val)); + c_topo->protocol = strdup(json_object_get_string(val)); + } else if (strcmp(key, "router_id") == 0) { + c_topo->self_id = strdup(json_object_get_string(val)); } else if (strcmp(key, "nodes") == 0) { int i, arraylen; json_object *array; @@ -147,26 +178,45 @@ topology_t parse_netjson(char* buffer) { arraylen = json_object_array_length(array); for (i = 0; i < arraylen; i++) { const char *node_id; - json_object *elem = json_object_array_get_idx(array, i); - json_object_object_foreach(elem, key, val) { + json_object *elem = + json_object_array_get_idx(array, i); + json_object_object_foreach(elem, key, val) + { if (strcmp(key, "id") == 0) { - node_id = json_object_get_string(val); + node_id = + json_object_get_string( + val); add_node(c_topo, node_id); - } else if (strcmp(key, "local_addresses") == 0) { + } else if (strcmp(key, + "local_addresses") + == 0) { int j, la_len; json_object *la_array; - json_object_object_get_ex(elem, key, &la_array); - la_len = json_object_array_length(la_array); - for (j = 0; j < la_len; j++) { - json_object *la_elem = json_object_array_get_idx(la_array, j); - node_t node = find_node(c_topo, node_id); - if (node == INVALID_NODE) { - fprintf(stderr, "Could not find node %s\n", node_id); - } - add_local_address(node, json_object_get_string(la_elem)); - } + json_object_object_get_ex( + elem, key, &la_array); + la_len = + json_object_array_length( + la_array); + for (j = 0; j < la_len; j++) { + json_object *la_elem = + json_object_array_get_idx( + la_array, + j); + node_t node = find_node( + c_topo, + node_id); + if (node + == INVALID_NODE) { + fprintf(stderr, + "Could not find node %s\n", + node_id); + } + add_local_address( + node, + json_object_get_string( + la_elem)); + } } - } } } else if (strcmp(key, "links") == 0) { @@ -176,24 +226,32 @@ topology_t parse_netjson(char* buffer) { int arraylen = json_object_array_length(jarray); if (arraylen == 0) { return 0; - } + } for (i = 0; i < arraylen; i++) { const char *source = NULL, *target = NULL; double cost = 0; - json_object *elem = json_object_array_get_idx(jarray, i); - json_object_object_foreach(elem, key, val) { + json_object *elem = + json_object_array_get_idx(jarray, i); + json_object_object_foreach(elem, key, val) + { if (strcmp(key, "source") == 0) { - source = json_object_get_string(val); + source = json_object_get_string( + val); } if (strcmp(key, "target") == 0) { - target = json_object_get_string(val); + target = json_object_get_string( + val); } if (strcmp(key, "cost") == 0) { - cost = json_object_get_double(val); + cost = json_object_get_double( + val); } - if (source && target && cost){ - if (add_neigh(c_topo, source, target, cost, 0)) { - fprintf(stderr, "error\n"); + if (source && target && cost) { + if (add_neigh(c_topo, source, + target, cost, + 0)) { + fprintf(stderr, + "error\n"); return 0; } @@ -201,11 +259,12 @@ topology_t parse_netjson(char* buffer) { cost = 0; } } - } } else { - fprintf(stderr, "Recieved an unknown key '%s' when parsing netjson\n", key); - } + fprintf(stderr, + "Recieved an unknown key '%s' when parsing netjson\n", + key); + } } json_object_put(topo); return c_topo;