void inform_neighbors(const polyhedron &newp, const polyhedron &oldp, int n, polyhedron *p) { int new_index = 0, old_index = 0; while (true) { if (new_index >= newp.num_neighbors) { for(int i=old_index; i<oldp.num_neighbors; i++) remove_old_neighbor(n, p, oldp.neighbors[i]); return; } if (old_index >= oldp.num_neighbors) { for(int i=new_index; i<newp.num_neighbors; i++) add_new_neighbor(n, p, newp.neighbors[i]); return; } if (newp.neighbors[new_index] < oldp.neighbors[old_index]) { add_new_neighbor(n, p, newp.neighbors[new_index]); new_index ++; } else if (oldp.neighbors[old_index] < newp.neighbors[new_index]) { remove_old_neighbor(n, p, oldp.neighbors[old_index]); old_index ++; } else { // (newp.neighbors[new_index] == oldp.neighbors[old_index]) new_index ++; old_index ++; } } }
static int parse_LSA(char *LSAs) { FILE *fLSA; char buf[BUFSIZE]; MList* tmp; node_t* tmp_node; char name[NAME_LENGTH]; int seq_num; char neighbors[BUFSIZE]; char* neighbor; fLSA = fopen(LSAs, "r"); if (fLSA == NULL) return -1; while (fgets(buf, BUFSIZE, fLSA)) { sscanf(buf, "%s %d %s", name, &seq_num, neighbors); tmp = add_new_node(name); tmp_node = (node_t*)tmp->data; /* make client list if necessary */ if (!mlist_find(servs, (void*)tmp)) { // this is not a server if (!startsWith(name, "router")) { // this is not a router either, then this is a client for sure if (!mlist_find(clits, (void*)tmp)) { // do I add this already? clits = mlist_append(clits, (void*)tmp); } } } if (tmp_node->seq_num >= seq_num) continue; // skip if the seq is not more recent tmp_node->seq_num = seq_num; mlist_free(tmp_node->neighbors); tmp_node->neighbors = NULL; // free all and re-add all neighbors neighbor = strtok(neighbors, ","); while (neighbor) { tmp = add_new_node(neighbor); // tmp would be a MList* of node_t* add_new_neighbor(tmp_node, tmp); neighbor = strtok(NULL, ","); } } fclose(fLSA); return 0; }