⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flute.c

📁 这个程序实现了FLUTE协议
💻 C
📖 第 1 页 / 共 4 页
字号:
	close_alc_session(*s_id);	return -4;      }    }  }  else if(a->alc_a.cc_id == RLC) {        *ch_id = add_alc_channel(*s_id, ports[0], addrs[0], a->alc_a.intface, a->alc_a.intface_name);        if(*ch_id == -1) {            close_alc_session(*s_id);      return -4;	    }  }  if(a->rx_object) {    retval = receiver_in_object_mode(s_id, a);    fdt_th_args->fdt = NULL;  }  else {    memset(filetoken, 0, MAX_PATH);        if(strstr(a->file_path, "*") != NULL) {            if(((a->file_path[0] == '*') && (a->file_path[(strlen(a->file_path) - 1)] == '*'))) {		if(a->file_path[1] == '.') {	  printf("Only *something* or *.something valid values in wild card mode!\n");	  fflush(stdout);	  return -1;	}	memcpy(filetoken, (a->file_path + 1), (strlen(a->file_path) - 2));      }      else if(((a->file_path[0] == '*') && (a->file_path[1] == '.'))) {	memcpy(filetoken, (a->file_path + 1), (strlen(a->file_path) - 1));      }      else {	printf("Only *something* or *.something valid values in wild card mode!\n");	fflush(stdout);	return -1;      }    }        fdt_th_args->fdt = NULL;    fdt_th_args->s_id = *s_id;    fdt_th_args->rx_automatic = a->rx_automatic;    if(strlen(filetoken) != 0) {      fdt_th_args->filetoken = filetoken;    }    else {      fdt_th_args->filetoken = NULL;    }    fdt_th_args->accept_expired_fdt_inst = a->alc_a.accept_expired_fdt_inst;    fdt_th_args->verbosity = a->alc_a.verbosity;        /* Create FDT receiving thread */    #ifdef WIN32	if(_beginthread((void*)fdt_thread, 0, (void*)fdt_th_args) < 0) {          printf("Error: flute_receiver, _beginthread\n");      fflush(stdout);      return -1;                  }#else    if((pthread_create(&fdt_thread_id, NULL, fdt_thread, (void*)fdt_th_args)) != 0) {      printf("Error: flute_receiver, pthread_create\n");      fflush(stdout);      return -1;            }        #endif    if(((a->rx_automatic) || (strstr(a->file_path, "*") != NULL))) {      retval = receiver_in_fdt_based_mode(s_id, a, fdt_th_args);    }    else if(strcmp(a->file_path, "") != 0) {      retval = receiver_in_file_list_mode(s_id, a, fdt_th_args);    }    else {      retval = receiver_in_ui_mode(s_id, a, fdt_th_args);    }  }    return retval;}/* * This function is flute sender's fdt based sending function. * * Params:	int *s_id: Pointer to session identifier, *			int *ch_id: Pointer to channel identifier, *			arguments_t *a: Pointer to arguments structure where command line arguments are parsed. * * * Return:	int: Different values (0, -1, -2) depending on how function ends. * */int sender_in_fdt_based_mode(int *s_id, int *ch_id, arguments_t *a) {  /* NEW STDOUT MODE */  sender_output_th_args_t sender_output_th_args;#ifndef WIN32  pthread_t sender_output_thread_id;#endif    int retval = 0;  int retcode = 0;    alc_session_t *s;    time_t systime;  bool is_printed = false;  #ifdef WIN32  ULONGLONG curr_time;#else  unsigned long long curr_time;#endif  struct stat fdt_file_stats;  FILE *fp;  fdt_t *fdt;  char *buf = NULL;#ifdef WIN32  ULONGLONG fdt_length = 0;  LONGLONG nbytes = 0;#else  unsigned long long fdt_length = 0;  long long nbytes = 0;#endif  if(stat(a->fdt_file, &fdt_file_stats) == -1) {    printf("Error: %s is not valid file name\n", a->fdt_file);    fflush(stdout);    return -1;  }  fdt_length = fdt_file_stats.st_size;  /* Allocate memory for buf, to read fdt file to it */  if(!(buf = (char*)calloc((unsigned int)(fdt_length + 1), sizeof(char)))) {    printf("Could not alloc memory for fdt buffer!\n");    return -1;  }  if((fp = fopen(a->fdt_file, "rb")) == NULL) {    printf("Error: unable to open FDT file %s\n", a->fdt_file);    fflush(stdout);    free(buf);    return -1;  }  nbytes = fread(buf, 1, (unsigned int)fdt_length, fp);  if(nbytes <= 0) {    printf("fread error\n");    fflush(stdout);    fclose(fp);    free(buf);    return -1;  }  fdt = decode_fdt_payload(buf);  free(buf);  fclose(fp);  if(fdt == NULL) {    return -1;  }  time(&systime);  curr_time = systime + 2208988800U;  if(fdt->expires < curr_time) {    set_session_state(*s_id, SExiting);    FreeFDT(fdt);    return -2;  }  s = get_alc_session(*s_id);  if(((a->alc_a.cc_id == RLC) || ((a->alc_a.cc_id == Null) && (a->alc_a.nb_channel != 1)))) {        /**** Start tx_thread ****/    #ifdef WIN32	if(_beginthread((void*)tx_thread, 0, (void*)s) < 0) {      perror("open_alc_session: _beginthread");      FreeFDT(fdt);      return -1;    }	#else    if (pthread_create(&s->tx_thread_id, NULL, tx_thread, (void*)s) != 0) {      perror("open_alc_session: pthread_create");      FreeFDT(fdt);      return -1;    }#endif      }    if(a->alc_a.start_time != 0) {    while(1) {            time(&systime);      curr_time = systime + 2208988800U;            if(a->alc_a.start_time > curr_time) {		if(!is_printed) {	  printf("Waiting for session start time...\n");	  fflush(stdout);	  is_printed = true;	}	#ifdef WIN32	Sleep(1000);#else	sleep(1);#endif      }      else {	break;      }            if(s->state == SExiting) {	FreeFDT(fdt);	return -2;      }    }  }  /* NEW STDOUT MODE */  if(a->alc_a.verbosity < 0) {    sender_output_th_args.s_id = *s_id;    sender_output_th_args.fdt = fdt;#ifdef WIN32	if(_beginthread((void*)sender_output_thread, 0, (void*)&sender_output_th_args) < 0) {      printf("Error: fdtbasedsend, _beginhread\n");      fflush(stdout);      return -1;    }#else    if((pthread_create(&sender_output_thread_id, NULL, sender_output_thread, (void*)&sender_output_th_args)) != 0) {      printf("Error: fdtbasedsend, pthread_create\n");      fflush(stdout);      return -1;    }#endif  }  while(a->alc_a.nb_tx) {        if(a->alc_a.cc_id == Null) {            if(a->alc_a.nb_channel == 1) {		  		  retcode = fdtbasedsend(fdt, s_id, ch_id, a);      }      else {	retcode = fdtbasedsend(fdt, s_id, NULL, a);      }    }    else if(a->alc_a.cc_id == RLC) {      retcode = fdtbasedsend(fdt, s_id, NULL, a);    }        if(retcode == -1) {      printf("\nError: fdtbasedsend() failed\n");      retval = -1;      break;    }    else if(retcode == -2) {      retval = -2;      break;    }    if(!a->cont) {      a->alc_a.nb_tx--;    }        if(((a->alc_a.cc_id == RLC) || ((a->alc_a.cc_id == Null) && (a->alc_a.nb_channel != 1)))) {            while(s->tx_queue_begin != NULL) {		if(s->state == SExiting) {#ifdef USE_ZLIB	  if(a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {	    remove_gz_files(a->fdt_file, a->alc_a.base_dir, a->file_path);	  }#endif	  FreeFDT(fdt);	  return -2;	}#ifdef WIN32	Sleep(1);#else	usleep(1000);#endif			      }    }  } #ifdef USE_ZLIB  if(a->alc_a.encode_content == ZLIB_FDT_AND_GZIP_FILES) {    remove_gz_files(a->fdt_file, a->alc_a.base_dir, a->file_path);  }#endif    set_session_state(*s_id, SExiting);  if(a->alc_a.verbosity < 0) {#ifdef WIN32    Sleep(1000);#else    sleep(1);#endif  }  FreeFDT(fdt);  return retval;} /* * This function is flute receiver's fdt based receiving function. * * Params:	int *s_id: Pointer to session identifier, *			arguments_t *a: Pointer to arguments structure where command line arguments are parsed, *			fdt_th_args_t *fdt_th_args: Pointer to structure conmtaining FDT and some other information. * * Return:	int: Different values (0, -1, -2, -3) depending on how function ends. * */int receiver_in_fdt_based_mode(int *s_id, arguments_t *a, fdt_th_args_t *fdt_th_args) {  int retval = 0;  int retcode = 0;  char *cont_desc = NULL;#ifndef WIN32  pthread_t receiver_output_thread_id;#endif  if(strcmp(a->sdp_file, "") != 0) {    cont_desc = sdp_attr_get(a->sdp, "content-desc");  }  if(a->rx_automatic) {    printf("FLUTE Receiver in automatic mode\n");  }  else {    printf("FLUTE Receiver in wild card mode\n");  }    if(cont_desc != NULL) {    printf("Session content information available at:\n");    printf("%s\n", cont_desc);  }    fflush(stdout);    if(a->alc_a.verbosity < 0) {#ifdef WIN32	if(_beginthread((void*)receiver_output_thread, 0, (void*)fdt_th_args) < 0) {           printf("Error: flute_receiver, _beginthread\n");      fflush(stdout);      return -1;                  }#else    if((pthread_create(&receiver_output_thread_id, NULL, receiver_output_thread, (void*)fdt_th_args)) != 0) {      printf("Error: flute_receiver, pthread_create\n");      fflush(stdout);      return -1;    }#endif  }      while(fdt_th_args->fdt == NULL) {        if(get_session_state(*s_id) == SExiting) {      return -2;    }    else if(get_session_state(*s_id) == STxStopped) {      return -3;    }    #ifdef WIN32    Sleep(1);#else    usleep(1000);#endif    continue;  }    retcode = fdtbasedrecv(fdt_th_args->fdt, *s_id, a->alc_a.rx_memory_mode, a->alc_a.accept_expired_fdt_inst,			 fdt_th_args->filetoken,#ifdef WIN32			 a->open_file,#else 			 0,#endif			 a->alc_a.verbosity);    if(retcode == -1) {    printf("Error: fdtbasedrecv() failed\n");    fflush(stdout);    retval = -1;  }  else if(retcode == -2) {    retval = -2;  }  else if(retcode == -3) {    retval = -3;  }    return retval;}/* * This function is flute receiver's fdt based ui mode receiving function. * * Params:	int *s_id: Pointer to session identifier, *			arguments_t *a: Pointer to arguments structure where command line arguments are parsed, *			fdt_th_args_t *fdt_th_args: Pointer to structure conmtaining FDT and some other information. * * Return:	int: Different values (0, -1, -2, -3) depending on how function ends. * */int receiver_in_ui_mode(int *s_id, arguments_t *a, fdt_th_args_t *fdt_th_args) {		file_t *file;	char input[100];#ifdef WIN32	ULONGLONG rec_toi;#else	unsigned long long rec_toi;#endif	char command;	bool valid_toi = false;	bool expired_toi = false;	char *filepath = NULL;	uri_t *uri = NULL;	int retval = 0;	int retcode = 0;	int content_enc_algo = -1;	time_t systime;#ifdef WIN32	ULONGLONG curr_time;#else	unsigned long long curr_time;#endif	char *cont_desc = NULL;	if(strcmp(a->sdp_file, "") != 0) {	  cont_desc = sdp_attr_get(a->sdp, "content-desc");	}	printf("FLUTE Receiver in UI-mode\n");	if(cont_desc != NULL) {		printf("Session content information available at:\n");		printf("%s\n", cont_desc);	}	fflush(stdout);	while(fdt_th_args->fdt == NULL) {		if(get_session_state(*s_id) == SExiting) {			return -2;		}		else if(get_session_state(*s_id) == STxStopped) {			return -3;		}#ifdef WIN32		Sleep(1);#else		usleep(1000);#endif		continue;	}	while(1) {		if(get_session_state(*s_id) == SExiting) {						return -2;		}		else if(get_session_state(*s_id) == STxStopped) {			return -3;		}			printf(">> Command: (D)ownload file (Q)uit\n");		printf("-> ");		if(fgets(input, 100, stdin) != NULL) {			if(sscanf(input, "%c", &command) != EOF) {				command = tolower(command);											if(command == 'd') {					printf("Files to download:\n");					fflush(stdout);										file = fdt_th_args->fdt->file_list;					while(file != NULL) {#ifdef WIN32						printf("\t%I64u) %s\n", file->toi, file->location);#else						printf("\t%llu) %s\n", file->toi, file->location);#endif						fflush(stdout);						file = file->next;					}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -