Exemplo n.º 1
0
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);

    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;

    return 0;
}
int result_spike(SPIKE_INFO &si) {

  int retval=0;

  if (signal_count >= swi.analysis_cfg.max_signals) {
    SETIERROR(RESULT_OVERFLOW,"in result_spike");
  }

  retval = outfile.printf("%s", si.s.print_xml(0,0,1).c_str());

  if (retval < 0) {
    SETIERROR(WRITE_FAILED,"in result_spike");
  } else {
    signal_count++;
    spike_count++;
  }

  return 0;
}
Exemplo n.º 3
0
/* Save the computation state into checkpoint file */
int do_checkpoint(MFILE& mf, int n, cl_float *input, int matrixSize) {
    int retval;
    string resolved_name;

    FILE* f = fopen("temp", "w");
    if (!f) return 1;
    fprintf(f, "%d", n); //write inversion number
    fprintf(f, " ");
    fprintf(f, "%d", matrixSize); //write matrixSize
    fprintf(f, " ");
    for (int i=0;i<matrixSize*matrixSize;++i) {
        fprintf(f, " ");
        fprintf(f, "%f", input[i]);
    }
    fclose(f);
    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;
    return 0; //return 0 to indicate success.
}
Exemplo n.º 4
0
int main(int argc, char * argv[]) {
    int i, retval, lastInversion=0, checkpointExists=0, matrixSize=0;
    double fd;
    char input_path[512], output_path[512], chkpt_path[512], buf[256];
    MFILE out;
    FILE* state, *infile;

    generate_random_input_file(MATRIX_SIZE); //call this if you don't want to
                                             //construct the input file manually
    
	for (i=0; i<argc; i++) {
        if (!strcmp(argv[i], "-early_exit")) early_exit = true;
        if (!strcmp(argv[i], "-early_crash")) early_crash = true;
        if (!strcmp(argv[i], "-early_sleep")) early_sleep = true;
        if (!strcmp(argv[i], "-run_slow")) run_slow = true;
        if (!strcmp(argv[i], "-cpu_time")) {
            cpu_time = atof(argv[++i]);
        }
    }
	
    retval = boinc_init();
    if (retval) {
        fprintf(stderr,
            "%s boinc_init returned %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), retval
        );
        exit(retval);
    }
    
    // open the input file (resolve logical name first)
    //
    boinc_resolve_filename(INPUT_FILENAME, input_path, sizeof(input_path));
    infile = boinc_fopen(input_path, "r");
    if (!infile) {
        fprintf(stderr,
            "%s Couldn't find input file in boinc\\win_build, resolved name %s.\n",
            boinc_msg_prefix(buf, sizeof(buf)), input_path
        );
        getchar();
        exit(-1);
    }
    
    boinc_resolve_filename(OUTPUT_FILENAME, output_path, sizeof(output_path));
    
    // See if there's a valid checkpoint file.
    // If so retrieve the current matrix and inversion number
    //
    boinc_resolve_filename(CHECKPOINT_FILE, chkpt_path, sizeof(chkpt_path));
    state = boinc_fopen(chkpt_path, "r");
    if (state) {
        printf("Checkpoint file is detected. Read from checkpoint file ... \n");
        checkpointExists=fscanf(state, "%d", &lastInversion); 
        if (checkpointExists == 1) {
            isStateFileInUse=true;
            printf("Last inversion # is : %d\n",lastInversion);	
            fscanf(state,"%d",&matrixSize);
            width=height=matrixSize;
            printf("Initialize host ....\n");
            initialize_host(state);
        }
        fclose(state);
    } else {
        printf("There's no valid checkpoint file!\n");
    }

    retval = out.open(output_path, "wb");

    if (retval) {
        fprintf(stderr,
            "%s APP: matrix_inversion output open failed:\n",
            boinc_msg_prefix(buf, sizeof(buf))
        );
        fprintf(stderr,
            "%s resolved name %s, retval %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), output_path, retval
        );
        perror("open");
        exit(1);
    }

#ifdef APP_GRAPHICS
    // create shared mem segment for graphics, and arrange to update it
    //
    shmem = (UC_SHMEM*)boinc_graphics_make_shmem("matrix_inversion", sizeof(UC_SHMEM));
    if (!shmem) {
        fprintf(stderr,
            "%s failed to create shared mem segment\n",
            boinc_msg_prefix(buf, sizeof(buf))
        );
    }
    update_shmem();
    boinc_register_timer_callback(update_shmem);
#endif

    if (checkpointExists != 1) { //checkpoint file is not found.
        matrixSize=get_matrix_size(infile);
        printf("Matrix Size: width = height = %d\n",matrixSize);
        width=height=matrixSize;
        // Initialize Host application
        printf("Initialize host ....\n");
        if (initialize_host(infile)==1) {
            return 1;	
        }
        out.printf("\n----------------- Before being inversed ----------------\n\n");
        printf("Computation is running ... Inverse the matrix %d times. Start at inversion #1\n",
               NUM_ITERATIONS);
    } else {
        out.printf("\n----------------- Last checkpointed inversion #%d ----------------\n\n",
                   lastInversion);
        printf("Computation is resumed ... Inverse the matrix %d more times. Start at inversion #%d\n",
               NUM_ITERATIONS-lastInversion,lastInversion+1);
    }

    // Initialize OpenCL resources
    if (initialize_cl()==1) {
        return 1;
    }

    print_to_file(&out,input,matrixSize);

    for (int i=lastInversion+1;i<=NUM_ITERATIONS;++i) {
        //the invert function will trigger kernel calls.
        invert(input,output,matrixSize);
        printf("Finish inversion #%d\n",i);
        for (int j=0;j<matrixSize*matrixSize;++j) {
            input[j]=output[j]; //change the input for the next iteration
        }
        if (run_slow) {
            boinc_sleep(1.);
        }

        if (early_exit && i>30) {
            exit(-10);
        }

        if (early_crash && i>30) {
            boinc_crash();
        }

        if (early_sleep && i>30) {
            g_sleep = true;
            while (1) boinc_sleep(1);
        }
		
        if (boinc_time_to_checkpoint()) {
            printf("Perform checkpointing at inversion # %d\n",i);
            //we'll need to write the current matrix to the state file.
            retval = do_checkpoint(out, i, input, matrixSize); 
            if (retval) {
                fprintf(stderr,
                    "%s APP: matrix_inversion checkpoint failed %d\n",
                    boinc_msg_prefix(buf, sizeof(buf)), retval
                );
                exit(retval);
            }
            boinc_checkpoint_completed();
        }
        fd = i/NUM_ITERATIONS;
        if (cpu_time) fd /= 2;
        boinc_fraction_done(fd);
    }

    out.printf("\n\n----------------- Final inversion #%d ----------------\n\n",
               NUM_ITERATIONS);
    print_to_file(&out,output,matrixSize);

    retval = out.flush(); //force the output file to be closed.
    if (retval) {
        fprintf(stderr,
            "%s APP: matrix_inversion flush failed %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), retval
        );
        exit(1);
    }

    // Releases OpenCL resources 
    if (cleanup_cl()==1) {
        printf("Error!");
        return 1;
    }

    // Release host resources
    cleanup_host();

    // burn up some CPU time if needed
    //
    if (cpu_time) {
        printf("\nBurning up some CPU time ... \n");
        double start = dtime();
        for (int i=0; ; i++) {
            double e = dtime()-start;
            if (e > cpu_time) break;
            fd = .5 + .5*(e/cpu_time);
            boinc_fraction_done(fd);

            if (boinc_time_to_checkpoint()) {
                retval = do_checkpoint(out, NUM_ITERATIONS, input, matrixSize);
                if (retval) {
                    fprintf(stderr,
                        "%s APP: maxtrix_inversion checkpoint failed %d\n",
                        boinc_msg_prefix(buf, sizeof(buf)), retval
                    );
                    exit(1);
                }
                boinc_checkpoint_completed();
            }
            comp_result = do_a_giga_flop(i);
        }
    }
    boinc_fraction_done(1);

#ifdef APP_GRAPHICS
    update_shmem();
#endif

    printf("\nDone! Please press ENTER to exit. ");
    getchar();
    boinc_finish(0);
}
int GUI_RPC_CONN::handle_rpc() {
    int n, retval=0;
    MIOFILE mf;
    MFILE m;
    char* p;
    mf.init_mfile(&m);

    int left = GUI_RPC_REQ_MSG_SIZE - request_nbytes;
#ifdef _WIN32
        n = recv(sock, request_msg+request_nbytes, left, 0);
#else
        n = read(sock, request_msg+request_nbytes, left);
#endif
    if (n <= 0) {
        request_nbytes = 0;
        return ERR_READ;
    }
    request_nbytes += n;

    // buffer full?
    if (request_nbytes >= GUI_RPC_REQ_MSG_SIZE) {
        request_nbytes = 0;
        return ERR_READ;
    }
    request_msg[request_nbytes] = 0;
    if (!strncmp(request_msg, "OPTIONS", 7)) {
        char buf[1024];
        sprintf(buf, "HTTP/1.1 200 OK\n"
            "Server: BOINC client\n"
            "Access-Control-Allow-Origin: *\n"
            "Access-Control-Allow-Methods: POST, GET, OPTIONS\n"
            "Content-Length: 0\n"
            "Keep-Alive: timeout=2, max=100\n"
            "Connection: Keep-Alive\n"
            "Content-Type: text/plain\n\n"
        );
        send(sock, buf, strlen(buf), 0);
        request_nbytes = 0;
        if (log_flags.gui_rpc_debug) {
            msg_printf(0, MSG_INFO,
                "[gui_rpc] processed OPTIONS"
            );
        }
        return 0;
    }
    bool http_request;
    if (complete_post_request(request_msg)) {
        http_request = true;
    } else {
        p = strchr(request_msg, 3);
        if (p) {
            *p = 0;
            http_request = false;
        } else {
            if (log_flags.gui_rpc_debug) {
                msg_printf(0, MSG_INFO,
                    "[gui_rpc] partial GUI RPC Command = '%s'\n", request_msg
                );
            }
            return 0;
        }
    }
    request_nbytes = 0;

    if (log_flags.gui_rpc_debug) {
        msg_printf(0, MSG_INFO,
            "[gui_rpc] GUI RPC Command = '%s'\n", request_msg
        );
    }

    // Policy:
    // - the first auth failure gets an error message; after that, disconnect
    // - if we get an unexpected auth1 or auth2, disconnect

    mf.printf("<boinc_gui_rpc_reply>\n");
    if (match_req(request_msg, "auth1")) {
        if (got_auth1 && auth_needed) {
            retval = ERR_AUTHENTICATOR;
        } else {
            handle_auth1(mf);
            got_auth1 = true;
        }
    } else if (match_req(request_msg, "auth2")) {
        if ((!got_auth1 || got_auth2) && auth_needed) {
            retval = ERR_AUTHENTICATOR;
        } else {
            retval = handle_auth2(request_msg, mf);
            got_auth2 = true;
        }
    } else if (auth_needed && !is_local) {
        auth_failure(mf);
        if (sent_unauthorized) {
            retval = ERR_AUTHENTICATOR;
        }
        sent_unauthorized = true;

    // operations that require authentication only for non-local clients start here.
    // Use this only for information that should be available to people
    // sharing this computer (e.g. what jobs are running)
    // but not for anything sensitive (passwords etc.)

    } else if (match_req(request_msg, "exchange_versions")) {
        handle_exchange_versions(mf);
    } else if (match_req(request_msg, "get_state")) {
        gstate.write_state_gui(mf);
    } else if (match_req(request_msg, "get_results")) {
        bool active_only = false;
        parse_bool(request_msg, "active_only", active_only);
        mf.printf("<results>\n");
        gstate.write_tasks_gui(mf, active_only);
        mf.printf("</results>\n");
    } else if (match_req(request_msg, "get_screensaver_tasks")) {
        handle_get_screensaver_tasks(mf);
    } else if (match_req(request_msg, "result_show_graphics")) {
        handle_result_show_graphics(request_msg, mf);
    } else if (match_req(request_msg, "get_file_transfers")) {
        gstate.write_file_transfers_gui(mf);
    } else if (match_req(request_msg, "get_simple_gui_info")) {
        handle_get_simple_gui_info(mf);
    } else if (match_req(request_msg, "get_project_status")) {
        handle_get_project_status(mf);
    } else if (match_req(request_msg, "get_disk_usage")) {
        handle_get_disk_usage(mf);
    } else if (match_req(request_msg, "get_messages")) {
        handle_get_messages(request_msg, mf);
    } else if (match_req(request_msg, "get_message_count")) {
        handle_get_message_count(request_msg, mf);
    } else if (match_req(request_msg, "get_host_info")) {
        handle_get_host_info(request_msg, mf);
    } else if (match_req(request_msg, "get_statistics")) {
        handle_get_statistics(request_msg, mf);
    } else if (match_req(request_msg, "get_newer_version")) {
        handle_get_newer_version(mf);
    } else if (match_req(request_msg, "get_cc_status")) {
        handle_get_cc_status(this, mf);
    } else if (match_req(request_msg, "get_all_projects_list")) {
        read_all_projects_list_file(mf);
    } else if (match_req(request_msg, "get_notices_public")) {
        handle_get_notices(request_msg, *this, mf, true);
        clear_notice_refresh();

    // Operations that require authentication start here

    } else if (auth_needed) {
        auth_failure(mf);
        if (sent_unauthorized) {
            retval = ERR_AUTHENTICATOR;
        }
        sent_unauthorized = true;
    } else if (match_req(request_msg, "project_nomorework")) {
         handle_project_op(request_msg, mf, "nomorework");
    } else if (match_req(request_msg, "project_allowmorework")) {
         handle_project_op(request_msg, mf, "allowmorework");
    } else if (match_req(request_msg, "project_detach_when_done")) {
         handle_project_op(request_msg, mf, "detach_when_done");
    } else if (match_req(request_msg, "project_dont_detach_when_done")) {
         handle_project_op(request_msg, mf, "dont_detach_when_done");
    } else if (match_req(request_msg, "set_network_mode")) {
        handle_set_network_mode(request_msg, mf);
    } else if (match_req(request_msg, "run_benchmarks")) {
        handle_run_benchmarks(request_msg, mf);
    } else if (match_req(request_msg, "get_proxy_settings")) {
        handle_get_proxy_settings(request_msg, mf);
    } else if (match_req(request_msg, "set_proxy_settings")) {
        handle_set_proxy_settings(request_msg, mf);
    } else if (match_req(request_msg, "network_available")) {
        handle_network_available(request_msg, mf);
    } else if (match_req(request_msg, "abort_file_transfer")) {
        handle_file_transfer_op(request_msg, mf, "abort");
    } else if (match_req(request_msg, "project_detach")) {
        handle_project_op(request_msg, mf, "detach");
    } else if (match_req(request_msg, "abort_result")) {
        handle_result_op(request_msg, mf, "abort");
    } else if (match_req(request_msg, "suspend_result")) {
        handle_result_op(request_msg, mf, "suspend");
    } else if (match_req(request_msg, "resume_result")) {
        handle_result_op(request_msg, mf, "resume");
    } else if (match_req(request_msg, "project_suspend")) {
        handle_project_op(request_msg, mf, "suspend");
    } else if (match_req(request_msg, "project_resume")) {
        handle_project_op(request_msg, mf, "resume");
    } else if (match_req(request_msg, "set_run_mode")) {
        handle_set_run_mode(request_msg, mf);
    } else if (match_req(request_msg, "set_gpu_mode")) {
        handle_set_gpu_mode(request_msg, mf);
    } else if (match_req(request_msg, "quit")) {
        handle_quit(request_msg, mf);
    } else if (match_req(request_msg, "auth_monitor")) {
        handle_auth_monitor(request_msg, is_local, mf);
    } else if (match_req(request_msg, "update_project_apps")) {
        handle_update_projects_apps(request_msg, is_local, mf);
    } else if (match_req(request_msg, "update_project_apps_poll")) {
        handle_update_projects_apps_poll(request_msg, is_local, mf);
    } else if (match_req(request_msg, "acct_mgr_info")) {
        handle_acct_mgr_info(request_msg, mf);
    } else if (match_req(request_msg, "read_global_prefs_override")) {
        mf.printf("<success/>\n");
        gstate.read_global_prefs();
        gstate.request_schedule_cpus("Preferences override");
        gstate.request_work_fetch("Preferences override");
    } else if (match_req(request_msg, "get_project_init_status")) {
        handle_get_project_init_status(request_msg, mf);
    } else if (match_req(request_msg, "get_global_prefs_file")) {
        handle_get_global_prefs_file(mf);
    } else if (match_req(request_msg, "get_global_prefs_working")) {
        handle_get_global_prefs_working(mf);
    } else if (match_req(request_msg, "get_global_prefs_override")) {
        handle_get_global_prefs_override(mf);
    } else if (match_req(request_msg, "set_global_prefs_override")) {
        handle_set_global_prefs_override(request_msg, mf);
    } else if (match_req(request_msg, "get_cc_config")) {
        handle_get_cc_config(mf);
    } else if (match_req(request_msg, "set_cc_config")) {
        handle_set_cc_config(request_msg, mf);
    } else if (match_req(request_msg, "read_cc_config")) {
        mf.printf("<success/>\n");
        read_config_file(false);
        config.show();
        log_flags.show();
        gstate.set_ncpus();
        gstate.request_schedule_cpus("Core client configuration");
        gstate.request_work_fetch("Core client configuration");
#ifndef USE_REC
    } else if (match_req(request_msg, "set_debts")) {
        handle_set_debts(request_msg, mf);
#endif
    } else if (match_req(request_msg, "get_notices")) {
        handle_get_notices(request_msg, *this, mf, false);
        clear_notice_refresh();
    } else if (match_req(request_msg, "battery_info")) {
        handle_battery_info(request_msg, is_local, mf);
    } else {

        // RPCs after this point require authentication,
        // and enable network communication for 5 minutes,
        // overriding other factors.
        // Things like attaching projects, etc.
        //

        double saved_time = gstate.gui_rpcs.time_of_last_rpc_needing_network;
        gstate.gui_rpcs.time_of_last_rpc_needing_network = gstate.now;

        if (match_req(request_msg, "retry_file_transfer")) {
            handle_file_transfer_op(request_msg, mf, "retry");
        } else if (match_req(request_msg, "project_reset")) {
            handle_project_op(request_msg, mf, "reset");
        } else if (match_req(request_msg, "project_update")) {
            handle_project_op(request_msg, mf, "update");
        } else if (match_req(request_msg, "get_project_config")) {
            handle_get_project_config(request_msg, mf);
        } else if (match_req(request_msg, "get_project_config_poll")) {
            handle_get_project_config_poll(request_msg, mf);
        } else if (match_req(request_msg, "lookup_account")) {
            handle_lookup_account(request_msg, mf);
        } else if (match_req(request_msg, "lookup_account_poll")) {
            handle_lookup_account_poll(request_msg, mf);
        } else if (match_req(request_msg, "create_account")) {
            handle_create_account(request_msg, mf);
        } else if (match_req(request_msg, "create_account_poll")) {
            handle_create_account_poll(request_msg, mf);
        } else if (match_req(request_msg, "project_attach")) {
            handle_project_attach(request_msg, mf);
        } else if (match_req(request_msg, "project_attach_poll")) {
            handle_project_attach_poll(request_msg, mf);
        } else if (match_req(request_msg, "acct_mgr_rpc")) {
            handle_acct_mgr_rpc(request_msg, mf);
        } else if (match_req(request_msg, "acct_mgr_rpc_poll")) {
            handle_acct_mgr_rpc_poll(request_msg, mf);

        // DON'T JUST ADD NEW RPCS HERE - THINK ABOUT THEIR
        // AUTHENTICATION AND NETWORK REQUIREMENTS FIRST

        } else {
            mf.printf("<error>unrecognized op: %s</error>\n", request_msg);
            gstate.gui_rpcs.time_of_last_rpc_needing_network = saved_time;
        }
    }

    mf.printf("</boinc_gui_rpc_reply>\n\003");
    m.get_buf(p, n);
    if (http_request) {
        char buf[1024];
        sprintf(buf,
            "HTTP/1.1 200 OK\n"
            "Date: Fri, 31 Dec 1999 23:59:59 GMT\n"
            "Server: BOINC client\n"
            "Connection: close\n"
            "Content-Type: text/xml; charset=utf-8\n"
            "Content-Length: %d\n\n",
            n
        );
        send(sock, buf, strlen(buf), 0);
    }
    if (p) {
        send(sock, p, n, 0);
        p[n-1]=0;   // replace 003 with NULL
        if (log_flags.gui_rpc_debug) {
            if (n > 128) p[128] = 0;
            msg_printf(0, MSG_INFO,
                "[gui_rpc] GUI RPC reply: '%s'\n", p
            );
        }
        free(p);
    }
    return retval;
}
Exemplo n.º 6
0
int main(int argc, char **argv) {
    int i;
    int c, nchars = 0, retval, n;
    double fsize, fd;
    char input_path[512], output_path[512], chkpt_path[512], buf[256];
    MFILE out;
    FILE* state, *infile;

    for (i=0; i<argc; i++) {
        if (!strcmp(argv[i], "-early_exit")) early_exit = true;
        if (!strcmp(argv[i], "-early_crash")) early_crash = true;
        if (!strcmp(argv[i], "-early_sleep")) early_sleep = true;
        if (!strcmp(argv[i], "-run_slow")) run_slow = true;
        if (!strcmp(argv[i], "-cpu_time")) {
            cpu_time = atof(argv[++i]);
        }
    }

    retval = boinc_init();
    if (retval) {
        fprintf(stderr, "%s boinc_init returned %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), retval
        );
        exit(retval);
    }

    // open the input file (resolve logical name first)
    //
    boinc_resolve_filename(INPUT_FILENAME, input_path, sizeof(input_path));
    infile = boinc_fopen(input_path, "r");
    if (!infile) {
        fprintf(stderr,
            "%s Couldn't find input file, resolved name %s.\n",
            boinc_msg_prefix(buf, sizeof(buf)), input_path
        );
        exit(-1);
    }

    // get size of input file (used to compute fraction done)
    //
    file_size(input_path, fsize);

    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");
    if (state) {
        n = fscanf(state, "%d", &nchars);
        fclose(state);
    }
    if (state && n==1) {
        fseek(infile, nchars, SEEK_SET);
        boinc_truncate(output_path, nchars);
        retval = out.open(output_path, "ab");
    } else {
        retval = out.open(output_path, "wb");
    }
    if (retval) {
        fprintf(stderr, "%s APP: upper_case output open failed:\n",
            boinc_msg_prefix(buf, sizeof(buf))
        );
        fprintf(stderr, "%s resolved name %s, retval %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), output_path, retval
        );
        perror("open");
        exit(1);
    }

#ifdef APP_GRAPHICS
    // create shared mem segment for graphics, and arrange to update it
    //
    shmem = (UC_SHMEM*)boinc_graphics_make_shmem("uppercase", sizeof(UC_SHMEM));
    if (!shmem) {
        fprintf(stderr, "%s failed to create shared mem segment\n",
            boinc_msg_prefix(buf, sizeof(buf))
        );
    }
    update_shmem();
    boinc_register_timer_callback(update_shmem);
#endif

    // main loop - read characters, convert to UC, write
    //
    for (i=0; ; i++) {
        c = fgetc(infile);

        if (c == EOF) break;
        c = toupper(c);
        out._putchar(c);
        nchars++;
        if (run_slow) {
            boinc_sleep(1.);
        }

        if (early_exit && i>30) {
            exit(-10);
        }

        if (early_crash && i>30) {
            boinc_crash();
        }
        if (early_sleep && i>30) {
            g_sleep = true;
            while (1) boinc_sleep(1);
        }

        if (boinc_time_to_checkpoint()) {
            retval = do_checkpoint(out, nchars);
            if (retval) {
                fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
                    boinc_msg_prefix(buf, sizeof(buf)), retval
                );
                exit(retval);
            }
            boinc_checkpoint_completed();
        }

        fd = nchars/fsize;
        if (cpu_time) fd /= 2;
        boinc_fraction_done(fd);
    }

    retval = out.flush();
    if (retval) {
        fprintf(stderr, "%s APP: upper_case flush failed %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), retval
        );
        exit(1);
    }

    // burn up some CPU time if needed
    //
    if (cpu_time) {
        double start = dtime();
        for (i=0; ; i++) {
            double e = dtime()-start;
            if (e > cpu_time) break;
            fd = .5 + .5*(e/cpu_time);
            boinc_fraction_done(fd);

            if (boinc_time_to_checkpoint()) {
                retval = do_checkpoint(out, nchars);
                if (retval) {
                    fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
                        boinc_msg_prefix(buf, sizeof(buf)), retval
                    );
                    exit(1);
                }
                boinc_checkpoint_completed();
            }
            comp_result = do_a_giga_flop(i);
        }
    }
    boinc_fraction_done(1);
