2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation;
|
|
|
|
// either version 2.1 of the License, or (at your option) any later version.
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// This software is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// To view the GNU Lesser General Public License visit
|
|
|
|
// http://www.gnu.org/copyleft/lesser.html
|
|
|
|
// or write to the Free Software Foundation, Inc.,
|
|
|
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
// NET_STATS estimates average network throughput,
|
|
|
|
// i.e. the average total throughput in both the up and down directions.
|
|
|
|
// Here's how it works: NET_STATS::poll() is called every second or so.
|
|
|
|
// If there are any file transfers active,
|
|
|
|
// it increments elapsed time and byte counts,
|
|
|
|
// and maintains an exponential average of throughput.
|
|
|
|
|
2003-10-16 19:03:49 +00:00
|
|
|
#include "cpp.h"
|
2004-03-04 11:41:43 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cmath>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2003-02-26 01:04:42 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "parse.h"
|
|
|
|
#include "time.h"
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
#include "util.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "client_msgs.h"
|
2005-06-07 19:22:50 +00:00
|
|
|
#include "client_state.h"
|
2003-07-03 05:01:29 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
#include "net_stats.h"
|
|
|
|
|
2005-11-25 18:17:09 +00:00
|
|
|
#define EXP_DECAY_RATE (1./3600)
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2006-07-17 22:18:17 +00:00
|
|
|
NET_STATUS net_status;
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
NET_STATS::NET_STATS() {
|
2003-02-26 00:47:57 +00:00
|
|
|
last_time = 0;
|
|
|
|
memset(&up, 0, sizeof(up));
|
|
|
|
memset(&down, 0, sizeof(down));
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
void NET_INFO::update(double dt, double nb, bool active) {
|
2005-11-25 18:17:09 +00:00
|
|
|
//msg_printf(NULL, MSG_ERROR, "dt %f nb %f active %d", dt, nb, active);
|
2003-02-26 00:47:57 +00:00
|
|
|
if (active) {
|
|
|
|
delta_t += dt;
|
|
|
|
delta_nbytes += nb-last_bytes;
|
|
|
|
}
|
|
|
|
last_bytes = nb;
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2003-02-26 00:47:57 +00:00
|
|
|
double NET_INFO::throughput() {
|
2006-01-06 19:27:12 +00:00
|
|
|
double x, tp, new_tp=0;
|
2003-02-26 00:47:57 +00:00
|
|
|
if (starting_throughput > 0) {
|
|
|
|
if (delta_t > 0) {
|
2005-06-09 05:43:35 +00:00
|
|
|
x = exp(-delta_t*EXP_DECAY_RATE);
|
2003-02-26 00:47:57 +00:00
|
|
|
tp = delta_nbytes/delta_t;
|
2005-11-25 18:17:09 +00:00
|
|
|
new_tp = x*starting_throughput + (1-x)*tp;
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
2005-11-25 18:17:09 +00:00
|
|
|
new_tp = starting_throughput;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2003-02-26 00:47:57 +00:00
|
|
|
} else if (delta_t > 0) {
|
2005-11-25 18:17:09 +00:00
|
|
|
new_tp = delta_nbytes/delta_t;
|
2006-01-06 19:27:12 +00:00
|
|
|
} else {
|
2003-02-26 00:47:57 +00:00
|
|
|
}
|
2005-11-25 18:17:09 +00:00
|
|
|
#if 0
|
|
|
|
msg_printf(NULL, MSG_ERROR, "start %f delta_t %f delta_nb %f new_tp %f",
|
|
|
|
starting_throughput, delta_t, delta_nbytes, new_tp
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
starting_throughput = new_tp;
|
|
|
|
delta_nbytes = delta_t = 0;
|
|
|
|
return new_tp;
|
2003-02-26 00:47:57 +00:00
|
|
|
}
|
|
|
|
|
2006-06-28 16:56:15 +00:00
|
|
|
void NET_STATS::poll(FILE_XFER_SET& fxs, HTTP_OP_SET& hops) {
|
2005-06-07 19:22:50 +00:00
|
|
|
double dt;
|
2003-02-26 00:47:57 +00:00
|
|
|
bool upload_active, download_active;
|
|
|
|
|
|
|
|
if (last_time == 0) {
|
|
|
|
dt = 0;
|
2002-04-30 22:22:54 +00:00
|
|
|
} else {
|
2005-06-07 19:22:50 +00:00
|
|
|
dt = gstate.now - last_time;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2005-06-07 19:22:50 +00:00
|
|
|
last_time = gstate.now;
|
2003-02-26 00:47:57 +00:00
|
|
|
|
2005-11-25 18:17:09 +00:00
|
|
|
fxs.check_active(upload_active, download_active);
|
2006-06-28 16:56:15 +00:00
|
|
|
up.update(dt, hops.bytes_up, upload_active);
|
|
|
|
down.update(dt, hops.bytes_down, download_active);
|
2006-08-29 18:18:36 +00:00
|
|
|
|
|
|
|
if (net_status.need_to_contact_reference_site && gstate.gui_http.state==GUI_HTTP_STATE_IDLE) {
|
|
|
|
net_status.contact_reference_site();
|
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Write XML based network statistics
|
|
|
|
//
|
2004-09-22 21:08:26 +00:00
|
|
|
int NET_STATS::write(MIOFILE& out) {
|
2004-06-12 04:45:36 +00:00
|
|
|
out.printf(
|
2002-04-30 22:22:54 +00:00
|
|
|
"<net_stats>\n"
|
2004-11-24 07:02:01 +00:00
|
|
|
" <bwup>%g</bwup>\n"
|
|
|
|
" <bwdown>%g</bwdown>\n"
|
2003-02-26 00:47:57 +00:00
|
|
|
"</net_stats>\n",
|
|
|
|
up.throughput(),
|
|
|
|
down.throughput()
|
2002-04-30 22:22:54 +00:00
|
|
|
);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-07-15 23:21:20 +00:00
|
|
|
// Read XML based network statistics
|
|
|
|
//
|
2004-06-12 04:45:36 +00:00
|
|
|
int NET_STATS::parse(MIOFILE& in) {
|
2002-04-30 22:22:54 +00:00
|
|
|
char buf[256];
|
2003-02-26 00:47:57 +00:00
|
|
|
double bwup, bwdown;
|
2002-08-15 22:03:41 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
memset(this, 0, sizeof(NET_STATS));
|
2004-06-12 04:45:36 +00:00
|
|
|
while (in.fgets(buf, 256)) {
|
2002-04-30 22:22:54 +00:00
|
|
|
if (match_tag(buf, "</net_stats>")) return 0;
|
2003-02-26 00:47:57 +00:00
|
|
|
else if (parse_double(buf, "<bwup>", bwup)) {
|
|
|
|
up.starting_throughput = bwup;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if (parse_double(buf, "<bwdown>", bwdown)) {
|
|
|
|
down.starting_throughput = bwdown;
|
|
|
|
continue;
|
|
|
|
}
|
2006-01-17 22:48:09 +00:00
|
|
|
else msg_printf(NULL, MSG_ERROR,
|
|
|
|
"Unrecognized network statistics line: %s", buf
|
|
|
|
);
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2004-01-30 22:19:19 +00:00
|
|
|
return ERR_XML_PARSE;
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2006-07-17 22:18:17 +00:00
|
|
|
// Return:
|
|
|
|
// 0 if we have network connections open
|
|
|
|
// 1 if we need a physical connection
|
|
|
|
// 2 if we don't have any connections, and don't need any
|
|
|
|
// 3 if a website lookup is pending (try again later)
|
|
|
|
//
|
|
|
|
// There's a 10-second slop factor;
|
|
|
|
// if we've done network comm in the last 10 seconds,
|
|
|
|
// we act as if we're doing it now.
|
|
|
|
// (so that polling mechanisms have a change to trigger)
|
|
|
|
//
|
|
|
|
int NET_STATUS::network_status() {
|
|
|
|
static double last_comm_time=0;
|
2006-08-29 18:18:36 +00:00
|
|
|
int retval;
|
2006-07-17 22:18:17 +00:00
|
|
|
|
|
|
|
if (gstate.http_ops->nops()) {
|
|
|
|
last_comm_time = gstate.now;
|
|
|
|
}
|
|
|
|
if (gstate.now - last_comm_time < 10) {
|
|
|
|
//msg_printf(0, MSG_INFO, "nops %d; return 0", http_ops->nops());
|
2006-08-29 18:18:36 +00:00
|
|
|
retval = NETWORK_STATUS_ONLINE;
|
|
|
|
} else if (gstate.lookup_website_op.error_num == ERR_IN_PROGRESS) {
|
|
|
|
retval = NETWORK_STATUS_LOOKUP_PENDING;
|
|
|
|
} else if (need_physical_connection) {
|
2006-07-17 22:18:17 +00:00
|
|
|
//msg_printf(0, MSG_INFO, "need phys conn; return 1");
|
2006-08-29 18:18:36 +00:00
|
|
|
retval = NETWORK_STATUS_WANT_CONNECTION;
|
|
|
|
} else if (gstate.active_tasks.want_network()) {
|
|
|
|
retval = NETWORK_STATUS_WANT_CONNECTION;
|
|
|
|
} else {
|
|
|
|
have_sporadic_connection = false;
|
2006-07-17 22:18:17 +00:00
|
|
|
//msg_printf(0, MSG_INFO, "returning 2");
|
2006-08-29 18:18:36 +00:00
|
|
|
retval = NETWORK_STATUS_WANT_DISCONNECT;
|
|
|
|
}
|
|
|
|
if (log_flags.network_status_debug) {
|
2006-09-07 20:39:25 +00:00
|
|
|
msg_printf(NULL, MSG_INFO, "[network_status_debug] status: %s", network_status_string(retval));
|
2006-08-29 18:18:36 +00:00
|
|
|
}
|
|
|
|
return retval;
|
2006-07-17 22:18:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// There's now a network connection, after some period of disconnection.
|
|
|
|
// Do all communication that we can.
|
|
|
|
//
|
|
|
|
void NET_STATUS::network_available() {
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
have_sporadic_connection = true;
|
|
|
|
for (i=0; i<gstate.pers_file_xfers->pers_file_xfers.size(); i++) {
|
|
|
|
PERS_FILE_XFER* pfx = gstate.pers_file_xfers->pers_file_xfers[i];
|
|
|
|
pfx->next_request_time = 0;
|
|
|
|
}
|
|
|
|
for (i=0; i<gstate.projects.size(); i++) {
|
|
|
|
PROJECT* p = gstate.projects[i];
|
|
|
|
p->min_rpc_time = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell active tasks that network is available (for Folding@home)
|
|
|
|
//
|
|
|
|
gstate.active_tasks.network_available();
|
|
|
|
}
|
|
|
|
|
|
|
|
// An HTTP operation failed;
|
|
|
|
// it could be because there's no physical network connection.
|
|
|
|
// Find out for sure by trying to contact google
|
|
|
|
//
|
|
|
|
void NET_STATUS::got_http_error() {
|
|
|
|
if ((gstate.lookup_website_op.error_num != ERR_IN_PROGRESS)
|
|
|
|
&& !need_physical_connection
|
|
|
|
) {
|
2006-08-29 18:18:36 +00:00
|
|
|
need_to_contact_reference_site = true;
|
2006-07-17 22:18:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-29 18:18:36 +00:00
|
|
|
void NET_STATUS::contact_reference_site() {
|
|
|
|
std::string url = "http://www.google.com";
|
2006-09-07 20:39:25 +00:00
|
|
|
if (log_flags.network_status_debug) {
|
2006-08-29 18:18:36 +00:00
|
|
|
msg_printf(0, MSG_ERROR,
|
2006-09-07 20:39:25 +00:00
|
|
|
"[network_status_debug] need_phys_conn %d; trying google", need_physical_connection
|
2006-08-29 18:18:36 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
gstate.lookup_website_op.do_rpc(url);
|
|
|
|
need_to_contact_reference_site = false;
|
|
|
|
}
|
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_733b4006f5 = "$Id$";
|