2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-02-10 20:31:11 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2005-02-10 20:31:11 +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-02-10 20:31:11 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-02-10 20:31:11 +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/>.
|
2005-02-10 20:31:11 +00:00
|
|
|
|
|
|
|
// Scheduler code for directing a client to one of several
|
|
|
|
// download servers based on its time zone
|
|
|
|
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2012-05-09 16:11:50 +00:00
|
|
|
#include <sys/param.h>
|
2005-02-10 20:31:11 +00:00
|
|
|
#include <string>
|
2009-02-26 00:23:23 +00:00
|
|
|
#include <cstdio>
|
2008-02-27 23:26:38 +00:00
|
|
|
#include <cstring>
|
2005-02-10 20:31:11 +00:00
|
|
|
|
|
|
|
#include "parse.h"
|
|
|
|
|
2009-08-10 04:49:02 +00:00
|
|
|
#include "sched_types.h"
|
2005-02-10 20:31:11 +00:00
|
|
|
#include "sched_msgs.h"
|
2009-03-02 23:47:11 +00:00
|
|
|
#include "sched_config.h"
|
2005-02-10 20:31:11 +00:00
|
|
|
|
|
|
|
#ifdef _USING_FCGI_
|
2008-09-09 19:10:42 +00:00
|
|
|
#include "boinc_fcgi.h"
|
2005-02-10 20:31:11 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef struct urltag {
|
|
|
|
int zone;
|
|
|
|
char name[124];
|
|
|
|
} URLTYPE;
|
|
|
|
|
|
|
|
|
|
|
|
// these global variables are needed to pass information into the
|
|
|
|
// compare function below.
|
|
|
|
//
|
|
|
|
static int tzone=0;
|
|
|
|
static int hostid=0;
|
|
|
|
|
|
|
|
// Evaluate differences between time-zone. Two time zones that differ
|
|
|
|
// by almost 24 hours are actually very close on the surface of the
|
|
|
|
// earth. This function finds the 'shortest way around'
|
|
|
|
//
|
|
|
|
static int compare(const void *x, const void *y) {
|
2005-02-16 23:17:43 +00:00
|
|
|
const URLTYPE *a=(const URLTYPE *)x;
|
|
|
|
const URLTYPE *b=(const URLTYPE *)y;
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
char longname[512];
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
const int twelve_hours = 12*3600;
|
|
|
|
|
|
|
|
int diffa = abs(tzone - (a->zone));
|
|
|
|
int diffb = abs(tzone - (b->zone));
|
|
|
|
|
|
|
|
if (diffa > twelve_hours) {
|
|
|
|
diffa = 2*twelve_hours-diffa;
|
|
|
|
}
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
if (diffb > twelve_hours) {
|
|
|
|
diffb = 2*twelve_hours-diffb;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (diffa < diffb) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
if (diffa > diffb) {
|
|
|
|
return +1;
|
|
|
|
}
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
// In order to ensure uniform distribution, we hash paths that are
|
|
|
|
// equidistant from the host's timezone in a way that gives a
|
|
|
|
// unique ordering for each host but which is effectively random
|
|
|
|
// between hosts.
|
|
|
|
//
|
|
|
|
sprintf(longname, "%s%d", a->name, hostid);
|
|
|
|
std::string sa = md5_string((const unsigned char *)longname, strlen((const char *)longname));
|
|
|
|
sprintf(longname, "%s%d", b->name, hostid);
|
|
|
|
std::string sb = md5_string((const unsigned char *)longname, strlen((const char *)longname));
|
|
|
|
int xa = strtol(sa.substr(1, 7).c_str(), 0, 16);
|
|
|
|
int xb = strtol(sb.substr(1, 7).c_str(), 0, 16);
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
if (xa<xb) {
|
|
|
|
return -1;
|
|
|
|
}
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
if (xa>xb) {
|
|
|
|
return 1;
|
|
|
|
}
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static URLTYPE *cached=NULL;
|
|
|
|
#define BLOCKSIZE 32
|
|
|
|
|
|
|
|
URLTYPE* read_download_list() {
|
|
|
|
int count=0;
|
|
|
|
int i;
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
if (cached) return cached;
|
2008-09-09 19:10:42 +00:00
|
|
|
|
2009-05-07 13:54:51 +00:00
|
|
|
const char *download_servers = config.project_path("download_servers");
|
2008-09-09 19:10:42 +00:00
|
|
|
#ifndef _USING_FCGI_
|
2009-05-07 13:54:51 +00:00
|
|
|
FILE *fp=fopen(download_servers, "r");
|
2011-01-07 20:23:22 +00:00
|
|
|
#else
|
2009-05-07 13:54:51 +00:00
|
|
|
FCGI_FILE *fp=FCGI::fopen(download_servers, "r");
|
2008-09-09 19:10:42 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!fp) {
|
2008-02-21 21:00:58 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL,
|
2009-05-07 13:54:51 +00:00
|
|
|
"File %s not found or unreadable!\n", download_servers
|
2005-02-10 20:31:11 +00:00
|
|
|
);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read in lines from file
|
|
|
|
while (1) {
|
|
|
|
// allocate memory in blocks
|
|
|
|
if ((count % BLOCKSIZE)==0) {
|
|
|
|
cached=(URLTYPE *)realloc(cached, (count+BLOCKSIZE)*sizeof(URLTYPE));
|
|
|
|
if (!cached) return NULL;
|
|
|
|
}
|
|
|
|
// read timezone offset and URL from file, and store in cache
|
|
|
|
// list
|
2008-09-09 19:10:42 +00:00
|
|
|
if (2==fscanf(fp, "%d %s", &(cached[count].zone), cached[count].name)) {
|
2005-02-10 20:31:11 +00:00
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
// provide a null terminator so we don't need to keep
|
|
|
|
// another global variable for count.
|
|
|
|
//
|
|
|
|
cached[count].name[0]='\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(fp);
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
if (!count) {
|
2008-02-21 21:00:58 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL,
|
2009-05-07 13:54:51 +00:00
|
|
|
"File %s contained no valid entries!\n"
|
2005-02-10 20:31:11 +00:00
|
|
|
"Format of this file is one or more lines containing:\n"
|
2009-05-07 13:54:51 +00:00
|
|
|
"TIMEZONE_OFFSET_IN_SEC http://some.url.path\n",
|
2010-07-30 21:43:23 +00:00
|
|
|
download_servers
|
2005-02-10 20:31:11 +00:00
|
|
|
);
|
|
|
|
free(cached);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
// sort URLs by distance from host timezone. See compare() above
|
|
|
|
// for details.
|
|
|
|
qsort(cached, count, sizeof(URLTYPE), compare);
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2008-02-21 21:00:58 +00:00
|
|
|
log_messages.printf(MSG_DEBUG,
|
2005-09-26 23:28:48 +00:00
|
|
|
"Sorted list of URLs follows [host timezone: UTC%+d]\n",
|
2005-02-10 20:31:11 +00:00
|
|
|
tzone
|
|
|
|
);
|
|
|
|
for (i=0; i<count; i++) {
|
2008-02-21 21:00:58 +00:00
|
|
|
log_messages.printf(MSG_DEBUG,
|
2005-09-26 23:28:48 +00:00
|
|
|
"zone=%+06d url=%s\n", cached[i].zone, cached[i].name
|
2005-02-10 20:31:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
return cached;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return number of bytes written, or <0 to indicate an error
|
|
|
|
//
|
2010-07-30 21:43:23 +00:00
|
|
|
int make_download_list(char *buffer, char *path, unsigned int lim, int tz) {
|
|
|
|
char *start = buffer;
|
|
|
|
unsigned int l,len = 0;
|
2005-02-10 20:31:11 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
// global variable used in the compare() function
|
2005-02-16 23:17:43 +00:00
|
|
|
tzone=tz;
|
2005-02-10 20:31:11 +00:00
|
|
|
URLTYPE *serverlist=read_download_list();
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
if (!serverlist) return -1;
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
// print list of servers in sorted order.
|
|
|
|
// Space is to format them nicely
|
|
|
|
//
|
2010-07-30 21:43:23 +00:00
|
|
|
for (i=0;
|
|
|
|
strlen(serverlist[i].name) && (config.max_download_urls_per_file ?(i < config.max_download_urls_per_file) :true);
|
|
|
|
i++
|
|
|
|
) {
|
|
|
|
l = sprintf(start, "%s<url>%s%s</url>", i?"\n ":"", serverlist[i].name, path);
|
|
|
|
len += l;
|
|
|
|
if (len >= lim) {
|
|
|
|
*start = '\0';
|
|
|
|
return (start-buffer);
|
|
|
|
}
|
|
|
|
start += l;
|
2005-02-10 20:31:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (start-buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns zero on success, non-zero to indicate an error
|
|
|
|
//
|
2005-02-16 23:17:43 +00:00
|
|
|
int add_download_servers(char *old_xml, char *new_xml, int tz) {
|
2005-02-10 20:31:11 +00:00
|
|
|
char *p, *q, *r;
|
2010-07-30 21:43:23 +00:00
|
|
|
int total_free = BLOB_SIZE - strlen(old_xml);
|
|
|
|
|
|
|
|
p = (r = old_xml);
|
2005-02-10 20:31:11 +00:00
|
|
|
|
2011-01-07 20:23:22 +00:00
|
|
|
// search for next URL to do surgery on
|
2005-02-10 20:31:11 +00:00
|
|
|
while ((q=strstr(p, "<url>"))) {
|
2009-03-02 23:47:11 +00:00
|
|
|
// p is at current position
|
|
|
|
// q is at beginning of next "<url>" tag
|
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
char *s;
|
2012-05-09 16:11:50 +00:00
|
|
|
char path[MAXPATHLEN];
|
2010-07-30 21:43:23 +00:00
|
|
|
int len = q-p;
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2009-03-02 23:47:11 +00:00
|
|
|
// copy everything from p to q to new_xml
|
|
|
|
//
|
2005-02-10 20:31:11 +00:00
|
|
|
strncpy(new_xml, p, len);
|
|
|
|
new_xml += len;
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
// locate next instance of </url>
|
|
|
|
//
|
2010-07-30 21:43:23 +00:00
|
|
|
if (!(r = strstr(q, "</url>"))) {
|
2005-02-10 20:31:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
r += strlen("</url>");
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2009-03-02 23:47:11 +00:00
|
|
|
// r points to the end of the whole "<url>...</url>" tag
|
|
|
|
// parse out the URL into 'path'
|
2005-02-10 20:31:11 +00:00
|
|
|
//
|
|
|
|
if (!parse_str(q, "<url>", path, 1024)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2009-03-02 23:47:11 +00:00
|
|
|
|
|
|
|
// check if path contains the string specified in config.xml
|
2005-02-10 20:31:11 +00:00
|
|
|
//
|
2010-07-30 21:43:23 +00:00
|
|
|
if (!(s = strstr(path,config.replace_download_url_by_timezone))) {
|
2009-03-02 23:47:11 +00:00
|
|
|
// if it doesn't, just copy the whole tag as it is
|
|
|
|
strncpy(new_xml, q, r-q);
|
|
|
|
new_xml += r-q;
|
|
|
|
p=r;
|
|
|
|
} else {
|
2011-01-07 20:23:22 +00:00
|
|
|
// calculate free space available for URL replaces
|
|
|
|
int lim = total_free - (len - (p - old_xml));
|
2010-07-30 21:43:23 +00:00
|
|
|
|
2009-03-02 23:47:11 +00:00
|
|
|
// find end of the specified replace string,
|
|
|
|
// i.e. start of the 'path'
|
|
|
|
s += strlen(config.replace_download_url_by_timezone);
|
2010-07-30 21:43:23 +00:00
|
|
|
|
2009-03-02 23:47:11 +00:00
|
|
|
// insert new download list in place of the original single URL
|
2010-07-30 21:43:23 +00:00
|
|
|
len = make_download_list(new_xml, s, lim, tz);
|
|
|
|
if (len == 0) {
|
2011-01-07 20:23:22 +00:00
|
|
|
// if the replacement would exceed the maximum XML length,
|
|
|
|
// just keep the original URL
|
|
|
|
len = r-q;
|
|
|
|
strncpy(new_xml, q, len);
|
2010-07-30 21:43:23 +00:00
|
|
|
} else if (len < 0) {
|
2009-03-02 23:47:11 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
new_xml += len;
|
|
|
|
// advance pointer to start looking for next <url> tag.
|
|
|
|
//
|
|
|
|
p=r;
|
2005-02-10 20:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2005-02-10 20:31:11 +00:00
|
|
|
strcpy(new_xml, r);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace the download URL for apps with a list of
|
|
|
|
// multiple download servers.
|
|
|
|
//
|
2008-12-19 18:14:02 +00:00
|
|
|
void process_av_timezone(APP_VERSION* avp, APP_VERSION& av2) {
|
2005-02-10 20:31:11 +00:00
|
|
|
int retval;
|
|
|
|
|
|
|
|
// set these global variables, needed by the compare()
|
|
|
|
// function so that the download URL list can be sorted by timezone
|
|
|
|
//
|
2008-12-19 18:14:02 +00:00
|
|
|
tzone = g_reply->host.timezone;
|
|
|
|
hostid = g_reply->host.id;
|
|
|
|
retval = add_download_servers(avp->xml_doc, av2.xml_doc, g_reply->host.timezone);
|
2005-02-10 20:31:11 +00:00
|
|
|
if (retval) {
|
2008-02-21 21:00:58 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL,
|
2005-02-10 20:31:11 +00:00
|
|
|
"add_download_servers(to APP version) failed\n"
|
|
|
|
);
|
|
|
|
// restore original WU!
|
2008-12-19 18:14:02 +00:00
|
|
|
av2 = *avp;
|
2005-02-10 20:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// replace the download URL for WU files with a list of
|
|
|
|
// multiple download servers.
|
|
|
|
//
|
|
|
|
void process_wu_timezone(
|
2008-12-19 18:14:02 +00:00
|
|
|
WORKUNIT& wu2, WORKUNIT& wu3
|
2005-02-10 20:31:11 +00:00
|
|
|
) {
|
|
|
|
int retval;
|
|
|
|
|
2008-12-19 18:14:02 +00:00
|
|
|
tzone = g_reply->host.timezone;
|
|
|
|
hostid = g_reply->host.id;
|
2011-01-07 20:23:22 +00:00
|
|
|
|
2008-12-19 18:14:02 +00:00
|
|
|
retval = add_download_servers(wu2.xml_doc, wu3.xml_doc, g_reply->host.timezone);
|
2005-02-10 20:31:11 +00:00
|
|
|
if (retval) {
|
2008-02-21 21:00:58 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL,
|
2005-02-10 20:31:11 +00:00
|
|
|
"add_download_servers(to WU) failed\n"
|
|
|
|
);
|
|
|
|
// restore original WU!
|
2008-12-19 18:14:02 +00:00
|
|
|
wu3 = wu2;
|
2005-02-10 20:31:11 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-11 04:00:51 +00:00
|
|
|
|
|
|
|
const char *BOINC_RCSID_28b6ac7093 = "$Id$";
|
|
|
|
|