add helper function to connect and send HTTP request
parent
e15e2364f7
commit
4e2ba97629
52
src/main.c
52
src/main.c
|
@ -418,6 +418,33 @@ static int tip_client_connect(const char *addr)
|
|||
return 0;
|
||||
}
|
||||
|
||||
#define MAX_RETRIES 5
|
||||
#define WAIT_RETRY 5 /* wait 5 seconds before retrying. */
|
||||
|
||||
static int tip_client_request_file(struct tip_client *cli,
|
||||
const char *server, const char *filename)
|
||||
{
|
||||
tip_client_connect(server);
|
||||
|
||||
while (cli->state != TIP_CLIENT_DONE && !cli->error)
|
||||
ev_loop(tip_main_loop, 0);
|
||||
|
||||
if (cli->error) {
|
||||
syslog(LOG_ERR, "Failed to fetch file %s\n", filename);
|
||||
sleep(WAIT_RETRY);
|
||||
if (cli->num_retries++ >= MAX_RETRIES) {
|
||||
syslog(LOG_ERR, "Maximum number of retries (%d), bailing out!\n",
|
||||
MAX_RETRIES);
|
||||
return -1;
|
||||
}
|
||||
cli->error = false;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
static uint32_t select_file_chunk(bool *file_chunk)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
@ -442,9 +469,6 @@ static uint32_t select_file_chunk(bool *file_chunk)
|
|||
|
||||
static char _filename[PATH_MAX + 1];
|
||||
|
||||
#define MAX_RETRIES 5
|
||||
#define WAIT_RETRY 5 /* wait 5 seconds before retrying. */
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct timeval tv_start, tv_stop, tv;
|
||||
|
@ -472,26 +496,12 @@ int main(int argc, char *argv[])
|
|||
k = select_file_chunk(file_chunk);
|
||||
snprintf(_filename, sizeof(_filename), "%s.%u", argv[2], k);
|
||||
filename = _filename;
|
||||
retry:
|
||||
syslog(LOG_INFO, "Requesting file %s to server\n", filename);
|
||||
|
||||
tip_client_connect(argv[1]);
|
||||
_cli.state = TIP_CLIENT_GET_HEADER;
|
||||
do {
|
||||
syslog(LOG_INFO, "Requesting file %s to server\n", filename);
|
||||
_cli.state = TIP_CLIENT_GET_HEADER;
|
||||
|
||||
while (_cli.state != TIP_CLIENT_DONE && !_cli.error)
|
||||
ev_loop(tip_main_loop, 0);
|
||||
|
||||
if (_cli.error) {
|
||||
syslog(LOG_ERR, "Failed to fetch file %s\n", filename);
|
||||
sleep(WAIT_RETRY);
|
||||
if (_cli.num_retries++ >= MAX_RETRIES) {
|
||||
syslog(LOG_ERR, "Maximum number of retries (%d), bailing out!\n",
|
||||
MAX_RETRIES);
|
||||
break;
|
||||
}
|
||||
_cli.error = false;
|
||||
goto retry;
|
||||
}
|
||||
} while (tip_client_request_file(&_cli, addr, filename) > 0);
|
||||
|
||||
tip_client_progress(&_cli, true);
|
||||
file_chunk[k] = true;
|
||||
|
|
Loading…
Reference in New Issue