akmhoque | 29c1db5 | 2012-09-07 14:47:43 -0500 | [diff] [blame^] | 1 | #include<stdio.h> |
| 2 | #include<string.h> |
| 3 | #include<stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <getopt.h> |
| 6 | #include <sys/time.h> |
| 7 | #include <assert.h> |
| 8 | #ifdef HAVE_CONFIG_H |
| 9 | #include <config.h> |
| 10 | #endif |
| 11 | #include <sys/types.h> |
| 12 | |
| 13 | |
| 14 | #include <ccn/ccn.h> |
| 15 | #include <ccn/uri.h> |
| 16 | #include <ccn/keystore.h> |
| 17 | #include <ccn/signing.h> |
| 18 | #include <ccn/schedule.h> |
| 19 | #include <ccn/hashtb.h> |
| 20 | |
| 21 | |
| 22 | #include "nlsr.h" |
| 23 | #include "nlsr_route.h" |
| 24 | #include "nlsr_lsdb.h" |
| 25 | |
| 26 | int |
| 27 | route_calculate(struct ccn_schedule *sched, void *clienth, struct ccn_scheduled_event *ev, int flags) |
| 28 | { |
| 29 | printf("route_calculate called\n"); |
| 30 | |
| 31 | if( ! nlsr->is_build_adj_lsa_sheduled ) |
| 32 | { |
| 33 | /* Calculate Route here */ |
| 34 | |
| 35 | struct hashtb_param param_me = {0}; |
| 36 | nlsr->map = hashtb_create(sizeof(struct map_entry), ¶m_me); |
| 37 | make_map(); |
| 38 | assign_mapping_number(); |
| 39 | print_map(); |
| 40 | |
| 41 | int i; |
| 42 | int **adj_matrix; |
| 43 | int map_element=hashtb_n(nlsr->map); |
| 44 | adj_matrix=malloc(map_element * sizeof(int *)); |
| 45 | for(i = 0; i < map_element; i++) |
| 46 | { |
| 47 | adj_matrix[i] = malloc(map_element * sizeof(int)); |
| 48 | } |
| 49 | make_adj_matrix(adj_matrix,map_element); |
| 50 | print_adj_matrix(adj_matrix,map_element); |
| 51 | |
| 52 | long int source=get_mapping_no(nlsr->router_name); |
| 53 | long int *parent=(long int *)malloc(map_element * sizeof(long int)); |
| 54 | |
| 55 | calculate_path(adj_matrix,parent, map_element, source); |
| 56 | |
| 57 | print_all_path_from_source(parent,source); |
| 58 | |
| 59 | |
| 60 | |
| 61 | for(i = 0; i < map_element; i++) |
| 62 | { |
| 63 | free(adj_matrix[i]); |
| 64 | } |
| 65 | free(parent); |
| 66 | free(adj_matrix); |
| 67 | hashtb_destroy(&nlsr->map); |
| 68 | |
| 69 | } |
| 70 | nlsr->is_route_calculation_scheduled=0; |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | calculate_path(int **adj_matrix, long int *parent, long int V, long int S) |
| 77 | { |
| 78 | int i; |
| 79 | long int v,u; |
| 80 | long int *dist=(long int *)malloc(V * sizeof(long int)); |
| 81 | long int *Q=(long int *)malloc(V * sizeof(long int)); |
| 82 | long int head=0; |
| 83 | /* Initial the Parent */ |
| 84 | for (i = 0 ; i < V; i++) |
| 85 | { |
| 86 | parent[i]=EMPTY_PARENT; |
| 87 | dist[i]=INF_DISTANCE; |
| 88 | Q[i]=i; |
| 89 | } |
| 90 | |
| 91 | dist[S]=0; |
| 92 | sort_queue_by_distance(Q,dist,head,V); |
| 93 | |
| 94 | while (head < V ) |
| 95 | { |
| 96 | u=Q[head]; |
| 97 | if(dist[u] == INF_DISTANCE) |
| 98 | { |
| 99 | break; |
| 100 | } |
| 101 | |
| 102 | for(v=0 ; v <V; v++) |
| 103 | { |
| 104 | if( adj_matrix[u][v] > 0 ) // |
| 105 | { |
| 106 | if ( is_not_explored(Q,v,head+1,V) ) |
| 107 | { |
| 108 | |
| 109 | if( dist[u] + adj_matrix[u][v] < dist[v]) |
| 110 | { |
| 111 | dist[v]=dist[u] + adj_matrix[u][v] ; |
| 112 | parent[v]=u; |
| 113 | } |
| 114 | |
| 115 | } |
| 116 | |
| 117 | } |
| 118 | |
| 119 | } |
| 120 | |
| 121 | head++; |
| 122 | sort_queue_by_distance(Q,dist,head,V); |
| 123 | } |
| 124 | |
| 125 | free(Q); |
| 126 | free(dist); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | print_all_path_from_source(long int *parent,long int source) |
| 131 | { |
| 132 | int i, map_element; |
| 133 | struct map_entry *me; |
| 134 | |
| 135 | struct hashtb_enumerator ee; |
| 136 | struct hashtb_enumerator *e = ⅇ |
| 137 | |
| 138 | hashtb_start(nlsr->map, e); |
| 139 | map_element=hashtb_n(nlsr->map); |
| 140 | |
| 141 | for(i=0;i<map_element;i++) |
| 142 | { |
| 143 | me=e->data; |
| 144 | if(me->mapping != source) |
| 145 | { |
| 146 | print_path(parent,(long int)me->mapping); |
| 147 | printf("\n"); |
| 148 | } |
| 149 | hashtb_next(e); |
| 150 | } |
| 151 | |
| 152 | hashtb_end(e); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | print_path(long int *parent, long int dest) |
| 158 | { |
| 159 | if (parent[dest] != EMPTY_PARENT ) |
| 160 | print_path(parent,parent[dest]); |
| 161 | |
| 162 | printf(" %ld",dest); |
| 163 | } |
| 164 | |
| 165 | int |
| 166 | is_not_explored(long int *Q, long int u,long int start, long int element) |
| 167 | { |
| 168 | int ret=0; |
| 169 | long int i; |
| 170 | for(i=start; i< element; i++) |
| 171 | { |
| 172 | if ( Q[i] == u ) |
| 173 | { |
| 174 | ret=1; |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | void |
| 182 | sort_queue_by_distance(long int *Q,long int *dist,long int start,long int element) |
| 183 | { |
| 184 | long int i,j; |
| 185 | long int temp_u; |
| 186 | |
| 187 | for ( i=start ; i < element ; i ++) |
| 188 | { |
| 189 | for( j=i+1; j<element; j ++) |
| 190 | { |
| 191 | if (dist[Q[j]] < dist[Q[i]]) |
| 192 | { |
| 193 | temp_u=Q[j]; |
| 194 | Q[j]=Q[i]; |
| 195 | Q[i]=temp_u; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | } |
| 201 | |
| 202 | void |
| 203 | print_map(void) |
| 204 | { |
| 205 | int i, map_element; |
| 206 | struct map_entry *me; |
| 207 | |
| 208 | struct hashtb_enumerator ee; |
| 209 | struct hashtb_enumerator *e = ⅇ |
| 210 | |
| 211 | hashtb_start(nlsr->map, e); |
| 212 | map_element=hashtb_n(nlsr->map); |
| 213 | |
| 214 | for(i=0;i<map_element;i++) |
| 215 | { |
| 216 | me=e->data; |
| 217 | printf("Router: %s Mapping Number: %d \n",me->router,me->mapping); |
| 218 | hashtb_next(e); |
| 219 | } |
| 220 | |
| 221 | hashtb_end(e); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | void |
| 226 | assign_mapping_number(void) |
| 227 | { |
| 228 | int i, map_element; |
| 229 | struct map_entry *me; |
| 230 | |
| 231 | struct hashtb_enumerator ee; |
| 232 | struct hashtb_enumerator *e = ⅇ |
| 233 | |
| 234 | hashtb_start(nlsr->map, e); |
| 235 | map_element=hashtb_n(nlsr->map); |
| 236 | |
| 237 | for(i=0;i<map_element;i++) |
| 238 | { |
| 239 | me=e->data; |
| 240 | me->mapping=i; |
| 241 | hashtb_next(e); |
| 242 | } |
| 243 | |
| 244 | hashtb_end(e); |
| 245 | } |
| 246 | |
| 247 | void |
| 248 | make_map(void) |
| 249 | { |
| 250 | |
| 251 | |
| 252 | int i, adj_lsdb_element; |
| 253 | struct alsa *adj_lsa; |
| 254 | |
| 255 | struct hashtb_enumerator ee; |
| 256 | struct hashtb_enumerator *e = ⅇ |
| 257 | |
| 258 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 259 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 260 | |
| 261 | for(i=0;i<adj_lsdb_element;i++) |
| 262 | { |
| 263 | adj_lsa=e->data; |
| 264 | add_adj_data_to_map(adj_lsa->header->orig_router->name,adj_lsa->body,adj_lsa->no_link); |
| 265 | hashtb_next(e); |
| 266 | } |
| 267 | |
| 268 | hashtb_end(e); |
| 269 | |
| 270 | } |
| 271 | |
| 272 | void |
| 273 | add_map_entry(char *router) |
| 274 | { |
| 275 | |
| 276 | struct map_entry *me=(struct map_entry*)malloc(sizeof(struct map_entry )); |
| 277 | |
| 278 | struct hashtb_enumerator ee; |
| 279 | struct hashtb_enumerator *e = ⅇ |
| 280 | int res; |
| 281 | |
| 282 | hashtb_start(nlsr->map, e); |
| 283 | res = hashtb_seek(e, router, strlen(router), 0); |
| 284 | |
| 285 | if(res == HT_NEW_ENTRY) |
| 286 | { |
| 287 | me=e->data; |
| 288 | me->router=(char *)malloc(strlen(router)+1); |
| 289 | memset(me->router,0,strlen(router)+1); |
| 290 | memcpy(me->router,router,strlen(router)); |
| 291 | me->mapping=0; |
| 292 | } |
| 293 | |
| 294 | hashtb_end(e); |
| 295 | |
| 296 | } |
| 297 | |
| 298 | |
| 299 | void |
| 300 | add_adj_data_to_map(char *orig_router, char *body, int no_link) |
| 301 | { |
| 302 | add_map_entry(orig_router); |
| 303 | |
| 304 | int i=0; |
| 305 | char *lsa_data=(char *)malloc(strlen(body)+1); |
| 306 | memset( lsa_data,0,strlen(body)+1); |
| 307 | memcpy(lsa_data,body,strlen(body)+1); |
| 308 | char *sep="|"; |
| 309 | char *rem; |
| 310 | char *rtr_id; |
| 311 | char *length; |
| 312 | char *face; |
| 313 | char *metric; |
| 314 | |
| 315 | if(no_link >0 ) |
| 316 | { |
| 317 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 318 | length=strtok_r(NULL,sep,&rem); |
| 319 | face=strtok_r(NULL,sep,&rem); |
| 320 | metric=strtok_r(NULL,sep,&rem); |
| 321 | |
| 322 | add_map_entry(rtr_id); |
| 323 | |
| 324 | for(i=1;i<no_link;i++) |
| 325 | { |
| 326 | rtr_id=strtok_r(NULL,sep,&rem); |
| 327 | length=strtok_r(NULL,sep,&rem); |
| 328 | face=strtok_r(NULL,sep,&rem); |
| 329 | metric=strtok_r(NULL,sep,&rem); |
| 330 | |
| 331 | add_map_entry(rtr_id); |
| 332 | |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | free(lsa_data); |
| 337 | } |
| 338 | |
| 339 | int |
| 340 | get_mapping_no(char *router) |
| 341 | { |
| 342 | struct map_entry *me; |
| 343 | |
| 344 | struct hashtb_enumerator ee; |
| 345 | struct hashtb_enumerator *e = ⅇ |
| 346 | int res,ret; |
| 347 | |
| 348 | hashtb_start(nlsr->map, e); |
| 349 | res = hashtb_seek(e, router, strlen(router), 0); |
| 350 | |
| 351 | if( res == HT_OLD_ENTRY ) |
| 352 | { |
| 353 | me=e->data; |
| 354 | ret=me->mapping; |
| 355 | } |
| 356 | else if(res == HT_NEW_ENTRY) |
| 357 | { |
| 358 | hashtb_delete(e); |
| 359 | } |
| 360 | |
| 361 | hashtb_end(e); |
| 362 | |
| 363 | return ret; |
| 364 | |
| 365 | } |
| 366 | |
| 367 | void |
| 368 | assign_adj_matrix_for_lsa(struct alsa *adj_lsa, int **adj_matrix) |
| 369 | { |
| 370 | int mapping_orig_router=get_mapping_no(adj_lsa->header->orig_router->name); |
| 371 | int mapping_nbr_router; |
| 372 | |
| 373 | int i; |
| 374 | char *lsa_data=(char *)malloc(strlen(adj_lsa->body)+1); |
| 375 | memset( lsa_data,0,strlen(adj_lsa->body)+1); |
| 376 | memcpy(lsa_data,adj_lsa->body,strlen(adj_lsa->body)+1); |
| 377 | char *sep="|"; |
| 378 | char *rem; |
| 379 | char *rtr_id; |
| 380 | char *length; |
| 381 | char *face; |
| 382 | char *metric; |
| 383 | |
| 384 | if(adj_lsa->no_link >0 ) |
| 385 | { |
| 386 | rtr_id=strtok_r(lsa_data,sep,&rem); |
| 387 | length=strtok_r(NULL,sep,&rem); |
| 388 | face=strtok_r(NULL,sep,&rem); |
| 389 | metric=strtok_r(NULL,sep,&rem); |
| 390 | |
| 391 | mapping_nbr_router=get_mapping_no(rtr_id); |
| 392 | adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric); |
| 393 | |
| 394 | for(i=1;i<adj_lsa->no_link;i++) |
| 395 | { |
| 396 | rtr_id=strtok_r(NULL,sep,&rem); |
| 397 | length=strtok_r(NULL,sep,&rem); |
| 398 | face=strtok_r(NULL,sep,&rem); |
| 399 | metric=strtok_r(NULL,sep,&rem); |
| 400 | |
| 401 | mapping_nbr_router=get_mapping_no(rtr_id); |
| 402 | adj_matrix[mapping_orig_router][mapping_nbr_router]=atoi(metric); |
| 403 | |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | free(lsa_data); |
| 408 | } |
| 409 | |
| 410 | void |
| 411 | make_adj_matrix(int **adj_matrix,int map_element) |
| 412 | { |
| 413 | |
| 414 | init_adj_matrix(adj_matrix,map_element); |
| 415 | |
| 416 | int i, adj_lsdb_element; |
| 417 | struct alsa *adj_lsa; |
| 418 | |
| 419 | struct hashtb_enumerator ee; |
| 420 | struct hashtb_enumerator *e = ⅇ |
| 421 | |
| 422 | hashtb_start(nlsr->lsdb->adj_lsdb, e); |
| 423 | adj_lsdb_element=hashtb_n(nlsr->lsdb->adj_lsdb); |
| 424 | |
| 425 | for(i=0;i<adj_lsdb_element;i++) |
| 426 | { |
| 427 | adj_lsa=e->data; |
| 428 | assign_adj_matrix_for_lsa(adj_lsa,adj_matrix); |
| 429 | hashtb_next(e); |
| 430 | } |
| 431 | |
| 432 | hashtb_end(e); |
| 433 | |
| 434 | } |
| 435 | |
| 436 | void |
| 437 | init_adj_matrix(int **adj_matrix,int map_element) |
| 438 | { |
| 439 | int i, j; |
| 440 | for(i=0;i<map_element;i++) |
| 441 | for(j=0;j<map_element;j++) |
| 442 | adj_matrix[i][j]=0; |
| 443 | } |
| 444 | |
| 445 | void print_adj_matrix(int **adj_matrix, int map_element) |
| 446 | { |
| 447 | int i, j; |
| 448 | for(i=0;i<map_element;i++) |
| 449 | { |
| 450 | for(j=0;j<map_element;j++) |
| 451 | printf("%d ",adj_matrix[i][j]); |
| 452 | printf("\n"); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | |