示例#1
0
void
dijkstra_calc_dest (dijkstra_graph_t *graph, int src, int dest)
{
    assert (graph);

    if (!graph->initialized) {
        memcpy (graph->nodes_cc, graph->nodes, graph->n_nodes * sizeof(node_t));
        graph->initialized = 1;
    }

    if (graph->src == src && graph->dest == dest)
        return; // we've already computed this

    graph->src = src;
    graph->dest = dest;

    // reset the heap
    memset (graph->heap, 0, graph->n_nodes*sizeof (node_t *));
    graph->heap_len = 0;
    graph->heap_idx = 0;

    // --- Dijkstra stuff; unreachable nodes will never make into the queue ---
    memcpy (graph->nodes, graph->nodes_cc, graph->n_nodes * sizeof(node_t));
    node_t *start = graph->nodes + src, *goal = graph->nodes + dest, *lead;
	set_dist (graph, start, start, 0);
	while ((lead = pop_queue (graph)) && lead != goal) {
		for (edge_t *e = lead->edge; e; e = e->sibling)
			set_dist (graph, e->node, lead, lead->dist + e->weight);
    }
}
示例#2
0
文件: dijkstra.c 项目: hungly/Pacman
void calc_all(node_t *start) {
    node_t *lead;
    edge_t *e;

    set_dist(start, start, 0);
    while ((lead = pop_queue()))
        for (e = lead->edge; e; e = e->sibling)
            set_dist(e->destination_node, lead, lead->distance + e->len);
}
/* --- Dijkstra stuff; unreachable nodes will never make into the queue --- */
void calc_all(node_t *const start) {
	node_t *lead;
	edge_t *e;
 
	set_dist(start, start, 0);
	while ((lead = pop_queue()))
		for (e = lead->edge; e; e = e->sibling)
			set_dist(e->nd, lead, lead->dist + e->len);
}
示例#4
0
void CClientPool::Create(IP_DIST_t  dist_value,
            uint32_t min_ip,
            uint32_t max_ip,
            double l_flow,
            double t_cps,
            CFlowGenListMac* mac_info,
            bool has_mac_map,
            uint16_t tcp_aging, 
            uint16_t udp_aging) {
    assert(max_ip>=min_ip);
    set_dist(dist_value);
    uint32_t total_ip = max_ip - min_ip +1;
    uint32_t avail_ip = total_ip;
    if (has_mac_map && (mac_info!=NULL)) {
        for(int idx=0;idx<total_ip;idx++){
            mac_addr_align_t *mac_adr = NULL;
            mac_adr = mac_info->get_mac_addr_by_ip(min_ip+idx);
            if (mac_adr == NULL) {
                avail_ip--;
            }
        }
    }
    if (avail_ip!=0) {
        m_ip_info.resize(avail_ip);
    } else {
        printf("\n Error, invalid mac file is configured.\n");
        assert(0);
    }

    int skip_cnt=0;
    if (total_ip > ((l_flow*t_cps/MAX_PORT))) {
        if (has_mac_map) {
            skip_cnt=0;
            for(int idx=0;idx<total_ip;idx++){
                mac_addr_align_t *mac_adr = NULL;
                mac_adr = mac_info->get_mac_addr_by_ip( min_ip+idx);
                if (mac_adr != NULL) {
                    m_ip_info[idx-skip_cnt] = new CClientInfoL(has_mac_map);
                    m_ip_info[idx-skip_cnt]->set_ip(min_ip+idx);
                    m_ip_info[idx-skip_cnt]->set_mac(mac_adr);
                } else {
                    skip_cnt++;
                }
            }
        } else {
            for(int idx=0;idx<total_ip;idx++){
                m_ip_info[idx] = new CClientInfoL(has_mac_map);
                m_ip_info[idx]->set_ip(min_ip+idx);
            }
        } 
    } else {
        if (has_mac_map) {
            skip_cnt=0;
            for(int idx=0;idx<total_ip;idx++){
                mac_addr_align_t *mac_adr = NULL;
                mac_adr = mac_info->get_mac_addr_by_ip(min_ip+idx);
                if (mac_adr != NULL) {
                    m_ip_info[idx-skip_cnt] = new CClientInfo(has_mac_map);
                    m_ip_info[idx-skip_cnt]->set_ip(min_ip+idx);
                    m_ip_info[idx-skip_cnt]->set_mac(mac_adr);
                } else {
                    skip_cnt++;
                }
            }
        } else {
            for(int idx=0;idx<total_ip;idx++){
                m_ip_info[idx] = new CClientInfo(has_mac_map);
                m_ip_info[idx]->set_ip(min_ip+idx);
            }
        } 
 
    }
    m_tcp_aging = tcp_aging;
    m_udp_aging = udp_aging;
    CreateBase();
}
示例#5
0
/* Activity on a socket: input, output, or error...
 */
