2002-04-30 22:22:54 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
// read "in", convert to UC, write to "out"
|
|
|
|
//
|
|
|
|
// This version does one char/second,
|
|
|
|
// and writes its state to disk every so often
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "api.h"
|
|
|
|
|
|
|
|
#define CHECKPOINT_FILE "uc_slow_state"
|
|
|
|
|
2002-07-01 19:35:19 +00:00
|
|
|
int do_checkpoint(MFILE& mf, int nchars) {
|
2002-04-30 22:22:54 +00:00
|
|
|
int retval;
|
2002-07-01 19:35:19 +00:00
|
|
|
char resolved_name[512],res_name2[512];
|
|
|
|
|
|
|
|
boinc_resolve_link( "temp", resolved_name );
|
|
|
|
FILE* f = fopen(resolved_name, "w");
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!f) return 1;
|
|
|
|
fprintf(f, "%d", nchars);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
fprintf(stderr, "APP: uc_slow checkpointing\n");
|
|
|
|
|
|
|
|
// hopefully atomic part starts here
|
|
|
|
retval = mf.flush();
|
|
|
|
if (retval) return retval;
|
2002-07-01 19:35:19 +00:00
|
|
|
boinc_resolve_link( CHECKPOINT_FILE, res_name2 );
|
|
|
|
retval = rename(resolved_name, res_name2);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
// hopefully atomic part ends here
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2002-07-10 22:32:36 +00:00
|
|
|
int c, nchars = 0, retval, n;
|
2002-07-01 19:35:19 +00:00
|
|
|
char resolved_name[512];
|
2002-04-30 22:22:54 +00:00
|
|
|
MFILE out;
|
|
|
|
FILE* state, *in;
|
2002-07-01 19:35:19 +00:00
|
|
|
APP_IN ai;
|
|
|
|
APP_OUT ao;
|
|
|
|
|
|
|
|
boinc_init( ai );
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-07-01 19:35:19 +00:00
|
|
|
boinc_resolve_link( "in", resolved_name );
|
|
|
|
in = fopen(resolved_name, "r");
|
|
|
|
boinc_resolve_link( CHECKPOINT_FILE, resolved_name );
|
|
|
|
state = fopen(resolved_name, "r");
|
2002-04-30 22:22:54 +00:00
|
|
|
if (state) {
|
|
|
|
fscanf(state, "%d", &nchars);
|
|
|
|
printf("nchars %d\n", nchars);
|
|
|
|
fseek(in, nchars, SEEK_SET);
|
2002-07-01 19:35:19 +00:00
|
|
|
boinc_resolve_link( "out", resolved_name );
|
|
|
|
retval = out.open(resolved_name, "a");
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
2002-07-01 19:35:19 +00:00
|
|
|
boinc_resolve_link( "out", resolved_name );
|
|
|
|
retval = out.open(resolved_name, "w");
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
fprintf(stderr, "APP: uc_slow starting\n");
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: uc_slow output open failed %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
c = fgetc(in);
|
|
|
|
if (c == EOF) break;
|
|
|
|
c = toupper(c);
|
|
|
|
out._putchar(c);
|
|
|
|
nchars++;
|
2002-07-10 22:32:36 +00:00
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while( n<100000 )
|
|
|
|
n++;
|
|
|
|
|
|
|
|
if( nchars % 2 == 0 )
|
|
|
|
sleep(1);
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-07-01 19:35:19 +00:00
|
|
|
if (time_to_checkpoint()) {
|
|
|
|
retval = do_checkpoint(out, nchars);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: uc_slow checkpoint failed %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-07-01 19:35:19 +00:00
|
|
|
ao.percent_done = 1;
|
|
|
|
checkpoint_completed(ao);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
retval = out.flush();
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: uc_slow flush failed %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "APP: uc_slow ending, wrote %d chars\n", nchars);
|
|
|
|
}
|