-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflector.cpp
462 lines (389 loc) · 10.7 KB
/
reflector.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
// creflector.cpp
// M17Refd
//
// Created by Jean-Luc Deltombe (LX3JL) on 31/10/2015.
// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved.
// Copyright © 2020 Thomas A. Early, N7TAE
//
// ----------------------------------------------------------------------------
// This file is part of M17Refd.
//
// M17Refd 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.
//
// M17Refd 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
// with this software. If not, see <http://www.gnu.org/licenses/>.
// ----------------------------------------------------------------------------
#include "main.h"
#include <string.h>
#include "reflector.h"
#include "gatekeeper.h"
////////////////////////////////////////////////////////////////////////////////////////
// constructor
CReflector::CReflector()
{
keep_running = true;
}
CReflector::CReflector(const CCallsign &callsign)
{
keep_running = true;
m_Callsign = callsign;
}
////////////////////////////////////////////////////////////////////////////////////////
// destructor
CReflector::~CReflector()
{
keep_running = false;
if ( m_XmlReportFuture.valid() )
{
m_XmlReportFuture.get();
}
for ( int i = 0; i < NB_OF_MODULES; i++ )
{
if ( m_RouterFuture[i].valid() )
{
m_RouterFuture[i].get();
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// operation
bool CReflector::Start(void)
{
// let's go!
keep_running = true;
// init gate keeper. It can only return true!
g_GateKeeper.Init();
// create protocols
if (! m_Protocol.Initialize(M17_PORT, true, true))
{
m_Protocol.Close();
return false;
}
// start one thread per reflector module
for ( int i = 0; i < NB_OF_MODULES; i++ )
{
m_RouterFuture[i] = std::async(std::launch::async, &CReflector::RouterThread, this, &(m_Stream[i]));
}
// start the reporting threads
m_XmlReportFuture = std::async(std::launch::async, &CReflector::XmlReportThread, this);
return true;
}
void CReflector::Stop(void)
{
// stop & delete all threads
keep_running = false;
// stop & delete report threads
if ( m_XmlReportFuture.valid() )
{
m_XmlReportFuture.get();
}
// stop & delete all router thread
for ( int i = 0; i < NB_OF_MODULES; i++ )
{
if ( m_RouterFuture[i].valid() )
{
m_RouterFuture[i].get();
}
}
// close protocols
m_Protocol.Close();
// close gatekeeper
g_GateKeeper.Close();
}
////////////////////////////////////////////////////////////////////////////////////////
// stream opening & closing
bool CReflector::IsStreaming(char module)
{
return false;
}
// clients MUST have been locked by the caller so we can freely access it within the fuction
CPacketStream *CReflector::OpenStream(std::unique_ptr<CPacket> &Header, std::shared_ptr<CClient>client)
{
// check sid is not zero
if ( 0U == Header->GetStreamId() )
{
std::cerr << "Incoming stream has zero streamID" << std::endl;
return nullptr;
}
// check if client is valid candidate
if ( ! m_Clients.IsClient(client) )
{
std::cerr << "can't find client " << client->GetCallsign() << std::endl;
return nullptr;
}
if ( client->IsAMaster() )
{
std::cerr << "Client " << client->GetCallsign() << " is already a Master" << std::endl;
return nullptr;
}
// get the module's queue
char module = Header->GetDestModule();
// check if no stream with same streamid already open
// to prevent loops
if ( IsStreamOpen(Header) )
{
std::cerr << "Detected stream loop on module " << module << " for client " << client->GetCallsign() << " with sid " << Header->GetStreamId() << std::endl;
return nullptr;
}
CPacketStream *stream = GetStream(module);
if ( stream == nullptr ) {
std::cerr << "Can't get stream from module '" << module << "'" << std::endl;
return nullptr;
}
stream->Lock();
// is it available ?
if ( stream->OpenPacketStream(*Header, client) )
{
// stream open, mark client as master
// so that it can't be deleted
client->SetMasterOfModule(module);
// update last heard time
client->Heard();
// report
std::cout << "Opening stream on module " << module << " for client " << client->GetCallsign() << " with id 0x" << std::hex << Header->GetStreamId() << std::dec << " by user " << Header->GetSourceCallsign() << std::endl;
// and push header packet
stream->Push(std::move(Header));
// notify
g_Reflector.OnStreamOpen(stream->GetUserCallsign());
}
stream->Unlock();
return stream;
}
void CReflector::CloseStream(CPacketStream *stream)
{
if ( stream != nullptr )
{
// wait queue is empty. this waits forever
bool bEmpty = false;
do
{
stream->Lock();
// do not use stream->IsEmpty() has this "may" never succeed
// and anyway, the DvLastFramPacket short-circuit the transcoder
// loop queues
bEmpty = stream->empty();
stream->Unlock();
if ( !bEmpty )
CTimePoint::TaskSleepFor(10);
}
while (!bEmpty);
GetClients(); // lock clients
stream->Lock(); // lock stream
// get and check the master
std::shared_ptr<CClient>client = stream->GetOwnerClient();
if ( client != nullptr )
{
// client no longer a master
client->NotAMaster();
// notify
OnStreamClose(stream->GetUserCallsign());
std::cout << "Closing stream on module " << GetStreamModule(stream) << std::endl;
}
// release clients
ReleaseClients();
// unlock before closing
// to avoid double lock in assiociated
// codecstream close/thread-join
stream->Unlock();
// and stop the queue
stream->ClosePacketStream();
}
}
////////////////////////////////////////////////////////////////////////////////////////
// router threads
void CReflector::RouterThread(CPacketStream *streamIn)
{
// get our module
uint8_t uiModuleId = GetStreamModule(streamIn);
// get on input queue
std::unique_ptr<CPacket> packet;
while (keep_running)
{
// any packet in our input queue ?
streamIn->Lock();
if ( !streamIn->empty() )
{
// get the packet
packet = streamIn->front();
streamIn->pop();
}
else
{
packet = nullptr;
}
streamIn->Unlock();
// route it
if ( packet != nullptr )
{
// duplicate packet
auto packetClone = packet->Duplicate();
// and push it
CPacketQueue *queue = m_Protocol.GetQueue();
queue->push(packetClone);
m_Protocol.ReleaseQueue();
}
else
{
CTimePoint::TaskSleepFor(10);
}
}
}
////////////////////////////////////////////////////////////////////////////////////////
// report threads
void CReflector::XmlReportThread()
{
while (keep_running)
{
// report to xml file
std::ofstream xmlFile;
xmlFile.open(XML_PATH, std::ios::out | std::ios::trunc);
if ( xmlFile.is_open() )
{
// write xml file
WriteXmlFile(xmlFile);
// and close file
xmlFile.close();
}
else
{
std::cout << "Failed to open " << XML_PATH << std::endl;
}
// and wait a bit
for (int i=0; i< XML_UPDATE_PERIOD && keep_running; i++)
CTimePoint::TaskSleepFor(1000);
}
}
////////////////////////////////////////////////////////////////////////////////////////
// notifications
void CReflector::OnPeersChanged(void)
{
CNotification notification(NOTIFICATION_PEERS);
m_Notifications.Lock();
m_Notifications.push(notification);
m_Notifications.Unlock();
}
void CReflector::OnClientsChanged(void)
{
CNotification notification(NOTIFICATION_CLIENTS);
m_Notifications.Lock();
m_Notifications.push(notification);
m_Notifications.Unlock();
}
void CReflector::OnUsersChanged(void)
{
CNotification notification(NOTIFICATION_USERS);
m_Notifications.Lock();
m_Notifications.push(notification);
m_Notifications.Unlock();
}
void CReflector::OnStreamOpen(const CCallsign &callsign)
{
CNotification notification(NOTIFICATION_STREAM_OPEN, callsign);
m_Notifications.Lock();
m_Notifications.push(notification);
m_Notifications.Unlock();
}
void CReflector::OnStreamClose(const CCallsign &callsign)
{
CNotification notification(NOTIFICATION_STREAM_CLOSE, callsign);
m_Notifications.Lock();
m_Notifications.push(notification);
m_Notifications.Unlock();
}
////////////////////////////////////////////////////////////////////////////////////////
// modules & queues
int CReflector::GetModuleIndex(char module) const
{
int i = (int)module - (int)'A';
if ( (i < 0) || (i >= NB_OF_MODULES) )
{
i = -1;
}
return i;
}
CPacketStream *CReflector::GetStream(char module)
{
int i = GetModuleIndex(module);
if ( i >= 0 )
{
return &(m_Stream[i]);
}
return nullptr;
}
bool CReflector::IsStreamOpen(const std::unique_ptr<CPacket> &DvHeader)
{
for ( unsigned i = 0; i < m_Stream.size(); i++ )
{
if ( (m_Stream[i].GetPacketStreamId() == DvHeader->GetStreamId()) && (m_Stream[i].IsOpen()) )
return true;
}
return false;
}
char CReflector::GetStreamModule(CPacketStream *stream)
{
for ( unsigned i = 0; i < m_Stream.size(); i++ )
{
if ( &(m_Stream[i]) == stream )
return GetModuleLetter(i);
}
return ' ';
}
////////////////////////////////////////////////////////////////////////////////////////
// xml helpers
void CReflector::WriteXmlFile(std::ofstream &xmlFile)
{
// write header
xmlFile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << std::endl;
// software version
char sz[64];
::sprintf(sz, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION);
xmlFile << "<Version>" << sz << "</Version>" << std::endl;
// linked peers
xmlFile << "<" << m_Callsign << " linked peers>" << std::endl;
// lock
CPeers *peers = GetPeers();
// iterate on peers
for ( auto pit=peers->cbegin(); pit!=peers->cend(); pit++ )
{
(*pit)->WriteXml(xmlFile);
}
// unlock
ReleasePeers();
xmlFile << "</" << m_Callsign << " linked peers>" << std::endl;
// linked nodes
xmlFile << "<" << m_Callsign << " linked nodes>" << std::endl;
// lock
CClients *clients = GetClients();
// iterate on clients
for ( auto cit=clients->cbegin(); cit!=clients->cend(); cit++ )
{
if ( (*cit)->IsNode() && (*cit)->GetCallsign().GetCS(4).compare("M17-") )
{
(*cit)->WriteXml(xmlFile);
}
}
// unlock
ReleaseClients();
xmlFile << "</" << m_Callsign << " linked nodes>" << std::endl;
// last heard users
xmlFile << "<" << m_Callsign << " heard users>" << std::endl;
// lock
CUsers *users = GetUsers();
// iterate on users
for ( auto it=users->begin(); it!=users->end(); it++ )
{
(*it).WriteXml(xmlFile);
}
// unlock
ReleaseUsers();
xmlFile << "</" << m_Callsign << " heard users>" << std::endl;
}