2006-04-06 22:20:29 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
|
|
|
//
|
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
2006-12-06 00:20:01 +00:00
|
|
|
// This is the primary sample BOINC application;
|
|
|
|
// it shows most of the features of the BOINC API.
|
|
|
|
//
|
|
|
|
// read "in", convert to upper case, write to "out"
|
|
|
|
//
|
|
|
|
// command line options (use for debugging various scenarios):
|
|
|
|
// -run_slow: sleep 1 second after each character; useful for debugging
|
2006-04-30 05:38:08 +00:00
|
|
|
// -cpu_time N: use about N CPU seconds after copying files
|
2006-12-06 00:20:01 +00:00
|
|
|
// -early_exit: exit(10) after 30 chars
|
|
|
|
// -early_crash: crash after 30 chars
|
2006-04-06 22:20:29 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include "boinc_win.h"
|
|
|
|
#else
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cctype>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <csignal>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2006-04-07 20:00:43 +00:00
|
|
|
#define BOINC_APP_GRAPHICS
|
|
|
|
|
2006-04-12 22:56:26 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
|
|
|
#include "graphics_api.h"
|
2006-04-13 21:33:19 +00:00
|
|
|
#include "graphics_lib.h"
|
2006-04-12 22:56:26 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-06 22:20:29 +00:00
|
|
|
#include "diagnostics.h"
|
2007-02-21 20:04:14 +00:00
|
|
|
#include "str_util.h"
|
2006-04-06 22:20:29 +00:00
|
|
|
#include "util.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "boinc_api.h"
|
|
|
|
#include "mfile.h"
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
|
|
|
#define CHECKPOINT_FILE "upper_case_state"
|
|
|
|
#define INPUT_FILENAME "in"
|
|
|
|
#define OUTPUT_FILENAME "out"
|
|
|
|
|
2006-12-06 00:20:01 +00:00
|
|
|
bool run_slow = false;
|
|
|
|
bool early_exit = false;
|
|
|
|
bool early_crash = false;
|
2006-12-08 23:50:14 +00:00
|
|
|
bool early_sleep = false;
|
2006-12-06 00:20:01 +00:00
|
|
|
double cpu_time = 20;
|
2006-04-06 22:20:29 +00:00
|
|
|
|
2006-12-08 23:50:14 +00:00
|
|
|
|
2006-11-22 19:30:43 +00:00
|
|
|
static void use_some_cpu() {
|
|
|
|
double j = 3.14159;
|
|
|
|
int i, n = 0;
|
|
|
|
for (i=0; i<20000000; i++) {
|
|
|
|
n++;
|
|
|
|
j *= n+j-3.14159;
|
|
|
|
j /= (float)n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-06 22:20:29 +00:00
|
|
|
int do_checkpoint(MFILE& mf, int nchars) {
|
|
|
|
int retval;
|
|
|
|
string resolved_name;
|
|
|
|
|
|
|
|
FILE* f = fopen("temp", "w");
|
|
|
|
if (!f) return 1;
|
|
|
|
fprintf(f, "%d", nchars);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
fprintf(stderr, "APP: upper_case checkpointing\n");
|
|
|
|
|
|
|
|
retval = mf.flush();
|
|
|
|
if (retval) return retval;
|
|
|
|
boinc_resolve_filename_s(CHECKPOINT_FILE, resolved_name);
|
|
|
|
retval = boinc_rename("temp", resolved_name.c_str());
|
|
|
|
if (retval) return retval;
|
|
|
|
|
2006-11-22 19:30:43 +00:00
|
|
|
//use_some_cpu();
|
|
|
|
fprintf(stderr, "APP: upper_case checkpoint done\n");
|
2006-04-06 22:20:29 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void worker() {
|
2007-01-11 22:06:51 +00:00
|
|
|
int c, nchars = 0, retval, n;
|
2006-04-06 22:20:29 +00:00
|
|
|
double fsize;
|
2007-01-11 22:06:51 +00:00
|
|
|
char input_path[512], output_path[512], chkpt_path[512];
|
2006-04-06 22:20:29 +00:00
|
|
|
MFILE out;
|
2006-05-27 04:45:52 +00:00
|
|
|
FILE* state, *infile;
|
2006-04-06 22:20:29 +00:00
|
|
|
|
2007-01-11 22:06:51 +00:00
|
|
|
// open the input file (resolve logical name first)
|
|
|
|
//
|
|
|
|
boinc_resolve_filename(INPUT_FILENAME, input_path, sizeof(input_path));
|
|
|
|
infile = boinc_fopen(input_path, "r");
|
2006-05-27 04:45:52 +00:00
|
|
|
if (!infile) {
|
2007-01-11 22:06:51 +00:00
|
|
|
fprintf(stderr,
|
|
|
|
"Couldn't find input file, resolved name %s.\n", input_path
|
2006-04-06 22:20:29 +00:00
|
|
|
);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
2007-01-11 22:06:51 +00:00
|
|
|
// get size of input file (used to compute fraction done)
|
|
|
|
//
|
|
|
|
file_size(input_path, fsize);
|
2006-04-06 22:20:29 +00:00
|
|
|
|
2007-01-11 22:06:51 +00:00
|
|
|
// open output file
|
|
|
|
//
|
|
|
|
boinc_resolve_filename(OUTPUT_FILENAME, output_path, sizeof(output_path));
|
|
|
|
|
|
|
|
// See if there's a valid checkpoint file.
|
|
|
|
// If so seek input file and truncate output file
|
|
|
|
//
|
|
|
|
boinc_resolve_filename(CHECKPOINT_FILE, chkpt_path, sizeof(chkpt_path));
|
|
|
|
state = boinc_fopen(chkpt_path, "r");
|
2006-04-06 22:20:29 +00:00
|
|
|
if (state) {
|
2007-01-11 22:06:51 +00:00
|
|
|
n = fscanf(state, "%d", &nchars);
|
2006-09-26 18:12:41 +00:00
|
|
|
fclose(state);
|
2007-01-11 22:06:51 +00:00
|
|
|
}
|
|
|
|
if (state && n==1) {
|
2006-05-27 04:45:52 +00:00
|
|
|
fseek(infile, nchars, SEEK_SET);
|
2007-01-11 22:06:51 +00:00
|
|
|
boinc_truncate(output_path, nchars);
|
|
|
|
retval = out.open(output_path, "a");
|
2006-04-06 22:20:29 +00:00
|
|
|
} else {
|
2007-01-11 22:06:51 +00:00
|
|
|
retval = out.open(output_path, "w");
|
2006-04-06 22:20:29 +00:00
|
|
|
}
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: upper_case output open failed:\n");
|
2007-01-11 22:06:51 +00:00
|
|
|
fprintf(stderr, "resolved name %s, retval %d\n", output_path, retval);
|
2006-04-06 22:20:29 +00:00
|
|
|
perror("open");
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-01-11 22:06:51 +00:00
|
|
|
|
|
|
|
// main loop - read characters, convert to UC, write
|
|
|
|
//
|
2006-12-08 23:50:14 +00:00
|
|
|
for (int i=0; ; i++) {
|
2006-05-27 04:45:52 +00:00
|
|
|
c = fgetc(infile);
|
2006-04-06 22:20:29 +00:00
|
|
|
|
|
|
|
if (c == EOF) break;
|
|
|
|
c = toupper(c);
|
|
|
|
out._putchar(c);
|
|
|
|
nchars++;
|
|
|
|
if (run_slow) {
|
|
|
|
boinc_sleep(1.);
|
|
|
|
}
|
|
|
|
|
2006-12-08 23:50:14 +00:00
|
|
|
if (early_exit && i>30) {
|
|
|
|
exit(-10);
|
2006-05-22 00:49:39 +00:00
|
|
|
}
|
|
|
|
|
2006-12-08 23:50:14 +00:00
|
|
|
if (early_crash && i>30) {
|
2006-05-22 00:49:39 +00:00
|
|
|
#ifdef _WIN32
|
2006-12-08 23:50:14 +00:00
|
|
|
DebugBreak();
|
2006-12-06 00:20:01 +00:00
|
|
|
#else
|
2006-12-08 23:50:14 +00:00
|
|
|
*(int*)0 = 0;
|
2006-05-22 00:49:39 +00:00
|
|
|
#endif
|
|
|
|
}
|
2006-12-08 23:50:14 +00:00
|
|
|
if (early_sleep && i>30) {
|
|
|
|
g_sleep = true;
|
|
|
|
while (1) boinc_sleep(1);
|
|
|
|
}
|
2006-05-22 00:49:39 +00:00
|
|
|
|
2006-12-06 00:20:01 +00:00
|
|
|
if (boinc_time_to_checkpoint()) {
|
2006-04-06 22:20:29 +00:00
|
|
|
retval = do_checkpoint(out, nchars);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: upper_case checkpoint failed %d\n", retval);
|
2006-12-06 00:20:01 +00:00
|
|
|
exit(retval);
|
2006-04-06 22:20:29 +00:00
|
|
|
}
|
|
|
|
boinc_checkpoint_completed();
|
|
|
|
}
|
|
|
|
|
2007-07-17 17:36:46 +00:00
|
|
|
double f = nchars/fsize;
|
|
|
|
if (cpu_time) f /= 2;
|
|
|
|
boinc_fraction_done(f);
|
2006-04-06 22:20:29 +00:00
|
|
|
}
|
2007-01-11 22:06:51 +00:00
|
|
|
|
2006-04-06 22:20:29 +00:00
|
|
|
retval = out.flush();
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: upper_case flush failed %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-01-11 22:06:51 +00:00
|
|
|
|
|
|
|
// burn up some CPU time if needed
|
|
|
|
//
|
2006-04-06 22:20:29 +00:00
|
|
|
if (cpu_time) {
|
|
|
|
double start = dtime();
|
2006-04-07 20:00:43 +00:00
|
|
|
while (1) {
|
|
|
|
double e = dtime()-start;
|
|
|
|
if (e > cpu_time) break;
|
2007-07-17 17:36:46 +00:00
|
|
|
boinc_fraction_done(.5 + e/(cpu_time*2));
|
2006-11-22 19:30:43 +00:00
|
|
|
|
|
|
|
if (boinc_time_to_checkpoint()) {
|
|
|
|
retval = do_checkpoint(out, nchars);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: upper_case checkpoint failed %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
boinc_checkpoint_completed();
|
|
|
|
}
|
|
|
|
|
2006-04-06 22:20:29 +00:00
|
|
|
use_some_cpu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
boinc_finish(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2006-05-31 08:14:21 +00:00
|
|
|
int i;
|
|
|
|
int retval = 0;
|
2006-04-06 22:20:29 +00:00
|
|
|
|
|
|
|
for (i=0; i<argc; i++) {
|
2006-12-06 00:20:01 +00:00
|
|
|
if (!strcmp(argv[i], "-early_exit")) early_exit = true;
|
|
|
|
if (!strcmp(argv[i], "-early_crash")) early_crash = true;
|
2006-12-08 23:50:14 +00:00
|
|
|
if (!strcmp(argv[i], "-early_sleep")) early_sleep = true;
|
2006-04-06 22:20:29 +00:00
|
|
|
if (!strcmp(argv[i], "-run_slow")) run_slow = true;
|
|
|
|
if (!strcmp(argv[i], "-cpu_time")) {
|
|
|
|
cpu_time = atof(argv[++i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
2006-12-27 20:15:46 +00:00
|
|
|
//if (boinc_graphics_possible()) {
|
2006-04-13 21:33:19 +00:00
|
|
|
#if defined(_WIN32) || defined(__APPLE__)
|
2006-05-31 08:14:21 +00:00
|
|
|
retval = boinc_init_graphics(worker);
|
2006-04-13 21:33:19 +00:00
|
|
|
#else
|
2006-05-31 08:14:21 +00:00
|
|
|
retval = boinc_init_graphics_lib(worker, argv[0]);
|
2006-11-05 00:02:27 +00:00
|
|
|
#endif
|
|
|
|
if (retval) exit(retval);
|
2006-12-27 20:15:46 +00:00
|
|
|
//}
|
2006-04-13 21:33:19 +00:00
|
|
|
#endif
|
2006-11-05 00:02:27 +00:00
|
|
|
|
2006-04-06 22:20:29 +00:00
|
|
|
retval = boinc_init();
|
|
|
|
if (retval) exit(retval);
|
|
|
|
worker();
|
|
|
|
}
|
|
|
|
|
2006-06-13 23:12:36 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
|
|
|
|
LPSTR command_line;
|
|
|
|
char* argv[100];
|
|
|
|
int argc;
|
|
|
|
|
|
|
|
command_line = GetCommandLine();
|
|
|
|
argc = parse_command_line( command_line, argv );
|
|
|
|
return main(argc, argv);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-04-06 22:20:29 +00:00
|
|
|
const char *BOINC_RCSID_33ac47a071 = "$Id$";
|