Ejemplo n.º 1
0
void ProcessRequest(ServerSettings *settings)
{
    int i;
    for(i=0;i < settings->NumOfRequest; i++)
    {
        char *req_path = workload_get_path();

        if(strlen(req_path) > 256)
        {
          fprintf(stderr, "Request path exceeded maximum of 256 characters\n");
          exit(EXIT_FAILURE);
        }

        if(req_path == NULL)
        {
            fprintf(stderr, "Request path cannot be NULL or empty\n");
            exit(EXIT_FAILURE);
        }

        printf("Processing request\n");

        FILE *file;
        char local_path[512];
        int returncode;

        localPath(req_path, local_path);

        pthread_mutex_lock(&_fileLock);

        file = openFile(local_path);

        gfcrequest_t *gfr;
        gfr = gfc_create();
        gfc_set_server(gfr, settings->Server);
        gfc_set_path(gfr, req_path);
        gfc_set_port(gfr, settings->Port);
        gfc_set_writefunc(gfr, writecb);
        gfc_set_writearg(gfr, file);

        fprintf(stdout, "Requesting %s%s\n", settings->Server, req_path);

        if ( 0 > (returncode = gfc_perform(gfr)))
        {
            fprintf(stdout, "gfc_perform returned an error %d\n", returncode);
            fclose(file);
            if ( 0 > unlink(local_path))
            {
                fprintf(stderr, "unlink failed on %s\n", local_path);
            }
        }
        else
        {
            fclose(file);
        }

        pthread_mutex_unlock(&_fileLock);

        if ( gfc_get_status(gfr) != GF_OK)
        {
            if ( 0 > unlink(local_path))
            {
                fprintf(stderr, "unlink failed on %s\n", local_path);
            }
        }

        fprintf(stdout, "Status: %s\n", gfc_strstatus(gfc_get_status(gfr)));
        fprintf(stdout, "Received %zu of %zu bytes\n", gfc_get_bytesreceived(gfr), gfc_get_filelen(gfr));
    }

    pthread_exit(NULL);
    free(settings);
}
/* Main ========================================================= */
int main(int argc, char **argv) {
/* COMMAND LINE OPTIONS ============================================= */
  char *server = "localhost";
  unsigned short port = 8888;
  char *workload_path = "workload.txt";

  int i;
  int option_char = 0;
  int nrequests = 1;
  int nthreads = 1;
  int returncode;
  gfcrequest_t *gfr;
  FILE *file;
  char *req_path;
  char local_path[512];

  // Parse and set command line arguments
  while ((option_char = getopt_long(argc, argv, "s:p:w:n:t:h", gLongOptions, NULL)) != -1) {
    switch (option_char) {
      case 's': // server
        server = optarg;
        break;
      case 'p': // port
        port = atoi(optarg);
        break;
      case 'w': // workload-path
        workload_path = optarg;
        break;
      case 'n': // nrequests
        nrequests = atoi(optarg);
        break;
      case 't': // nthreads
        nthreads = atoi(optarg);
        if(nthreads != 1){
          fprintf(stderr, "Multiple threads not yet supported.\n");
          exit(0);
        }
        break;
      case 'h': // help
        Usage();
        exit(0);
        break;                      
      default:
        Usage();
        exit(1);
    }
  }

  if( EXIT_SUCCESS != workload_init(workload_path)){
    fprintf(stderr, "Unable to load workload file %s.\n", workload_path);
    exit(EXIT_FAILURE);
  }

  gfc_global_init();

  /*Making the requests...*/
  for(i = 0; i < nrequests * nthreads; i++){
    req_path = workload_get_path();

    if(strlen(req_path) > 256){
      fprintf(stderr, "Request path exceeded maximum of 256 characters\n.");
      exit(EXIT_FAILURE);
    }

    localPath(req_path, local_path);

    file = openFile(local_path);

    gfr = gfc_create();
    gfc_set_server(gfr, server);
    gfc_set_path(gfr, req_path);
    gfc_set_port(gfr, port);
    gfc_set_writefunc(gfr, writecb);
    gfc_set_writearg(gfr, file);

    fprintf(stdout, "Requesting %s%s\n", server, req_path);

    if ( 0 > (returncode = gfc_perform(gfr))){
      fprintf(stdout, "gfc_perform returned an error %d\n", returncode);
      fclose(file);
      if ( 0 > unlink(local_path))
        fprintf(stderr, "unlink failed on %s\n", local_path);
    }
    else {
        fclose(file);
    }

    if ( gfc_get_status(gfr) != GF_OK){
      if ( 0 > unlink(local_path))
        fprintf(stderr, "unlink failed on %s\n", local_path);
    }

    fprintf(stdout, "Status: %s\n", gfc_strstatus(gfc_get_status(gfr)));
    fprintf(stdout, "Received %zu of %zu bytes\n", gfc_get_bytesreceived(gfr), gfc_get_filelen(gfr));
	  

  }
  
	
	
  gfc_global_cleanup();

  return 0;
}