Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
build/
.cache/
.vs
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ target_sources(multicast PRIVATE
udp.cpp
serial.cpp
serial.hpp
)
"client.cpp" "udp_client.cpp" "serial_client.cpp")
21 changes: 21 additions & 0 deletions client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "client.hpp"
#include <string_view>

namespace multicast
{
namespace client
{

Client::Client()
{

}

bool Client::send(std::string_view message)
{
(void)message;
return true;
}

} // namespace client
} // namespace multicast
18 changes: 18 additions & 0 deletions client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include <string_view>

namespace multicast
{
namespace client
{

class Client
{
public:
Client();

bool send(std::string_view message);
};

} // namespace client
} // namespace multicast
1 change: 1 addition & 0 deletions config.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <filesystem>
#include <string_view>
#include <vector>
Expand Down
15 changes: 11 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include "config.hpp"
#include "udp.hpp"
#include "client.hpp"
#include "udp_client.hpp"
#include "serial_client.hpp"

namespace
{
Expand Down Expand Up @@ -45,23 +48,27 @@ int main(int argc, char* argv[])
Config config{argv[1]};
udp::Socket socket{};

std::vector<client::Client> 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{};
while (print_prompt(), std::getline(std::cin, input))
{
input.push_back('\n');

// TODO: отправьте всем клиентам
for (client::Client& client : clients)
{
client.send(input);
}
}
return EXIT_SUCCESS;
}
Expand Down
12 changes: 12 additions & 0 deletions serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <utility>

#include "serial.hpp"

Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions serial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
19 changes: 19 additions & 0 deletions serial_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <string_view>
#include "serial_client.hpp"
#include <utility>

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
23 changes: 23 additions & 0 deletions serial_client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once
#include <string_view>
#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
1 change: 1 addition & 0 deletions udp.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma once
#include <string_view>

#include <netinet/in.h>
Expand Down
21 changes: 21 additions & 0 deletions udp_client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <string_view>
#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
24 changes: 24 additions & 0 deletions udp_client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include <string_view>
#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