Skip to content

Commit 97fe081

Browse files
committed
Restructure application layer into CommandLine, Engine, ReceiverTracker and FrontendConfig
1 parent 4f518de commit 97fe081

48 files changed

Lines changed: 3104 additions & 2633 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,21 @@ set(CPP
237237
Source/Application/ControlCore.cpp
238238
Source/Application/ControlServer.cpp
239239
Source/Application/DeviceManager.cpp
240+
Source/Application/Engine.cpp
241+
Source/Application/CommandLine.cpp
240242
Source/Application/Main.cpp
241243
Source/Application/ManagedMain.cpp
242244
Source/Application/MapTiles.cpp
243245
Source/Application/Prometheus.cpp
244246
Source/Application/Receiver.cpp
247+
Source/IO/HTTPServer.cpp
245248
Source/Device/FileRAW.cpp
246249
Source/Device/FileWAV.cpp
247250
Source/Device/RTLTCP.cpp
248251
Source/Device/Serial.cpp
249252
Source/Device/SpyServer.cpp
250253
Source/Device/UDP.cpp
251-
Source/DSP/Decoder/V2/Engine.cpp
254+
Source/DSP/Decoder/V2/V2Engine.cpp
252255
Source/DSP/Demod.cpp
253256
Source/DSP/DSP.cpp
254257
Source/DSP/Model.cpp
@@ -317,13 +320,15 @@ endif()
317320

318321
if(WEBVIEWER)
319322
list(APPEND CPP
323+
Source/Application/BackupManager.cpp
324+
Source/Application/FrontendConfig.cpp
320325
Source/Application/WebDB.cpp
321326
Source/Application/WebViewer.cpp
322-
Source/IO/HTTPServer.cpp)
327+
Source/Tracking/ReceiverTracker.cpp)
323328
endif()
324329