#ifdef APP_GRAPHICS
    update_shmem();
#endif
    boinc_finish(0);
}
Exemplo n.º 7
0
// Write the client_state.xml file
//
int CLIENT_STATE::write_state_file() {
    MFILE mf;
    int retval, ret1, ret2, attempt;
#ifdef _WIN32
    char win_error_msg[4096];
#endif

    for (attempt=1; attempt<=MAX_STATE_FILE_WRITE_ATTEMPTS; attempt++) {
        if (attempt > 1) boinc_sleep(1.0);
            
        if (log_flags.statefile_debug) {
            msg_printf(0, MSG_INFO,
                "[statefile] Writing state file"
            );
        }
#ifdef _WIN32
        retval = mf.open(STATE_FILE_NEXT, "wc");
#else
        retval = mf.open(STATE_FILE_NEXT, "w");
#endif
        if (retval) {
            if ((attempt == MAX_STATE_FILE_WRITE_ATTEMPTS) || log_flags.statefile_debug) {
                msg_printf(0, MSG_INTERNAL_ERROR,
                    "Can't open %s: %s",
                    STATE_FILE_NEXT, boincerror(retval)
                );
            }
            if (attempt < MAX_STATE_FILE_WRITE_ATTEMPTS) continue;
            return ERR_FOPEN;
        }
        MIOFILE miof;
        miof.init_mfile(&mf);
        ret1 = write_state(miof);
        ret2 = mf.close();
        if (ret1) {
            if ((attempt == MAX_STATE_FILE_WRITE_ATTEMPTS) || log_flags.statefile_debug) {
                msg_printf(NULL, MSG_INTERNAL_ERROR,
                    "Couldn't write state file: %s", boincerror(retval)
                );
            }
            if (attempt < MAX_STATE_FILE_WRITE_ATTEMPTS) continue;
            return ret1;
        }
        if (ret2) {
            if (attempt < MAX_STATE_FILE_WRITE_ATTEMPTS) continue;
            return ret2;
        }

        // only attempt to rename the current state file if it exists.
        //
        if (boinc_file_exists(STATE_FILE_NAME)) {
            if (boinc_file_exists(STATE_FILE_PREV)) {
                retval = boinc_delete_file(STATE_FILE_PREV);
                if (retval) {
                    if ((attempt == MAX_STATE_FILE_WRITE_ATTEMPTS) || log_flags.statefile_debug) {
#ifdef _WIN32
                        msg_printf(0, MSG_INFO,
                            "Can't delete previous state file; %s",
                            windows_format_error_string(GetLastError(), win_error_msg, sizeof(win_error_msg))
                        );
#else
                        msg_printf(0, MSG_INFO,
                            "Can't delete previous state file: %s",
                            strerror(errno)
                        );
#endif
                    }
                    if (attempt < MAX_STATE_FILE_WRITE_ATTEMPTS) continue;
                }
            }
            
            retval = boinc_rename(STATE_FILE_NAME, STATE_FILE_PREV);
            if (retval) {
                if ((attempt == MAX_STATE_FILE_WRITE_ATTEMPTS) || log_flags.statefile_debug) {
#ifdef _WIN32
                    msg_printf(0, MSG_INFO,
                        "Can't rename current state file to previous state file; %s",
                        windows_format_error_string(GetLastError(), win_error_msg, sizeof(win_error_msg))
                    );
#else
                    msg_printf(0, MSG_INFO, 
                        "Can't rename current state file to previous state file: %s", 
                        strerror(errno)
                    );
#endif
                }
                if (attempt < MAX_STATE_FILE_WRITE_ATTEMPTS) continue;
            }
        }

        retval = boinc_rename(STATE_FILE_NEXT, STATE_FILE_NAME);
        if (log_flags.statefile_debug) {
            msg_printf(0, MSG_INFO,
                "[statefile] Done writing state file"
            );
        }
        if (!retval) break;     // Success!
        
        if ((attempt == MAX_STATE_FILE_WRITE_ATTEMPTS) || log_flags.statefile_debug) {
#ifdef _WIN32
            msg_printf(0, MSG_INFO,
                "rename error: %s",
                windows_format_error_string(GetLastError(), win_error_msg, sizeof(win_error_msg))
            );
#elif defined (__APPLE__)
            if (log_flags.statefile_debug) {
                system("ls -al /Library/Application\\ Support/BOINC\\ Data/client*.*");
            }
#endif
        }
        if (attempt < MAX_STATE_FILE_WRITE_ATTEMPTS) continue;
        return ERR_RENAME;
    }
    return 0;
}
Exemplo n.º 8
0
Arquivo: uc2.cpp Projeto: Ashod/Boinc
int main(int argc, char **argv) {
    int i;
    int c, nchars = 0, retval, n;
    double fsize, fd;
    char input_path[512], output_path[512], chkpt_path[512], buf[256];
    MFILE out;
    FILE* state, *infile;

    for (i=0; i<argc; i++) {
        if (strstr(argv[i], "early_exit")) early_exit = true;
        if (strstr(argv[i], "early_crash")) early_crash = true;
        if (strstr(argv[i], "early_sleep")) early_sleep = true;
        if (strstr(argv[i], "run_slow")) run_slow = true;
        if (strstr(argv[i], "critical_section")) critical_section = true;
        if (strstr(argv[i], "network_usage")) network_usage = true;
        if (strstr(argv[i], "cpu_time")) {
            cpu_time = atof(argv[++i]);
        }
        if (strstr(argv[i], "trickle_up")) trickle_up = true;
        if (strstr(argv[i], "trickle_down")) trickle_down = true;
    }
    retval = boinc_init();
    if (retval) {
        fprintf(stderr, "%s boinc_init returned %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), retval
        );
        exit(retval);
    }

    fprintf(stderr, "%s app started; CPU time %f, flags:%s%s%s%s%s%s%s\n",
        boinc_msg_prefix(buf, sizeof(buf)),
        cpu_time,
        early_exit?" early_exit":"",
        early_crash?" early_crash":"",
        early_sleep?" early_sleep":"",
        run_slow?" run_slow":"",
        critical_section?" critical_section":"",
        trickle_up?" trickle_up":"",
        trickle_down?" trickle_down":""
    );

    // open the input file (resolve logical name first)
    //
    boinc_resolve_filename(INPUT_FILENAME, input_path, sizeof(input_path));
    infile = boinc_fopen(input_path, "r");
    if (!infile) {
        fprintf(stderr,
            "%s Couldn't find input file, resolved name %s.\n",
            boinc_msg_prefix(buf, sizeof(buf)), input_path
        );
        exit(-1);
    }

    // get size of input file (used to compute fraction done)
    //
    file_size(input_path, fsize);

    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");
    if (state) {
        n = fscanf(state, "%d", &nchars);
        fclose(state);
    }
    if (state && n==1) {
        fseek(infile, nchars, SEEK_SET);
        boinc_truncate(output_path, nchars);
        retval = out.open(output_path, "ab");
    } else {
        retval = out.open(output_path, "wb");
    }
    if (retval) {
        fprintf(stderr, "%s APP: upper_case output open failed:\n",
            boinc_msg_prefix(buf, sizeof(buf))
        );
        fprintf(stderr, "%s resolved name %s, retval %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), output_path, retval
        );
        perror("open");
        exit(1);
    }

#ifdef APP_GRAPHICS
    // create shared mem segment for graphics, and arrange to update it
    //
    shmem = (UC_SHMEM*)boinc_graphics_make_shmem("uppercase", sizeof(UC_SHMEM));
    if (!shmem) {
        fprintf(stderr, "%s failed to create shared mem segment\n",
            boinc_msg_prefix(buf, sizeof(buf))
        );
    }
    update_shmem();
    boinc_register_timer_callback(update_shmem);
#endif

    if (network_usage) {
        boinc_network_usage(5., 17.);
    }

    // main loop - read characters, convert to UC, write
    //
    for (i=0; ; i++) {
        c = fgetc(infile);
        if (c == EOF) break;

        c = toupper(c);
        out._putchar(c);
        nchars++;

        if (run_slow) {
            boinc_sleep(1.);
        }

        if (early_exit && i>30) {
            exit(-10);
        }

        if (early_crash && i>30) {
            boinc_crash();
        }
        if (early_sleep && i>30) {
            boinc_disable_timer_thread = true;
            while (1) boinc_sleep(1);
        }

        if (boinc_time_to_checkpoint()) {
            retval = do_checkpoint(out, nchars);
            if (retval) {
                fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
                    boinc_msg_prefix(buf, sizeof(buf)), retval
                );
                exit(retval);
            }
            boinc_checkpoint_completed();
        }

		if (report_fraction_done) {
			fd = nchars/fsize;
			if (cpu_time) fd /= 2;
			boinc_fraction_done(fd);
		}
    }

    retval = out.flush();
    if (retval) {
        fprintf(stderr, "%s APP: upper_case flush failed %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), retval
        );
        exit(1);
    }

    if (trickle_up) {
        boinc_send_trickle_up(
            const_cast<char*>("example_app"),
            const_cast<char*>("sample trickle message")
        );
    }

    if (trickle_down) {
        boinc_sleep(10);
        retval = boinc_receive_trickle_down(buf, sizeof(buf));
        if (!retval) {
            fprintf(stderr, "Got trickle-down message: %s\n", buf);
        }
    }

    // burn up some CPU time if needed
    //
    if (cpu_time) {
        double start = dtime();
        for (i=0; ; i++) {
            double e = dtime()-start;
            if (e > cpu_time) break;
			if (report_fraction_done) {
				fd = .5 + .5*(e/cpu_time);
				boinc_fraction_done(fd);
			}

            if (boinc_time_to_checkpoint()) {
                retval = do_checkpoint(out, nchars);
                if (retval) {
                    fprintf(stderr, "%s APP: upper_case checkpoint failed %d\n",
                        boinc_msg_prefix(buf, sizeof(buf)), retval
                    );
                    exit(1);
                }
                boinc_checkpoint_completed();
            }
            if (critical_section) {
                boinc_begin_critical_section();
            }
            comp_result = do_some_computing(i);
            if (critical_section) {
                boinc_end_critical_section();
            }
        }
    }
    boinc_fraction_done(1);
