forked from davidmoreno/rtpmidid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
168 lines (147 loc) · 4.79 KB
/
main.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Real Time Protocol Music Instrument Digital Interface Daemon
* Copyright (C) 2019-2023 David Moreno Montero <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "argv.hpp"
#include "aseq.hpp"
#include "control_socket.hpp"
#include "factory.hpp"
#include "hwautoannounce.hpp"
#include "local_rawmidi_peer.hpp"
#include "midipeer.hpp"
#include "rtpmidid/exceptions.hpp"
#include "rtpmidid/logger.hpp"
#include "rtpmidid/mdns_rtpmidi.hpp"
#include "rtpmidid/poller.hpp"
#include "rtpmidiremotehandler.hpp"
#include "settings.hpp"
#include <chrono>
#include <signal.h>
#include <unistd.h>
namespace rtpmididns {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
std::shared_ptr<::rtpmidid::mdns_rtpmidi_t> mdns;
} // namespace rtpmididns
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
static bool exiting = false;
void sigterm_f(int) {
if (exiting) {
exit(1);
}
exiting = true;
INFO("SIGTERM received. Closing.");
rtpmidid::poller.close();
}
void sigint_f(int) {
if (exiting) {
exit(1);
}
exiting = true;
INFO("SIGINT received. Closing.");
rtpmidid::poller.close();
}
class main_t {
protected:
std::shared_ptr<rtpmididns::midirouter_t> router;
std::shared_ptr<rtpmididns::aseq_t> aseq;
std::optional<rtpmididns::HwAutoAnnounce> hwautoannounce;
rtpmididns::control_socket_t control;
std::optional<rtpmididns::rtpmidi_remote_handler_t> rtpmidi_remote_handler;
public:
// I want setup inside a try catch (and survive it), so I need a setup method
void setup() {
if (aseq.get() == nullptr)
aseq =
std::make_shared<rtpmididns::aseq_t>(rtpmididns::settings.alsa_name);
if (rtpmididns::mdns.get() == nullptr)
rtpmididns::mdns = std::make_unique<rtpmidid::mdns_rtpmidi_t>();
router = std::make_shared<rtpmididns::midirouter_t>();
control.router = router;
control.aseq = aseq;
control.mdns = rtpmididns::mdns;
rtpmidi_remote_handler.emplace(router, aseq);
setup_local_alsa_multilistener();
setup_network_rtpmidi_multilistener();
setup_network_rtpmidi_listener();
setup_rawmidi_peers();
hwautoannounce.emplace(aseq, router);
}
void close() { rtpmididns::mdns = nullptr; }
protected:
void setup_local_alsa_multilistener() {
// Create all the alsa network midipeers
for (const auto &announce : rtpmididns::settings.alsa_announces) {
router->add_peer(
rtpmididns::make_local_alsa_multi_listener(announce.name, aseq));
}
}
void setup_network_rtpmidi_multilistener() {
// Create all the rtpmidi network midipeers
for (const auto &announce : rtpmididns::settings.rtpmidi_announces) {
router->add_peer(rtpmididns::make_network_rtpmidi_multi_listener(
announce.name, announce.port, aseq));
}
}
void setup_network_rtpmidi_listener() {
// Connect to all static endpoints
for (const auto &connect_to : rtpmididns::settings.connect_to) {
router->add_peer(rtpmididns::make_local_alsa_listener(
router, connect_to.name, connect_to.hostname, connect_to.port, aseq,
connect_to.local_udp_port));
}
}
void setup_rawmidi_peers() {
for (const auto &rawmidi : rtpmididns::settings.rawmidi) {
create_rawmidi_rtpclient_pair(router.get(), rawmidi);
}
}
};
// NOLINTNEXTLINE(bugprone-exception-escape)
int main(int argc, char **argv) {
std::vector<std::string> args;
for (int i = 1; i < argc; i++) {
args.push_back(argv[i]);
}
rtpmididns::parse_argv(std::move(args), &rtpmididns::settings);
std::optional<rtpmididns::HwAutoAnnounce> hwautoannounce;
signal(SIGINT, sigint_f);
signal(SIGTERM, sigterm_f);
main_t maindata;
// SETUP
try {
maindata.setup();
} catch (const std::exception &exc) {
ERROR("Error on setup: {}", exc.what());
return 1;
} catch (...) {
ERROR("Unhandled exception in setup!");
return 1;
}
// MAIN RUN
try {
INFO("Waiting for connections.");
while (rtpmidid::poller.is_open()) {
rtpmidid::poller.wait();
}
} catch (const std::exception &exc) {
ERROR("Unhandled exception: {}!", exc.what());
} catch (...) {
ERROR("Unhandled exception!");
}
maindata.close();
INFO("FIN");
return 0;
}