diff --git a/src/server/slave.c b/src/server/slave.c --- a/src/server/slave.c +++ b/src/server/slave.c @@ -33,18 +33,35 @@ int main(int argc, char *argv[]) { - int counter; + char curopt; + char *username; - /* Parse arguments */ - for(counter = 0; counter < argc; counter ++) - if(strcmp(argv[counter], "-h") == 0) - { - fprintf(stderr, "Usage: distrenslave [option] \nStarts a distren slave\n\t-h\tshow this help\n"); - return 2; - } + while(-1 != (curopt = getopt(argc, argv, "u:h"))) + { + if(curopt == ':') + { + fprintf(stderr, "-%c: is missing an argument\n", optopt); + return 1; + } + else if(curopt == '?') + { + fprintf(stderr, "-%c: invalid option specified\n", optopt); + return 1; + } + else if(curopt == 'h') + { + fprintf(stderr, "Usage: distrenslave [option] \nStarts a distren slave\n\t-u\tset username (run after fresh install)\n\t-h\tshow this help\n"); + return 2; + } + else if(curopt == 'u') + username = strdup(optarg); + fprintf(stderr, "Putting username \"%s\" in distrenslave.conf\n", username); + conf_replace("distrenslave.conf", "!username", username); + fprintf(stderr, "Please invoke distrenslave with no arguments to run with the username you just set\n"); + return 0; + } /* Get conf data */ - char *username; char *datadir; cfg_t * my_cfg; cfg_opt_t myopts[] = { @@ -103,6 +120,7 @@ int main(int argc, char *argv[]) { tell_the_server(0); // Give me some work! + /* If we got a frame */ if(haveWork) { @@ -133,17 +151,17 @@ int main(int argc, char *argv[]) } else { fprintf(stderr, "Downloading job data from server failed. Check your network connection.\n"); - return 1; // for now + return 1; // Eventually make a retry loop } } else fprintf(stderr, "Using cached job file...\n"); - _distren_asprintf(&outdir, "/tmp/distren/job%d", jobnum); /*< @TODO: free() */ - mkdir("/tmp/distren", 0750); /* @TODO: Make this less *nix-specific */ + _distren_asprintf(&outdir, "/tmp/distren/job%d", jobnum); + mkdir("/tmp/distren", 0750); /* @TODO: Change to tmpdir once it exists */ mkdir(outdir, 0750); - _distren_asprintf(&tarcmd, "tar -xvf \"%s\" -C \"%s\"", pathtoTar, outdir); /* @TODO: Make this portable. Libtar or something? */ + _distren_asprintf(&tarcmd, "tar -xvf \"%s\" -C \"%s\"", pathtoTar, outdir); /* @TODO:Use a lib here! */ system(tarcmd); free(tarcmd); free(pathtoTar); @@ -165,34 +183,38 @@ int main(int argc, char *argv[]) } /* Variable-fillers which require XML */ - outputExt = myjob->output_format; /* @TODO: FIXME! <-- I WILL! */ + outputExt = myjob->output_format; /* Prepares the path to the jobfile */ _distren_asprintf(&pathtoOutput, "%s/job%d/output/job%d-frame%d.%s", datadir, jobnum, jobnum, framenum, outputExt ); free(outputExt); /* Execute blender */ - if(exec_blender(pathtoJobfile, pathtoOutput, framenum)){ /* @TODO: This warning should be fixed :D */ + if(exec_blender(pathtoJobfile, pathtoOutput, framenum)) + { fprintf(stderr,"Error running Blender. Check your installation and/or your PATH."); return 1; - } + } free(pathtoJobfile); - /* When blender is finished, run this... */ + /* Post-execution */ fprintf(stderr, "Finished frame %d in job %d, uploading...", framenum, jobnum); - if( !curlpost(pathtoOutput, urltoOutput)){ // Uploads output - fprintf(stderr,"Upload successful, removing old output...\n"); - remove(pathtoOutput); // Delete the file after its uploaded - } - else{ - fprintf(stderr,"Upload failed. Check your network connection. Trying again..."); - int tries=0; - while(tries<10 && curlpost(pathtoOutput, urltoOutput)){ - fprintf(stderr, "Upload failed. Trying again in 10 seconds... (attempt %d of 10)", tries); - tries++; - sleep(10); + if( !curlpost(pathtoOutput, urltoOutput)) // Uploads output + { + fprintf(stderr,"Upload successful, removing old output...\n"); + remove(pathtoOutput); // Delete the file after its uploaded } - // @TODO: Keep track of files that we were unable to upload, and upload them later - } + else + { + fprintf(stderr,"Upload failed. Check your network connection. Trying again..."); + int tries=0; + while(tries<10 && curlpost(pathtoOutput, urltoOutput)) + { + fprintf(stderr, "Upload failed. Trying again in 10 seconds... (attempt %d of 10)", tries); + tries++; + sleep(10); + } + // @TODO: Keep track of files that we were unable to upload, and upload them later + } free(urltoOutput); free(pathtoOutput); @@ -202,11 +224,12 @@ int main(int argc, char *argv[]) fprintf(stderr,"Nothing to do. Idling...\n"); // If the server says that every frame for the last jobnum is finished, OR if the data is getting old - if(1 == 0){ - // Note: individual frames are already deleted after uploading, - // except for ones that couldn't be uploaded - delete_jobdata(jobnum, datadir); - } + if(1 == 0) + { + // Note: individual frames are already deleted after uploading, + // except for ones that couldn't be uploaded + delete_jobdata(jobnum, datadir); + } sleep(5); // Poll 5 seconds. @TODO: Remove all polling }