#ifdef APP_GRAPHICS
    update_shmem();
#endif
    boinc_finish(0);
}
Exemplo n.º 9
0
int main(int argc, char **argv) {
    int i,p,q,pq;
    int c, nchars = 0, retval, n;
    double fsize, fd;
    char input_path[512], output_path[512], chkpt_path[512], buf[256],sentence[1025];
    MFILE out;
    FILE* state, *infile;

    for (i=0; i<argc; i++) {
        if (!strcmp(argv[i], "-early_exit")) early_exit = true;
        if (!strcmp(argv[i], "-early_crash")) early_crash = true;
        if (!strcmp(argv[i], "-early_sleep")) early_sleep = true;
        if (!strcmp(argv[i], "-run_slow")) run_slow = true;
        if (!strcmp(argv[i], "-cpu_time")) {
            cpu_time = atof(argv[++i]);
        }
    }

    retval = boinc_init();
    if (retval) {
        fprintf(stderr, "%s boinc_init returned %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), retval
        );
        exit(retval);
    }

    // open the input file (resolve logical name first)
    //
    boinc_resolve_filename(INPUT_FILENAME, input_path, sizeof(input_path));
    infile = boinc_fopen(input_path, "r");
    if (!infile) {
        fprintf(stderr,
            "%s Couldn't find input file, resolved name %s.\n",
            boinc_msg_prefix(buf, sizeof(buf)), input_path
        );
        exit(-1);
    }

    // get size of input file (used to compute fraction done)
    //
    file_size(input_path, fsize);

    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");
    if (state) {
        n = fscanf(state, "%d", &nchars);
        fclose(state);
    }
    if (state && n==1) {
        fseek(infile, nchars, SEEK_SET);
        boinc_truncate(output_path, nchars);
        retval = out.open(output_path, "ab");
    } else {
        retval = out.open(output_path, "wb");
    }
    if (retval) {
        fprintf(stderr, "%s APP: encrypt output open failed:\n",
            boinc_msg_prefix(buf, sizeof(buf))
        );
        fprintf(stderr, "%s resolved name %s, retval %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), output_path, retval
        );
        perror("open");
        exit(1);
    }

for(i=0; ;i++){
c = fgetc(infile);
if(c==EOF) break;
sentence[i]=c;
}
sentence[i]='\0';
p=43;
q=3;
pq=p*q;
//e=19 d=31
strcpy(sentence,rsaencrypt(sentence,19,pq));

    for (i=0;sentence[i]!='\0'; i++) {
	c=sentence[i];
        out._putchar(c);
        nchars++;
        if (run_slow) {
            boinc_sleep(1.);
        }

        if (early_exit && i>30) {
            exit(-10);
        }

        if (early_crash && i>30) {
            boinc_crash();
        }
        if (early_sleep && i>30) {
            boinc_disable_timer_thread = true;
            while (1) boinc_sleep(1);
        }

        if (boinc_time_to_checkpoint()) {
            retval = do_checkpoint(out, nchars);
            if (retval) {
                fprintf(stderr, "%s APP: encrypt checkpoint failed %d\n",
                    boinc_msg_prefix(buf, sizeof(buf)), retval
                );
                exit(retval);
            }
            boinc_checkpoint_completed();
        }

        fd = nchars/fsize;
        if (cpu_time) fd /= 2;
        boinc_fraction_done(fd);
    }

    retval = out.flush();
    if (retval) {
        fprintf(stderr, "%s APP: encrypt flush failed %d\n",
            boinc_msg_prefix(buf, sizeof(buf)), retval
        );
        exit(1);
    }

    boinc_fraction_done(1);
    boinc_finish(0);
}
int GUI_RPC_CONN::handle_rpc() {
    char request_msg[4096];
    int n, retval=0;
    MIOFILE mf;
    MFILE m;
    char* p;
    mf.init_mfile(&m);

    // read the request message in one read()
    // so that the core client won't hang because
    // of malformed request msgs
    //
#ifdef _WIN32
        n = recv(sock, request_msg, 4095, 0);
#else
        n = read(sock, request_msg, 4095);
#endif
    if (n <= 0) return ERR_READ;
    request_msg[n-1] = 0;   // replace 003 with NULL

    if (log_flags.guirpc_debug) {
        msg_printf(0, MSG_INFO,
            "[guirpc_debug] GUI RPC Command = '%s'\n", request_msg
        );
    }

    // Policy:
    // - the first auth failure gets an error message; after that, disconnect
    // - if we get an unexpected auth1 or auth2, disconnect

    mf.printf("<boinc_gui_rpc_reply>\n");
    if (match_req(request_msg, "auth1")) {
        if (got_auth1 && auth_needed) {
            retval = ERR_AUTHENTICATOR;
        } else {
            handle_auth1(mf);
            got_auth1 = true;
        }
    } else if (match_req(request_msg, "auth2")) {
        if ((!got_auth1 || got_auth2) && auth_needed) {
            retval = ERR_AUTHENTICATOR;
        } else {
            retval = handle_auth2(request_msg, mf);
            got_auth2 = true;
        }
    } else if (auth_needed && !is_local) {
        auth_failure(mf);
        if (sent_unauthorized) {
            retval = ERR_AUTHENTICATOR;
        }
        sent_unauthorized = true;

    // operations that require authentication only for non-local clients start here.
    // Use this only for information that should be available to people
    // sharing this computer (e.g. what jobs are running)
    // but not for anything sensitive (passwords etc.)

    } else if (match_req(request_msg, "exchange_versions")) {
        handle_exchange_versions(mf);
    } else if (match_req(request_msg, "get_state")) {
        gstate.write_state_gui(mf);
    } else if (match_req(request_msg, "get_results")) {
        bool active_only = false;
        parse_bool(request_msg, "active_only", active_only);
        mf.printf("<results>\n");
        gstate.write_tasks_gui(mf, active_only);
        mf.printf("</results>\n");
    } else if (match_req(request_msg, "get_screensaver_tasks")) {
        handle_get_screensaver_tasks(mf);
    } else if (match_req(request_msg, "result_show_graphics")) {
        handle_result_show_graphics(request_msg, mf);
    } else if (match_req(request_msg, "get_file_transfers")) {
        gstate.write_file_transfers_gui(mf);
    } else if (match_req(request_msg, "get_simple_gui_info")) {
        handle_get_simple_gui_info(mf);
    } else if (match_req(request_msg, "get_project_status")) {
        handle_get_project_status(mf);
    } else if (match_req(request_msg, "get_disk_usage")) {
        handle_get_disk_usage(mf);
    } else if (match_req(request_msg, "get_messages")) {
        handle_get_messages(request_msg, mf);
    } else if (match_req(request_msg, "get_message_count")) {
        handle_get_message_count(request_msg, mf);
    } else if (match_req(request_msg, "get_host_info")) {
        handle_get_host_info(request_msg, mf);
    } else if (match_req(request_msg, "get_statistics")) {
        handle_get_statistics(request_msg, mf);
    } else if (match_req(request_msg, "get_newer_version")) {
        handle_get_newer_version(mf);
    } else if (match_req(request_msg, "get_cc_status")) {
        handle_get_cc_status(this, mf);
    } else if (match_req(request_msg, "get_all_projects_list")) {
        read_all_projects_list_file(mf);

    // Operations that require authentication start here

    } else if (auth_needed) {
        auth_failure(mf);
        if (sent_unauthorized) {
            retval = ERR_AUTHENTICATOR;
        }
        sent_unauthorized = true;
    } else if (match_req(request_msg, "project_nomorework")) {
         handle_project_op(request_msg, mf, "nomorework");
     } else if (match_req(request_msg, "project_allowmorework")) {
         handle_project_op(request_msg, mf, "allowmorework");
    } else if (match_req(request_msg, "project_detach_when_done")) {
         handle_project_op(request_msg, mf, "detach_when_done");
    } else if (match_req(request_msg, "project_dont_detach_when_done")) {
         handle_project_op(request_msg, mf, "dont_detach_when_done");
    } else if (match_req(request_msg, "set_network_mode")) {
        handle_set_network_mode(request_msg, mf);
    } else if (match_req(request_msg, "run_benchmarks")) {
        handle_run_benchmarks(request_msg, mf);
    } else if (match_req(request_msg, "get_proxy_settings")) {
        handle_get_proxy_settings(request_msg, mf);
    } else if (match_req(request_msg, "set_proxy_settings")) {
        handle_set_proxy_settings(request_msg, mf);
    } else if (match_req(request_msg, "network_available")) {
        handle_network_available(request_msg, mf);
    } else if (match_req(request_msg, "abort_file_transfer")) {
        handle_file_transfer_op(request_msg, mf, "abort");
    } else if (match_req(request_msg, "project_detach")) {
        handle_project_op(request_msg, mf, "detach");
    } else if (match_req(request_msg, "abort_result")) {
        handle_result_op(request_msg, mf, "abort");
    } else if (match_req(request_msg, "suspend_result")) {
        handle_result_op(request_msg, mf, "suspend");
    } else if (match_req(request_msg, "resume_result")) {
        handle_result_op(request_msg, mf, "resume");
    } else if (match_req(request_msg, "project_suspend")) {
        handle_project_op(request_msg, mf, "suspend");
    } else if (match_req(request_msg, "project_resume")) {
        handle_project_op(request_msg, mf, "resume");
    } else if (match_req(request_msg, "set_run_mode")) {
        handle_set_run_mode(request_msg, mf);
    } else if (match_req(request_msg, "set_gpu_mode")) {
        handle_set_gpu_mode(request_msg, mf);
    } else if (match_req(request_msg, "quit")) {
        handle_quit(request_msg, mf);
    } else if (match_req(request_msg, "acct_mgr_info")) {
        handle_acct_mgr_info(request_msg, mf);
    } else if (match_req(request_msg, "read_global_prefs_override")) {
        mf.printf("<success/>\n");
        gstate.read_global_prefs();
        gstate.request_schedule_cpus("Preferences override");
        gstate.request_work_fetch("Preferences override");
    } else if (match_req(request_msg, "get_project_init_status")) {
        handle_get_project_init_status(request_msg, mf);
    } else if (match_req(request_msg, "get_global_prefs_file")) {
        handle_get_global_prefs_file(mf);
    } else if (match_req(request_msg, "get_global_prefs_working")) {
        handle_get_global_prefs_working(mf);
    } else if (match_req(request_msg, "get_global_prefs_override")) {
        handle_get_global_prefs_override(mf);
    } else if (match_req(request_msg, "set_global_prefs_override")) {
        handle_set_global_prefs_override(request_msg, mf);
    } else if (match_req(request_msg, "get_cc_config")) {
        handle_get_cc_config(mf);
    } else if (match_req(request_msg, "set_cc_config")) {
        handle_set_cc_config(request_msg, mf);
    } else if (match_req(request_msg, "read_cc_config")) {
        mf.printf("<success/>\n");
        read_config_file(false);
        msg_printf(0, MSG_INFO, "Re-read config file");
        config.show();
        log_flags.show();
        gstate.set_ncpus();
        gstate.request_schedule_cpus("Core client configuration");
        gstate.request_work_fetch("Core client configuration");
    } else if (match_req(request_msg, "set_debts")) {
        handle_set_debts(request_msg, mf);
    } else {

        // RPCs after this point require authentication,
        // and enable network communication for 5 minutes, overriding other factors.
        // Things like attaching projects, etc.
        //

        double saved_time = gstate.gui_rpcs.time_of_last_rpc_needing_network;
        gstate.gui_rpcs.time_of_last_rpc_needing_network = gstate.now;

        if (match_req(request_msg, "retry_file_transfer")) {
            handle_file_transfer_op(request_msg, mf, "retry");
        } else if (match_req(request_msg, "project_reset")) {
            handle_project_op(request_msg, mf, "reset");
        } else if (match_req(request_msg, "project_update")) {
            handle_project_op(request_msg, mf, "update");
        } else if (match_req(request_msg, "get_project_config")) {
            handle_get_project_config(request_msg, mf);
        } else if (match_req(request_msg, "get_project_config_poll")) {
            handle_get_project_config_poll(request_msg, mf);
        } else if (match_req(request_msg, "lookup_account")) {
            handle_lookup_account(request_msg, mf);
        } else if (match_req(request_msg, "lookup_account_poll")) {
            handle_lookup_account_poll(request_msg, mf);
        } else if (match_req(request_msg, "create_account")) {
            handle_create_account(request_msg, mf);
        } else if (match_req(request_msg, "create_account_poll")) {
            handle_create_account_poll(request_msg, mf);
        } else if (match_req(request_msg, "project_attach")) {
            handle_project_attach(request_msg, mf);
        } else if (match_req(request_msg, "project_attach_poll")) {
            handle_project_attach_poll(request_msg, mf);
        } else if (match_req(request_msg, "acct_mgr_rpc")) {
            handle_acct_mgr_rpc(request_msg, mf);
        } else if (match_req(request_msg, "acct_mgr_rpc_poll")) {
            handle_acct_mgr_rpc_poll(request_msg, mf);

        // DON'T JUST ADD NEW RPCS HERE - THINK ABOUT THEIR
        // AUTHENTICATION AND NETWORK REQUIREMENTS FIRST

        } else {
            mf.printf("<error>unrecognized op: %s</error>\n", request_msg);
            gstate.gui_rpcs.time_of_last_rpc_needing_network = saved_time;
        }
    }

    mf.printf("</boinc_gui_rpc_reply>\n\003");
    m.get_buf(p, n);
    if (p) {
        send(sock, p, n, 0);
        p[n-1]=0;   // replace 003 with NULL
        if (log_flags.guirpc_debug) {
            if (n > 50) p[50] = 0;
            msg_printf(0, MSG_INFO,
                "[guirpc_debug] GUI RPC reply: '%s'\n", p
            );
        }
        free(p);
    }
    return retval;
}
int ReportPulseEvent(float PulsePower,float MeanPower, float period,
                     int time_bin,int freq_bin, float snr, float thresh, float *folded_pot,
                     int scale, int write_pulse) {
  PULSE_INFO pi;
  pulse pulse;
  int retval=0, i, len_prof=static_cast<int>(floor(period));
  float step,norm,index,MinPower=PulsePower*MeanPower*scale;

// debug possible heap corruption -- jeffc
#ifdef _WIN32
BOINCASSERT(_CrtCheckMemory());
#endif

  // pulse info
  pi.score=snr/thresh;
  pi.p.peak_power=PulsePower-1;
  pi.p.mean_power=MeanPower;
  pi.p.fft_len=ChirpFftPairs[analysis_state.icfft].FftLen;
  pi.p.chirp_rate=ChirpFftPairs[analysis_state.icfft].ChirpRate;
  pi.p.period=static_cast<float>(period*static_cast<double>(pi.p.fft_len)/swi.subband_sample_rate);
  pi.p.snr = snr;
  pi.p.thresh = thresh;
  pi.p.len_prof = len_prof;
  pi.freq_bin=freq_bin;
  pi.time_bin=time_bin;
  pi.p.freq=cnvt_bin_hz(freq_bin, pi.p.fft_len);
  double t_offset=(static_cast<double>(time_bin)+0.5)
       *static_cast<double>(pi.p.fft_len)/
         swi.subband_sample_rate;
  pi.p.detection_freq=calc_detection_freq(pi.p.freq,pi.p.chirp_rate,t_offset);
  pi.p.time=swi.time_recorded+t_offset/86400.0;
  time_to_ra_dec(pi.p.time, &pi.p.ra, &pi.p.decl);

  for (i=0;i<len_prof;i++) {
    if (folded_pot[i]<MinPower) MinPower=folded_pot[i];
  }  
  norm=255.0f/((PulsePower*MeanPower*scale-MinPower));
  
  // Populate the min and max PoT arrays.  These are only used
  // for graphics.
#ifdef BOINC_APP_GRAPHICS
  if (!nographics()) {
    step=static_cast<float>(len_prof)/swi.analysis_cfg.pulse_pot_length;
    index=0;
    for (i=0;i<swi.analysis_cfg.pulse_pot_length;i++) {
      pi.pot_min[i]=255;
      pi.pot_max[i]=0;
      int j;
      for (j=0; j<step; j++) {
        unsigned int pot = static_cast<unsigned int>((folded_pot[static_cast<int>(floor(index))+j]-MinPower)*norm);
        if (pot<pi.pot_min[i]) {
          pi.pot_min[i]=pot;
        }
        if (pi.pot_min[i] >= 256) pi.pot_min[i] = 255; // kludge until we fix the assert failures
        BOINCASSERT(pi.pot_min[i] < 256);
        if (pot>pi.pot_max[i])
          pi.pot_max[i]=pot;
        if (pi.pot_max[i] >= 256) pi.pot_max[i] = 255; // kludge until we fix the assert failures
        BOINCASSERT(pi.pot_max[i] < 256);
      }
      index+=step;
    }
  }
#endif

  // Populate the result PoT if the folded PoT will fit.
  if (pi.p.len_prof < swi.analysis_cfg.pulse_pot_length) {
	pi.p.pot.resize(len_prof);
  	for (i=0;i<len_prof;i++) {
		pi.p.pot[i] = (unsigned char)((folded_pot[i]-MinPower)*norm);
  	}
  } else {
    pi.p.pot.clear();
  }

  // Update gdata pulse info regardless of whether it is the
  // best thus far.  If a pulse has made it this far, display it.
#ifdef BOINC_APP_GRAPHICS
    if (!nographics()) sah_graphics->pi.copy(&pi);
#endif

  // best thus far ?
  if (pi.score>best_pulse->score) {
    *best_pulse=pi;
  }

  if (write_pulse) {

    if (signal_count > swi.analysis_cfg.max_signals) {
      SETIERROR(RESULT_OVERFLOW,"in ReportPulseEvent");
    }

    //for (i=0;i<len_prof;i++) {
//	sprintf(&pi.p.pot[i], "%02x",(int)((folded_pot[i]-MinPower)*norm));
 //   }

    retval = outfile.printf("%s", pi.p.print_xml(0,0,1).c_str());

    if (retval >= 0) {
      outfile.printf("\n");
    }

    if (retval < 0) {
      SETIERROR(WRITE_FAILED,"in ReportPulseEvent");
    } else {
      signal_count++;
      pulse_count++;
    }

  }

// debug possible heap corruption -- jeffc
#ifdef _WIN32
BOINCASSERT(_CrtCheckMemory());
#endif


  return(retval);
}
int ReportTripletEvent(
  float Power, float MeanPower, float period,
  float mid_time_bin, int start_time_bin, int freq_bin,
  int pot_len,const float *PoT, int write_triplet
) {
  TRIPLET_INFO ti;
  triplet triplet;
  int retval=0, i, j;
  double step,norm,index;
  double max_power=0;
  static int * inv;

// debug possible heap corruption -- jeffc
#ifdef _WIN32
BOINCASSERT(_CrtCheckMemory());
#endif

  if (!inv) inv = (int*)calloc_a(swi.analysis_cfg.triplet_pot_length, sizeof(int), MEM_ALIGN);

  // triplet info
  ti.score=Power;
  ti.t.peak_power=Power;
  ti.t.mean_power=MeanPower;
  ti.freq_bin=freq_bin;
  ti.time_bin=mid_time_bin+start_time_bin+0.5f;
  ti.t.chirp_rate=ChirpFftPairs[analysis_state.icfft].ChirpRate;
  ti.t.fft_len=ChirpFftPairs[analysis_state.icfft].FftLen;
  ti.bperiod=period;
  ti.t.period=static_cast<float>(period*static_cast<double>(ti.t.fft_len)/swi.subband_sample_rate);
  ti.t.freq=cnvt_bin_hz(freq_bin, ti.t.fft_len);
  double t_offset=(static_cast<double>(mid_time_bin)+start_time_bin+0.5)
      *static_cast<double>(ti.t.fft_len)/
         swi.subband_sample_rate;
  ti.t.detection_freq=calc_detection_freq(ti.t.freq,ti.t.chirp_rate,t_offset);
  ti.t.time=swi.time_recorded+t_offset/86400.0;
  time_to_ra_dec(ti.t.time, &ti.t.ra, &ti.t.decl);

  // Populate the min and max PoT arrays.  These are only used
  // for graphics.
  memset(ti.pot_min,0xff,swi.analysis_cfg.triplet_pot_length*sizeof(int));
  memset(ti.pot_max,0,swi.analysis_cfg.triplet_pot_length*sizeof(int));
  step=static_cast<double>(pot_len)/swi.analysis_cfg.triplet_pot_length;
  ti.scale=static_cast<float>(1.0/step);
  index=0;
  for (i=0;i<pot_len;i++) {
    if (PoT[i]>max_power) max_power=PoT[i];
  }
  norm=255.0/max_power;
  float mtb = mid_time_bin;
  if (pot_len > swi.analysis_cfg.triplet_pot_length) {
    ti.tpotind0_0 = ti.tpotind0_1 = static_cast<int>(((mtb-period)*swi.analysis_cfg.triplet_pot_length)/pot_len);
    ti.tpotind1_0 = ti.tpotind1_1 = static_cast<int>(((mtb)*swi.analysis_cfg.triplet_pot_length)/pot_len);
    ti.tpotind2_0 = ti.tpotind2_1 = static_cast<int>(((mtb+period)*swi.analysis_cfg.triplet_pot_length)/pot_len);
    for (j=0; j<pot_len; j++) {
      i = (j*swi.analysis_cfg.triplet_pot_length)/pot_len;
      if ((PoT[j]*norm)<ti.pot_min[i]) {
        ti.pot_min[i]=static_cast<unsigned int>(floor(PoT[j]*norm));
      }
      if ((PoT[j]*norm)>ti.pot_max[i]) {
        ti.pot_max[i]=static_cast<unsigned int>(floor(PoT[j]*norm));
      }
    }
  } else {
    memset(inv, -1, sizeof(inv));
    for (i=0;i<swi.analysis_cfg.triplet_pot_length;i++) {
      j = (i*pot_len)/swi.analysis_cfg.triplet_pot_length;
      if (inv[j] < 0) inv[j] = i;
      if ((PoT[j]*norm)<ti.pot_min[i]) {
        ti.pot_min[i]=static_cast<unsigned int>(floor(PoT[j]*norm));
      }
      if ((PoT[j]*norm)>ti.pot_max[i]) {
        ti.pot_max[i]=static_cast<unsigned int>(floor(PoT[j]*norm));
      }
    }
    ti.tpotind0_0 = inv[static_cast<int>(mtb-period)];
    ti.tpotind0_1 = inv[static_cast<int>(mtb-period+1)];
    ti.tpotind1_0 = (inv[static_cast<int>(mtb)]+inv[static_cast<int>(mtb+1)])/2;
    ti.tpotind1_1 = (inv[static_cast<int>(mtb+1)]+inv[static_cast<int>(mtb+2)])/2;
    ti.tpotind2_0 = inv[static_cast<int>(mtb+period)];
    if (mtb+period+1 >= pot_len) ti.tpotind2_1 = swi.analysis_cfg.triplet_pot_length-1;
    else ti.tpotind2_1 = inv[static_cast<int>(mtb+period+1)];
  }

  // Update sah_graphics triplet info regardless of whether it is the
  // best thus far.  If a triplet has made it this far, display it.
#ifdef BOINC_APP_GRAPHICS
    if (!nographics()) sah_graphics->ti.copy(&ti);
#endif

  // best thus far ?
  if (ti.score>best_triplet->score) {
    *best_triplet=ti;
  }


  if (write_triplet) {

    if (signal_count > swi.analysis_cfg.max_signals) {
      SETIERROR(RESULT_OVERFLOW,"in ReportTripletEvent");
    }

    retval = outfile.printf("%s", ti.t.print_xml(0,0,1).c_str());

    if (retval < 0) {
      SETIERROR(WRITE_FAILED,"in ReportTripletEvent");
    } else {
      signal_count++;
      triplet_count++;
    }

  }

// debug possible heap corruption -- jeffc
#ifdef _WIN32
BOINCASSERT(_CrtCheckMemory());
#endif

  return(retval);
}