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):
|
|
|
|
//
|
|
|
|
|
2002-06-06 18:42:01 +00:00
|
|
|
#include "windows_cpp.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2002-06-06 18:42:01 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include <stdio.h>
|
2002-06-21 18:31:32 +00:00
|
|
|
#include <time.h>
|
2002-04-30 22:22:54 +00:00
|
|
|
|
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "file_names.h"
|
2002-08-07 22:52:10 +00:00
|
|
|
#include "filesys.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "hostinfo.h"
|
|
|
|
#include "log_flags.h"
|
|
|
|
#include "parse.h"
|
2002-06-21 18:31:32 +00:00
|
|
|
#include "speed_stats.h"
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "client_state.h"
|
|
|
|
|
2002-06-21 18:31:32 +00:00
|
|
|
#define SECONDS_IN_MONTH 2592000
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Global CLIENT_STATE object
|
2002-04-30 22:22:54 +00:00
|
|
|
CLIENT_STATE gstate;
|
|
|
|
|
|
|
|
CLIENT_STATE::CLIENT_STATE() {
|
|
|
|
net_xfers = new NET_XFER_SET;
|
|
|
|
http_ops = new HTTP_OP_SET(net_xfers);
|
|
|
|
file_xfers = new FILE_XFER_SET(http_ops);
|
2002-08-07 22:52:10 +00:00
|
|
|
pers_xfers = new PERS_FILE_XFER_SET(file_xfers);
|
2002-06-21 06:52:47 +00:00
|
|
|
scheduler_op = new SCHEDULER_OP(http_ops);
|
2002-04-30 22:22:54 +00:00
|
|
|
client_state_dirty = false;
|
|
|
|
exit_when_idle = false;
|
2002-06-28 18:32:18 +00:00
|
|
|
run_time_test = true;
|
2002-08-07 22:52:10 +00:00
|
|
|
giveup_after = PERS_GIVEUP;
|
2002-04-30 22:22:54 +00:00
|
|
|
contacted_sched_server = false;
|
|
|
|
activities_suspended = false;
|
2002-05-17 22:33:57 +00:00
|
|
|
version = VERSION;
|
|
|
|
platform_name = HOST;
|
2002-07-01 18:16:31 +00:00
|
|
|
exit_after = -1;
|
2002-07-31 19:05:15 +00:00
|
|
|
app_started = 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-06-21 06:52:47 +00:00
|
|
|
int CLIENT_STATE::init(PREFS* p) {
|
2002-04-30 22:22:54 +00:00
|
|
|
nslots = 1;
|
|
|
|
unsigned int i;
|
2002-07-29 00:39:45 +00:00
|
|
|
if (p==NULL) {
|
2002-07-11 01:09:53 +00:00
|
|
|
fprintf(stderr, "error: CLIENT_STATE.init: unexpected NULL pointer p\n");
|
|
|
|
return ERR_NULL;
|
|
|
|
}
|
2002-06-21 06:52:47 +00:00
|
|
|
prefs = p;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
// Initialize the random number generator
|
|
|
|
srand( clock() );
|
|
|
|
|
2002-06-21 06:52:47 +00:00
|
|
|
// copy all PROJECTs from the prefs to the client state.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2002-06-21 06:52:47 +00:00
|
|
|
for (i=0; i<p->projects.size(); i++) {
|
|
|
|
projects.push_back(p->projects[i]);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-06-21 06:52:47 +00:00
|
|
|
// Then parse the client state file,
|
|
|
|
// ignoring any <project> tags (and associated stuff)
|
|
|
|
// for projects not in the prefs
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2002-06-21 06:52:47 +00:00
|
|
|
parse_state_file();
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-06-21 06:52:47 +00:00
|
|
|
if (log_flags.state_debug) {
|
|
|
|
print_counts();
|
|
|
|
}
|
2002-07-15 23:21:20 +00:00
|
|
|
|
|
|
|
// Finally, set up the project and slot directories
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
make_project_dirs();
|
|
|
|
make_slot_dirs();
|
|
|
|
|
2002-06-28 18:32:18 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if time tests should be run
|
2002-07-15 23:21:20 +00:00
|
|
|
// This is determined by seeing if the user passed the "-no_time_test"
|
|
|
|
// flag or if it's been a month since we last checked time stats
|
2002-06-28 18:32:18 +00:00
|
|
|
//
|
|
|
|
bool CLIENT_STATE::run_time_tests() {
|
|
|
|
return (run_time_test && (
|
|
|
|
difftime(time(0), (time_t)host_info.p_calculated) > SECONDS_IN_MONTH
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Updates computer statistics (roughly once a month)
|
2002-06-28 18:32:18 +00:00
|
|
|
//
|
|
|
|
int CLIENT_STATE::time_tests() {
|
|
|
|
get_host_info(host_info); // this is platform dependent
|
|
|
|
host_info.p_fpops = run_double_prec_test(4); //these are not
|
|
|
|
host_info.p_iops = run_int_test(4);
|
2002-07-29 00:39:45 +00:00
|
|
|
//host_info.p_membw = run_mem_bandwidth_test(4);
|
|
|
|
host_info.p_membw = 100000;
|
2002-06-28 18:32:18 +00:00
|
|
|
host_info.p_calculated = (double)time(0); //set time calculated
|
2002-07-01 22:41:09 +00:00
|
|
|
//host_info.m_cache = check_cache_size(CACHE_MAX);
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
// Update the net_stats object, since it's private
|
|
|
|
//
|
|
|
|
void CLIENT_STATE::update_net_stats(bool is_upload, double nbytes, double nsecs) {
|
|
|
|
net_stats.update( is_upload, nbytes, nsecs );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert an object into the file_xfers object, since it's private
|
|
|
|
//
|
|
|
|
int CLIENT_STATE::insert_file_xfer( FILE_XFER *fxp ) {
|
|
|
|
return file_xfers->insert(fxp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the maximum allowed disk usage as determined by user preferences.
|
|
|
|
// Since there are three different settings in the prefs, it returns the least
|
|
|
|
// of the three.
|
|
|
|
double CLIENT_STATE::allowed_disk_usage() {
|
|
|
|
double percent_space,min_val;
|
|
|
|
|
|
|
|
// Calculate allowed disk usage based on % pref
|
|
|
|
percent_space = host_info.d_total*prefs->disk_max_used_pct/100.0;
|
|
|
|
|
|
|
|
// Return the minimum of the three
|
|
|
|
return min(min(prefs->disk_max_used_gb, percent_space),min_val);
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
// See if (on the basis of user prefs) we should suspend activities.
|
|
|
|
// If so, suspend tasks
|
|
|
|
//
|
|
|
|
int CLIENT_STATE::check_suspend_activities() {
|
|
|
|
bool should_suspend = false;
|
2002-06-21 06:52:47 +00:00
|
|
|
if (prefs->dont_run_on_batteries && host_is_running_on_batteries()) {
|
2002-04-30 22:22:54 +00:00
|
|
|
should_suspend = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_suspend) {
|
|
|
|
if (!activities_suspended) {
|
|
|
|
if (log_flags.task_debug) printf("SUSPENDING ACTIVITIES\n");
|
|
|
|
active_tasks.suspend_all();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (activities_suspended) {
|
|
|
|
if (log_flags.task_debug) printf("UNSUSPENDING ACTIVITIES\n");
|
|
|
|
active_tasks.unsuspend_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
activities_suspended = should_suspend;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// do_something is where all the action happens. This is part of the
|
|
|
|
// finite state machine abstraction of the client. Each of the key
|
|
|
|
// elements of the client is given a chance to perform work here.
|
2002-04-30 22:22:54 +00:00
|
|
|
// return true if something happened
|
2002-08-07 22:52:10 +00:00
|
|
|
// TODO: handle errors passed back up to here?
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
|
|
|
bool CLIENT_STATE::do_something() {
|
|
|
|
int nbytes;
|
|
|
|
bool action = false;
|
|
|
|
|
|
|
|
check_suspend_activities();
|
|
|
|
if (!activities_suspended) {
|
|
|
|
net_xfers->poll(999999, nbytes);
|
|
|
|
if (nbytes) action = true;
|
2002-08-07 22:52:10 +00:00
|
|
|
// If pers_xfers returns true, we've made a change to a
|
|
|
|
// persistent transfer which must be recorded in the
|
|
|
|
// client_state.xml file
|
|
|
|
if (pers_xfers->poll()) {
|
|
|
|
action = client_state_dirty = true;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
action |= http_ops->poll();
|
|
|
|
action |= file_xfers->poll();
|
|
|
|
action |= active_tasks.poll();
|
2002-06-21 18:31:32 +00:00
|
|
|
action |= active_tasks.poll_time();
|
2002-07-15 05:34:32 +00:00
|
|
|
action |= scheduler_rpc_poll();
|
2002-04-30 22:22:54 +00:00
|
|
|
action |= garbage_collect();
|
|
|
|
action |= start_apps();
|
|
|
|
action |= handle_running_apps();
|
|
|
|
action |= start_file_xfers();
|
|
|
|
write_state_file_if_needed();
|
|
|
|
}
|
|
|
|
if (!action) time_stats.update(true, !activities_suspended);
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Parse the client_state.xml file
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int CLIENT_STATE::parse_state_file() {
|
|
|
|
char buf[256];
|
|
|
|
FILE* f = fopen(STATE_FILE_NAME, "r");
|
2002-07-31 19:05:15 +00:00
|
|
|
PROJECT temp_project, *project;
|
2002-04-30 22:22:54 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
if (!f) {
|
|
|
|
if (log_flags.state_debug) {
|
2002-05-17 22:33:57 +00:00
|
|
|
printf("No state file; will create one\n");
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
fgets(buf, 256, f);
|
|
|
|
if (!match_tag(buf, "<client_state>")) return -1;
|
|
|
|
while (fgets(buf, 256, f)) {
|
|
|
|
if (match_tag(buf, "</client_state>")) {
|
|
|
|
return 0;
|
|
|
|
} else if (match_tag(buf, "<project>")) {
|
2002-07-31 19:05:15 +00:00
|
|
|
temp_project.parse_state(f);
|
|
|
|
project = lookup_project(temp_project.master_url);
|
|
|
|
if (project) {
|
|
|
|
project->copy_state_fields(temp_project);
|
2002-06-21 06:52:47 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Project %s found in state file but not prefs.\n",
|
2002-07-31 19:05:15 +00:00
|
|
|
temp_project.master_url
|
2002-06-21 06:52:47 +00:00
|
|
|
);
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
} else if (match_tag(buf, "<app>")) {
|
|
|
|
APP* app = new APP;
|
|
|
|
app->parse(f);
|
2002-06-21 06:52:47 +00:00
|
|
|
if (project) {
|
|
|
|
retval = link_app(project, app);
|
|
|
|
if (!retval) apps.push_back(app);
|
|
|
|
} else {
|
|
|
|
delete app;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
} else if (match_tag(buf, "<file_info>")) {
|
|
|
|
FILE_INFO* fip = new FILE_INFO;
|
2002-07-05 05:33:40 +00:00
|
|
|
fip->parse(f, false);
|
2002-06-21 06:52:47 +00:00
|
|
|
if (project) {
|
|
|
|
retval = link_file_info(project, fip);
|
|
|
|
if (!retval) file_infos.push_back(fip);
|
2002-08-07 22:52:10 +00:00
|
|
|
// Init PERS_FILE_XFER and push it onto pers_file_xfer stack
|
|
|
|
if (fip->pers_file_xfer) {
|
|
|
|
fip->pers_file_xfer->init( fip, fip->upload_when_present );
|
|
|
|
pers_xfers->insert( fip->pers_file_xfer );
|
|
|
|
}
|
2002-06-21 06:52:47 +00:00
|
|
|
} else {
|
|
|
|
delete fip;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
} else if (match_tag(buf, "<app_version>")) {
|
|
|
|
APP_VERSION* avp = new APP_VERSION;
|
|
|
|
avp->parse(f);
|
2002-06-21 06:52:47 +00:00
|
|
|
if (project) {
|
|
|
|
retval = link_app_version(project, avp);
|
|
|
|
if (!retval) app_versions.push_back(avp);
|
|
|
|
} else {
|
|
|
|
delete avp;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
} else if (match_tag(buf, "<workunit>")) {
|
|
|
|
WORKUNIT* wup = new WORKUNIT;
|
|
|
|
wup->parse(f);
|
2002-06-21 06:52:47 +00:00
|
|
|
if (project) {
|
|
|
|
retval = link_workunit(project, wup);
|
|
|
|
if (!retval) workunits.push_back(wup);
|
|
|
|
} else {
|
|
|
|
delete wup;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
} else if (match_tag(buf, "<result>")) {
|
|
|
|
RESULT* rp = new RESULT;
|
2002-07-05 05:33:40 +00:00
|
|
|
rp->parse_state(f);
|
2002-06-21 06:52:47 +00:00
|
|
|
if (project) {
|
|
|
|
retval = link_result(project, rp);
|
|
|
|
if (!retval) results.push_back(rp);
|
|
|
|
} else {
|
2002-07-31 19:05:15 +00:00
|
|
|
fprintf(stderr, "error: link_result failed\n");
|
2002-06-21 06:52:47 +00:00
|
|
|
delete rp;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
} else if (match_tag(buf, "<host_info>")) {
|
|
|
|
host_info.parse(f);
|
|
|
|
} else if (match_tag(buf, "<time_stats>")) {
|
|
|
|
time_stats.parse(f);
|
|
|
|
} else if (match_tag(buf, "<net_stats>")) {
|
|
|
|
net_stats.parse(f);
|
|
|
|
} else if (match_tag(buf, "<active_task_set>")) {
|
|
|
|
active_tasks.parse(f, this);
|
2002-05-17 22:33:57 +00:00
|
|
|
} else if (match_tag(buf, "<platform_name>")) {
|
|
|
|
// should match out current platform name
|
|
|
|
} else if (match_tag(buf, "<version>")) {
|
|
|
|
// could put logic here to detect incompatible state files
|
|
|
|
// after core client update
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "CLIENT_STATE::parse_state_file: unrecognized: %s\n", buf);
|
2002-07-31 19:05:15 +00:00
|
|
|
return ERR_XML_PARSE;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-31 19:05:15 +00:00
|
|
|
return 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Make a directory for each of the projects present
|
|
|
|
// in the client state
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int CLIENT_STATE::make_project_dirs() {
|
|
|
|
unsigned int i;
|
|
|
|
for (i=0; i<projects.size(); i++) {
|
|
|
|
make_project_dir(*projects[i]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Make a directory for each of the available slots specified
|
|
|
|
// in the client state
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int CLIENT_STATE::make_slot_dirs() {
|
|
|
|
unsigned int i;
|
|
|
|
for (i=0; i<nslots; i++) {
|
|
|
|
make_slot_dir(i);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Perform a graceful shutdown of the client, including quitting
|
|
|
|
// all applications, checking their final status, and writing
|
|
|
|
// the client_state.xml file
|
|
|
|
//
|
2002-07-31 19:05:15 +00:00
|
|
|
int CLIENT_STATE::exit() {
|
2002-07-12 22:08:09 +00:00
|
|
|
int retval;
|
2002-07-11 01:09:53 +00:00
|
|
|
active_tasks.poll_time();
|
2002-07-12 22:08:09 +00:00
|
|
|
retval = write_state_file();
|
2002-07-31 19:05:15 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "error: CLIENT_STATE.exit: write_state_file failed\n");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
retval = exit_tasks();
|
2002-07-29 00:39:45 +00:00
|
|
|
if (retval) {
|
2002-07-31 19:05:15 +00:00
|
|
|
fprintf(stderr, "error: CLIENT_STATE.exit: exit_tasks failed\n");
|
2002-07-12 22:08:09 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2002-07-01 18:16:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-31 19:05:15 +00:00
|
|
|
int CLIENT_STATE::exit_tasks() {
|
|
|
|
active_tasks.exit_tasks();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-29 00:39:45 +00:00
|
|
|
// Write the client_state.xml file
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
int CLIENT_STATE::write_state_file() {
|
|
|
|
unsigned int i, j;
|
2002-06-21 00:29:08 +00:00
|
|
|
FILE* f = fopen(STATE_FILE_TEMP, "wb");
|
2002-04-30 22:22:54 +00:00
|
|
|
int retval;
|
|
|
|
|
2002-06-10 06:14:18 +00:00
|
|
|
if (log_flags.state_debug) {
|
|
|
|
printf("Writing state file\n");
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!f) {
|
|
|
|
fprintf(stderr, "can't open temp state file: %s\n", STATE_FILE_TEMP);
|
|
|
|
return ERR_FOPEN;
|
|
|
|
}
|
|
|
|
fprintf(f, "<client_state>\n");
|
|
|
|
host_info.write(f);
|
|
|
|
time_stats.write(f, false);
|
|
|
|
net_stats.write(f, false);
|
|
|
|
for (j=0; j<projects.size(); j++) {
|
|
|
|
PROJECT* p = projects[j];
|
2002-06-21 06:52:47 +00:00
|
|
|
p->write_state(f);
|
2002-04-30 22:22:54 +00:00
|
|
|
for (i=0; i<apps.size(); i++) {
|
|
|
|
if (apps[i]->project == p) apps[i]->write(f);
|
|
|
|
}
|
|
|
|
for (i=0; i<file_infos.size(); i++) {
|
|
|
|
if (file_infos[i]->project == p) {
|
|
|
|
file_infos[i]->write(f, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i=0; i<app_versions.size(); i++) {
|
|
|
|
if (app_versions[i]->project == p) app_versions[i]->write(f);
|
|
|
|
}
|
|
|
|
for (i=0; i<workunits.size(); i++) {
|
|
|
|
if (workunits[i]->project == p) workunits[i]->write(f);
|
|
|
|
}
|
|
|
|
for (i=0; i<results.size(); i++) {
|
|
|
|
if (results[i]->project == p) results[i]->write(f, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
active_tasks.write(f);
|
2002-05-17 22:33:57 +00:00
|
|
|
fprintf(f,
|
|
|
|
"<platform_name>%s</platform_name>\n"
|
|
|
|
"<version>%d</version>\n",
|
|
|
|
platform_name,
|
|
|
|
version
|
|
|
|
);
|
2002-04-30 22:22:54 +00:00
|
|
|
fprintf(f, "</client_state>\n");
|
|
|
|
fclose(f);
|
|
|
|
retval = rename(STATE_FILE_TEMP, STATE_FILE_NAME);
|
2002-06-10 06:14:18 +00:00
|
|
|
if (log_flags.state_debug) {
|
|
|
|
printf("Done writing state file\n");
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
if (retval) return ERR_RENAME;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// See if the project specified by master_url already exists
|
|
|
|
// in the client state record.
|
|
|
|
//
|
2002-06-21 06:52:47 +00:00
|
|
|
PROJECT* CLIENT_STATE::lookup_project(char* master_url) {
|
2002-04-30 22:22:54 +00:00
|
|
|
for (unsigned int i=0; i<projects.size(); i++) {
|
2002-06-21 06:52:47 +00:00
|
|
|
if (!strcmp(master_url, projects[i]->master_url)) {
|
2002-04-30 22:22:54 +00:00
|
|
|
return projects[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
APP* CLIENT_STATE::lookup_app(PROJECT* p, char* name) {
|
|
|
|
for (unsigned int i=0; i<apps.size(); i++) {
|
|
|
|
APP* app = apps[i];
|
|
|
|
if (app->project == p && !strcmp(name, app->name)) return app;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RESULT* CLIENT_STATE::lookup_result(PROJECT* p, char* name) {
|
|
|
|
for (unsigned int i=0; i<results.size(); i++) {
|
|
|
|
RESULT* rp = results[i];
|
|
|
|
if (rp->project == p && !strcmp(name, rp->name)) return rp;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
WORKUNIT* CLIENT_STATE::lookup_workunit(PROJECT* p, char* name) {
|
|
|
|
for (unsigned int i=0; i<workunits.size(); i++) {
|
|
|
|
WORKUNIT* wup = workunits[i];
|
|
|
|
if (wup->project == p && !strcmp(name, wup->name)) return wup;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
APP_VERSION* CLIENT_STATE::lookup_app_version(APP* app, int version_num) {
|
|
|
|
for (unsigned int i=0; i<app_versions.size(); i++) {
|
|
|
|
APP_VERSION* avp = app_versions[i];
|
|
|
|
if (avp->app == app && version_num==avp->version_num) {
|
|
|
|
return avp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE_INFO* CLIENT_STATE::lookup_file_info(PROJECT* p, char* name) {
|
|
|
|
for (unsigned int i=0; i<file_infos.size(); i++) {
|
|
|
|
FILE_INFO* fip = file_infos[i];
|
|
|
|
if (fip->project == p && !strcmp(fip->name, name)) {
|
|
|
|
return fip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// functions to create links between state objects
|
2002-05-17 22:33:57 +00:00
|
|
|
// (which, in their XML form, reference one another by name)
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
|
|
|
int CLIENT_STATE::link_app(PROJECT* p, APP* app) {
|
|
|
|
app->project = p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CLIENT_STATE::link_file_info(PROJECT* p, FILE_INFO* fip) {
|
|
|
|
fip->project = p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CLIENT_STATE::link_app_version(PROJECT* p, APP_VERSION* avp) {
|
|
|
|
APP* app;
|
|
|
|
FILE_INFO* fip;
|
2002-05-17 22:33:57 +00:00
|
|
|
FILE_REF file_ref;
|
|
|
|
unsigned int i;
|
2002-07-29 00:39:45 +00:00
|
|
|
|
2002-05-17 22:33:57 +00:00
|
|
|
avp->project = p;
|
2002-04-30 22:22:54 +00:00
|
|
|
app = lookup_app(p, avp->app_name);
|
|
|
|
if (!app) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"app_version refers to nonexistent app: %s\n", avp->app_name
|
|
|
|
);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
avp->app = app;
|
2002-05-17 22:33:57 +00:00
|
|
|
|
|
|
|
for (i=0; i<avp->app_files.size(); i++) {
|
|
|
|
file_ref = avp->app_files[i];
|
|
|
|
fip = lookup_file_info(p, file_ref.file_name);
|
|
|
|
if (!fip) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"app_version refers to nonexistent file: %s\n",
|
|
|
|
file_ref.file_name
|
|
|
|
);
|
|
|
|
return 1;
|
|
|
|
}
|
2002-07-07 20:39:24 +00:00
|
|
|
|
|
|
|
// any file associated with an app version must be signed
|
|
|
|
//
|
|
|
|
fip->signature_required = true;
|
2002-05-17 22:33:57 +00:00
|
|
|
avp->app_files[i].file_info = fip;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-05-17 22:33:57 +00:00
|
|
|
int CLIENT_STATE::link_file_ref(PROJECT* p, FILE_REF* file_refp) {
|
2002-04-30 22:22:54 +00:00
|
|
|
FILE_INFO* fip;
|
2002-07-29 00:39:45 +00:00
|
|
|
|
2002-05-17 22:33:57 +00:00
|
|
|
fip = lookup_file_info(p, file_refp->file_name);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!fip) {
|
|
|
|
fprintf(stderr,
|
2002-05-17 22:33:57 +00:00
|
|
|
"I/O desc links to nonexistent file: %s\n", file_refp->file_name
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
|
|
|
return 1;
|
|
|
|
}
|
2002-05-17 22:33:57 +00:00
|
|
|
file_refp->file_info = fip;
|
2002-04-30 22:22:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CLIENT_STATE::link_workunit(PROJECT* p, WORKUNIT* wup) {
|
|
|
|
APP* app;
|
|
|
|
APP_VERSION* avp;
|
|
|
|
unsigned int i;
|
|
|
|
int retval;
|
2002-07-29 00:39:45 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
app = lookup_app(p, wup->app_name);
|
|
|
|
if (!app) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"WU refers to nonexistent app: %s\n", wup->app_name
|
|
|
|
);
|
|
|
|
return 1;
|
|
|
|
}
|
2002-07-29 00:39:45 +00:00
|
|
|
avp = lookup_app_version(app, wup->version_num);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (!avp) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"WU refers to nonexistent app_version: %s %d\n",
|
|
|
|
wup->app_name, wup->version_num
|
|
|
|
);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
wup->project = p;
|
|
|
|
wup->app = app;
|
|
|
|
wup->avp = avp;
|
|
|
|
for (i=0; i<wup->input_files.size(); i++) {
|
2002-05-17 22:33:57 +00:00
|
|
|
retval = link_file_ref(p, &wup->input_files[i]);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2002-07-29 00:39:45 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
int CLIENT_STATE::link_result(PROJECT* p, RESULT* rp) {
|
|
|
|
WORKUNIT* wup;
|
|
|
|
unsigned int i;
|
|
|
|
int retval;
|
2002-07-29 00:39:45 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
wup = lookup_workunit(p, rp->wu_name);
|
|
|
|
if (!wup) {
|
|
|
|
fprintf(stderr, "result refers to nonexistent WU: %s\n", rp->wu_name);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
rp->project = p;
|
|
|
|
rp->wup = wup;
|
|
|
|
rp->app = wup->app;
|
|
|
|
for (i=0; i<rp->output_files.size(); i++) {
|
2002-05-17 22:33:57 +00:00
|
|
|
retval = link_file_ref(p, &rp->output_files[i]);
|
2002-07-31 19:05:15 +00:00
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "error: link_result: link_file_ref failed\n");
|
|
|
|
return retval;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-29 00:39:45 +00:00
|
|
|
int CLIENT_STATE::latest_version_num(char* app_name) {
|
|
|
|
unsigned int i;
|
|
|
|
int best = -1;
|
|
|
|
APP_VERSION* avp;
|
|
|
|
|
|
|
|
for (i=0; i<app_versions.size(); i++) {
|
|
|
|
avp = app_versions[i];
|
|
|
|
if (strcmp(avp->app_name, app_name)) continue;
|
|
|
|
if (avp->version_num < best) continue;
|
|
|
|
best = avp->version_num;
|
|
|
|
}
|
|
|
|
if (best < 0) fprintf(stderr, "CLIENT_STATE::latest_version_num: no version\n");
|
|
|
|
return best;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Print debugging information about how many projects/files/etc
|
|
|
|
// are currently in the client state record
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
void CLIENT_STATE::print_counts() {
|
|
|
|
if (log_flags.state_debug) {
|
|
|
|
printf(
|
|
|
|
"Client state file:\n"
|
|
|
|
"%d projects\n"
|
|
|
|
"%d file_infos\n"
|
|
|
|
"%d app_versions\n"
|
|
|
|
"%d workunits\n"
|
|
|
|
"%d results\n",
|
|
|
|
projects.size(),
|
|
|
|
file_infos.size(),
|
|
|
|
app_versions.size(),
|
|
|
|
workunits.size(),
|
|
|
|
results.size()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete unneeded records and files
|
|
|
|
//
|
|
|
|
bool CLIENT_STATE::garbage_collect() {
|
|
|
|
unsigned int i;
|
2002-08-07 22:52:10 +00:00
|
|
|
PERS_FILE_XFER* pfxp;
|
2002-04-30 22:22:54 +00:00
|
|
|
FILE_INFO* fip;
|
|
|
|
RESULT* rp;
|
|
|
|
WORKUNIT* wup;
|
2002-08-07 22:52:10 +00:00
|
|
|
vector<PERS_FILE_XFER*>::iterator pers_iter;
|
2002-04-30 22:22:54 +00:00
|
|
|
vector<RESULT*>::iterator result_iter;
|
|
|
|
vector<WORKUNIT*>::iterator wu_iter;
|
|
|
|
vector<FILE_INFO*>::iterator fi_iter;
|
|
|
|
bool action = false;
|
|
|
|
|
|
|
|
// zero references counts on WUs and FILE_INFOs
|
|
|
|
for (i=0; i<workunits.size(); i++) {
|
|
|
|
wup = workunits[i];
|
|
|
|
wup->ref_cnt = 0;
|
|
|
|
}
|
|
|
|
for (i=0; i<file_infos.size(); i++) {
|
|
|
|
fip = file_infos[i];
|
|
|
|
fip->ref_cnt = 0;
|
|
|
|
}
|
|
|
|
|
2002-08-07 22:52:10 +00:00
|
|
|
// delete PERS_FILE_XFERs that have finished and their
|
|
|
|
// associated FILE_INFO and FILE_XFER objects
|
|
|
|
//
|
|
|
|
pers_iter = pers_xfers->pers_file_xfers.begin();
|
|
|
|
while (pers_iter != pers_xfers->pers_file_xfers.end()) {
|
|
|
|
pfxp = *pers_iter;
|
|
|
|
if (pfxp->xfer_done) {
|
|
|
|
// TODO: *** Set the exit status of the related result
|
|
|
|
// to ERR_GIVEUP. The failure will be reported to the
|
|
|
|
// server and related file infos, results, and workunits
|
|
|
|
// will be deleted if necessary
|
|
|
|
|
|
|
|
pers_iter = pers_xfers->pers_file_xfers.erase(pers_iter);
|
|
|
|
if (pfxp->fip) {
|
|
|
|
pfxp->fip->pers_file_xfer = NULL;
|
|
|
|
}
|
|
|
|
delete pfxp;
|
|
|
|
|
|
|
|
// Update the client_state file
|
|
|
|
client_state_dirty = true;
|
|
|
|
action = true;
|
|
|
|
} else {
|
|
|
|
pers_iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
// delete RESULTs that have been finished and reported;
|
|
|
|
// reference-count files referred to by other results
|
|
|
|
//
|
|
|
|
result_iter = results.begin();
|
|
|
|
while (result_iter != results.end()) {
|
|
|
|
rp = *result_iter;
|
|
|
|
if (rp->is_server_ack) {
|
|
|
|
if (log_flags.state_debug) printf("deleting result %s\n", rp->name);
|
|
|
|
delete rp;
|
|
|
|
result_iter = results.erase(result_iter);
|
|
|
|
action = true;
|
|
|
|
} else {
|
|
|
|
rp->wup->ref_cnt++;
|
|
|
|
for (i=0; i<rp->output_files.size(); i++) {
|
|
|
|
rp->output_files[i].file_info->ref_cnt++;
|
|
|
|
}
|
|
|
|
result_iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete WORKUNITs not referenced by any result;
|
|
|
|
// reference-count files referred to by other WUs
|
|
|
|
//
|
|
|
|
wu_iter = workunits.begin();
|
|
|
|
while (wu_iter != workunits.end()) {
|
|
|
|
wup = *wu_iter;
|
|
|
|
if (wup->ref_cnt == 0) {
|
2002-06-10 06:14:18 +00:00
|
|
|
if (log_flags.state_debug) {
|
|
|
|
printf("deleting workunit %s\n", wup->name);
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
delete wup;
|
|
|
|
wu_iter = workunits.erase(wu_iter);
|
|
|
|
action = true;
|
|
|
|
} else {
|
|
|
|
for (i=0; i<wup->input_files.size(); i++) {
|
|
|
|
wup->input_files[i].file_info->ref_cnt++;
|
|
|
|
}
|
|
|
|
wu_iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete FILE_INFOs (and corresponding files)
|
|
|
|
// that are not referenced by any WORKUNIT or RESULT,
|
|
|
|
// and are not sticky.
|
|
|
|
//
|
|
|
|
fi_iter = file_infos.begin();
|
|
|
|
while (fi_iter != file_infos.end()) {
|
|
|
|
fip = *fi_iter;
|
|
|
|
if (fip->ref_cnt==0 && !fip->sticky && !fip->executable) {
|
|
|
|
fip->delete_file();
|
|
|
|
if (log_flags.state_debug) printf("deleting file %s\n", fip->name);
|
|
|
|
delete fip;
|
|
|
|
fi_iter = file_infos.erase(fi_iter);
|
|
|
|
action = true;
|
|
|
|
} else {
|
|
|
|
fi_iter++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: delete obsolete APP_VERSIONs
|
|
|
|
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: write no more often than X seconds
|
2002-08-07 22:52:10 +00:00
|
|
|
// Write the client_state.xml file if necessary
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
|
|
|
int CLIENT_STATE::write_state_file_if_needed() {
|
|
|
|
int retval;
|
|
|
|
if (client_state_dirty) {
|
|
|
|
retval = write_state_file();
|
|
|
|
if (retval) return retval;
|
|
|
|
client_state_dirty = false;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-29 00:39:45 +00:00
|
|
|
// Parse the command line arguments passed to the client
|
2002-07-15 23:21:20 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
void CLIENT_STATE::parse_cmdline(int argc, char** argv) {
|
|
|
|
int i;
|
2002-07-29 00:39:45 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-exit_when_idle")) {
|
|
|
|
exit_when_idle = true;
|
2002-08-07 22:52:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-06-28 18:32:18 +00:00
|
|
|
if (!strcmp(argv[i], "-no_time_test")) {
|
|
|
|
run_time_test = false;
|
2002-07-01 18:16:31 +00:00
|
|
|
continue;
|
|
|
|
};
|
|
|
|
if (!strcmp(argv[i], "-exit_after")) {
|
2002-08-07 22:52:10 +00:00
|
|
|
exit_after = atoi(argv[i+1]);
|
|
|
|
continue;
|
|
|
|
};
|
|
|
|
// Give up on file transfers after x seconds. Default value is 1209600 (2 weeks)
|
|
|
|
if (!strcmp(argv[i], "-giveup_after")) {
|
|
|
|
giveup_after = atoi(argv[i+1]);
|
|
|
|
continue;
|
|
|
|
};
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Returns true if the core client should exit
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
bool CLIENT_STATE::time_to_exit() {
|
2002-07-31 19:05:15 +00:00
|
|
|
if (!exit_when_idle && (exit_after == -1)) return false;
|
|
|
|
if ((exit_after != -1) && app_started &&
|
|
|
|
(difftime(time(0), app_started) >= exit_after)) return true;
|
|
|
|
if (exit_when_idle && (results.size() == 0) && contacted_sched_server) {
|
|
|
|
return true;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
return false;
|
|
|
|
}
|