-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaturatr.cc
99 lines (75 loc) · 2.75 KB
/
saturatr.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <string>
#include <vector>
#include <poll.h>
#include <assert.h>
#include <sys/types.h>
#include <unistd.h>
#include "acker.hh"
#include "saturateservo.hh"
using namespace std;
int main( int argc, char *argv[] )
{
if ( argc != 1 && argc != 6 ) {
fprintf( stderr, "Usage: %s [RELIABLE_IP RELIABLE_DEV TEST_IP TEST_DEV SERVER_IP]\n",
argv[ 0 ]);
exit( 1 );
}
Socket data_socket, feedback_socket;
bool server;
int sender_id = getpid();
Socket::Address remote_data_address( UNKNOWN ), remote_feedback_address( UNKNOWN );
uint64_t ts=Socket::timestamp();
if ( argc == 1 ) { /* server */
server = true;
data_socket.bind( Socket::Address( "0.0.0.0", 9001 ) );
feedback_socket.bind( Socket::Address( "0.0.0.0", 9002 ) );
} else { /* client */
server = false;
const char *reliable_ip = argv[ 1 ];
const char *reliable_dev = argv[ 2 ];
const char *test_ip = argv[ 3 ];
const char *test_dev = argv[ 4 ];
const char *server_ip = argv[ 5 ];
sender_id = ( (int)(ts/1e9) );
data_socket.bind( Socket::Address( test_ip, 9003 ) );
data_socket.bind_to_device( test_dev );
remote_data_address = Socket::Address( server_ip, 9001 );
feedback_socket.bind( Socket::Address( reliable_ip, 9004 ) );
feedback_socket.bind_to_device( reliable_dev );
remote_feedback_address = Socket::Address( server_ip, 9002 );
}
FILE* log_file;
char log_file_name[50];
sprintf(log_file_name,"%s-%d-%d",server ? "server" : "client",(int)(ts/1e9),sender_id);
log_file=fopen(log_file_name,"w");
SaturateServo saturatr( "OUTGOING", log_file, feedback_socket, data_socket, remote_data_address, server, sender_id );
Acker acker( "INCOMING", log_file, data_socket, feedback_socket, remote_feedback_address, server, sender_id );
saturatr.set_acker( &acker );
acker.set_saturatr( &saturatr );
while ( 1 ) {
fflush( NULL );
/* possibly send packet */
saturatr.tick();
acker.tick();
/* wait for incoming packet OR expiry of timer */
struct pollfd poll_fds[ 2 ];
poll_fds[ 0 ].fd = data_socket.get_sock();
poll_fds[ 0 ].events = POLLIN;
poll_fds[ 1 ].fd = feedback_socket.get_sock();
poll_fds[ 1 ].events = POLLIN;
struct timespec timeout;
uint64_t next_transmission_delay = std::min( saturatr.wait_time(), acker.wait_time() );
if ( next_transmission_delay == 0 ) {
fprintf( stderr, "ZERO %ld %ld\n", saturatr.wait_time(), acker.wait_time() );
}
timeout.tv_sec = next_transmission_delay / 1000000000;
timeout.tv_nsec = next_transmission_delay % 1000000000;
ppoll( poll_fds, 2, &timeout, NULL );
if ( poll_fds[ 0 ].revents & POLLIN ) {
acker.recv();
}
if ( poll_fds[ 1 ].revents & POLLIN ) {
saturatr.recv();
}
}
}