2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-05-12 21:39:23 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC 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 3 of the License, or (at your option) any later version.
|
2005-01-20 23:22:22 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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.
|
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2004-05-12 21:39:23 +00:00
|
|
|
|
2004-05-12 21:46:35 +00:00
|
|
|
#include "cpp.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
|
2004-05-12 21:46:35 +00:00
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2011-09-21 19:18:22 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define strdup _strdup
|
|
|
|
#endif
|
2005-11-21 18:34:44 +00:00
|
|
|
#else
|
|
|
|
#include "config.h"
|
2010-05-11 19:10:29 +00:00
|
|
|
#include <cstring>
|
2004-05-12 21:46:35 +00:00
|
|
|
#endif
|
|
|
|
|
2004-05-12 21:39:23 +00:00
|
|
|
#include "error_numbers.h"
|
|
|
|
#include "file_names.h"
|
|
|
|
#include "filesys.h"
|
|
|
|
#include "parse.h"
|
2007-06-22 20:17:08 +00:00
|
|
|
#include "util.h"
|
2013-06-07 00:31:46 +00:00
|
|
|
#include "str_replace.h"
|
2007-02-21 16:26:51 +00:00
|
|
|
#include "str_util.h"
|
2012-04-30 21:00:28 +00:00
|
|
|
|
2011-09-22 17:30:57 +00:00
|
|
|
#include "client_msgs.h"
|
2012-04-30 21:00:28 +00:00
|
|
|
#include "client_state.h"
|
|
|
|
#include "project.h"
|
|
|
|
#include "sandbox.h"
|
2004-05-12 21:39:23 +00:00
|
|
|
|
|
|
|
// Scan project dir for file names of the form trickle_up_X_Y
|
|
|
|
// where X is a result name and Y is a timestamp.
|
|
|
|
// Convert them to XML (for sched request message)
|
|
|
|
//
|
|
|
|
int CLIENT_STATE::read_trickle_files(PROJECT* project, FILE* f) {
|
2013-04-18 20:57:33 +00:00
|
|
|
char *p, *q, result_name[256], fname[256];
|
2012-05-09 16:11:50 +00:00
|
|
|
char* file_contents, path[MAXPATHLEN], newpath[MAXPATHLEN];
|
2004-05-12 21:39:23 +00:00
|
|
|
string fn;
|
|
|
|
time_t t;
|
|
|
|
int retval;
|
|
|
|
|
2013-04-18 20:57:33 +00:00
|
|
|
DirScanner ds(project->project_dir());
|
2004-05-12 21:39:23 +00:00
|
|
|
|
2005-03-09 00:16:38 +00:00
|
|
|
// trickle-up filenames are of the form trickle_up_RESULTNAME_TIME[.sent]
|
|
|
|
//
|
2017-04-30 23:40:02 +00:00
|
|
|
const size_t prefix_len = strlen("trickle_up_");
|
2004-05-12 21:39:23 +00:00
|
|
|
while (ds.scan(fn)) {
|
2013-06-04 03:24:48 +00:00
|
|
|
safe_strcpy(fname, fn.c_str());
|
2004-05-12 22:15:14 +00:00
|
|
|
if (strstr(fname, "trickle_up_") != fname) continue;
|
2017-04-30 23:40:02 +00:00
|
|
|
q = fname + prefix_len;
|
2004-05-12 21:39:23 +00:00
|
|
|
p = strrchr(fname, '_');
|
|
|
|
if (p <= q) continue;
|
|
|
|
*p = 0;
|
2013-06-04 03:24:48 +00:00
|
|
|
safe_strcpy(result_name, q);
|
2004-05-12 22:15:14 +00:00
|
|
|
*p = '_';
|
2004-05-12 21:39:23 +00:00
|
|
|
t = atoi(p+1);
|
|
|
|
|
2016-02-18 05:59:13 +00:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", project->project_dir(), fname);
|
2004-05-12 21:39:23 +00:00
|
|
|
retval = read_file_malloc(path, file_contents);
|
2014-07-26 00:08:31 +00:00
|
|
|
if (retval) {
|
|
|
|
if (log_flags.trickle_debug) {
|
|
|
|
msg_printf(project, MSG_INFO,
|
|
|
|
"[trickle] can't read trickle file %s", path
|
|
|
|
);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (log_flags.trickle_debug) {
|
|
|
|
msg_printf(project, MSG_INFO,
|
|
|
|
"[trickle] read trickle file %s", path
|
|
|
|
);
|
|
|
|
}
|
2004-05-12 21:39:23 +00:00
|
|
|
fprintf(f,
|
2004-07-06 04:01:15 +00:00
|
|
|
" <msg_from_host>\n"
|
2004-05-13 04:43:42 +00:00
|
|
|
" <result_name>%s</result_name>\n"
|
2004-05-12 21:39:23 +00:00
|
|
|
" <time>%d</time>\n"
|
|
|
|
"%s\n"
|
2004-07-06 04:01:15 +00:00
|
|
|
" </msg_from_host>\n",
|
2004-05-12 21:39:23 +00:00
|
|
|
result_name,
|
|
|
|
(int)t,
|
|
|
|
file_contents
|
|
|
|
);
|
2011-09-22 17:30:57 +00:00
|
|
|
send_replicated_trickles(project, file_contents, result_name, t);
|
2004-05-12 21:39:23 +00:00
|
|
|
free(file_contents);
|
2005-03-09 00:16:38 +00:00
|
|
|
|
|
|
|
// append .sent to filename, so we'll know which ones to delete later
|
|
|
|
//
|
|
|
|
if (!ends_with(fname, ".sent")) {
|
2016-02-18 05:59:13 +00:00
|
|
|
snprintf(newpath, sizeof(newpath), "%s/%s.sent", project->project_dir(), fname);
|
2005-03-09 00:16:38 +00:00
|
|
|
boinc_rename(path, newpath);
|
|
|
|
}
|
2004-05-12 21:39:23 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-10-04 16:00:28 +00:00
|
|
|
// Remove files when ack has been received.
|
|
|
|
// Remove only those ending with ".sent"
|
2005-03-09 00:16:38 +00:00
|
|
|
// (others arrived from application while RPC was happening)
|
2004-05-12 21:39:23 +00:00
|
|
|
//
|
|
|
|
int CLIENT_STATE::remove_trickle_files(PROJECT* project) {
|
2013-04-18 20:57:33 +00:00
|
|
|
char path[MAXPATHLEN], fname[256];
|
2004-05-12 21:39:23 +00:00
|
|
|
string fn;
|
|
|
|
|
2013-04-18 20:57:33 +00:00
|
|
|
DirScanner ds(project->project_dir());
|
2004-05-12 21:39:23 +00:00
|
|
|
|
|
|
|
while (ds.scan(fn)) {
|
2013-06-04 03:24:48 +00:00
|
|
|
safe_strcpy(fname, fn.c_str());
|
2005-03-09 00:16:38 +00:00
|
|
|
if (!starts_with(fname, "trickle_up")) continue;
|
|
|
|
if (!ends_with(fname, ".sent")) continue;
|
2016-02-18 05:59:13 +00:00
|
|
|
snprintf(path, sizeof(path), "%s/%s", project->project_dir(), fname);
|
2008-01-15 19:12:43 +00:00
|
|
|
delete_project_owned_file(path, true);
|
2004-05-12 21:39:23 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse a trickle-down message in a scheduler reply.
|
|
|
|
// Locate the corresponding active task,
|
|
|
|
// write a file in the slot directory,
|
|
|
|
// and notify the task
|
|
|
|
//
|
|
|
|
int CLIENT_STATE::handle_trickle_down(PROJECT* project, FILE* in) {
|
|
|
|
char buf[256];
|
2012-05-09 16:11:50 +00:00
|
|
|
char result_name[256], path[MAXPATHLEN];
|
2004-05-12 21:39:23 +00:00
|
|
|
string body;
|
2006-07-28 20:32:27 +00:00
|
|
|
int send_time=0;
|
2004-05-12 21:39:23 +00:00
|
|
|
|
2013-06-04 03:24:48 +00:00
|
|
|
safe_strcpy(result_name, "");
|
2004-05-12 21:39:23 +00:00
|
|
|
while (fgets(buf, 256, in)) {
|
|
|
|
if (match_tag(buf, "</trickle_down>")) {
|
|
|
|
RESULT* rp = lookup_result(project, result_name);
|
|
|
|
if (!rp) return ERR_NULL;
|
|
|
|
ACTIVE_TASK* atp = lookup_active_task_by_result(rp);
|
|
|
|
if (!atp) return ERR_NULL;
|
2016-02-18 05:59:13 +00:00
|
|
|
snprintf(path, sizeof(path), "%s/trickle_down_%d", atp->slot_dir, send_time);
|
2004-05-12 21:39:23 +00:00
|
|
|
FILE* f = fopen(path, "w");
|
|
|
|
if (!f) return ERR_FOPEN;
|
|
|
|
fputs(body.c_str(), f);
|
|
|
|
fclose(f);
|
2004-07-02 04:49:17 +00:00
|
|
|
atp->have_trickle_down = true;
|
2004-05-12 21:39:23 +00:00
|
|
|
return 0;
|
|
|
|
} else if (parse_str(buf, "<result_name>", result_name, 256)) {
|
|
|
|
continue;
|
2004-07-06 04:01:15 +00:00
|
|
|
} else if (parse_int(buf, "<time>", send_time)) {
|
2004-05-12 21:39:23 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
2004-07-06 04:01:15 +00:00
|
|
|
body += buf;
|
2004-05-12 21:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2011-09-20 18:49:38 +00:00
|
|
|
/////////////// STUFF RELATED TO REPLICATED TRICKLES FOLLOWS //////////////
|
|
|
|
|
|
|
|
bool trickle_up_poll() {
|
|
|
|
unsigned int i, j;
|
|
|
|
for (i=0; i<gstate.projects.size(); i++) {
|
|
|
|
PROJECT* p = gstate.projects[i];
|
|
|
|
for (j=0; j<p->trickle_up_ops.size(); j++) {
|
|
|
|
TRICKLE_UP_OP *t = p->trickle_up_ops[j];
|
2011-09-22 17:30:57 +00:00
|
|
|
t->gui_http->poll();
|
2011-09-20 18:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-22 17:30:57 +00:00
|
|
|
return false;
|
2011-09-20 18:49:38 +00:00
|
|
|
}
|
|
|
|
|
2011-09-22 17:30:57 +00:00
|
|
|
static void trickle_up_request_message(
|
2016-07-17 07:24:44 +00:00
|
|
|
PROJECT* p, const char* msg, char* result_name, int t, char* buf, size_t len
|
2011-09-22 17:30:57 +00:00
|
|
|
) {
|
2016-02-18 05:59:13 +00:00
|
|
|
snprintf(buf, len,
|
2011-09-22 17:30:57 +00:00
|
|
|
"<scheduler_request>\n"
|
|
|
|
" <authenticator>%s</authenticator>\n"
|
|
|
|
" <hostid>%d</hostid>\n"
|
|
|
|
" <rpc_seqno>%d</rpc_seqno>\n"
|
|
|
|
" <core_client_major_version>%d</core_client_major_version>\n"
|
|
|
|
" <core_client_minor_version>%d</core_client_minor_version>\n"
|
|
|
|
" <core_client_release>%d</core_client_release>\n"
|
|
|
|
" <platform_name>%s</platform_name>\n"
|
|
|
|
" <msg_from_host>\n"
|
|
|
|
" <result_name>%s</result_name>\n"
|
|
|
|
" <time>%d</time>\n"
|
|
|
|
"%s\n"
|
|
|
|
" </msg_from_host>\n"
|
|
|
|
"</scheduler_request>\n",
|
|
|
|
p->authenticator,
|
|
|
|
p->hostid,
|
|
|
|
p->rpc_seqno,
|
|
|
|
gstate.core_client_version.major,
|
|
|
|
gstate.core_client_version.minor,
|
|
|
|
//gstate.core_client_version.release,
|
|
|
|
99,
|
|
|
|
gstate.get_primary_platform(),
|
|
|
|
result_name,
|
|
|
|
t,
|
|
|
|
msg
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-10-01 18:02:49 +00:00
|
|
|
void send_replicated_trickles(
|
|
|
|
PROJECT* p, const char* msg, char* result_name, int now
|
|
|
|
) {
|
2011-11-07 03:46:03 +00:00
|
|
|
if (!p->trickle_up_ops.size()) return;
|
2016-07-17 07:24:44 +00:00
|
|
|
size_t trickle_len = strlen(msg) + 4096;
|
2016-02-18 05:59:13 +00:00
|
|
|
char *buf = (char*)malloc(trickle_len);
|
2011-11-07 03:46:03 +00:00
|
|
|
if (!buf) return;
|
2016-02-18 05:59:13 +00:00
|
|
|
trickle_up_request_message(p, msg, result_name, now, buf, trickle_len);
|
2011-09-20 18:49:38 +00:00
|
|
|
for (unsigned int i=0; i<p->trickle_up_ops.size(); i++) {
|
|
|
|
TRICKLE_UP_OP *t = p->trickle_up_ops[i];
|
2011-09-22 17:30:57 +00:00
|
|
|
if (t->gui_http->is_busy()) {
|
2011-09-22 18:52:21 +00:00
|
|
|
if (log_flags.trickle_debug) {
|
|
|
|
msg_printf(p, MSG_INFO,
|
|
|
|
"[trickle] Trickle channel to %s is busy", t->url.c_str()
|
|
|
|
);
|
|
|
|
}
|
2011-11-07 03:46:03 +00:00
|
|
|
continue;
|
2011-09-22 17:30:57 +00:00
|
|
|
}
|
2011-09-22 18:52:21 +00:00
|
|
|
if (log_flags.trickle_debug) {
|
|
|
|
msg_printf(p, MSG_INFO,
|
|
|
|
"[trickle] Sending replicated trickle to %s", t->url.c_str()
|
|
|
|
);
|
|
|
|
}
|
2011-09-22 17:30:57 +00:00
|
|
|
t->do_rpc(buf);
|
2011-09-20 18:49:38 +00:00
|
|
|
}
|
2011-11-07 03:46:03 +00:00
|
|
|
free(buf);
|
2011-09-20 18:49:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A scheduler reply gave us a list of trickle handler URLs.
|
2011-09-22 05:39:42 +00:00
|
|
|
// Add and remove as needed.
|
2011-09-20 18:49:38 +00:00
|
|
|
//
|
|
|
|
void update_trickle_up_urls(PROJECT* p, vector<string> &urls) {
|
|
|
|
unsigned int i, j;
|
2011-09-22 05:39:42 +00:00
|
|
|
|
|
|
|
// add new URLs
|
|
|
|
//
|
2011-09-20 18:49:38 +00:00
|
|
|
for (i=0; i<urls.size(); i++) {
|
|
|
|
string& url = urls[i];
|
|
|
|
bool found = false;
|
|
|
|
for (j=0; j<p->trickle_up_ops.size(); j++) {
|
|
|
|
TRICKLE_UP_OP *t = p->trickle_up_ops[j];
|
|
|
|
if (t->url == url) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
2011-09-22 05:39:42 +00:00
|
|
|
p->trickle_up_ops.push_back(new TRICKLE_UP_OP(url));
|
2011-09-20 18:49:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-09-22 05:39:42 +00:00
|
|
|
|
|
|
|
// remove old URLs
|
|
|
|
//
|
|
|
|
vector<TRICKLE_UP_OP*>::iterator iter = p->trickle_up_ops.begin();
|
|
|
|
while (iter != p->trickle_up_ops.end()) {
|
|
|
|
TRICKLE_UP_OP *t = *iter;
|
|
|
|
bool found = false;
|
|
|
|
for (i=0; i<urls.size(); i++) {
|
|
|
|
string& url = urls[i];
|
|
|
|
if (t->url == url) {
|
|
|
|
found = true;
|
2011-09-20 18:49:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-09-22 05:39:42 +00:00
|
|
|
if (!found) {
|
|
|
|
gstate.http_ops->remove(&(t->gui_http->http_op));
|
|
|
|
delete t;
|
|
|
|
iter = p->trickle_up_ops.erase(iter);
|
|
|
|
} else {
|
2015-01-14 20:18:29 +00:00
|
|
|
++iter;
|
2011-09-22 05:39:42 +00:00
|
|
|
}
|
2011-09-20 18:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-22 17:30:57 +00:00
|
|
|
int TRICKLE_UP_OP::do_rpc(const char* msg) {
|
2011-11-03 02:35:04 +00:00
|
|
|
int n = (int)strlen(msg)+1;
|
2011-11-06 06:25:48 +00:00
|
|
|
if (n<65536) n = 65536; // make it big enough to handle the reply
|
2011-09-22 18:52:21 +00:00
|
|
|
req_buf = (char*)malloc(n);
|
2016-02-16 05:14:52 +00:00
|
|
|
strlcpy(req_buf, msg, n);
|
2011-09-20 18:49:38 +00:00
|
|
|
int retval = gui_http->do_rpc_post_str(
|
2011-09-22 18:52:21 +00:00
|
|
|
this, const_cast<char*>(url.c_str()), req_buf, n
|
2011-09-20 18:49:38 +00:00
|
|
|
);
|
|
|
|
if (retval) {
|
2011-09-22 18:52:21 +00:00
|
|
|
msg_printf(0, MSG_INTERNAL_ERROR, "Trickle-up post failed: %d", retval);
|
2011-09-20 18:49:38 +00:00
|
|
|
free(req_buf);
|
|
|
|
req_buf = 0;
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2016-12-16 20:12:00 +00:00
|
|
|
int parse_trickle_up_urls(XML_PARSER& xp, vector<string>& urls) {
|
2011-09-20 18:49:38 +00:00
|
|
|
string s;
|
|
|
|
while (!xp.get_tag()) {
|
|
|
|
if (xp.match_tag("/trickle_up_urls")) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (xp.parse_string("url", s)) {
|
|
|
|
urls.push_back(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ERR_XML_PARSE;
|
|
|
|
}
|
|
|
|
|
2011-09-22 17:30:57 +00:00
|
|
|
void TRICKLE_UP_OP::handle_reply(int http_op_retval) {
|
2011-09-22 18:52:21 +00:00
|
|
|
if (log_flags.trickle_debug) {
|
|
|
|
msg_printf(0, MSG_INFO,
|
|
|
|
"[trickle] Replicated trickle done; retval %d", http_op_retval
|
|
|
|
);
|
|
|
|
}
|
2011-09-22 17:30:57 +00:00
|
|
|
if (req_buf) {
|
|
|
|
free(req_buf);
|
|
|
|
req_buf = 0;
|
|
|
|
}
|
2016-09-01 16:20:08 +00:00
|
|
|
}
|