mirror of
https://github.com/30hours/blah2.git
synced 2024-11-08 12:25:42 +00:00
Create and implement socket class
This commit is contained in:
parent
76286c376e
commit
16cba62d34
4 changed files with 89 additions and 56 deletions
|
@ -51,6 +51,7 @@ add_executable(blah2
|
||||||
src/process/tracker/Tracker.cpp
|
src/process/tracker/Tracker.cpp
|
||||||
src/process/spectrum/SpectrumAnalyser.cpp
|
src/process/spectrum/SpectrumAnalyser.cpp
|
||||||
src/process/meta/HammingNumber.cpp
|
src/process/meta/HammingNumber.cpp
|
||||||
|
src/process/meta/Socket.cpp
|
||||||
src/data/IqData.cpp
|
src/data/IqData.cpp
|
||||||
src/data/Map.cpp
|
src/data/Map.cpp
|
||||||
src/data/Detection.cpp
|
src/data/Detection.cpp
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include "process/detection/Interpolate.h"
|
#include "process/detection/Interpolate.h"
|
||||||
#include "process/spectrum/SpectrumAnalyser.h"
|
#include "process/spectrum/SpectrumAnalyser.h"
|
||||||
#include "process/tracker/Tracker.h"
|
#include "process/tracker/Tracker.h"
|
||||||
|
#include "process/meta/Socket.h"
|
||||||
#include "data/meta/Constants.h"
|
#include "data/meta/Constants.h"
|
||||||
|
|
||||||
#include <ryml/ryml.hpp>
|
#include <ryml/ryml.hpp>
|
||||||
|
@ -118,40 +119,12 @@ int main(int argc, char **argv)
|
||||||
tree["network"]["ports"]["timing"] >> port_timing;
|
tree["network"]["ports"]["timing"] >> port_timing;
|
||||||
tree["network"]["ports"]["iqdata"] >> port_iqdata;
|
tree["network"]["ports"]["iqdata"] >> port_iqdata;
|
||||||
tree["network"]["ip"] >> ip;
|
tree["network"]["ip"] >> ip;
|
||||||
asio::io_service io_service;
|
Socket socket_map(ip, port_map);
|
||||||
asio::ip::tcp::socket socket_map(io_service);
|
Socket socket_detection(ip, port_detection);
|
||||||
asio::ip::tcp::socket socket_detection(io_service);
|
Socket socket_track(ip, port_track);
|
||||||
asio::ip::tcp::socket socket_track(io_service);
|
Socket socket_timestamp(ip, port_timestamp);
|
||||||
asio::ip::tcp::socket socket_timestamp(io_service);
|
Socket socket_timing(ip, port_timing);
|
||||||
asio::ip::tcp::socket socket_timing(io_service);
|
Socket socket_iqdata(ip, port_iqdata);
|
||||||
asio::ip::tcp::socket socket_iqdata(io_service);
|
|
||||||
asio::ip::tcp::endpoint endpoint_map;
|
|
||||||
asio::ip::tcp::endpoint endpoint_detection;
|
|
||||||
asio::ip::tcp::endpoint endpoint_track;
|
|
||||||
asio::ip::tcp::endpoint endpoint_timestamp;
|
|
||||||
asio::ip::tcp::endpoint endpoint_timing;
|
|
||||||
asio::ip::tcp::endpoint endpoint_iqdata;
|
|
||||||
endpoint_map = asio::ip::tcp::endpoint(
|
|
||||||
asio::ip::address::from_string(ip), port_map);
|
|
||||||
endpoint_detection = asio::ip::tcp::endpoint(
|
|
||||||
asio::ip::address::from_string(ip), port_detection);
|
|
||||||
endpoint_track = asio::ip::tcp::endpoint(
|
|
||||||
asio::ip::address::from_string(ip), port_track);
|
|
||||||
endpoint_timestamp = asio::ip::tcp::endpoint(
|
|
||||||
asio::ip::address::from_string(ip), port_timestamp);
|
|
||||||
endpoint_timing = asio::ip::tcp::endpoint(
|
|
||||||
asio::ip::address::from_string(ip), port_timing);
|
|
||||||
endpoint_iqdata = asio::ip::tcp::endpoint(
|
|
||||||
asio::ip::address::from_string(ip), port_iqdata);
|
|
||||||
socket_map.connect(endpoint_map);
|
|
||||||
socket_detection.connect(endpoint_detection);
|
|
||||||
socket_track.connect(endpoint_track);
|
|
||||||
socket_timestamp.connect(endpoint_timestamp);
|
|
||||||
socket_timing.connect(endpoint_timing);
|
|
||||||
socket_iqdata.connect(endpoint_iqdata);
|
|
||||||
asio::error_code err;
|
|
||||||
std::string subdata;
|
|
||||||
uint32_t MTU = 1024;
|
|
||||||
|
|
||||||
// setup process ambiguity
|
// setup process ambiguity
|
||||||
int32_t delayMin, delayMax;
|
int32_t delayMin, delayMax;
|
||||||
|
@ -278,11 +251,7 @@ int main(int argc, char **argv)
|
||||||
|
|
||||||
// output IqData meta data
|
// output IqData meta data
|
||||||
jsonIqData = x->to_json(time[0]/1000);
|
jsonIqData = x->to_json(time[0]/1000);
|
||||||
for (int i = 0; i < (jsonIqData.size() + MTU - 1) / MTU; i++)
|
socket_iqdata.sendData(jsonIqData);
|
||||||
{
|
|
||||||
subdata = jsonIqData.substr(i * MTU, MTU);
|
|
||||||
socket_iqdata.write_some(asio::buffer(subdata, subdata.size()), err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// output map data
|
// output map data
|
||||||
mapJson = map->to_json(time[0]/1000);
|
mapJson = map->to_json(time[0]/1000);
|
||||||
|
@ -291,30 +260,19 @@ int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
map->save(mapJson, saveMapPath);
|
map->save(mapJson, saveMapPath);
|
||||||
}
|
}
|
||||||
for (int i = 0; i < (mapJson.size() + MTU - 1) / MTU; i++)
|
socket_map.sendData(mapJson);
|
||||||
{
|
|
||||||
subdata = mapJson.substr(i * MTU, MTU);
|
|
||||||
socket_map.write_some(asio::buffer(subdata, subdata.size()), err);
|
|
||||||
}
|
|
||||||
// output detection data
|
// output detection data
|
||||||
detectionJson = detection->to_json(time[0]/1000);
|
detectionJson = detection->to_json(time[0]/1000);
|
||||||
detectionJson = detection->delay_bin_to_km(detectionJson, fs);
|
detectionJson = detection->delay_bin_to_km(detectionJson, fs);
|
||||||
for (int i = 0; i < (detectionJson.size() + MTU - 1) / MTU; i++)
|
socket_detection.sendData(detectionJson);
|
||||||
{
|
|
||||||
subdata = detectionJson.substr(i * MTU, MTU);
|
|
||||||
socket_detection.write_some(asio::buffer(subdata, subdata.size()), err);
|
|
||||||
}
|
|
||||||
delete detection;
|
delete detection;
|
||||||
delete detection1;
|
delete detection1;
|
||||||
delete detection2;
|
delete detection2;
|
||||||
|
|
||||||
// output tracker data
|
// output tracker data
|
||||||
jsonTracker = track->to_json(time[0]/1000);
|
jsonTracker = track->to_json(time[0]/1000);
|
||||||
for (int i = 0; i < (jsonTracker.size() + MTU - 1) / MTU; i++)
|
socket_track.sendData(jsonTracker);
|
||||||
{
|
|
||||||
subdata = jsonTracker.substr(i * MTU, MTU);
|
|
||||||
socket_track.write_some(asio::buffer(subdata, subdata.size()), err);
|
|
||||||
}
|
|
||||||
|
|
||||||
// output radar data timer
|
// output radar data timer
|
||||||
timing_helper(timing_name, timing_time, time, "output_radar_data");
|
timing_helper(timing_name, timing_time, time, "output_radar_data");
|
||||||
|
@ -329,13 +287,13 @@ int main(int argc, char **argv)
|
||||||
// output timing data
|
// output timing data
|
||||||
timing->update(time[0]/1000, timing_time, timing_name);
|
timing->update(time[0]/1000, timing_time, timing_name);
|
||||||
jsonTiming = timing->to_json();
|
jsonTiming = timing->to_json();
|
||||||
socket_timing.write_some(asio::buffer(jsonTiming, 1500), err);
|
socket_timing.sendData(jsonTiming);
|
||||||
timing_time.clear();
|
timing_time.clear();
|
||||||
timing_name.clear();
|
timing_name.clear();
|
||||||
|
|
||||||
// output CPI timestamp for updating data
|
// output CPI timestamp for updating data
|
||||||
std::string t0_string = std::to_string(time[0]/1000);
|
std::string t0_string = std::to_string(time[0]/1000);
|
||||||
socket_timestamp.write_some(asio::buffer(t0_string, 100), err);
|
socket_timestamp.sendData(t0_string);
|
||||||
time.clear();
|
time.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
27
src/process/meta/Socket.cpp
Normal file
27
src/process/meta/Socket.cpp
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
#include "Socket.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
asio::io_context Socket::io_context;
|
||||||
|
const uint32_t Socket::MTU = 1024;
|
||||||
|
|
||||||
|
Socket::Socket(const std::string& ip, uint16_t port)
|
||||||
|
: endpoint(asio::ip::address::from_string(ip), port), socket(io_context) {
|
||||||
|
socket.connect(endpoint);
|
||||||
|
}
|
||||||
|
|
||||||
|
Socket::~Socket()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Socket::sendData(const std::string& data) {
|
||||||
|
asio::error_code err;
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < (data.size() + MTU - 1) / MTU; ++i) {
|
||||||
|
std::string subdata = data.substr(i * MTU, MTU);
|
||||||
|
socket.write_some(asio::buffer(subdata, subdata.size()), err);
|
||||||
|
|
||||||
|
if (err) {
|
||||||
|
std::cerr << "Error sending data: " << err.message() << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
47
src/process/meta/Socket.h
Normal file
47
src/process/meta/Socket.h
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
/// @file Socket.h
|
||||||
|
/// @class Socket
|
||||||
|
/// @brief A class to implement network socket functionality.
|
||||||
|
/// @details Used to pass radar data from app to the API.
|
||||||
|
/// @author 30hours
|
||||||
|
|
||||||
|
#ifndef SOCKET_H
|
||||||
|
#define SOCKET_H
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <asio.hpp>
|
||||||
|
|
||||||
|
class Socket
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
/// @brief Common io_context for all socket objects.
|
||||||
|
static asio::io_context io_context;
|
||||||
|
|
||||||
|
/// @brief Common MTU size for all socket objects.
|
||||||
|
static const uint32_t MTU;
|
||||||
|
|
||||||
|
/// @brief The ASIO socket.
|
||||||
|
asio::ip::tcp::socket socket;
|
||||||
|
|
||||||
|
/// @brief The ASIO endpoint.
|
||||||
|
asio::ip::tcp::endpoint endpoint;
|
||||||
|
|
||||||
|
public:
|
||||||
|
/// @brief Constructor for Socket.
|
||||||
|
/// @param ip IP address of data destination.
|
||||||
|
/// @param port Port of data destination.
|
||||||
|
/// @return The object.
|
||||||
|
Socket(const std::string& ip, uint16_t port);
|
||||||
|
|
||||||
|
/// @brief Destructor.
|
||||||
|
/// @return Void.
|
||||||
|
~Socket();
|
||||||
|
|
||||||
|
/// @brief Helper function to send data in chunks.
|
||||||
|
/// @param data String of complete data to send.
|
||||||
|
/// @return Void.
|
||||||
|
void sendData(const std::string& data);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
Loading…
Reference in a new issue