コード例 #1
0
/* Set the distance from src to dst.
 */
void set_dist(struct node_list *nl, int graph[], int nnodes, char *src, char *dst, int dist){
	int x = nl_index(nl, src), y = nl_index(nl, dst);
	if (x < 0 || y < 0) {
		printf("this is the src: %s\n", src);
		printf("this is the dst: %s,\n", dst);
		fprintf(stderr, "set_dist: bad source or destination\n");
		return;
	}
	graph[INDEX(x, y, nnodes)] = dist;
	//graph[INDEX(y, x, nnodes)] = dist;
}
コード例 #2
0
void nl_add(struct node_list *nl, char *node){
    /* No duplicate nodes.
     */
    if (nl_index(nl, node) != -1) {
        return;
    }
	/* Create a copy of the site.
	 */
	int len = strlen(node);
	char *copy = malloc(len + 1);
	strcpy(copy, node);

	/* Add this copy to the list.
	 */
	nl->nodes= (char **) realloc(nl->nodes, sizeof(char *) * (nl->nnodes + 1));
	nl->nodes[nl->nnodes++] = copy;
	nl->unsorted = 1;
}
コード例 #3
0
ファイル: connect.c プロジェクト: Amanjot1507/OS-Work
void send_handler(struct file_info *fi, char *msg)
{
  	printf ("SEND HANDLER CALLED\n");
  	if(isalpha(*msg))
  	{
  		printf("Incorrect send command\n");
  		return;
  	}	

	int len = strlen(msg);
	char *ctr = index(msg, '/');
	*ctr++ = 0;

	char *payload = rindex(ctr, '/');
	*payload++ = 0;
	char *my_addr_str = addr_to_string(my_addr);
	if(strcmp(msg, my_addr_str) == 0)
	{
		printf("%s\n", payload);
		free(my_addr_str);
		return;
	}
	else
	{
		int ttl = atoi(ctr);

		if(ttl == 0)
		{
			//free(msg);
			free(my_addr_str);
			return;
		}
		
		int dest_index = nl_index(nl, msg);
		if (dest_index == -1) {
			//free(msg);
			free(my_addr_str);
			return;
		}
		int my_index =  nl_index(nl, my_addr_str);
		
		while (prev[dest_index] != my_index) {
			dest_index = prev[dest_index];
			//Node disconnected 
			if(dest_index == -1)
			{
				free(my_addr_str);
				return;
			}
		}
		// send to the dest_index
		char *dest_addr = nl_name(nl, dest_index);
		ttl--;
		char *str = (char *) malloc(len);
		sprintf(str, "S%s/%d/%s\n",msg, ttl, payload);
		
		struct file_info *f = file_info;
		
		while(f)
		{
		  char *f_addr_str = addr_to_string(f->addr);
		  if(strcmp(dest_addr, f_addr_str) == 0 &&
		     (f->type == FI_INCOMING || (f->type == FI_OUTGOING && f->status == FI_KNOWN
						 && f->u.fi_outgoing.status == FI_CONNECTED)))
		    break;
		    
		  
		  f = f->next;
		  //free(f_addr_str);
		  //free(myaddr);
		}
		
		if (f) {
		  printf("%s\n", addr_to_string(f->addr));
		  file_info_send(f, str, strlen(str));
		}
		else
		  printf("f not found\n");
	}
	free(my_addr_str);

}
コード例 #4
0
ファイル: connect.c プロジェクト: Amanjot1507/OS-Work
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]);
	}
}