This commit is contained in:
2024-10-30 18:20:16 +03:00
parent f14b4fdc10
commit 9cde583688
5 changed files with 125 additions and 16 deletions

View File

@@ -1,12 +1,86 @@
#include <cxxopts.hpp>
#include <iostream>
#include "../device/adc_device.h"
#include "../device/adc_device_attribute.h"
#include "../device/adc_device_command.h"
#include "../net/adc_endpoint.h"
#include "../net/adc_netproto.h"
#include "../net/asio/adc_device_netserver_asio.h"
class Device1 : public adc::AdcGenericDevice<std::string,
adc::AdcDeviceAttribute<std::string>,
adc::AdcDeviceCommand<std::string>>
{
typedef adc::AdcGenericDevice<std::string, adc::AdcDeviceAttribute<std::string>, adc::AdcDeviceCommand<std::string>>
base_t;
public:
template <adc::traits::adc_char_range R>
Device1(R&& id) : base_t(id)
{
if constexpr (std::is_array_v<std::decay_t<R>>) {
this->_ident = std::forward<R>(id);
} else {
std::ranges::copy(std::forward<R>(id), std::back_inserter(this->_ident));
}
}
};
class Device2
: public adc::AdcGenericDevice<std::string, adc::AdcDeviceAttribute<size_t>, adc::AdcDeviceCommand<std::string>>
{
typedef adc::AdcGenericDevice<std::string, adc::AdcDeviceAttribute<size_t>, adc::AdcDeviceCommand<std::string>>
base_t;
public:
template <adc::traits::adc_char_range R>
Device2(R&& id) : base_t(id)
{
if constexpr (std::is_array_v<std::decay_t<R>>) {
this->_ident = std::forward<R>(id);
} else {
std::ranges::copy(std::forward<R>(id), std::back_inserter(this->_ident));
}
}
};
using t_t = std::tuple<float, std::string>;
static t_t tuple_val{7.7, "TUPVAL"};
t_t gt()
{
return tuple_val;
}
void st(const t_t& v)
{
tuple_val = v;
}
int main(int argc, char* argv[])
{
/* CREATE AND INIT DEVICES */
int dev1_val1 = 1;
Device1 dev1("DEVICE#1");
Device2 dev2("DEVICE#2");
// device#1
dev1.addCommand({"DEV1::COM1", []() { std::cout << "EXEC DEV1::COM1\n"; }});
dev1.addAttribute(
{"DEV1::ATTR1", [&dev1_val1]() { return dev1_val1; }, [&dev1_val1](const int& v) { dev1_val1 = v; }});
dev1.addAttribute({"DEV1::ATTR2", gt, st});
// device#2
// read-only attr
dev2.addAttribute({0x1, [&dev1_val1]() { return dev1_val1; }});
// write-only
dev2.addAttribute({0x2, [&dev1_val1](const int& v) { dev1_val1 = v; }});
/* COMMANDLINE OPTS */
cxxopts::Options options(argv[0], "ADC-library test device network server (ASIO implementation)\n");
@@ -14,10 +88,14 @@ int main(int argc, char* argv[])
options.add_options()("h,help", "Print usage");
options.add_options()(
"endpoints", "endpoints server will be listening for",
cxxopts::value<std::vector<std::string>>()->default_value("local://stream/ADC_ASIO_TEST_SERVER"));
// cxxopts::value<std::vector<std::string>>()->default_value("local://stream/@ADC_ASIO_TEST_SERVER"));
"endpoints",
"endpoints server will be listening for. For 'local' endpoint the '@' symbol at the beginning of the path "
"means "
"abstract namespace socket.",
cxxopts::value<std::vector<std::string>>()->default_value("local://stream/@ADC_ASIO_TEST_SERVER"));
options.positional_help("[endpoint0] [enpoint1] ... [endpointN]");
options.parse_positional({"endpoints"});
try {
@@ -25,6 +103,11 @@ int main(int argc, char* argv[])
if (opt_result["help"].count()) {
std::cout << options.help();
std::cout << "\n";
std::cout << "[endpoint0] [enpoint1] ... [endpointN] - endpoints server will be listening for. For 'local' "
"endpoint the '@' symbol at the beginning of the path "
"means abstract namespace socket."
<< "\n";
return 0;
}
@@ -36,6 +119,10 @@ int main(int argc, char* argv[])
adc::impl::AdcDeviceNetServerASIO server("TEST SRV", io_ctx);
server.setupSignals();
server.addDevice(&dev1);
server.addDevice(&dev2, adc::traits::adc_char_identity<std::string>{},
adc::utils::AdcDefaultValueConverter<>::serialize<std::string, Device2::attr_ident_t>);
auto epnt = opt_result["endpoints"].as<std::vector<std::string>>();
for (auto& ep : epnt) {

View File

@@ -96,7 +96,9 @@ TEST_CASE("[ADC NET MESSAGE]")
std::cout << std::boolalpha << "IS UDP: " << prs.isUDP() << "\n";
std::cout << "\n";
std::string bs = "local://seqpaCKet:dwe/tmp.dev/213";
std::string bs = "local://seqpaCKet:dwe/@tmp.dev/213";
auto it = std::ranges::find(bs, '@');
*it = '\0';
std::cout << "EPT CHAR RANGE: " << bs << "\n";
prs = AdcEndpointParser(bs);
std::cout << std::boolalpha << "IS VALID: " << prs.isValid() << "\n";
@@ -104,6 +106,7 @@ TEST_CASE("[ADC NET MESSAGE]")
std::cout << "HOST: [" << prs.host() << "]\n";
std::cout << "PORT: [" << prs.port() << "]\n";
std::cout << "PATH: [" << prs.path() << "]\n";
std::cout << "PATH SZ: [" << prs.path().size() << "]\n";
std::cout << std::boolalpha << "IS LOCAL STREAM: " << prs.isLocalStream() << "\n";
std::cout << std::boolalpha << "IS LOCAL SEQPACKET: " << prs.isLocalSeqpacket() << "\n";
std::cout << std::boolalpha << "IS UDP: " << prs.isUDP() << "\n";