void mark_ifaces (int termif) { struct node *n, *peer ; for (n = mobj_head (nodemobj) ; n != NULL ; n = n->next) { if (n->nodetype == NT_L1 && ! MK_ISSET (n, MK_IFACE)) { peer = get_neighbour (n, NT_L1) ; if (termif) { /* we don't want terminal interfaces */ if (peer == NULL) MK_SET (n, MK_IFACE) ; } else { /* we don't want backbone interfaces */ if (peer != NULL) { MK_SET (n, MK_IFACE) ; MK_SET (peer, MK_IFACE) ; /* optimization */ } } } } }
int get_l3tol1 (FILE *fp, struct node *n, l3tol1_t *tab, int max) { struct linklist *ll ; int idx ; /* * Search for routing instance if any */ tab [0].r = get_neighbour (n, NT_ROUTER) ; idx = 0 ; for (ll = n->linklist ; ll != NULL ; ll = ll->next) { struct node *p ; p = getlinkpeer (ll->link, n) ; if (p->nodetype == NT_L2) { struct node *m ; for (m = mobj_head (nodemobj) ; m != NULL ; m = m->next) MK_CLEAR (m, MK_OUTPUTLINK) ; idx = get_l3tol1_L2 (fp, p, tab, max, idx) ; } } return idx ; }
int image_connected_components(LImage* image) { int n = image->size; int number_connected_components = 0; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(image->grid[i*n+j].type == 1 && image->grid[i*n+j].componentNum == 0) { number_connected_components++; image->grid[i*n+j].componentNum = number_connected_components; for(int x = 0; x < n * n / 4; ++x) { for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(image->grid[i*n+j].enabled && image->grid[i*n+j].componentNum == 0) { for(int k = 0; k < 8; k++) { if(image->grid[get_neighbour(i, j, k, n)].componentNum == number_connected_components) image->grid[i*n+j].componentNum = number_connected_components; } } } } } } } } return number_connected_components; }
bool TransitionPaintExecution::apply_transition(const std::vector<int>& dirs, const std::vector<int>& frame_nums) { for (auto dir_iter = dirs.begin(); dir_iter != dirs.end(); dir_iter++) { Point neighbour_pos = get_neighbour(tile_pos, *dir_iter); if (!game->level.contains(neighbour_pos)) { return false; } TileViewDef::pointer neighbour_def = view->level_view.tile_views[neighbour_pos].view_def; if (!neighbour_def) { return false; } if (!boost::regex_match(static_cast<std::string>(neighbour_def->name), pattern_re)) { return false; } } if (frame_nums.empty()) return false; int variation = get("tile_variation"); int r = variation % frame_nums.size(); paint_frame(frame_nums[r]); return true; }
int image_points_curvature(LImage* image) { int n = image->size; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { int count = image_counter(image, i, j, n); if(image->grid[i*n+j].enabled && count == 2) { for(int k = 0; k < 8; k++) { if(image->grid[get_neighbour(i, j, k, n)].enabled) { if(image->grid[get_neighbour(i, j, (k + 3) % 8, n)].enabled) image->grid[i*n+j].curvature = 1; if(image->grid[get_neighbour(i, j, (k - 3) % 8, n)].enabled) image->grid[i*n+j].curvature = 1; if(image->grid[get_neighbour(i, j, (k + 2) % 8, n)].enabled) image->grid[i*n+j].curvature = 2; if(image->grid[get_neighbour(i, j, (k - 2) % 8, n)].enabled) image->grid[i*n+j].curvature = 2; if(image->grid[get_neighbour(i, j, (k + 1) % 8, n)].enabled) image->grid[i*n+j].curvature = 3; if(image->grid[get_neighbour(i, j, (k - 1) % 8, n)].enabled) image->grid[i*n+j].curvature = 3; } } } } } return 0; }
vector<route_t*>* BCubeTopology::get_paths(int src, int dest) { vector<route_t*>* paths = new vector<route_t*>(); for (int i=K; i>=0; i--) { if (addresses[src][i]!=addresses[dest][i]) paths->push_back(dc_routing(src,dest,i)); else paths->push_back(alt_dc_routing(src,dest,i,get_neighbour(src,i))); } return paths; }
int image_line_type(LImage* image) { int n = image->size; int diagonal_number = 0; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { int count = image_counter(image, i, j, n); if(image->grid[i*n+j].enabled && count == 2) { if(image->grid[get_neighbour(i, j, 0, n)].enabled && image->grid[get_neighbour(i, j, 4, n)].enabled) { diagonal_number++; image->grid[i*n+j].diagonalNum = diagonal_number; for(int x = 0; x < sqrt(2) * n; ++x) { for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { int countt = image_counter(image, i, j, n); if(image->grid[i*n+j].enabled && image->grid[i*n+j].diagonalNum == 0 && countt == 2) { for(int k = 0; k < 8; k++) { if(image->grid[get_neighbour(i, j, k, n)].diagonalNum == diagonal_number && image->grid[get_neighbour(i, j, (k + 4) % 8, n)].enabled) image->grid[i*n+j].diagonalNum = diagonal_number; } } } } } } if(image->grid[get_neighbour(i, j, 2, n)].enabled && image->grid[get_neighbour(i, j, 6, n)].enabled) { diagonal_number++; image->grid[i*n+j].diagonalNum = diagonal_number; for(int x = 0; x < sqrt(2) * n; ++x) { for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { int countt = image_counter(image, i, j, n); if(image->grid[i*n+j].enabled && image->grid[i*n+j].diagonalNum == 0 && countt == 2) { for(int k = 0; k < 8; k++) { if(image->grid[get_neighbour(i, j, k, n)].diagonalNum == diagonal_number && image->grid[get_neighbour(i, j, (k + 4) % 8, n)].enabled) image->grid[i*n+j].diagonalNum = diagonal_number; } } } } } } } } } return 0; }
void output_collect_L2 (FILE *fp, struct node *L2node) { struct node *L1node ; L1node = get_neighbour (L2node, NT_L1) ; if (L1node) { /* * L2 is a collect point */ if (L2node->u.l2.stat != NULL) { fprintf (fp, "trafic %s %s %s ", L2node->u.l2.stat, L1node->eq->name, L1node->eq->snmp ) ; /* * Logical interface name present */ if(L2node->u.l2.ifname != NULL && strcmp(L2node->u.l2.ifname, "-")) { /* trafic <id collect> <eq> <comm> <logical iface> - */ fprintf (fp, "%s -\n", L2node->u.l2.ifname) ; } else { /* trafic <id collect> <eq> <comm> <phys iface> vlan */ fprintf (fp, "%s %d\n", L1node->u.l1.ifname, L2node->u.l2.vlan) ; } L2node->mark = 0 ; } /* * L1 is a collect point and a native Ethernet (not-802.1Q) interface */ if (L1node->u.l1.stat && L1node->u.l1.l1type == L1T_ETHER) output_collect_L1 (fp, L1node) ; } }
static void sel_mark_net (ip_t *addr) { struct node *n, *l2node ; struct network *net ; for (n = mobj_head (nodemobj) ; n != NULL ; n = n->next) { if (n->nodetype == NT_L3 && ip_match (&n->u.l3.addr, addr, 1)) { MK_SELECT (n) ; l2node = get_neighbour (n, NT_L2) ; if (l2node != NULL) transport_vlan_on_L2 (l2node, l2node->u.l2.vlan) ; } } for (net = mobj_head (netmobj) ; net != NULL ; net = net->next) if (ip_match (&net->addr, addr, 1)) MK_SELECT (net) ; for (n = mobj_head (nodemobj) ; n != NULL ; n = n->next) if (MK_ISSET (n, MK_L2TRANSPORT)) MK_SELECT (n) ; }
void Battle::set_up_participants() { BOOST_LOG_TRIVIAL(trace) << "Setting up battle participants"; UnitStack::pointer attacker = game->level.tiles[attacking_point].stack; for (int dir = 0; dir < 7; dir++) { UnitStack::pointer stack; Point stack_point = get_neighbour(target_point, dir); if (game->level.contains(stack_point)) { stack = game->level.tiles[stack_point].stack; } if (stack && stack->owner == attacker->owner) { stacks[dir] = stack; stack_sides[dir] = Attacker; } else if (stack && stack->owner != attacker->owner) { stacks[dir] = stack; stack_sides[dir] = Defender; } else { stacks[dir] = UnitStack::pointer(); stack_sides[dir] = None; } } for (int dir = 0; dir < 7; dir++) { if (stacks[dir]) { BOOST_LOG_TRIVIAL(trace) << "Stack " << dir << ": " << stacks[dir]->id; for (unsigned int i = 0; i < stacks[dir]->units.size(); i++) { int participant_id = participants.size(); participants.push_back(Participant(participant_id, stack_sides[dir], dir, stacks[dir], i)); BOOST_LOG_TRIVIAL(trace) << "Participant: " << participants[participant_id]; } } } }
void output_portmac (FILE *fp, struct node *bridgenode) { struct linklist *ll ; struct node *n ; vlanset_t vlanset; vlan_t v; int done = 0; /* get all VLAN ids */ vlan_zero(vlanset); for (ll = bridgenode->linklist ; ll != NULL ; ll = ll->next) { struct link *l ; struct node *other ; l = ll->link ; other = getlinkpeer (l, bridgenode) ; if(other->nodetype == NT_L2) vlan_set (vlanset, other->u.l2.vlan); } /* for each VLAN found */ for (v = 0 ; v < MAXVLAN ; v++) { if(vlan_isset (vlanset, v)) { /* process each L2 node */ for (ll = bridgenode->linklist ; ll != NULL ; ll = ll->next) { struct link *l ; struct node *L2node ; l = ll->link ; L2node = getlinkpeer (l, bridgenode) ; /* examine associated L1 */ if(L2node->nodetype == NT_L2) { struct node *L1node ; L1node = get_neighbour (L2node, NT_L1) ; /* only edge L1 interface */ if(L1node != NULL && MK_ISSELECTED (L1node) && !MK_ISSET(L1node, MK_PORTMAC) && strcmp (L1node->u.l1.link, EXTLINK) == 0) { char *ifname = L1node->u.l1.ifname; if(!done) { /* portmac <id_collect> <eq> <comm> <eqtype> <iflist> <vlan> */ fprintf (fp, "portmac P%s.%d %s %s %s %s", bridgenode->eq->name, v, bridgenode->eq->name, bridgenode->eq->snmp, bridgenode->eq->type, ifname ) ; done = 1 ; } else { fprintf(fp, ",%s", ifname) ; } /* mark node */ MK_SET(L1node, MK_PORTMAC) ; } } } if(done) { /* output vlan id */ fprintf(fp, " %d\n", v) ; done = 0 ; } /* clear mark */ for (n = mobj_head (nodemobj) ; n != NULL ; n = n->next) { if(n->nodetype == NT_L1) MK_CLEAR (n, MK_PORTMAC) ; } } } }
int image_branch_points(LImage* image) { int n = image->size; //First mark all pixels with more than 2 neighbors with a dummy variable for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { int count = image_counter(image, i, j, n); if(image->grid[i*n+j].enabled && count > 2) { image->grid[i*n+j].dummy = 1; }else{ image->grid[i*n+j].dummy = 0; } } } for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { int count_dummy = image_counter_dummy(image, i, j, n); if(image->grid[i*n+j].dummy == 1 && count_dummy > 1) { for(int k = 0; k < 16; k++) { if(image->grid[get_neighbour(i, j, k, n)].dummy == 1 && image->grid[get_neighbour(i, j, k + 2, n)].dummy == 1) { image->grid[get_neighbour(i, j, k, n)].dummy = 0; image->grid[get_neighbour(i, j, k + 2, n)].dummy = 0; image->grid[get_neighbour(i, j, k + 1, n)].enabled = 1; image->grid[get_neighbour(i, j, k + 1, n)].type = 2; image->grid[i*n+j].dummy = 0; } } } } } for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(image->grid[i*n+j].type == 2) { for(int k = 0; k < 8; k++) { if(image->grid[get_neighbour(i, j, k, n)].dummy == 1) image->grid[get_neighbour(i, j, k, n)].dummy = 0; if(image->grid[get_neighbour(i, j, k, n)].type == 2) image->grid[get_neighbour(i, j, k, n)].type = 0; } } } } for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(image->grid[i*n+j].dummy == 1) { image->grid[i*n+j].dummy = 0; image->grid[i*n+j].type = 2; } } } for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(image->grid[i*n+j].type == 2) { for(int k = 0; k < 8; k++) { if(image->grid[get_neighbour(i, j, k, n)].type == 2) image->grid[get_neighbour(i, j, k, n)].type = 0; } } } } return 0; }
int image_counter_dummy(LImage* image, int i, int j, int n) { int count = 0; for(int k = 0; k < 8; ++k) if(image->grid[get_neighbour(i,j,k,n)].dummy == 1) count++; return count; }
int image_thin(LImage* image) { int n = image->size; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(image->grid[i*n+j].enabled) { //We want to check if we can remove this pixel //We have to check it the surrounding 8 pixels are //still connected when we remove this pixel int count = image_counter(image, i, j, n); if(count >= 7) //With 7 or more surrounding pixels they will always be connected { image->grid[i*n+j].enabled = 0; //So we can remove the center pixel } else if(count >= 2) { //Now we want to check if the surrounding pixels are connected //We find the first black pixel, then from there search for the //first white pixel that might disconnect it int firstBlack = -1; int state = 0; int disconnections = 0; for(int k = 0; k < 16; k++) { if(state == 0) //Searching for black pixel { if(image->grid[get_neighbour(i, j, k, n)].enabled) { if( firstBlack == -1 ) firstBlack = k; state = 1; } } else if(state == 1) //Black pixel was found, keep following untill white { if(!image->grid[get_neighbour(i, j, k, n)].enabled) state = 2; } else if(state == 2) //White pixel found. Did this white pixel disconnect the black pixels? { //Check the pixel after the white pixel if(image->grid[get_neighbour(i, j, k, n)].enabled) { //There was a black pixel after the white pixel //If the white pixel was at a corner, then it //is not disconnected. If the white pixel is in the middle //then it is disconnected if(k%2 == 1) state = 1; else { state = 0; disconnections++; } } else //A second white pixel. Then yes this was a disconnection { state = 0; disconnections++; } } //Check if we already went a full round if( k >= 8 ){ if( (k-8) >= firstBlack ) break; } } //If the 8 pixels have one disconnection or less //Then they are still connected so we can remove this pixel if(disconnections <= 1) { image->grid[i*n+j].enabled = 0; } } } } } return 0; }
int main (int argc, char *argv []) { int i ; struct node *n ; struct eq *eq ; ip_t cidr ; int c, err ; char *prog, *errstr ; prog = argv [0] ; err = 0 ; sel_init () ; while ((c = getopt (argc, argv, "an:e:E:tv:m")) != -1) { switch (c) { case 'a' : case 'n' : case 'e' : case 'E' : case 't' : case 'v' : case 'm' : if ((errstr = sel_register (c, optarg)) != NULL) { fprintf (stderr, "%s: %s\n", prog, errstr) ; err = 1 ; } break ; case '?' : default : usage (prog) ; } } if (err) exit (1) ; argc -= optind ; argv += optind ; if (argc == 0) usage (prog) ; /* * Read the graph and select subgraph */ bin_read (stdin, mobjlist) ; sel_mark () ; /* * First pass : mark all L3 nodes matching CIDR arguments */ for (i = 0 ; i < argc ; i++) { if (! ip_pton (argv [i], &cidr)) { fprintf (stderr, "%s: Invalid cidr '%s'\n", prog, argv [i]) ; exit (1) ; } for (n = mobj_head (nodemobj) ; n != NULL ; n = n->next) if (n->nodetype == NT_L3 && ip_match (&n->u.l3.addr, &cidr, 0)) MK_SET (n, MK_IPMATCH) ; } /* * Output selection arguments */ fprintf (stdout, "selection") ; for (i = 0 ; i < argc ; i++) fprintf (stdout, " %s", argv [i]) ; fprintf (stdout, "\n") ; /* * Second pass : identify all routing instances connected to those * L3 nodes we have marked before, and print equipements which are * routing instances. */ for (n = mobj_head (nodemobj) ; n != NULL ; n = n->next) { if (n->nodetype == NT_L3 && MK_ISSET (n, MK_IPMATCH)) { struct node *r ; MK_SET (n->eq, MK_IPMATCH) ; r = get_neighbour (n, NT_ROUTER) ; if (r != NULL && ! MK_ISSET (r, MK_ISROUTER)) { MK_SET (n, MK_ISROUTER) ; MK_SET (r, MK_ISROUTER) ; MK_SET (n->eq, MK_ISROUTER) ; if (MK_ISSELECTED (n)) fprintf (stdout, "eq %s:%s router\n", n->eq->name, r->u.router.name) ; } } } /* * Third pass : print equipements which are not routing instances */ for (eq = mobj_head (eqmobj) ; eq != NULL ; eq = eq->next) if (MK_ISSET (eq, MK_IPMATCH) && ! MK_ISSET (eq, MK_ISROUTER)) if (MK_ISSELECTED (eq)) fprintf (stdout, "eq %s host\n", eq->name) ; /* * Fourth pass : identify broadcast domain for each L3 */ for (n = mobj_head (nodemobj) ; n != NULL ; n = n->next) { if (n->nodetype == NT_L3 && MK_ISSET (n, MK_IPMATCH) && ! MK_ISSET (n, MK_PROCESSED)) walkl3 (stdout, n) ; } sel_end () ; exit (0) ; }
void walkl3 (FILE *fp, struct node *n) { struct node *l2 ; struct node *m ; char cloudname [MAXCLOUDNAME] ; int l1count, l3count ; struct node *directl1 [2], *directl3 [2] ; int selected ; /* * Reset all non-L3 nodes */ for (m = mobj_head (nodemobj) ; m != NULL ; m = m->next) { if (m->nodetype == NT_L3) MK_CLEAR (m, MK_L2TRANSPORT) ; else { MK_CLEAR (m, MK_L2TRANSPORT) ; MK_CLEAR (m, MK_IPMATCH) ; MK_CLEAR (m, MK_ISROUTER) ; MK_CLEAR (m, MK_VLANTRAVERSAL) ; MK_CLEAR (m, MK_PROCESSED) ; } vlan_zero (m->vlanset) ; } l2 = get_neighbour (n, NT_L2) ; if (l2 != NULL) transport_vlan_on_L2 (l2, l2->u.l2.vlan) ; /* * All reachable L2 nodes are marked. * * Count physical interfaces (L1 nodes) to see if this equipement * is connected via a direct link to another node, or if this * equipement is connected to a broadcast domain. * * Count marked L1 nodes. If there is only 2 of them, check if * linkname matches (if not "X", or EXTLINK). */ l1count = 0 ; l3count = 0 ; selected = 0 ; for (m = mobj_head (nodemobj) ; m != NULL ; m = m->next) { if (m->nodetype == NT_L1 && MK_ISSET (m, MK_L2TRANSPORT)) { if (MK_ISSELECTED (m) || MK_ISSELECTED (m->eq)) selected = 1 ; if (l1count < 2) directl1 [l1count] = m ; l1count++ ; } if (m->nodetype == NT_L3 && MK_ISSET (m, MK_L2TRANSPORT) && MK_ISSET (m, MK_IPMATCH)) { if (MK_ISSELECTED (m) || MK_ISSELECTED (m->eq)) selected = 1 ; if (l3count < 2) directl3 [l3count] = m ; l3count++ ; } } if (! selected) return ; if (l1count == 2 && l3count == 2 && directl1 [0]->u.l1.link == directl1 [1]->u.l1.link) { /* * This is a direct link between two L3 nodes */ output_direct (fp, directl3) ; MK_SET (directl3 [0], MK_PROCESSED) ; MK_SET (directl3 [1], MK_PROCESSED) ; } else { /* * This is a cloud */ for (m = mobj_head (nodemobj) ; m != NULL ; m = m->next) { if (m->nodetype == NT_L2 && MK_ISSET (m, MK_L2TRANSPORT)) { struct linklist *ll ; struct node *r ; for (ll = m->linklist ; ll != NULL ; ll = ll->next) { r = getlinkpeer (ll->link, m) ; if (r->nodetype == NT_L3 && MK_ISSET (r, MK_IPMATCH)) MK_SET (r, MK_VLANTRAVERSAL) ; } } } /* * Output cloud */ output_cloud (fp, n, cloudname, sizeof cloudname) ; /* * Output links to this cloud */ for (m = mobj_head (nodemobj) ; m != NULL ; m = m->next) { if (m->nodetype == NT_L3 && MK_ISSET (m, MK_VLANTRAVERSAL)) { output_link (fp, m, cloudname) ; MK_CLEAR (m, MK_VLANTRAVERSAL) ; MK_SET (m, MK_PROCESSED) ; } } } }
void output_cloud (FILE *fp, struct node *n, char *cloudname, size_t size) { static int cloudno = 0 ; struct node *l1node, *l2node ; vlanset_t vs ; ip_t tabnet [MAXNET] ; int ntab ; iptext_t addr ; int i ; cloudno++ ; snprintf (cloudname, size, CLOUDFORMAT, cloudno) ; fprintf (fp, "cloud %s", cloudname) ; /* * Reference to the broadcast domain * - get the first marked L1 interface * - name of equipement of this L1 * - get the first marked L2 from this L1 */ find_interface (&l1node, &l2node) ; if (l1node == NULL || l2node == NULL) { /* * Degenerated case such as an L3 node not connected to the * rest of the world... */ if (l2node == NULL) l2node = get_neighbour (n, NT_L2) ; if (l2node != NULL) fprintf (fp, " {%s %s %d}", l2node->eq->name, "-", l2node->u.l2.vlan) ; else fprintf (fp, " {%s %s %d}", "-", "-", 0) ; } else fprintf (fp, " {%s %s %d}", l1node->eq->name, l1node->u.l1.ifname, l2node->u.l2.vlan) ; /* * Get all vlan used */ traversed_vlans (vs) ; fprintf (fp, " {") ; print_vlanlist (fp, vs, 1) ; fprintf (fp, "}") ; /* * Get all network CIDR from L3 nodes */ ntab = find_networks (tabnet, NTAB (tabnet)) ; fprintf (fp, " {") ; for (i = 0 ; i < ntab ; i++) { if (i != 0) fprintf (fp, " ") ; ip_ntop (&tabnet [i], addr, 1) ; fprintf (fp, "%s", addr) ; } fprintf (fp, "}") ; fprintf (fp, "\n") ; }
void pim_interface::handle_hello(const sockaddr_in6 *from, pim_hello_message *msg, uint16_t len) { m_stats.counter(HelloCount, RX)++; /* rejected by configuration */ if (!conf()->neigh_acl_accepts(from->sin6_addr)) return; uint16_t holdtime = 0; bool has_dr_priority = false; uint32_t dr_priority = 0; bool has_genid = false; uint32_t genid = mrd::get_randu32(); bool has_lan_delay = false; uint16_t propdelay = 0, overrinter = 0; bool trackbit = false; int address_count = 0; pim_encoded_unicast_address *addresses = 0; int old_address_count = 0; pim_encoded_unicast_address *old_addresses = 0; int slen = sizeof(pim_hello_message); pim_hello_option *opt = msg->options(); while (slen < len) { uint16_t optlen = ntoh(opt->length); switch (ntoh(opt->type)) { case pim_hello_option::holdtime: if (optlen == 2) holdtime = ntoh(opt->data16()[0]); break; case pim_hello_option::lan_prune_delay: if (optlen == 4) { has_lan_delay = true; propdelay = ntoh(opt->data16()[0]); overrinter = ntoh(opt->data16()[1]); trackbit = (propdelay & 0x8000) != 0; propdelay &= 0x7fff; } break; case pim_hello_option::dr_priority: if (optlen == 4) { has_dr_priority = true; dr_priority = ntoh(opt->data32()[0]); } break; case pim_hello_option::genid: if (optlen == 4) { has_genid = true; genid = ntoh(opt->data32()[0]); } break; case pim_hello_option::addrlist: if ((optlen % sizeof(pim_encoded_unicast_address)) == 0) { address_count = optlen / sizeof(pim_encoded_unicast_address); addresses = (pim_encoded_unicast_address *)opt->data(); } break; case pim_hello_option::cisco_old_addrlist: if ((optlen % sizeof(pim_encoded_unicast_address)) == 0) { old_address_count = optlen / sizeof(pim_encoded_unicast_address); old_addresses = (pim_encoded_unicast_address *)opt->data(); } break; } slen += sizeof(pim_hello_option) + optlen; opt = opt->next(); } pim_neighbour *neigh; if ((neigh = get_neighbour(from->sin6_addr))) { if (holdtime == 0) { neighbour_timed_out(neigh); return; } if (!neigh->compare_genid(genid)) { if (should_log(NORMAL)) neigh->log().writeline("Had different GenID, forcing timeout."); remove_neighbour(neigh, false); neigh = 0; } } bool is_new = false; if (!neigh) { if (!(neigh = allocate_neighbour(from->sin6_addr))) { if (should_log(DEBUG)) log().writeline("Failed to allocate neighbor state."); return; } is_new = true; } if (!conf()->support_old_cisco_addrlist()) { old_addresses = 0; old_address_count = 0; } neigh->update_from_hello(addresses, address_count, old_addresses, old_address_count, holdtime); if (has_dr_priority) neigh->set_dr_priority(dr_priority); if (has_genid) neigh->set_genid(genid); if (has_lan_delay) neigh->set_lan_delay(propdelay, overrinter, trackbit); if (is_new) found_new_neighbour(neigh); check_lan_delay(); elect_subnet_dr(); }
static void omp_force (sotl_device_t *dev) { struct timeval tv1,tv2; sotl_atom_set_t *set = &dev->atom_set; gettimeofday(&tv1,NULL); int start_ind, end_ind; #pragma omp parallel for for (unsigned current = 0; current < set->natoms; current++) { calc_t force[3] = { 0.0, 0.0, 0.0 }; // // } // /*Version naive*/ // for (unsigned other = 0; other < set->natoms; other++) // if (current != other) // { // calc_t sq_dist = squared_distance (set, current, other); // // if (sq_dist < LENNARD_SQUARED_CUTOFF) // { // calc_t intensity = lennard_jones (sq_dist); // // force[0] += intensity * (set->pos.x[current] - set->pos.x[other]); // force[1] += intensity * (set->pos.x[set->offset + current] - // set->pos.x[set->offset + other]); // force[2] += intensity * (set->pos.x[set->offset * 2 + current] - // set->pos.x[set->offset * 2 + other]); // } // // } /* Fin version naive */ /** * Version Tri par boites : */ unsigned other; int box_id_current = atom_box_calc(dev, current); if (is_valid_box(dev, box_id_current)) { for (int i = -1; i<2; i++) { for (int j = -1; j<2; j++) { for (int k = -1; k<2; k++) { int box_id_other = get_neighbour(dev, box_id_current, i, j, k); if (is_valid_box(dev, box_id_other)) { start_ind = box_count_cummul[box_id_other]; end_ind = box_count_cummul[box_id_other-1]; int size = start_ind - end_ind; for (int l = 0; l < size; l++) { other = box_count_cummul[box_id_other]-l-1; if (other != current) { calc_t sq_dist = squared_distance (set, current, other); if (sq_dist < LENNARD_SQUARED_CUTOFF) { calc_t intensity = lennard_jones (sq_dist); force[0] += intensity * (set->pos.x[current] - set->pos.x[other]); force[1] += intensity * (set->pos.x[set->offset + current] - set->pos.x[set->offset + other]); force[2] += intensity * (set->pos.x[set->offset * 2 + current] - set->pos.x[set->offset * 2 + other]); } } } } } } } } /*Fin version Tri par Boites */ /** Version Tri par Z : */ // for (unsigned other = current+1; other < set->natoms && ((set->pos.z[other] - set->pos.z[current]) < LENNARD_CUTOFF); other++) // { // //Now takes the atoms near from current according to the sorted array // if (current != other) // { // calc_t sq_dist = squared_distance (set, current, other); // // if (sq_dist < LENNARD_SQUARED_CUTOFF) // { // calc_t intensity = lennard_jones (sq_dist); // // force[0] += intensity * (set->pos.x[current] - set->pos.x[other]); // force[1] += intensity * (set->pos.x[set->offset + current] - // set->pos.x[set->offset + other]); // force[2] += intensity * (set->pos.x[set->offset * 2 + current] - // set->pos.x[set->offset * 2 + other]); // } // } // } // for (unsigned other = current; other > 0 && ((set->pos.z[current] - set->pos.z[other-1]) < LENNARD_CUTOFF); other--) // { // //Now takes the atoms near from current according to the sorted array // if (current != other-1) // { // calc_t sq_dist = squared_distance (set, current, other-1); // // if (sq_dist < LENNARD_SQUARED_CUTOFF) // { // calc_t intensity = lennard_jones (sq_dist); // // force[0] += intensity * (set->pos.x[current] - set->pos.x[other-1]); // force[1] += intensity * (set->pos.x[set->offset + current] - // set->pos.x[set->offset + other-1]); // force[2] += intensity * (set->pos.x[set->offset * 2 + current] - // set->pos.x[set->offset * 2 + other-1]); // } // } // } /*Fin version Tri par Z */ set->speed.dx[current] += force[0]; set->speed.dx[set->offset + current] += force[1]; set->speed.dx[set->offset * 2 + current] += force[2]; } gettimeofday(&tv2,NULL); time_force += (float)TIME_DIFF(tv1,tv2); }
/** * IMPLEMENTATION D'UN RECUIT SIMULÉ * * Voisinage : On enlève une station d'une remorque, et on l'insert_best * (ou juste insert) dans une des remorque (sa remorque y compris, sa place * peut n'être plus optimale avec d'autres insertions) * * Energie : * - Version 0 : Cout de la solution * * Temperature : * - Version 0 : Décroissance de la température en T(n+1) = lambda * T(n) * avec lambda = 0.99 * * Solution initiale : La solution la plus bête possible, pour ne pas ensuite * être gêné par une solution qui serait trop bonne. * * Algorithme : * s := s0 // solution * e := E(s) // energie * k := 0 // Iteration * tant que k < kmax et e > emax * sn := voisin(s) * en := E(sn) * si en < e ou aléatoire() < P(en - e, temp(k/kmax)) alors * s := sn; e := en * k := k + 1 * retourne s * * @return Si une solution a pu être trouvée */ bool AnnealingSolver::solve() { if (log2()) { cout << "\n---AnnealingSolver::solve START instance: " << inst->name << " de taille " << inst->stations->size() << "\n"; } // Seed initialisation srand (time(NULL)); // ((double) rand() / (RAND_MAX)) // double btwn 0 and 1 // Arguments Options* args = Options::args; const string sinserter = args->station_inserter; const string rchooser = args->remorque_chooser; int itermax = args->itermax; int recuit_variant = args->recuit_variant; double temp_init = args->temp_init; double lambda = args->lambda; int size_palier = args->size_palier; // Par défaut (-1) on ne fait qu'une seule itération itermax = itermax == -1 ? 1 : itermax; temp_init = temp_init == 10000.0 ? 10000.0 : temp_init; lambda = lambda == 0.99 ? 0.99 : lambda; size_palier = size_palier == 10 ? 10 : size_palier; double taux_acceptation_min = 0.000001; double taux_acceptation = 1; double nb_accepted = 0; // Recuit informations double temperature = temp_init; // Initial temperature (default) double energy_max = 0; // Energy maximum of acceptation double energy; // Energy int k = 0; // Iteration Solution* neighbour = new Solution(inst); // Intermediate solution // INITIAL SOLUTION (Stupid) Solution* sol = new Solution(inst); get_initial_solution(sol); energy = get_energy(sol); logn2("AnnealingSolver::Cost of the initial solution = "+sol->get_cost_string()); logn2("AnnealingSolver::Energy of the initial solution = "+to_string(energy)); // Result File ofstream energy_file; energy_file.open("energy.csv",ios::app); energy_file << "\n"<< inst->name << ";" << time(NULL) << "\nEnergy;"; ofstream temp_file; temp_file.open("temp.csv",ios::app); temp_file << "\n"<< inst->name << ";" << time(NULL) << "\nTemperature;"; ofstream taux_acceptation_file; taux_acceptation_file.open("taux_acceptation.csv",ios::app); taux_acceptation_file << "\n"<< inst->name << ";" << time(NULL) << "\nTaux d'acceptation;"; Solution* best_sol = new Solution(inst); best_sol->copy(sol); // RECUIT while (k < itermax && energy > energy_max && taux_acceptation > taux_acceptation_min) { logn3("AnnealingSolver:: Iteration number "+to_string(k)); energy_file << energy << ";"; temp_file << temperature << ";"; taux_acceptation_file << taux_acceptation << ";"; // Updating Temperature if (k % size_palier == 0) temperature = get_next_temperature(temperature,lambda); logn3("AnnealingSolver:: Temperature value "+to_string(temperature)); // Getting neighbour get_neighbour(neighbour,sol,recuit_variant); double energy_neighbour = get_energy(neighbour); logn3("AnnealingSolver:: Energy of neighbour = "+to_string(energy_neighbour)); // Choosing the solution if (energy_neighbour < energy || ((double) rand() / (RAND_MAX)) < exp((energy - energy_neighbour)/temperature)) { logn3("AnnealingSolver:: New solution found, energy = "+to_string(energy_neighbour)); if (energy_neighbour < energy) cout << "-"; // Better else cout << "+"; // Metropolis nb_accepted++; sol->clear(); sol->copy(neighbour); if (sol->get_cost() < best_sol->get_cost()) { // We have a better solution cout << endl << "AnnealingSolver:: Better sol found : " << sol->get_cost() << " Temp : " << temperature << endl; best_sol->clear(); best_sol->copy(sol); } energy = energy_neighbour; } else { // neighbour rejected cout << "."; } // Updating iteration ++k; taux_acceptation = nb_accepted / k ; } cout << endl; energy_file << "\n" << best_sol->get_cost_string() << "\n"; temp_file << "\n" << best_sol->get_cost_string() << "\n"; taux_acceptation_file << "\n" << best_sol->get_cost_string() << "\n"; energy_file.close(); temp_file.close(); taux_acceptation_file.close(); this->found = true; this->solution = best_sol; logn2("StupidSolver::solve: END"); return found; }
void pim_interface::handle_assert(const sockaddr_in6 *from, pim_assert_message *msg, uint16_t length) { m_stats.counter(AssertCount, RX)++; if (should_log(MESSAGE_CONTENT)) { base_stream &os = log(); os.inc_level(); _debug_pim_dump(os, *msg); os.dec_level(); } if (!get_neighbour(from->sin6_addr)) { m_stats.counter(AssertCount, Bad)++; return; } inet6_addr grpaddr(msg->gaddr.addr, msg->gaddr.masklen); pim_group_node *node = pim->get_group(grpaddr); bool rpt = msg->rpt(); uint32_t metric_pref = msg->metric_pref(); uint32_t metric = ntoh(msg->metric); if (node) { if (!IN6_IS_ADDR_UNSPECIFIED(&msg->saddr.addr)) { pim_group_source_state *state = node->get_state(msg->saddr.addr); if (state) { pim_common_oif::assert_state prev = pim_common_oif::AssertNoInfo; bool existed = false; pim_common_oif *oif = (pim_common_oif *)state->get_oif(owner()); if (oif) { prev = oif->current_assert_state(); existed = true; } state->handle_assert(owner(), from->sin6_addr, rpt, metric, metric_pref); /* for some future reason the oif may be released meanwhile */ oif = (pim_common_oif *)state->get_oif(owner()); if (!oif && existed) { /* transitioned somehow */ return; } pim_common_oif::assert_state current = oif ? oif->current_assert_state() : pim_common_oif::AssertNoInfo; if (current != pim_common_oif::AssertNoInfo || current != prev) { /* (S,G) Assert state machine is not NoInfo * or a transition occurred: no (*,G) handling */ return; } } } if (node->has_wildcard()) { node->wildcard()->handle_assert(owner(), from->sin6_addr, rpt, metric, metric_pref); } } }