-
Notifications
You must be signed in to change notification settings - Fork 0
/
sender-main.cc
104 lines (96 loc) · 2.75 KB
/
sender-main.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
100
101
102
103
104
#include <iostream>
#include <netdb.h>
#include <cstring>
#include <unistd.h>
#include <sstream>
#include "audio-transmission.h"
#include "udp.h"
#include "sender.h"
#include "control-protocol.h"
using namespace std;
static const uint16_t MIN_PORT = 1;
static const uint16_t MAX_PORT = std::numeric_limits<uint16_t>::max();
uint16_t validatePort(const string &port) {
unsigned long ul = stoul(port);
if (ul >= MIN_PORT && ul <= MAX_PORT) {
return (uint16_t) ul;
} else {
throw std::exception();
}
}
void getConfiguration(int argc, char *argv[]) {
string mcast;
string data_port;
string control_port;
string psize;
string fsize;
string rtime;
string name;
int opt;
while ((opt = getopt(argc, argv, "a:P:C:p:f:R:n:")) != -1) {
switch (opt) {
case 'a':
mcast = optarg;
break;
case 'P':
data_port = optarg;
break;
case 'C':
control_port = optarg;
break;
case 'p':
psize = optarg;
break;
case 'f':
fsize = optarg;
break;
case 'R':
rtime = optarg;
break;
case 'n':
name = optarg;
break;
default: {
std::cout << "Invalid arguments." << std::endl;
exit(1);
}
}
}
try {
configuration() = std::make_unique<Configuration>(mcast);
if (!data_port.empty()) {
configuration()->data_address.setPort(validatePort(data_port));
}
if (!control_port.empty()) {
configuration()->control_port = validatePort(control_port);
}
if (!psize.empty()) {
configuration()->psize = stoul(psize);
}
if (!fsize.empty()) {
configuration()->fsize = stoul(fsize);
}
if (!rtime.empty()) {
configuration()->rtime = stol(rtime);
if (configuration()->rtime <= 0) throw std::exception();
}
if (!name.empty()) {
if (name.size() > ctrl::MAX_NAME_LENGTH) throw std::exception();
configuration()->name = name;
}
} catch (...) {
std::cout << "Invalid arguments." << std::endl;
exit(1);
}
}
int main(int argc, char *argv[]) {
getConfiguration(argc, argv);
packet_fifo() =
std::make_unique<PacketFifo>(configuration()->fsize / configuration()->psize, configuration()->psize);
std::thread controller_thread(controller);
std::thread resender_thread(resender);
streamer();
controller_thread.join();
resender_thread.join();
return 0;
}