2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2003-07-15 22:13:38 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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.
|
2003-07-15 22:13:38 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-09-04 18:18:40 +00:00
|
|
|
// read "in", convert to UC, write to "out"
|
|
|
|
// command line options:
|
2003-09-05 21:26:21 +00:00
|
|
|
// -run_slow: sleep 1 second after each character, useful for debugging
|
|
|
|
// -cpu_time: chew up some CPU cycles after each character,
|
|
|
|
// -signal: raise SIGHUP signal (for testing signal handler)
|
|
|
|
// -exit: exit with status -10 (for testing exit handler)
|
2002-09-04 18:18:40 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-03-04 11:41:43 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:29:48 +00:00
|
|
|
#include "boinc_win.h"
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cctype>
|
|
|
|
#include <ctime>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <csignal>
|
2002-10-24 17:06:42 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2002-09-18 22:31:36 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
2004-07-13 13:54:09 +00:00
|
|
|
#include "boinc_gl.h"
|
2003-09-30 21:17:20 +00:00
|
|
|
#include "graphics_api.h"
|
2002-09-18 22:31:36 +00:00
|
|
|
#endif
|
2002-09-18 21:54:36 +00:00
|
|
|
|
2004-03-24 07:39:24 +00:00
|
|
|
#include "diagnostics.h"
|
2002-09-04 22:14:20 +00:00
|
|
|
#include "util.h"
|
2002-09-04 18:18:40 +00:00
|
|
|
#include "filesys.h"
|
2002-08-05 00:29:34 +00:00
|
|
|
#include "boinc_api.h"
|
2004-04-15 00:21:29 +00:00
|
|
|
#include "mfile.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2004-06-30 18:17:21 +00:00
|
|
|
using std::string;
|
|
|
|
|
2002-09-04 18:18:40 +00:00
|
|
|
#define CHECKPOINT_FILE "upper_case_state"
|
2005-04-28 23:19:58 +00:00
|
|
|
#define INPUT_FILENAME "in"
|
|
|
|
#define OUTPUT_FILENAME "out"
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
2003-09-04 00:18:27 +00:00
|
|
|
char display_buf[10];
|
2002-09-18 21:54:36 +00:00
|
|
|
double xPos=0, yPos=0;
|
|
|
|
double xDelta=0.03, yDelta=0.07;
|
2003-09-05 21:26:21 +00:00
|
|
|
#endif
|
2002-09-18 21:54:36 +00:00
|
|
|
|
2005-04-28 23:19:58 +00:00
|
|
|
bool run_slow;
|
|
|
|
bool raise_signal;
|
|
|
|
bool random_exit;
|
|
|
|
bool cpu_time;
|
2002-09-18 21:54:36 +00:00
|
|
|
APP_INIT_DATA uc_aid;
|
2002-09-04 18:18:40 +00:00
|
|
|
|
|
|
|
int do_checkpoint(MFILE& mf, int nchars) {
|
|
|
|
int retval;
|
2003-11-08 00:56:55 +00:00
|
|
|
string resolved_name;
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2003-11-08 00:56:55 +00:00
|
|
|
FILE* f = fopen("temp", "w");
|
2002-09-04 18:18:40 +00:00
|
|
|
if (!f) return 1;
|
|
|
|
fprintf(f, "%d", nchars);
|
|
|
|
fclose(f);
|
|
|
|
|
|
|
|
fprintf(stderr, "APP: upper_case checkpointing\n");
|
|
|
|
|
|
|
|
retval = mf.flush();
|
|
|
|
if (retval) return retval;
|
2004-06-08 14:37:42 +00:00
|
|
|
boinc_resolve_filename_s(CHECKPOINT_FILE, resolved_name);
|
2003-11-08 00:56:55 +00:00
|
|
|
retval = boinc_rename("temp", resolved_name.c_str());
|
2002-09-04 18:18:40 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-04-28 23:19:58 +00:00
|
|
|
static void use_some_cpu() {
|
|
|
|
#if 0
|
|
|
|
double j = 3.14159;
|
|
|
|
int i, n = 0;
|
|
|
|
for (i=0; i<20000000; i++) {
|
|
|
|
n++;
|
|
|
|
j *= n+j-3.14159;
|
|
|
|
j /= (float)n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
boinc_sleep(5.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TEST_INT_UPLOAD 1
|
|
|
|
|
|
|
|
#ifdef TEST_INT_UPLOAD
|
|
|
|
#define UPLOAD_FILE_NAME "int_upload"
|
|
|
|
|
|
|
|
int write_upload_file() {
|
|
|
|
std::string physname;
|
|
|
|
int retval = boinc_resolve_filename_s(UPLOAD_FILE_NAME, physname);
|
|
|
|
if (retval) return retval;
|
|
|
|
FILE* f = boinc_fopen(physname.c_str(), "w");
|
|
|
|
fprintf(f, "blah blah\nfoobar\n");
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-08-14 23:02:32 +00:00
|
|
|
#ifdef _WIN32
|
2002-09-04 22:14:20 +00:00
|
|
|
|
|
|
|
extern int main(int argc, char** argv);
|
|
|
|
|
2002-08-14 23:02:32 +00:00
|
|
|
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
|
2002-10-24 17:06:42 +00:00
|
|
|
LPSTR command_line;
|
2002-09-04 18:18:40 +00:00
|
|
|
char* argv[100];
|
2002-10-24 17:06:42 +00:00
|
|
|
int argc;
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2002-10-24 17:06:42 +00:00
|
|
|
command_line = GetCommandLine();
|
2002-10-29 18:58:42 +00:00
|
|
|
argc = parse_command_line( command_line, argv );
|
2002-09-04 18:18:40 +00:00
|
|
|
return main(argc, argv);
|
|
|
|
}
|
2002-08-14 23:02:32 +00:00
|
|
|
#endif
|
2002-09-04 18:18:40 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2005-04-28 23:19:58 +00:00
|
|
|
int c, nchars = 0, retval, i;
|
|
|
|
double fsize;
|
2002-09-04 18:18:40 +00:00
|
|
|
char resolved_name[512];
|
2003-09-05 21:26:21 +00:00
|
|
|
MFILE out;
|
2002-09-04 18:18:40 +00:00
|
|
|
FILE* state, *in;
|
2002-06-20 23:35:44 +00:00
|
|
|
|
2005-04-28 23:19:58 +00:00
|
|
|
int flags =
|
2004-06-08 14:37:42 +00:00
|
|
|
BOINC_DIAG_DUMPCALLSTACKENABLED |
|
2004-03-24 07:39:24 +00:00
|
|
|
BOINC_DIAG_HEAPCHECKENABLED |
|
|
|
|
BOINC_DIAG_REDIRECTSTDERR;
|
2005-04-28 23:19:58 +00:00
|
|
|
|
2004-03-25 22:24:33 +00:00
|
|
|
boinc_init_diagnostics(flags);
|
2004-03-24 07:39:24 +00:00
|
|
|
|
2005-04-28 23:19:58 +00:00
|
|
|
retval = boinc_init();
|
|
|
|
if (retval) exit(retval);
|
2004-03-24 07:39:24 +00:00
|
|
|
|
2003-09-09 07:46:29 +00:00
|
|
|
// NOTE: if you change output here, remember to change the output that
|
|
|
|
// test_uc.py pattern-matches against.
|
|
|
|
|
2003-09-09 04:38:44 +00:00
|
|
|
for (i=0; i<argc; i++) {
|
|
|
|
fprintf(stderr, "APP: upper_case: argv[%d] is %s\n", i, argv[i]);
|
|
|
|
if (!strcmp(argv[i], "-run_slow")) run_slow = true;
|
|
|
|
if (!strcmp(argv[i], "-cpu_time")) cpu_time = 1;
|
|
|
|
if (!strcmp(argv[i], "-signal")) raise_signal = true;
|
|
|
|
if (!strcmp(argv[i], "-exit")) random_exit = true;
|
|
|
|
}
|
|
|
|
|
2003-06-20 21:23:41 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
2003-09-05 21:26:21 +00:00
|
|
|
strcpy(display_buf, "(none)\0");
|
2002-09-18 22:31:36 +00:00
|
|
|
retval = boinc_init_opengl();
|
|
|
|
if (retval) exit(retval);
|
2003-06-20 21:23:41 +00:00
|
|
|
#endif
|
2002-09-18 22:31:36 +00:00
|
|
|
|
|
|
|
boinc_get_init_data(uc_aid);
|
2002-09-04 18:18:40 +00:00
|
|
|
|
2002-09-22 23:27:14 +00:00
|
|
|
fprintf(stderr, "APP: upper_case: starting, argc %d\n", argc);
|
2003-11-10 06:49:45 +00:00
|
|
|
|
2005-04-28 23:19:58 +00:00
|
|
|
boinc_resolve_filename(INPUT_FILENAME, resolved_name, sizeof(resolved_name));
|
|
|
|
in = boinc_fopen(resolved_name, "r");
|
2002-10-29 18:58:42 +00:00
|
|
|
if (in == NULL) {
|
2003-09-04 00:18:27 +00:00
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"Couldn't find input file, resolved name %s.\n",
|
|
|
|
resolved_name
|
|
|
|
);
|
2002-10-30 04:30:28 +00:00
|
|
|
exit(-1);
|
2002-10-29 18:58:42 +00:00
|
|
|
}
|
2003-09-04 00:18:27 +00:00
|
|
|
|
2003-11-10 06:49:45 +00:00
|
|
|
file_size(resolved_name, fsize);
|
|
|
|
|
2003-09-04 00:18:27 +00:00
|
|
|
boinc_resolve_filename(CHECKPOINT_FILE, resolved_name, sizeof(resolved_name));
|
2005-04-28 23:19:58 +00:00
|
|
|
state = boinc_fopen(resolved_name, "r");
|
2002-09-04 18:18:40 +00:00
|
|
|
if (state) {
|
|
|
|
fscanf(state, "%d", &nchars);
|
|
|
|
printf("nchars %d\n", nchars);
|
|
|
|
fseek(in, nchars, SEEK_SET);
|
2005-04-28 23:19:58 +00:00
|
|
|
boinc_resolve_filename(OUTPUT_FILENAME, resolved_name, sizeof(resolved_name));
|
2002-09-04 18:18:40 +00:00
|
|
|
retval = out.open(resolved_name, "a");
|
|
|
|
} else {
|
2005-04-28 23:19:58 +00:00
|
|
|
boinc_resolve_filename(OUTPUT_FILENAME, resolved_name, sizeof(resolved_name));
|
2002-09-04 18:18:40 +00:00
|
|
|
retval = out.open(resolved_name, "w");
|
|
|
|
}
|
|
|
|
if (retval) {
|
2003-09-04 00:18:27 +00:00
|
|
|
fprintf(stderr, "APP: upper_case output open failed:\n");
|
|
|
|
fprintf(stderr, "resolved name %s, retval %d\n", resolved_name, retval);
|
|
|
|
perror("open");
|
2002-09-04 18:18:40 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
while (1) {
|
2002-09-04 18:18:40 +00:00
|
|
|
c = fgetc(in);
|
2003-09-05 21:26:21 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
if (c == EOF) break;
|
2003-09-05 21:26:21 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
2003-09-04 00:18:27 +00:00
|
|
|
sprintf(display_buf, "%c -> %c", c, toupper(c));
|
2003-09-05 21:26:21 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
c = toupper(c);
|
2002-09-04 18:18:40 +00:00
|
|
|
out._putchar(c);
|
|
|
|
nchars++;
|
|
|
|
|
2005-04-28 23:19:58 +00:00
|
|
|
#ifdef TEST_INT_UPLOAD
|
|
|
|
if (nchars == fsize/2) {
|
2005-04-30 12:05:39 +00:00
|
|
|
string tmpname=UPLOAD_FILE_NAME;
|
2005-04-28 23:19:58 +00:00
|
|
|
retval = write_upload_file();
|
|
|
|
fprintf(stderr, "write_upload_file() %d\n", retval);
|
2005-04-30 12:05:39 +00:00
|
|
|
retval = boinc_upload_file(tmpname);
|
2005-04-28 23:19:58 +00:00
|
|
|
fprintf(stderr, "boinc_upload_file() %d\n", retval);
|
|
|
|
} else if (nchars >= fsize/2) {
|
2005-04-30 12:05:39 +00:00
|
|
|
string tmpname=UPLOAD_FILE_NAME;
|
|
|
|
retval = boinc_upload_status(tmpname);
|
2005-04-28 23:19:58 +00:00
|
|
|
fprintf(stderr, "upload status %d\n", retval);
|
|
|
|
}
|
|
|
|
#endif
|
2002-09-04 18:18:40 +00:00
|
|
|
if (cpu_time) {
|
2005-04-28 23:19:58 +00:00
|
|
|
use_some_cpu();
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (run_slow) {
|
2003-03-11 22:18:01 +00:00
|
|
|
boinc_sleep(1.);
|
2002-09-04 18:18:40 +00:00
|
|
|
}
|
2003-06-11 23:09:11 +00:00
|
|
|
|
2003-06-28 00:10:47 +00:00
|
|
|
#ifdef HAVE_SIGNAL_H
|
2002-10-28 23:06:44 +00:00
|
|
|
if (raise_signal) {
|
|
|
|
raise(SIGHUP);
|
|
|
|
}
|
2002-11-01 19:32:09 +00:00
|
|
|
#endif
|
2003-06-28 00:10:47 +00:00
|
|
|
if (random_exit) {
|
2003-11-09 23:45:31 +00:00
|
|
|
if (drand() < 0.05) {
|
2003-06-28 00:10:47 +00:00
|
|
|
exit(-10);
|
|
|
|
}
|
|
|
|
}
|
2002-10-28 23:06:44 +00:00
|
|
|
|
2004-12-22 15:02:03 +00:00
|
|
|
int flag = boinc_time_to_checkpoint();
|
2003-09-05 21:26:21 +00:00
|
|
|
if (flag) {
|
2002-09-04 18:18:40 +00:00
|
|
|
retval = do_checkpoint(out, nchars);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: upper_case checkpoint failed %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-08-24 00:41:25 +00:00
|
|
|
boinc_checkpoint_completed();
|
2002-06-24 21:29:05 +00:00
|
|
|
}
|
2003-11-10 06:49:45 +00:00
|
|
|
|
|
|
|
boinc_fraction_done(nchars/fsize);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-09-04 18:18:40 +00:00
|
|
|
retval = out.flush();
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "APP: upper_case flush failed %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-28 00:10:47 +00:00
|
|
|
if (random_exit) exit(-10);
|
2002-09-04 18:18:40 +00:00
|
|
|
fprintf(stderr, "APP: upper_case ending, wrote %d chars\n", nchars);
|
2003-06-11 23:09:11 +00:00
|
|
|
|
2003-06-20 21:23:41 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
2002-10-24 17:06:42 +00:00
|
|
|
boinc_finish_opengl();
|
2003-06-20 21:23:41 +00:00
|
|
|
#endif
|
2002-08-05 00:29:34 +00:00
|
|
|
boinc_finish(0);
|
2003-06-11 23:09:11 +00:00
|
|
|
|
2002-06-20 23:35:44 +00:00
|
|
|
return 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-09-18 21:54:36 +00:00
|
|
|
|
2002-09-18 22:31:36 +00:00
|
|
|
#ifdef BOINC_APP_GRAPHICS
|
2003-06-06 22:08:46 +00:00
|
|
|
extern GLuint main_font;
|
2002-09-18 21:54:36 +00:00
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
void app_init_gl() {}
|
2003-05-14 23:51:54 +00:00
|
|
|
|
2003-09-04 00:18:27 +00:00
|
|
|
bool app_render(int xs, int ys, double time_of_day) {
|
2002-09-26 05:57:10 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
|
|
|
|
glLoadIdentity(); // Reset The Current Modelview Matrix
|
2002-09-18 22:31:36 +00:00
|
|
|
glColor3f(1,1,1);
|
2002-10-24 17:06:42 +00:00
|
|
|
|
2002-10-30 04:30:28 +00:00
|
|
|
glRasterPos2f(xPos, yPos);
|
2003-09-04 00:18:27 +00:00
|
|
|
glPrint(main_font, display_buf);
|
2002-10-24 17:06:42 +00:00
|
|
|
|
2002-09-18 22:31:36 +00:00
|
|
|
xPos += xDelta;
|
|
|
|
yPos += yDelta;
|
2002-09-22 23:27:14 +00:00
|
|
|
if (xPos < -1 || xPos > 1) xDelta *= -1;
|
|
|
|
if (yPos < -1 || yPos > 1) yDelta *= -1;
|
2003-06-11 23:09:11 +00:00
|
|
|
|
2002-10-30 04:30:28 +00:00
|
|
|
glRasterPos2f(-0.9, 0.9);
|
2002-10-31 00:13:50 +00:00
|
|
|
glPrint(main_font, "User: %s", uc_aid.user_name);
|
2002-09-18 22:31:36 +00:00
|
|
|
|
2002-10-30 04:30:28 +00:00
|
|
|
glRasterPos2f(-0.9, 0.8);
|
2002-10-31 00:13:50 +00:00
|
|
|
glPrint(main_font, "Team: %s", uc_aid.team_name);
|
2002-10-24 17:06:42 +00:00
|
|
|
|
2002-10-30 04:30:28 +00:00
|
|
|
glRasterPos2f(-0.9, 0.7);
|
2002-10-31 00:13:50 +00:00
|
|
|
glPrint(main_font, "CPU Time: %f", uc_aid.wu_cpu_time);
|
2002-09-18 22:31:36 +00:00
|
|
|
|
2002-11-12 20:59:00 +00:00
|
|
|
return true; // Everything Went OK
|
2002-09-18 21:54:36 +00:00
|
|
|
}
|
2002-09-18 22:31:36 +00:00
|
|
|
|
|
|
|
#endif
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_33ac47a071 = "$Id$";
|