open two simultaneous connections using two processes
parent
9e80376cb8
commit
161a49b683
61
src/main.c
61
src/main.c
|
@ -25,6 +25,7 @@
|
|||
#include <sys/time.h>
|
||||
#include <limits.h>
|
||||
#include <syslog.h>
|
||||
#include <sys/wait.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
|
@ -593,23 +594,23 @@ static int tip_client_run(struct tip_client *cli, int fd, const char *addr,
|
|||
}
|
||||
|
||||
static int tip_client_report(struct tip_client *cli, const struct timeval *tv,
|
||||
uint64_t data_len)
|
||||
uint64_t data_len, pid_t pid)
|
||||
{
|
||||
if (cli->state == TIP_CLIENT_DONE) {
|
||||
printf("OK.\n");
|
||||
syslog(LOG_INFO, "Done in %lu.%06lu seconds (%lu Mbytes/second). "
|
||||
printf("(%u) OK.\n", pid);
|
||||
syslog(LOG_INFO, "(%u) Done in %lu.%06lu seconds (%lu Mbytes/second). "
|
||||
"Direct from server: %u Redirected: %u\n",
|
||||
tv->tv_sec, tv->tv_usec,
|
||||
pid, tv->tv_sec, tv->tv_usec,
|
||||
tv->tv_sec > 0 ? data_len / 1024000 / tv->tv_sec : data_len / 1024000,
|
||||
tip_client_stats.direct_from_server,
|
||||
tip_client_stats.redirects);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
printf("Failure, see syslog for details.\n");
|
||||
syslog(LOG_INFO, "Failure after %lu.%06lu seconds (%lu Mbytes/second). "
|
||||
printf("(%u) Failure, see syslog for details.\n", pid);
|
||||
syslog(LOG_INFO, "(%u) Failure after %lu.%06lu seconds (%lu Mbytes/second). "
|
||||
"Direct from server: %u Redirected: %u\n",
|
||||
tv->tv_sec, tv->tv_usec,
|
||||
pid, tv->tv_sec, tv->tv_usec,
|
||||
tv->tv_sec > 0 ? data_len / 1024000 / tv->tv_sec : data_len / 1024000,
|
||||
tip_client_stats.direct_from_server,
|
||||
tip_client_stats.redirects);
|
||||
|
@ -617,14 +618,17 @@ static int tip_client_report(struct tip_client *cli, const struct timeval *tv,
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
#define NUM_CONN 2
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
uint32_t chunk_array[NUM_CONN][MAX_CHUNKS / NUM_CONN] = {};
|
||||
struct timeval tv_start, tv_stop, tv;
|
||||
uint64_t data_len = 0, file_size = 0;
|
||||
bool file_chunk[MAX_CHUNKS] = {};
|
||||
char filename[PATH_MAX + 1];
|
||||
uint64_t file_size = 0;
|
||||
int i, j, k, fd, ret;
|
||||
uint64_t chunk_size;
|
||||
int i, k, fd, ret;
|
||||
|
||||
if (argc != 3) {
|
||||
printf("%s [ip] [file]\n", argv[0]);
|
||||
|
@ -653,21 +657,40 @@ int main(int argc, char *argv[])
|
|||
file_size = _cli.content_len;
|
||||
chunk_size = file_size / MAX_CHUNKS;
|
||||
|
||||
for (i = 0; i < MAX_CHUNKS; i++) {
|
||||
k = select_file_chunk(file_chunk);
|
||||
snprintf(filename, sizeof(filename), "%s.%u", argv[2], k);
|
||||
|
||||
if (tip_client_run(&_cli, fd, addr, filename, k, chunk_size) < 0)
|
||||
goto err_bailout;
|
||||
|
||||
file_chunk[k] = true;
|
||||
data_len += _cli.data_len;
|
||||
for (i = 0; i < NUM_CONN; i++) {
|
||||
for (j = 0; j < MAX_CHUNKS / NUM_CONN; j++) {
|
||||
k = select_file_chunk(file_chunk);
|
||||
chunk_array[i][j] = k;
|
||||
file_chunk[k] = true;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < NUM_CONN; i++) {
|
||||
ret = fork();
|
||||
if (ret > 0) {
|
||||
for (j = 0; j < MAX_CHUNKS / NUM_CONN; j++) {
|
||||
k = chunk_array[i][j];
|
||||
snprintf(filename, sizeof(filename), "%s.%u", argv[2], k);
|
||||
|
||||
if (tip_client_run(&_cli, fd, addr, filename, k, chunk_size) < 0)
|
||||
goto err_bailout;
|
||||
}
|
||||
break;
|
||||
} else if (ret < 0) {
|
||||
syslog(LOG_ERR, "failed to fork process");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (ret == 0) {
|
||||
while (wait(NULL) > 0);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
gettimeofday(&tv_stop, NULL);
|
||||
timersub(&tv_stop, &tv_start, &tv);
|
||||
|
||||
return tip_client_report(&_cli, &tv, data_len);
|
||||
return tip_client_report(&_cli, &tv, file_size / 2, ret);
|
||||
|
||||
err_bailout:
|
||||
if (_cli.fatal)
|
||||
|
|
Loading…
Reference in New Issue