Exemple #1
0
/*Sorting REVERSE-order arrays.*/
void sort_reversed(int **a, int n_piv)
{
	int i, j;

	printf("\nInitialising REVERSED arrays ...\n");
	for (i = 0; i < N_ARR; ++i) {
		for (j = 0; j < N_ELEM; ++j) {
			a[i][j] = RANGE - 1 - (long) (RANGE * j) / N_ELEM;
		}
	}

	check_arr(a);
	printf("Press 'enter' to sort REVERSED arrays:");
	if (getchar() != '\n') {
		exit(1);
	}
	sort(a, n_piv);
	check_arr(a);
}
Exemple #2
0
/*Sorting RANDOM arrays.*/
void sort_rand(int **a, int n_piv)
{
	int i, j;

	printf("\nInitialising RANDOM arrays ...\n");
	for (i = 0; i < N_ARR; ++i) {
		for (j = 0; j < N_ELEM; ++j) {
			a[i][j] = rand() % RANGE;
		}
	}

	check_arr(a);
	printf("Press 'enter' to sort RANDOM arrays:");
	if (getchar() != '\n') {
		exit(1);
	}
	sort(a, n_piv);
	check_arr(a);
}
Exemple #3
0
void sender(short int source, short int arr[]) {
  struct myudp* udp = (struct myudp*)malloc(sizeof(struct myudp));
  udp->dest = htons(source);
  udp->src = htons(destination);
  udp->seq = htons(9999);
  udp->dest1 = htons(0);
  udp->dest2 = htons(0);

  // copy the udp header info 
  memset(buffer,0,ETH_FRAME_LEN);
  memcpy(buffer,udp,sizeof(struct myudp)); 
  memcpy(buffer+sizeof(struct myudp),array2bit(arr),SIZE_BIT_ARRAY);
  int i=0;
  int check = check_arr(arr);
   
  for(i =0; i<f;i++) {
     if (sendto(s, buffer,sizeof(struct myudp)+SIZE_BIT_ARRAY, 0,(struct sockaddr*)&saddrll, sizeof(saddrll)) < 0)
           printf("Error, could not send\n");
     usleep(200);
  }
  //printf("Sent ACK array\n");
}
Exemple #4
0
void receiver() {
  // Initializations
  char *heap_mem1 = (char *)malloc(MAX_PAY_SIZE*MAX_PKT_NO);
  char *heap_mem2 = (char *)malloc(MAX_PAY_SIZE*MAX_PKT_NO);
  FILE *fp1 = fopen(output1,"w");
  FILE *fp2 = fopen(output2,"w");
  assert(heap_mem1 != NULL);
  assert(fp1 != NULL);
  assert(heap_mem2 != NULL);
  assert(fp2 != NULL);
  struct myudp* head;
  int length = 0;
 
  // start Listening
  printf("Listening\n");
  while(1){
    length = recvfrom(s, buffer, ETH_FRAME_LEN,0,(struct sockaddr *)&saddrll, &sll_len);
    f = 15;

    if (file_flag1 && file_flag2) {
      
       gettimeofday(&end, NULL);
       printf("END time = %f \n", (double)(end.tv_sec + end.tv_usec / 1000000));
       exit(1);
    }
    if (length <= 0){ 
      f = 5;
      printf("Sending if hang\n");
      sender(source1, ack_arr1);
      sender(source2, ack_arr2);
      //usleep(500);
      continue;
    }
    if(buffer == NULL) {
      continue;
    }
    head = (struct myudp *) buffer;
    if (ntohs(head->dest) == destination) {


       if(ntohs(head->seq) != 9998 && ntohs(head->seq) != 9999) {
	 u_char *payload = (u_char*)(buffer + sizeof(struct myudp));
	 if(payload == NULL) {
	   printf("Payload NULL\n");
	   exit(0);
	 }
	 if(ntohs(head->src) == source1) {
	   // printf("SEQ : %d : SCR : %d\n", ntohs(head->seq), source1);

	  memcpy(heap_mem1+(htons(head->seq))*MAX_PAY_SIZE, payload, MAX_PAY_SIZE);
	  ack_arr1[htons(head->seq)] = 0;
	  if (gg == 0) {
	    gettimeofday(&start, NULL);
	    printf("START time = %f\n", (double)(start.tv_sec + start.tv_usec / 1000000));

	    gg = 1;
	  }

  
	  rem_count = check_arr(ack_arr1);
	  if(rem_count == 0 && file_flag1 == 0) {	  
	    sender(source1, ack_arr1);
	    printf("Writing to file for %d\n", source1);
	    fwrite(heap_mem1,1,TEN_MB,fp1);
	    fclose(fp1);
	    file_flag1 = 1;
	  }
	 }

        if(ntohs(head->src) == source2) {
	  //printf("SEQ : %d : SCR : %d\n", ntohs(head->seq),source2);

	 memcpy(heap_mem2+(htons(head->seq))*MAX_PAY_SIZE, payload, MAX_PAY_SIZE);
         ack_arr2[htons(head->seq)] = 0;
        
	 rem_count = check_arr(ack_arr2);
	 if(rem_count == 0 && file_flag2 == 0) {
	   sender(source2, ack_arr2);
	   printf("Writing to file for %d\n", source2);
	   fwrite(heap_mem2,1,TEN_MB,fp2);
	   fclose(fp2);
	   file_flag2 = 1;
	   }
         }
       }
       else if(ntohs(head->seq) == 9998 && ntohs(head->seq) != 9999) {
	 if(ntohs(head->src) == source1) {
	  int cnt = check_arr(ack_arr1);
	    if(cnt  < prev1){
	      sender(source1, ack_arr1);
	      prev1 = cnt;
	    }
	 }
	 if(ntohs(head->src) == source2) {
	  int cnt = check_arr(ack_arr2);
	    if(cnt  < prev2){
	      sender(source2, ack_arr2);
	      prev2 = cnt;
	   }
	 }
       }
    }
  }
}