diff --git a/.gitignore b/.gitignore index a4fb4fb..d877c2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ build/ .cache/ +.vs \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index a61bd74..be82c96 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,4 +15,4 @@ target_sources(multicast PRIVATE udp.cpp serial.cpp serial.hpp -) + "client.cpp" "udp_client.cpp" "serial_client.cpp") diff --git a/client.cpp b/client.cpp new file mode 100644 index 0000000..c95e148 --- /dev/null +++ b/client.cpp @@ -0,0 +1,21 @@ +#include "client.hpp" +#include + +namespace multicast +{ +namespace client +{ + +Client::Client() +{ + +} + +bool Client::send(std::string_view message) +{ + (void)message; + return true; +} + +} // namespace client +} // namespace multicast \ No newline at end of file diff --git a/client.hpp b/client.hpp new file mode 100644 index 0000000..1ea3509 --- /dev/null +++ b/client.hpp @@ -0,0 +1,18 @@ +#pragma once +#include + +namespace multicast +{ +namespace client +{ + +class Client +{ +public: + Client(); + + bool send(std::string_view message); +}; + +} // namespace client +} // namespace multicast \ No newline at end of file diff --git a/config.hpp b/config.hpp index 1e82971..1b1b2cd 100644 --- a/config.hpp +++ b/config.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include #include diff --git a/main.cpp b/main.cpp index a709ecb..3657626 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,9 @@ #include "config.hpp" #include "udp.hpp" +#include "client.hpp" +#include "udp_client.hpp" +#include "serial_client.hpp" namespace { @@ -45,15 +48,16 @@ int main(int argc, char* argv[]) Config config{argv[1]}; udp::Socket socket{}; + std::vector clients; + for ([[maybe_unused]] const auto& entry : config.GetUdpEntries()) { - // TODO: добавьте клиента (используйте объект socket) + clients.push_back(udp_client::UdpClient(&socket, entry.address)); } for ([[maybe_unused]] const auto& entry : config.GetSerialEntries()) { - // TODO: добавьте клиента (для каждого клиента создавайте - // новые объекты serial::Device) + clients.push_back(serial_client::SerialClient(serial::Device(entry.device))); } std::string input{}; @@ -61,7 +65,10 @@ int main(int argc, char* argv[]) { input.push_back('\n'); - // TODO: отправьте всем клиентам + for (client::Client& client : clients) + { + client.send(input); + } } return EXIT_SUCCESS; } diff --git a/serial.cpp b/serial.cpp index 0edeb76..180cf95 100644 --- a/serial.cpp +++ b/serial.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "serial.hpp" @@ -52,6 +53,17 @@ int Device::Handler() const noexcept return fd_; } +Device::Device(Device&& other) noexcept : fd_(std::exchange(other.fd_, -1)) { } + +Device& Device::operator=(Device&& other) noexcept +{ + if (this != &other) + { + fd_ = std::exchange(other.fd_, -1); + } + return *this; +} + bool send(const Device& device, std::string_view message) { return write(device.Handler(), message.data(), message.size()) >= 0; diff --git a/serial.hpp b/serial.hpp index cbf833e..489cd02 100644 --- a/serial.hpp +++ b/serial.hpp @@ -12,6 +12,8 @@ class Device public: Device(std::string_view path); int Handler() const noexcept; + Device(Device&&) noexcept; + Device& operator=(Device&&) noexcept; private: int fd_; diff --git a/serial_client.cpp b/serial_client.cpp new file mode 100644 index 0000000..2193c5a --- /dev/null +++ b/serial_client.cpp @@ -0,0 +1,19 @@ +#include +#include "serial_client.hpp" +#include + +namespace multicast +{ +namespace serial_client +{ + +SerialClient::SerialClient(serial::Device&& device_) : device(std::move(device_)) +{ } + +bool SerialClient::send(std::string_view message) +{ + return serial::send(device, message); +} + +} // namespace serial_client +} // namespace multicast \ No newline at end of file diff --git a/serial_client.hpp b/serial_client.hpp new file mode 100644 index 0000000..1b621de --- /dev/null +++ b/serial_client.hpp @@ -0,0 +1,23 @@ +#pragma once +#include +#include "client.hpp" +#include "serial.hpp" + +namespace multicast +{ +namespace serial_client +{ + +class SerialClient : public client::Client +{ +public: + SerialClient(serial::Device& device); + + bool send(std::string_view message); + +private: + const serial::Device device; +}; + +} // namespace serial_client +} // namespace multicast \ No newline at end of file diff --git a/udp.hpp b/udp.hpp index 54de239..6fd770c 100644 --- a/udp.hpp +++ b/udp.hpp @@ -1,3 +1,4 @@ +#pragma once #include #include diff --git a/udp_client.cpp b/udp_client.cpp new file mode 100644 index 0000000..bdc19f5 --- /dev/null +++ b/udp_client.cpp @@ -0,0 +1,21 @@ +#include +#include "udp_client.hpp" + +namespace multicast +{ +namespace udp_client +{ + +UdpClient::UdpClient(const udp::Socket* socket, const sockaddr_in& address) + : socket(socket), address(address) +{ +} + +bool UdpClient::send(std::string_view message) +{ + if (!socket) return false; + return udp::send(*socket, address, message); +} + +} // namespace udp_client +} // namespace multicast \ No newline at end of file diff --git a/udp_client.hpp b/udp_client.hpp new file mode 100644 index 0000000..9ffb358 --- /dev/null +++ b/udp_client.hpp @@ -0,0 +1,24 @@ +#pragma once +#include +#include "client.hpp" +#include "udp.hpp" + +namespace multicast +{ +namespace udp_client +{ + +class UdpClient: public client::Client +{ +public: + UdpClient(const udp::Socket* socket, const sockaddr_in& address); + + bool send(std::string_view message); + +private: + const udp::Socket* socket; + sockaddr_in address{}; +}; + +} // namespace udp_client +} // namespace multicast \ No newline at end of file