2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-04-30 22:22:54 +00:00
|
|
|
// 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
|
2003-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-07-03 00:48:43 +00:00
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// 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
|
2003-07-03 00:48:43 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-07-03 00:48:43 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-04-30 22:22:54 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
2003-07-01 20:37:09 +00:00
|
|
|
|
2002-07-11 01:09:53 +00:00
|
|
|
#include <stdio.h>
|
2002-09-16 20:11:52 +00:00
|
|
|
#include <string.h>
|
2003-01-02 23:12:05 +00:00
|
|
|
#include <stdlib.h>
|
2002-09-16 20:11:52 +00:00
|
|
|
#include <math.h>
|
2002-10-22 22:44:52 +00:00
|
|
|
#include <ctype.h>
|
2002-12-16 23:51:33 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2003-02-14 19:02:44 +00:00
|
|
|
#ifdef HAVE_SYS_FILE_H
|
2003-02-14 07:45:43 +00:00
|
|
|
#include <sys/file.h>
|
2003-02-14 19:02:44 +00:00
|
|
|
#endif
|
2003-06-24 00:17:36 +00:00
|
|
|
#include <string>
|
2003-06-24 20:13:02 +00:00
|
|
|
#include <algorithm>
|
2003-06-24 00:17:36 +00:00
|
|
|
using namespace std;
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2002-08-15 21:52:04 +00:00
|
|
|
#ifdef _WIN32
|
2002-06-06 18:50:12 +00:00
|
|
|
#include <time.h>
|
2002-10-06 00:43:54 +00:00
|
|
|
#include <afxwin.h>
|
2002-08-15 21:52:04 +00:00
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
2002-06-06 18:50:12 +00:00
|
|
|
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2002-08-09 21:43:19 +00:00
|
|
|
#include "util.h"
|
2002-07-11 01:09:53 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
// Converts a double precision time (where the value of 1 represents
|
|
|
|
// a day) into a string. smallest_timescale determines the smallest
|
|
|
|
// unit of time division used
|
|
|
|
// smallest_timescale: 0=seconds, 1=minutes, 2=hours, 3=days, 4=years
|
|
|
|
//
|
|
|
|
int double_to_ydhms (double x, int smallest_timescale, char *buf) {
|
|
|
|
double years, days, hours, minutes, seconds;
|
|
|
|
char year_buf[64], day_buf[16], hour_buf[16], min_buf[16], sec_buf[16];
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (x < 0 || buf == NULL) return -1;
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
years = x / 365.25;
|
|
|
|
days = fmod(x, 365.25);
|
|
|
|
hours = fmod(x*24, 24);
|
|
|
|
minutes = fmod(x*24*60, 60);
|
|
|
|
seconds = fmod(x*24*60*60, 60);
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==4) {
|
|
|
|
sprintf( year_buf, "%.3f yr ", years );
|
|
|
|
} else if (years > 1 && smallest_timescale < 4) {
|
|
|
|
sprintf( year_buf, "%d yr ", (int)years );
|
|
|
|
} else {
|
|
|
|
strcpy( year_buf, "" );
|
|
|
|
}
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==3) {
|
2003-04-01 18:49:54 +00:00
|
|
|
sprintf( day_buf, "%.2f day%s ", days, (days>1?"s":"") );
|
2002-09-16 20:11:52 +00:00
|
|
|
} else if (days > 1 && smallest_timescale < 3) {
|
2003-04-01 18:49:54 +00:00
|
|
|
sprintf( day_buf, "%d day%s ", (int)days, (days>1?"s":"") );
|
2002-09-16 20:11:52 +00:00
|
|
|
} else {
|
|
|
|
strcpy( day_buf, "" );
|
|
|
|
}
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==2) {
|
|
|
|
sprintf( hour_buf, "%.2f hr ", hours );
|
|
|
|
} else if (hours > 1 && smallest_timescale < 2) {
|
|
|
|
sprintf( hour_buf, "%d hr ", (int)hours );
|
|
|
|
} else {
|
|
|
|
strcpy( hour_buf, "" );
|
|
|
|
}
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==1) {
|
|
|
|
sprintf( min_buf, "%.2f min ", minutes );
|
|
|
|
} else if (minutes > 1 && smallest_timescale < 1) {
|
|
|
|
sprintf( min_buf, "%d min ", (int)minutes );
|
|
|
|
} else {
|
|
|
|
strcpy( min_buf, "" );
|
|
|
|
}
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
if (smallest_timescale==0) {
|
|
|
|
sprintf( sec_buf, "%.2f sec ", seconds );
|
|
|
|
} else if (seconds > 1 && smallest_timescale < 0) {
|
|
|
|
sprintf( sec_buf, "%d sec ", (int)seconds );
|
|
|
|
} else {
|
|
|
|
strcpy( sec_buf, "" );
|
|
|
|
}
|
|
|
|
// the "-0.05" below is to prevent it from printing 60.0 sec
|
|
|
|
// when the real value is e.g. 59.91
|
|
|
|
//
|
|
|
|
sprintf(buf, "%s%s%s%s%s", year_buf, day_buf, hour_buf, min_buf, sec_buf);
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-09-16 20:11:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-06 21:23:14 +00:00
|
|
|
// Convert nbytes into a string. If total_bytes is non-zero,
|
|
|
|
// convert the two into a fractional display (i.e. 4/16 KB)
|
|
|
|
//
|
|
|
|
void get_byte_string(double nbytes, double total_bytes, char* str, int len) {
|
|
|
|
char buf[256];
|
|
|
|
double xTera = (1024.0*1024.0*1024.0*1024.0);
|
|
|
|
double xGiga = (1024.0*1024.0*1024.0);
|
|
|
|
double xMega = (1024.0*1024.0);
|
|
|
|
double xKilo = (1024.0);
|
|
|
|
|
|
|
|
if (total_bytes != 0) {
|
|
|
|
if (total_bytes >= xTera) {
|
|
|
|
sprintf(buf, "%0.2f/%0.2f TB", nbytes/xTera, total_bytes/xTera);
|
|
|
|
} else if (total_bytes >= xGiga) {
|
|
|
|
sprintf(buf, "%0.2f/%0.2f GB", nbytes/xGiga, total_bytes/xGiga);
|
|
|
|
} else if (total_bytes >= xMega) {
|
|
|
|
sprintf(buf, "%0.2f/%0.2f MB", nbytes/xMega, total_bytes/xMega);
|
|
|
|
} else if (total_bytes >= xKilo) {
|
|
|
|
sprintf(buf, "%0.2f/%0.2f KB", nbytes/xKilo, total_bytes/xKilo);
|
|
|
|
} else {
|
|
|
|
sprintf(buf, "%0.0f/%0.0f bytes", nbytes, total_bytes);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (nbytes >= xTera) {
|
|
|
|
sprintf(buf, "%0.2f TB", nbytes/xTera);
|
|
|
|
} else if (nbytes >= xGiga) {
|
|
|
|
sprintf(buf, "%0.2f GB", nbytes/xGiga);
|
|
|
|
} else if (nbytes >= xMega) {
|
|
|
|
sprintf(buf, "%0.2f MB", nbytes/xMega);
|
|
|
|
} else if (nbytes >= xKilo) {
|
|
|
|
sprintf(buf, "%0.2f KB", nbytes/xKilo);
|
|
|
|
} else {
|
|
|
|
sprintf(buf, "%0.0f bytes", nbytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
safe_strncpy(str, buf, len);
|
|
|
|
}
|
2002-09-16 20:11:52 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
// return time of day as a double
|
2003-02-28 23:30:05 +00:00
|
|
|
// Not necessarily in terms of UNIX time (especially on Windows)
|
2002-04-30 22:22:54 +00:00
|
|
|
//
|
|
|
|
double dtime() {
|
2002-08-15 21:52:04 +00:00
|
|
|
#ifdef _WIN32
|
2003-06-06 17:43:45 +00:00
|
|
|
LARGE_INTEGER time;
|
|
|
|
FILETIME sysTime;
|
|
|
|
GetSystemTimeAsFileTime(&sysTime);
|
2003-02-28 23:30:05 +00:00
|
|
|
time.LowPart = sysTime.dwLowDateTime;
|
|
|
|
time.HighPart = sysTime.dwHighDateTime; // Time is in 100 ns units
|
2003-06-06 17:43:45 +00:00
|
|
|
return (double)time.QuadPart/10000000; // Convert to 1 s units
|
2002-08-15 21:52:04 +00:00
|
|
|
#else
|
2002-04-30 22:22:54 +00:00
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, 0);
|
|
|
|
return tv.tv_sec + (tv.tv_usec/1.e6);
|
2002-08-15 21:52:04 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
}
|
2002-06-06 18:50:12 +00:00
|
|
|
|
2002-08-15 21:52:04 +00:00
|
|
|
// sleep for a specified number of seconds
|
|
|
|
//
|
2003-03-11 22:18:01 +00:00
|
|
|
void boinc_sleep(double seconds) {
|
2002-06-06 18:50:12 +00:00
|
|
|
#ifdef _WIN32
|
2003-03-11 22:18:01 +00:00
|
|
|
::Sleep((int)(1000*seconds));
|
2002-06-06 18:50:12 +00:00
|
|
|
#else
|
2003-03-11 22:18:01 +00:00
|
|
|
sleep((int)seconds);
|
2003-05-09 20:33:57 +00:00
|
|
|
usleep((int)fmod(seconds*1000000,1000000));
|
2002-06-06 18:50:12 +00:00
|
|
|
#endif
|
2002-08-15 21:52:04 +00:00
|
|
|
}
|
2002-10-22 22:44:52 +00:00
|
|
|
|
|
|
|
// take a string containing some space separated words.
|
|
|
|
// return an array of pointers to the null-terminated words.
|
|
|
|
// Modifies the string arg.
|
|
|
|
// Returns argc
|
|
|
|
// TODO: use strtok here
|
2003-02-28 23:42:25 +00:00
|
|
|
|
|
|
|
#define NOT_IN_TOKEN 0
|
|
|
|
#define IN_SINGLE_QUOTED_TOKEN 1
|
|
|
|
#define IN_DOUBLE_QUOTED_TOKEN 2
|
|
|
|
#define IN_UNQUOTED_TOKEN 3
|
|
|
|
|
2002-10-22 22:44:52 +00:00
|
|
|
int parse_command_line(char* p, char** argv) {
|
2003-02-28 23:42:25 +00:00
|
|
|
int state = NOT_IN_TOKEN;
|
2002-10-22 22:44:52 +00:00
|
|
|
int argc=0;
|
|
|
|
|
|
|
|
while (*p) {
|
2003-02-28 23:42:25 +00:00
|
|
|
switch(state) {
|
|
|
|
case NOT_IN_TOKEN:
|
|
|
|
if (isspace(*p)) {
|
|
|
|
} else if (*p == '\'') {
|
|
|
|
p++;
|
|
|
|
argv[argc++] = p;
|
|
|
|
state = IN_SINGLE_QUOTED_TOKEN;
|
|
|
|
break;
|
|
|
|
} else if (*p == '\"') {
|
|
|
|
p++;
|
|
|
|
argv[argc++] = p;
|
|
|
|
state = IN_DOUBLE_QUOTED_TOKEN;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
argv[argc++] = p;
|
|
|
|
state = IN_UNQUOTED_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IN_SINGLE_QUOTED_TOKEN:
|
|
|
|
if (*p == '\'') {
|
|
|
|
*p = 0;
|
|
|
|
state = NOT_IN_TOKEN;
|
2002-10-22 22:44:52 +00:00
|
|
|
}
|
2003-02-28 23:42:25 +00:00
|
|
|
break;
|
|
|
|
case IN_DOUBLE_QUOTED_TOKEN:
|
|
|
|
if (*p == '\"') {
|
|
|
|
*p = 0;
|
|
|
|
state = NOT_IN_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IN_UNQUOTED_TOKEN:
|
|
|
|
if (isspace(*p)) {
|
|
|
|
*p = 0;
|
|
|
|
state = NOT_IN_TOKEN;
|
|
|
|
}
|
|
|
|
break;
|
2002-10-22 22:44:52 +00:00
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
2003-02-28 23:42:25 +00:00
|
|
|
argv[argc] = 0;
|
2002-10-22 22:44:52 +00:00
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
2002-12-16 23:51:33 +00:00
|
|
|
int lock_file(char* filename) {
|
|
|
|
int retval;
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-12-16 23:51:33 +00:00
|
|
|
// some systems have both!
|
2003-02-14 07:42:53 +00:00
|
|
|
#ifdef HAVE_FLOCK
|
|
|
|
int lock = open(filename, O_WRONLY|O_CREAT, 0644);
|
|
|
|
retval = flock(lock, LOCK_EX|LOCK_NB);
|
2003-06-06 17:43:45 +00:00
|
|
|
#elif HAVE_LOCKF
|
2002-12-16 23:51:33 +00:00
|
|
|
int lock = open(filename, O_WRONLY|O_CREAT, 0644);
|
|
|
|
retval = lockf(lock, F_TLOCK, 1);
|
|
|
|
// must leave fd open
|
|
|
|
#endif
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2002-12-16 23:51:33 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE hfile = CreateFile(
|
|
|
|
filename, GENERIC_WRITE,
|
|
|
|
0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0
|
|
|
|
);
|
|
|
|
if (hfile == INVALID_HANDLE_VALUE) retval = 1;
|
|
|
|
else retval = 0;
|
|
|
|
#endif
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2003-01-30 23:03:52 +00:00
|
|
|
static char x2c(char *what) {
|
|
|
|
register char digit;
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2003-01-30 23:03:52 +00:00
|
|
|
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
|
|
|
|
digit *= 16;
|
|
|
|
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
|
|
|
|
return(digit);
|
|
|
|
}
|
|
|
|
|
2003-03-08 23:49:35 +00:00
|
|
|
void c2x(char *what) {
|
2003-01-30 23:03:52 +00:00
|
|
|
char buf[3];
|
|
|
|
char num = atoi(what);
|
|
|
|
char d1 = num / 16;
|
|
|
|
char d2 = num % 16;
|
|
|
|
int abase1, abase2;
|
|
|
|
if (d1 < 10) abase1 = 48;
|
|
|
|
else abase1 = 55;
|
|
|
|
if (d2 < 10) abase2 = 48;
|
|
|
|
else abase2 = 55;
|
|
|
|
buf[0] = d1+abase1;
|
|
|
|
buf[1] = d2+abase2;
|
|
|
|
buf[2] = 0;
|
|
|
|
|
|
|
|
strcpy(what, buf);
|
|
|
|
}
|
|
|
|
|
2003-05-13 23:10:06 +00:00
|
|
|
void strip_whitespace(char *str) {
|
2003-05-15 17:10:26 +00:00
|
|
|
int read_pos=0, write_pos=0;
|
|
|
|
while (str[read_pos]) {
|
2003-05-13 23:10:06 +00:00
|
|
|
if (!isspace(str[read_pos])) {
|
2003-05-15 17:10:26 +00:00
|
|
|
str[write_pos++] = str[read_pos];
|
2003-05-13 23:10:06 +00:00
|
|
|
}
|
|
|
|
read_pos++;
|
|
|
|
}
|
2003-05-15 17:10:26 +00:00
|
|
|
str[write_pos] = 0;
|
2003-05-13 23:10:06 +00:00
|
|
|
}
|
|
|
|
|
2003-01-30 23:03:52 +00:00
|
|
|
void unescape_url(char *url) {
|
|
|
|
register int x,y;
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2003-01-30 23:03:52 +00:00
|
|
|
for(x=0,y=0;url[y];++x,++y) {
|
|
|
|
if((url[x] = url[y]) == '%') {
|
|
|
|
url[x] = x2c(&url[y+1]);
|
|
|
|
y+=2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
url[x] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void escape_url(char *in, char*out) {
|
|
|
|
int x, y;
|
|
|
|
for (x=0, y=0; in[x]; ++x) {
|
|
|
|
if (isalnum(in[x])) {
|
|
|
|
out[y] = in[x];
|
|
|
|
++y;
|
|
|
|
} else {
|
|
|
|
out[y] = '%';
|
|
|
|
++y;
|
|
|
|
out[y] = 0;
|
|
|
|
char buf[256];
|
|
|
|
sprintf(buf, "%d", (char)in[x]);
|
|
|
|
c2x(buf);
|
|
|
|
strcat(out, buf);
|
|
|
|
y += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out[y] = 0;
|
|
|
|
}
|
|
|
|
|
2003-05-20 00:03:39 +00:00
|
|
|
// Escape a URL for the project directory, cutting off the "http://",
|
|
|
|
// converting '\' '/' and ' ' to '_',
|
|
|
|
// and converting the non alphanumeric characters to %XY
|
|
|
|
// where XY is their hexadecimal equivalent
|
|
|
|
//
|
|
|
|
void escape_url_readable(char *in, char* out) {
|
|
|
|
int x, y;
|
|
|
|
char *temp;
|
|
|
|
char buf[256];
|
2003-06-09 23:50:49 +00:00
|
|
|
|
2003-05-20 00:03:39 +00:00
|
|
|
temp = strstr(in,"://");
|
|
|
|
if (temp) {
|
|
|
|
in = temp + strlen("://");
|
|
|
|
}
|
|
|
|
for (x=0, y=0; in[x]; ++x) {
|
|
|
|
if (isalnum(in[x]) || in[x]=='.' || in[x]=='-' || in[x]=='_') {
|
|
|
|
out[y] = in[x];
|
|
|
|
++y;
|
|
|
|
} else if (in[x] == '/' || in[x] == '\\' || in[x] == ' ') {
|
|
|
|
out[y] = '_';
|
|
|
|
++y;
|
|
|
|
} else {
|
|
|
|
out[y] = '%';
|
|
|
|
++y;
|
|
|
|
out[y] = 0;
|
|
|
|
sprintf(buf, "%d", (char)in[x]);
|
|
|
|
c2x(buf);
|
|
|
|
strcat(out, buf);
|
|
|
|
y += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out[y] = 0;
|
|
|
|
}
|
|
|
|
|
2003-06-24 00:17:36 +00:00
|
|
|
inline void replace_string(string& s, string const& src,
|
|
|
|
string const& dest, string::size_type start=0)
|
|
|
|
{
|
|
|
|
string::size_type p;
|
|
|
|
while ( (p=s.find(src, start)) != string::npos ) {
|
|
|
|
s.replace(p, src.length(), dest);
|
|
|
|
start = p + dest.length();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Canonicalize a master url.
|
|
|
|
// - Convert the first part of a URL (before the "://") to lowercase
|
|
|
|
// - Remove double slashes
|
|
|
|
// - Add a trailing slash if necessary
|
2003-06-06 17:43:45 +00:00
|
|
|
//
|
2003-06-24 00:17:36 +00:00
|
|
|
void canonicalize_master_url(string& url) {
|
|
|
|
string::size_type p = url.find("://");
|
|
|
|
// lowercase http://
|
|
|
|
if (p != string::npos) {
|
|
|
|
transform(url.begin(), url.begin()+p, url.begin(), tolower);
|
|
|
|
p += 3;
|
|
|
|
} else {
|
|
|
|
p = 0;
|
2003-06-06 17:43:45 +00:00
|
|
|
}
|
2003-06-24 00:17:36 +00:00
|
|
|
// remove double slashes
|
|
|
|
replace_string(url, "//", "/", p);
|
|
|
|
|
|
|
|
// ensure trailing slash
|
|
|
|
if (url[url.length()-1] != '/') {
|
|
|
|
url += '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void canonicalize_master_url(char *xurl) {
|
|
|
|
string url = xurl;
|
|
|
|
canonicalize_master_url(url);
|
|
|
|
strcpy(xurl, url.c_str());
|
2003-06-06 17:43:45 +00:00
|
|
|
}
|
|
|
|
|
2003-04-03 18:35:40 +00:00
|
|
|
void safe_strncpy(char* dst, char* src, int len) {
|
|
|
|
strncpy(dst, src, len);
|
2003-03-06 00:42:18 +00:00
|
|
|
dst[len-1]=0;
|
|
|
|
}
|
2003-03-06 17:42:49 +00:00
|
|
|
|
2003-05-14 21:17:58 +00:00
|
|
|
// return current time of day as ASCII string, no CR
|
|
|
|
//
|
2003-03-06 17:42:49 +00:00
|
|
|
char* timestamp() {
|
2003-07-01 00:56:18 +00:00
|
|
|
// time_t now = time(0);
|
|
|
|
// char* p = ctime(&now);
|
|
|
|
// *(strchr(p, '\n')) = 0;
|
|
|
|
// return p;
|
|
|
|
static char buf[100];
|
2003-03-06 17:42:49 +00:00
|
|
|
time_t now = time(0);
|
2003-07-01 00:56:18 +00:00
|
|
|
struct tm* tm = localtime(&now);
|
|
|
|
strftime(buf, sizeof(buf)-1, "%Y-%m-%d %H:%M:%S", tm);
|
|
|
|
return buf;
|
2003-03-06 17:42:49 +00:00
|
|
|
}
|
2003-06-28 00:15:42 +00:00
|
|
|
|
|
|
|
// set by command line
|
|
|
|
bool debug_fake_exponential_backoff = false;
|
|
|
|
double debug_total_exponential_backoff = 0;
|
|
|
|
|
|
|
|
// return a random integer in the range [MIN,min(e^n,MAX))
|
|
|
|
int calculate_exponential_backoff(const char* debug_descr, int n, double MIN, double MAX, double factor /*=1.0*/)
|
|
|
|
{
|
|
|
|
double rmax = min(MAX, factor*exp((double)n));
|
|
|
|
|
|
|
|
if (debug_fake_exponential_backoff) {
|
|
|
|
// For debugging/testing purposes, fake exponential back-off by
|
|
|
|
// returning 0 seconds; report arguments so we can tell what we would
|
|
|
|
// have done (this doesn't test the rand_range() functions but is
|
|
|
|
// very useful for testing backoff/retry policies).
|
|
|
|
double expected_backoff = (MIN > rmax) ? MIN : (rmax-MIN)/2.0;
|
|
|
|
|
|
|
|
debug_total_exponential_backoff += expected_backoff;
|
|
|
|
fprintf(stderr,
|
|
|
|
"## calculate_exponential_backoff(): descr=\"%s\", n=%d, MIN=%.1f, MAX=%.1f, factor=%.1f; rand_range [%.1f,%.1f); total expected backoff=%.1f\n",
|
|
|
|
debug_descr, n, MIN, MAX, factor,
|
|
|
|
MIN, rmax, debug_total_exponential_backoff);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) rand_range(MIN, rmax);
|
|
|
|
}
|
2003-07-03 00:48:43 +00:00
|
|
|
|
|
|
|
string timediff_format(long tdiff)
|
|
|
|
{
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
int sex = tdiff % 60;
|
|
|
|
tdiff /= 60;
|
|
|
|
if (!tdiff) {
|
|
|
|
sprintf(buf, "%d seconds", sex);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int min = tdiff % 60;
|
|
|
|
tdiff /= 60;
|
|
|
|
if (!tdiff) {
|
|
|
|
sprintf(buf, "%d minutes and %d seconds", min, sex);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hours = tdiff % 24;
|
|
|
|
tdiff /= 24;
|
|
|
|
if (!tdiff) {
|
|
|
|
sprintf(buf, "%d hours, %d minutes, and %d seconds", hours, min, sex);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
int days = tdiff % 7;
|
|
|
|
tdiff /= 7;
|
|
|
|
if (!tdiff) {
|
|
|
|
sprintf(buf, "%d days, %d hours, %d minutes, and %d seconds", days, hours, min, sex);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(buf, "%d weeks, %d days, %d hours, %d minutes, and %d seconds", tdiff, days, hours, min, sex);
|
|
|
|
return buf;
|
|
|
|
}
|