示例#1
0
文件: peerman.c 项目: aido/picocoin
static bool peerman_read_rec(struct peer_manager *peers,
			     const struct p2p_message *msg)
{
	if (!strncmp(msg->hdr.command, "magic.peers",
		     sizeof(msg->hdr.command)))
		return true;

	if (strncmp(msg->hdr.command, "peer", sizeof(msg->hdr.command)))
		return false;

	struct const_buffer buf = { msg->data, msg->hdr.data_len };
	struct peer *peer;

	peer = calloc(1, sizeof(*peer));
	peer_init(peer);

	if (deser_peer(CADDR_TIME_VERSION, peer, &buf) &&
	    !peerman_has_addr(peers, peer->addr.ip))
		__peerman_add(peers, peer, false);
	else {
		peer_free(peer);
		free(peer);
	}

	return true;
}
示例#2
0
文件: peerman.c 项目: aido/picocoin
void peerman_add_addr(struct peer_manager *peers,
		 const struct bp_address *addr_in, bool known_working)
{
	if (peerman_has_addr(peers, addr_in->ip))
		return;

	struct peer *peer;
	peer = malloc(sizeof(*peer));
	if (!peer)
		return;

	peer_init(peer);
	bp_addr_copy(&peer->addr, addr_in);

	__peerman_add(peers, peer, !known_working);
}
示例#3
0
文件: peer.c 项目: R7R8/simgrid
/** Peer function  */
int peer(int argc, char *argv[])
{
  float start_time, end_time;
  peer_t p = xbt_new(s_peer_t, 1);
  msg_error_t status;

  XBT_DEBUG("peer");

  peer_init(p, argc, argv);
  start_time = MSG_get_clock();
  status = peer_wait_for_message(p);
  peer_shutdown(p);
  end_time = MSG_get_clock();
  peer_print_stats(p, end_time - start_time);
  peer_delete(p);

  return status;
}
示例#4
0
struct peer* peer_new(void)
{
	pthread_mutex_lock(&m_lock);
	if(!m_peers) m_peers=calloc(1, m_peersize*sizeof(struct peer));
	if(m_peercount>=m_peersize)
	{
		m_peersize*=2;
		struct peer* newp=(struct peer*)realloc(m_peers, m_peersize*sizeof(struct peer));
		if(!newp)
		{
			free(newp);
			pthread_mutex_unlock(&m_lock);
			return NULL;
		}
		else m_peers=newp;
	}
	struct peer* ret = &m_peers[m_peercount++];
	peer_init(ret);
	pthread_mutex_unlock(&m_lock);
	return ret;
}
示例#5
0
文件: communicate.c 项目: Oximore/P2P
/*
int main()
{
  return 0;
}
*/
int communicate(struct donnees* donnees)
{
  printf("communicate\n");
  struct client* client=donnees->client;
  ulong ip = client->sockaddr->sin_addr.s_addr;
  struct peer* peer = find_peer(donnees->peer_list, ip);
  struct file_list* file_list = donnees->file_list;
  struct peer_list* peer_list = donnees->peer_list;
  int refresh_time=get_refresh_time();
  printf("refresh time:%d\n",refresh_time);
  char* recv_buffer= malloc(sizeof(char)*RECV_BUF_SIZE);
  char* send_buffer= malloc(sizeof(char)*SEND_BUF_SIZE);  
  char* s1 = malloc(RECV_BUF_SIZE*sizeof(char));
  char* s2 = malloc(RECV_BUF_SIZE*sizeof(char));
  char* s3 = malloc(RECV_BUF_SIZE*sizeof(char));
  char* s4 = malloc(RECV_BUF_SIZE*sizeof(char));
  char* s5 = malloc(RECV_BUF_SIZE*sizeof(char));
  char* s6 = malloc(RECV_BUF_SIZE*sizeof(char));
  char* s0 = malloc(RECV_BUF_SIZE*sizeof(char));
  char* saux = malloc(RECV_BUF_SIZE*sizeof(char));
  char** tab = malloc(7*sizeof(char *));
  tab[0]=s0;
  tab[1]=s1;
  tab[2]=s2;
  tab[3]=s3;
  tab[4]=s4;
  tab[5]=s5;
  tab[6]=s6;
  int port;
  int read;
  int decalage;
  int length;
  int piece_size;
  struct timeval *t = malloc(sizeof(struct timeval));
  struct timeval *t2 = malloc(sizeof(struct timeval));
  struct timezone *tz = malloc(sizeof(struct timezone));
  struct timezone *tz2 = malloc(sizeof(struct timezone));
  gettimeofday(t,tz);
  while(1)
    { 
      recv_buffer[0]='\0';
      send_buffer[0]='\0';
      gettimeofday(t2,tz2);
      if(( (int)(t2->tv_sec - t->tv_sec))>3*refresh_time)
	{
	  if(peer!=NULL)
	    {
	      peer->time--;
	      if(!peer->time)
		{
		  peer_list_peer_delete(peer_list,file_list,peer->ip_address);
		}
	    }
	  //free  
	  free(s0);
	  free(s1);
	  free(s2);
	  free(s3);
	  free(s4);
	  free(s5);
	  free(s6);
	  free(saux);
	  free(recv_buffer);
	  free(send_buffer);
	  free(tab);
	  free(t);
	  free(t2);
	  free(tz);
	  free(tz2);
	  end(client,donnees->ct);
	  return 0;
	}
      read=read_client(client->sock, recv_buffer);
      if(read > 0)
	{
	  gettimeofday(t,tz);
	  if(peer!=NULL)
	    peer->time ++;
	  //print_data(file_list, peer_list);	  
	  printf("received:\n");
	  switch(recv_buffer[0])
	    {
	     pthread_mutex_lock(& mutex);
	    case'a':
	      decalage=0;
	      while(compte_crochet_fermant(recv_buffer)<2)
		{
		  decalage+=read;
		  read=read_client(client->sock, recv_buffer + decalage);
		}	  
	      recv_buffer[decalage+read]='\0';
	      printf("%s\n", recv_buffer);
	      parse(recv_buffer, tab);
	      port = atoi(s2);
	      if(strcmp(s0,"announce")==0 && strcmp(s1,"listen")==0 && strcmp(s3,"seed")==0 && strcmp(s5,"leech")==0)
		{
		  
		  if(peer == NULL)
		    {
		      peer = peer_init(ip, port);
		      peer_list_add(peer_list, peer);
		      peer->time=1;
		    }
		  int i=0;
		  while(strlen(s4+i) > 0)
		    {
		      if(compte_espace(s4+i)>3)
			{		 
			  
			  sscanf(s4+i, "%s %d %d %s", s1, &length, &piece_size, s2);
			  sprintf(saux, "%s %d %d %s", s1, length, piece_size, s2);
			  struct file* file = find_file(file_list, s1);
			  if(NULL == file)
			    {
			      file = remplit_file(s1, length, piece_size, s2);
			      file_list_add(file_list, file);
			    }
			  add_link(file, peer); 
			  i+=strlen(saux)+1;
			  
			}
		      else
			{
			  
			  sscanf(s4+i, "%s %d %d %s", s1, &length, &piece_size, s2); 
			  struct file* file = find_file(file_list, s1);
			  if(NULL == file)
			    {
			      file = remplit_file(s1, length, piece_size, s2);
			      file_list_add(file_list, file);
			    }
			  add_link(file, peer); 
			  s4[i]='\0';			
			}
		    }
		  struct file_list* f_add = keys_string_to_file_list(s6);
		  
		  update_add(file_list, peer, f_add);
		  file_list_delete(f_add);
		  write_client(client->sock, "ok");
		  printf("replied:ok\n");
		  
		}
	      break;
	      
	    case'u':
	      decalage=0;
	      while(compte_crochet_fermant(recv_buffer)<2)
		{
		  decalage+=read;
		  read=read_client(client->sock, recv_buffer + decalage);
		}	  
	      recv_buffer[decalage+read]='\0'; 
	      printf("%s\n", recv_buffer);	      
	      parse(recv_buffer, tab);
	      if(strcmp(s0,"update")==0 && strcmp(s1,"seed")==0 && strcmp(s3,"leech")==0)
		{
		  if(peer==NULL){
		    //free  
		    free(s0);
		    free(s1);
		    free(s2);
		    free(s3);
		    free(s4);
		    free(s5);
		    free(s6);
		    free(saux);
		    free(recv_buffer);
		    free(send_buffer);
		    free(tab);
		    free(t);
		    free(t2);
		    free(tz);
		    free(tz2);
		    end(client, donnees->ct);
		    
		    return 0;}//ferme la socket
		  else
		    { 
		    
		      char* res=fusion_keys_string(s2, s4);
		      struct file_list* f = keys_string_to_file_list(res);
		      update_diff(f, peer->file_list, file_list, peer);
		      file_list_delete(f);
		      free(res);
		      write_client(client->sock, "ok");
		      printf("replied:ok\n");
		  
		    }	    
		}
	      break;
	      
	    case'l':
	      decalage=0;
	      while(compte_crochet_fermant(recv_buffer)<1)
		{
		  decalage+=read;
		  read=read_client(client->sock, recv_buffer + decalage);
		}	  
	      recv_buffer[decalage+read]='\0'; 
	      printf("%s\n", recv_buffer);	      
	      parse(recv_buffer, tab);
	      if(strcmp(s0,"look")==0)
		{
		  char* filename="filename";
		  int egal=1;
		  int i;
		  s2[0]='\n';
		  for(i=0;i<8;i++)
		    {
		      if(s1[i]!=filename[i]) egal --;
		    }
		  if(egal==1)
		    {
		      for(i=10;i<((int) strlen(s1))-1;i++)
			{
			  s2[i-10]=s1[i];
			}
		      s2[strlen(s1)-11]='\0';
		      
		      printf("recherche:\nfilename=%s\n",s2);
		      struct file* file=find_file_name(file_list, s2);
		      if(file==NULL) write_client(client->sock, "list []");
		      else
			{
			  sprintf(send_buffer, "list [%s %d %d %s]", s2, file->length, file->p_size, file->key);
			  write_client(client->sock, send_buffer);
			  printf("replied:%s\n", send_buffer);
			}
		    }
		}
	      break;

	    case'g':
	      recv_buffer[read]='\0'; 
	      printf("%s\n", recv_buffer);	      
	      parse(recv_buffer, tab);
	      if(strcmp(s0,"getfile")==0)
		{
		  struct file* f=find_file(file_list, s1);
		  if(f==NULL||f->peer_list->first==NULL)
		    {
		      sprintf(send_buffer, "peers %s []", s1);
		      write_client(client->sock, send_buffer);
		    }
		  else
		    {
		      struct elt_peer* aux=f->peer_list->first;
		      struct in_addr* in_addr1=malloc(sizeof(struct in_addr));
		      in_addr1->s_addr=aux->peer->ip_address;
		      char* d= inet_ntoa(*in_addr1);
		      sprintf(send_buffer, "peers %s [%s:%d", s1, d, aux->peer->port);
		      aux=aux->next;
		      while(aux!=NULL)
			{
			  struct in_addr* in_addr=malloc(sizeof(struct in_addr));
			  in_addr->s_addr=aux->peer->ip_address;
			  char* c= inet_ntoa(*in_addr);
			  sprintf(send_buffer+strlen(send_buffer), " %s:%d", c, aux->peer->port);
			  aux=aux->next;
			}
		      sprintf(send_buffer+strlen(send_buffer), "]");
		      write_client(client->sock, send_buffer);
		      printf("replied:%s\n", send_buffer);
		    }
		}
	      break;

	    default:
	      printf("entree non valide");
	      //free  
	      free(s0);
	      free(s1);
	      free(s2);
	      free(s3);
	      free(s4);
	      free(s5);
	      free(s6);
	      free(saux);
	      free(recv_buffer);
	      free(send_buffer);
	      free(tab);
	      free(t);
	      free(t2);
	      free(tz);
	      free(tz2);
	      end(client,donnees->ct);
	      return 0;
	      
	     
	    }
	  pthread_mutex_unlock( &mutex);
	}
      
    }
  return EXIT_SUCCESS;
}