static void message_handler(struct file_info *fi, int events){
  	printf("Message handler called\n");
	char buf[512];

	if (events & (POLLERR | POLLHUP)) {
		double time;
		int error;
		socklen_t len = sizeof(error);
		if (getsockopt(fi->fd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) {
			perror("getsockopt");
		}
		switch (error) {
		case 0:
			printf("Lost connection on socket %d\n", fi->fd);
			time = timer_now() + 1;
			break;
		default:
			printf("Error '%s' on socket %d\n", strerror(error), fi->fd);
			time = timer_now() + 5;
		}

		close(fi->fd);

		/* Start a timer to reconnect.
		 */
		if (fi->type == FI_OUTGOING) {
			timer_start(time, timer_reconnect, fi->uid);
			fi->fd = -1;
			fi->u.fi_outgoing.status = FI_CONNECTING;
		}
		else {
			fi->type = FI_FREE;
		}
		char *my_addr_str = addr_to_string(my_addr);
		char *fi_addr_str = addr_to_string(fi->addr);
		  
		set_dist(nl, graph, nl_nsites(nl), fi_addr_str, my_addr_str, 0);
		updateGraph();
		send_gossip();
		free (my_addr_str);
		free (fi_addr_str);

		return;
	}
	if (events & POLLOUT) {
		int n = send(fi->fd, fi->output_buffer, fi->amount_to_send, 0);
		if (n < 0) {
			perror("send");
		}
		if (n > 0) {
			if (n == fi->amount_to_send) {
				fi->amount_to_send = 0;
			}
			else {
				memmove(&fi->output_buffer[n], fi->output_buffer, fi->amount_to_send - n);
				fi->amount_to_send -= n;
			}
		}
	}
	if (events & POLLIN) {
		add_input(fi);
	}
	if (events & ~(POLLIN|POLLOUT|POLLERR|POLLHUP)) {
		printf("message_handler: unknown events\n");
		fi->events = 0;
	}
}
示例#6
0
void updateGraph()
{
  if(dist) {
    free(dist);
    dist = NULL;
  }
  if(prev) {
    free(prev);
    prev = NULL;
  }
  if(graph) {
    free(graph);
    graph = NULL;
  }
int r= 0, s=0;
  dist = (int *) malloc(nl_nsites(nl) * sizeof(int));
  prev = (int *) malloc(nl_nsites(nl) * sizeof(int));
  graph = (int *) malloc(sizeof(int) * (nl_nsites(nl)) * (nl_nsites(nl)));
	for(r=0;r<nl_nsites(nl);r++)
    	{
    		dist[r] = INFINITY;
    		prev[r] = UNDEFINED;
    	for(s=0;s<nl_nsites(nl);s++)
    	{
    		graph[INDEX(r, s, nl_nsites(nl))] = 0;
    	}
    }


	struct gossip *g = gossip;

	while(g != NULL)
	{
		int len = strlen(gossip_latest(g));
		char *lat = (char *) malloc(sizeof(char) * (len+1));
		strcpy(lat, gossip_latest(g));

		char *addr = lat;

		char *ctr = index(addr, '/');		
		*ctr++ = 0;

		char *payload = index(ctr, '/');		
		*payload++ = 0;

		char *token = strtok(payload, ";");	

		while (token) 
		{	
			//printf("Address = %s\n", addr);
			//printf("Token = %s\n", token);	
			set_dist(nl, graph, nl_nsites(nl), addr, token, 1);
			token = strtok(NULL, ";");
		}
		g = gossip_next(g);
		free(lat);
	}
	char *my_addr_str = addr_to_string(my_addr);
	int my_index = nl_index(nl, my_addr_str);
	// update connections of immediate neighbours
	struct file_info *f = file_info;
	while (f) {
		char *addr = addr_to_string(f->addr);

		if(strcmp(addr,my_addr_str ) != 0 && (f->type == FI_INCOMING || (f->type == FI_OUTGOING && f->status == FI_KNOWN && f->u.fi_outgoing.status == FI_CONNECTED)))
		{
			set_dist(nl, graph, nl_nsites(nl), addr, my_addr_str, 1);
		}

		f = f->next;
		free(addr);
	}
	free(my_addr_str);
	// call graph on updated graph
	dijkstra(graph, nl_nsites(nl), my_index, dist, prev);
	
	printf("PRINTING GRAPH\n");
    for(r=0;r<nl_nsites(nl);r++)
    {
    	for(s=0;s<nl_nsites(nl);s++)
    	{
    		printf("%d ", graph[INDEX(r, s, nl_nsites(nl))]);
    	}

    	printf("\n");
    }

	printf("\nPRINTING DISTANCE\n");
	for(r=0;r<nl_nsites(nl);r++)
	{
	  printf("Distance to Site [%d] %s = %d\n", r, nl_name(nl,r), dist[r]);
	}

	printf("\nPRINTING PREV\n");
	for(r=0;r<nl_nsites(nl);r++)
	{
	  printf("Previous to Site [%d] %s = %d\n", r, nl_name(nl,r), prev[r]);
	}
}