325330
set(HEADER
326-
Source/Application/AIS-catcher.h Source/Application/Prometheus.h Source/Application/Config.h Source/Application/DeviceManager.h Source/Application/WebDB.h Source/Library/Logger.h Source/Application/WebViewer.h Source/Application/Receiver.h Source/Tracking/Ships.h Source/Tracking/DB.h Source/DBMS/PostgreSQL.h Source/IO/HTTPClient.h Source/Application/MapTiles.h Source/Aviation/Beast.h
331+
Source/Application/AIS-catcher.h Source/Application/Prometheus.h Source/Application/Config.h Source/Application/DeviceManager.h Source/Application/WebDB.h Source/Library/Logger.h Source/Application/WebViewer.h Source/Application/Receiver.h Source/Tracking/Ships.h Source/Tracking/DB.h Source/Tracking/ReceiverTracker.h Source/Application/FrontendConfig.h Source/Application/BackupManager.h Source/DBMS/PostgreSQL.h Source/IO/HTTPClient.h Source/Application/MapTiles.h Source/Aviation/Beast.h
327332
Source/Device/Device.h Source/Device/FileWAV.h Source/Device/RTLTCP.h Source/Device/UDP.h Source/DSP/Demod.h Source/DSP/Filters.h Source/Marine/AIS.h Source/Marine/Message.h Source/Marine/MessageHistory.h Source/Marine/NMEA.h Source/Library/ZIP.h Source/Library/Signals.h Source/Device/SoapySDR.h Source/JSON/JSONAIS.h Source/JSON/JSON.h Source/Aviation/Basestation.h Source/Aviation/ADSB.h
328333
Source/Device/AIRSPY.h Source/Library/FIFO.h Source/Device/N2KsktCAN.h Source/Device/HACKRF.h Source/Device/HYDRASDR.h Source/Device/SDRPLAY.h Source/DSP/DSP.h Source/DSP/Model.h Source/Tracking/History.h Source/Tracking/Statistics.h Source/Library/Common.h Source/Library/Stream.h Source/Library/SWAR.h Source/Device/SpyServer.h Source/JSON/Keys.h Source/JSON/Writer.h Source/JSON/Parser.h Source/Tracking/PlaneDB.h
329334
Source/Device/Serial.h Source/IO/N2KInterface.h Source/Marine/N2K.h Source/IO/N2KStream.h Source/Device/AIRSPYHF.h Source/Device/FileRAW.h Source/Device/RTLSDR.h Source/Device/ZMQ.h Source/DSP/FFT.h Source/IO/MsgOut.h Source/IO/Screen.h Source/IO/File.h Source/IO/StreamCounter.h Source/IO/Network.h Source/IO/HTTPServer.h Source/Utilities/StreamHelpers.h Source/IO/TCPServer.h Source/IO/Protocol.h

Makefile

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
SRC = Application/Config.cpp Application/ControlCore.cpp Application/ControlServer.cpp Application/DeviceManager.cpp Application/Main.cpp Application/ManagedMain.cpp Application/MapTiles.cpp Application/Prometheus.cpp Application/Receiver.cpp Application/WebDB.cpp Application/WebViewer.cpp DBMS/PostgreSQL.cpp Device/AIRSPY.cpp Device/AIRSPYHF.cpp Device/FileRAW.cpp Device/FileWAV.cpp Device/HACKRF.cpp Device/HYDRASDR.cpp Device/N2KsktCAN.cpp Device/RTLSDR.cpp Device/RTLTCP.cpp Device/SDRPLAY.cpp Device/Serial.cpp Device/SoapySDR.cpp Device/SpyServer.cpp Device/UDP.cpp Device/ZMQ.cpp DSP/Decoder/V2/Engine.cpp DSP/Demod.cpp DSP/DSP.cpp DSP/Model.cpp IO/HTTPClient.cpp IO/HTTPServer.cpp IO/MsgOut.cpp IO/Screen.cpp IO/N2KInterface.cpp IO/N2KStream.cpp IO/Network.cpp IO/Protocol.cpp JSON/JSON.cpp JSON/JSONAIS.cpp JSON/Keys.cpp JSON/Parser.cpp Aviation/ADSB.cpp Aviation/Basestation.cpp Aviation/Beast.cpp Marine/AIS.cpp Marine/Message.cpp Marine/N2K.cpp Marine/NMEA.cpp Library/Logger.cpp IO/TCPServer.cpp Tracking/DB.cpp Tracking/Ships.cpp Utilities/Parse.cpp Utilities/Convert.cpp Utilities/Helper.cpp Utilities/TemplateString.cpp Utilities/StreamHelpers.cpp
2-
OBJ = Config.o ControlCore.o ControlServer.o DeviceManager.o Main.o ManagedMain.o MapTiles.o Prometheus.o Receiver.o WebDB.o WebViewer.o PostgreSQL.o AIRSPY.o AIRSPYHF.o FileRAW.o FileWAV.o HACKRF.o HYDRASDR.o N2KsktCAN.o RTLSDR.o RTLTCP.o SDRPLAY.o Serial.o SoapySDR.o SpyServer.o UDP.o ZMQ.o Engine.o Demod.o DSP.o Model.o HTTPClient.o HTTPServer.o MsgOut.o Screen.o N2KInterface.o N2KStream.o Network.o Protocol.o JSON.o JSONAIS.o Keys.o Parser.o ADSB.o Basestation.o Beast.o AIS.o Message.o N2K.o NMEA.o Logger.o TCPServer.o DB.o Ships.o Parse.o Convert.o Helper.o TemplateString.o StreamHelpers.o
1+
SRC = Application/Config.cpp Application/ControlCore.cpp Application/ControlServer.cpp Application/DeviceManager.cpp Application/BackupManager.cpp Application/Engine.cpp Application/FrontendConfig.cpp Application/CommandLine.cpp Application/Main.cpp Application/ManagedMain.cpp Application/MapTiles.cpp Application/Prometheus.cpp Application/Receiver.cpp Application/WebDB.cpp Application/WebViewer.cpp DBMS/PostgreSQL.cpp Device/AIRSPY.cpp Device/AIRSPYHF.cpp Device/FileRAW.cpp Device/FileWAV.cpp Device/HACKRF.cpp Device/HYDRASDR.cpp Device/N2KsktCAN.cpp Device/RTLSDR.cpp Device/RTLTCP.cpp Device/SDRPLAY.cpp Device/Serial.cpp Device/SoapySDR.cpp Device/SpyServer.cpp Device/UDP.cpp Device/ZMQ.cpp DSP/Decoder/V2/V2Engine.cpp DSP/Demod.cpp DSP/DSP.cpp DSP/Model.cpp IO/HTTPClient.cpp IO/HTTPServer.cpp IO/MsgOut.cpp IO/Screen.cpp IO/N2KInterface.cpp IO/N2KStream.cpp IO/Network.cpp IO/Protocol.cpp JSON/JSON.cpp JSON/JSONAIS.cpp JSON/Keys.cpp JSON/Parser.cpp Aviation/ADSB.cpp Aviation/Basestation.cpp Aviation/Beast.cpp Marine/AIS.cpp Marine/Message.cpp Marine/N2K.cpp Marine/NMEA.cpp Library/Logger.cpp IO/TCPServer.cpp Tracking/DB.cpp Tracking/ReceiverTracker.cpp Tracking/Ships.cpp Utilities/Parse.cpp Utilities/Convert.cpp Utilities/Helper.cpp Utilities/TemplateString.cpp Utilities/StreamHelpers.cpp
2+
OBJ = $(addprefix obj/,$(SRC:.cpp=.o))
33
INCLUDE = -I. -ISource -ISource/JSON/ -ISource/DBMS/ -ISource/Tracking/ -ISource/Library/ -ISource/Marine/ -ISource/Aviation/ -ISource/DSP/ -ISource/Application/ -ISource/IO/ -ISource/Utilities/
44
CC = clang
55

@@ -128,46 +128,15 @@ soapysdr-only: CFLAGS_ALL = $(CFLAGS_SOAPYSDR)
128128
soapysdr-only: LFLAGS_ALL = $(LFLAGS_SOAPYSDR)
129129
soapysdr-only: AIS-catcher
130130

131-
# Pattern rule for compiling .cpp files to .o files
132-
%.o: Source/Application/%.cpp
133-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
134-
135-
%.o: Source/Tracking/%.cpp
136-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
137-
138-
%.o: Source/DBMS/%.cpp
139-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
140-
141-
%.o: Source/Device/%.cpp
142-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
143-
144-
%.o: Source/DSP/%.cpp
145-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
146-
147-
%.o: Source/DSP/Decoder/V2/%.cpp
148-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
149-
150-
%.o: Source/IO/%.cpp
151-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
152-
153-
%.o: Source/JSON/%.cpp
154-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
155-
156-
%.o: Source/Library/%.cpp
157-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
158-
159-
%.o: Source/Marine/%.cpp
160-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
161-
162-
%.o: Source/Aviation/%.cpp
163-
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
164-
165-
%.o: Source/Utilities/%.cpp
131+
# One rule for every source: objects mirror the source tree under obj/,
132+
# so two files with the same name in different directories cannot collide.
133+
obj/%.o: Source/%.cpp
134+
@mkdir -p $(dir $@)
166135
$(CC) -c $< $(CFLAGS) $(CFLAGS_ALL) -o $@
167136

168137
clean:
169-
rm *.o
170-
rm AIS-catcher
138+
rm -rf obj
139+
rm -f AIS-catcher
171140

172141
install:
173142
cp AIS-catcher /usr/local/bin/AIS-catcher
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
Copyright(c) 2021-2026 jvde.github@gmail.com
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
#include "BackupManager.h"
19+
#include "Logger.h"
20+
21+
#include <cerrno>
22+
#include <chrono>
23+
#include <cstdio>
24+
#include <cstring>
25+
#include <fstream>
26+
27+
void BackupManager::run()
28+
{
29+
Debug() << "Server: starting backup service every " << interval << " minutes.";
30+
31+
while (running)
32+
{
33+
std::unique_lock<std::mutex> lock(mtx);
34+
cv.wait_for(lock, std::chrono::minutes(interval), [this]
35+
{ return !running.load(); });
36+
37+
if (running && !save())
38+
Error() << "Server: failed to write backup: " << filename;
39+
}
40+
41+
Debug() << "Server: stopping backup service.";
42+
}
43+
44+
void BackupManager::start()
45+
{
46+
if (interval <= 0 || !tracker || filename.empty())
47+
return;
48+
49+
running = true;
50+
thread = std::thread(&BackupManager::run, this);
51+
}
52+
53+
void BackupManager::stop()
54+
{
55+
if (running.load())
56+
{
57+
{
58+
// must hold mtx so the store cannot slip between the run thread's
59+
// predicate check and its wait, which would stall join() a full interval
60+
std::lock_guard<std::mutex> lock(mtx);
61+
running = false;
62+
}
63+
cv.notify_all();
64+
if (thread.joinable())
65+
thread.join();
66+
}
67+
}
68+
69+
bool BackupManager::save()
70+
{
71+
if (filename.empty() || !tracker)
72+
return false;
73+
74+
const std::string tmp = filename + ".tmp";
75+
76+
try
77+
{
78+
std::ofstream outfile(tmp, std::ios::binary | std::ios::trunc);
79+
80+
if (!outfile.is_open())
81+
{
82+
Error() << "Server: cannot open backup file for writing: "
83+
<< tmp << " (" << std::strerror(errno) << ")";
84+
return false;
85+
}
86+
87+
outfile.exceptions(std::ios::failbit | std::ios::badbit);
88+
89+
if (!tracker->save(outfile))
90+
{
91+
outfile.close();
92+
std::remove(tmp.c_str());
93+
return false;
94+
}
95+
96+
outfile.close();
97+
}
98+
catch (const std::ios_base::failure &e)
99+
{
100+
Error() << "Server: write error on " << tmp << " (" << std::strerror(errno) << ")";
101+
std::remove(tmp.c_str());
102+
return false;
103+
}
104+
catch (const std::exception &e)
105+
{
106+
Error() << e.what();
107+
std::remove(tmp.c_str());
108+
return false;
109+
}
110+
catch (...)
111+
{
112+
Error() << "Server: unknown error writing " << tmp;
113+
std::remove(tmp.c_str());
114+
return false;
115+
}
116+
117+
#ifdef _WIN32
118+
std::remove(filename.c_str());
119+
#endif
120+
if (std::rename(tmp.c_str(), filename.c_str()) != 0)
121+
{
122+
Error() << "Server: cannot rename " << tmp << " -> " << filename
123+
<< " (" << std::strerror(errno) << ")";
124+
std::remove(tmp.c_str());
125+
return false;
126+
}
127+
128+
return true;
129+
}
130+
131+
bool BackupManager::load()
132+
{
133+
if (filename.empty() || !tracker)
134+
return false;
135+
136+
Info() << "Server: reading statistics from " << filename;
137+
try
138+
{
139+
std::ifstream infile(filename, std::ios::binary);
140+
141+
if (!infile.is_open())
142+
{
143+
Warning() << "Server: cannot open backup file for reading: "
144+
<< filename << " (" << std::strerror(errno) << ")";
145+
return false;
146+
}
147+
148+
if (!tracker->load(infile))
149+
{
150+
Warning() << "Server: Could not load statistics from backup";
151+
return false;
152+
}
153+
154+
infile.close();
155+
}
156+
catch (const std::exception &e)
157+
{
158+
Error() << e.what();
159+
return false;
160+
}
161+
162+
return true;
163+
}

Source/Application/BackupManager.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright(c) 2021-2026 jvde.github@gmail.com
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
#pragma once
19+
#include <atomic>
20+
#include <condition_variable>
21+
#include <mutex>
22+
#include <string>
23+
#include <thread>
24+
25+
#include "ReceiverTracker.h"
26+
27+
class BackupManager
28+
{
29+
std::thread thread;
30+
std::mutex mtx;
31+
std::condition_variable cv;
32+
std::atomic<bool> running{false};
33+
int interval = -1;
34+
std::string filename;
35+
ReceiverTracker *tracker = nullptr;
36+
37+
void run();
38+
39+
public:
40+
void setInterval(int minutes) { interval = minutes; }
41+
void setFilename(const std::string &f) { filename = f; }
42+
void resetSettings()
43+
{
44+
interval = -1;
45+
filename.clear();
46+
}
47+
const std::string &getFilename() const { return filename; }
48+
void setTracker(ReceiverTracker *t) { tracker = t; }
49+
50+
void start();
51+
void stop();
52+
bool save();
53+
bool load();
54+
55+
~BackupManager() { stop(); }
56+
};

0 commit comments

Comments
 (0)