/* 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;

    // 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);
                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();

    InitializeThreadConstructs();

    /*Making the threads for requests...*/
    for(i = 0; i < nthreads; i++)
    {
        printf("Creating thread: %d\n", i);

        ServerSettings *settings = malloc(sizeof(ServerSettings));
        settings->Port = port;
        settings->Server = server;
        settings->NumOfRequest = nrequests;
        settings->ThreadID = _tid[i];

        int err = 0;
        err = pthread_create(&(_tid[i]), NULL, (void*)&ProcessRequest, settings);
        if (err != 0)
        {
            printf("Can't create thread :[%s]\n", strerror(err));
        }
    }

    // Wait for processing to complete
    for(i=0;i < nthreads; i++)
    {
        pthread_join(_tid[i], NULL);
        printf("Joined thread: %d\n", i);
    }

    gfc_global_cleanup();

    return 0;
}
/* 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;
}