iceoryx QOS 策略

一、PublisherOptions & SubscriberOptions

1、PublisherOptions

/// @brief This struct is used to configure the publisher
struct PublisherOptions
{
    /// @brief The size of the history chunk queue
    uint64_t historyCapacity{0U};

    /// @brief The name of the node where the publisher should belong to
    iox::NodeName_t nodeName{""};

    /// @brief The option whether the publisher should already be offered when creating it
    bool offerOnCreate{true};

    /// @brief The option whether the publisher should block when the subscriber queue is full
    ConsumerTooSlowPolicy subscriberTooSlowPolicy{ConsumerTooSlowPolicy::DISCARD_OLDEST_DATA};

    /// @brief serialization of the PublisherOptions
    cxx::Serialization serialize() const noexcept;
    /// @brief deserialization of the PublisherOptions
    static cxx::expected<PublisherOptions, cxx::Serialization::Error>
    deserialize(const cxx::Serialization& serialized) noexcept;
};

详细解释

  1. historyCapacity:历史数据块队列的大小。这决定了发布者在发布数据时保留的历史数据块数量。

  2. nodeName:发布者所属节点的名称。

  3. offerOnCreate:一个布尔选项,用于指示发布者在创建时是否已经提供数据。

  4. subscriberTooSlowPolicy:当订阅者队列已满时,发布者是否应该阻塞的选项。默认行为是丢弃最旧的数据。

  5. serialize():发布者选项的序列化方法,用于将 PublisherOptions 转换为可存储或传输的格式。

  6. deserialize():发布者选项的反序列化方法,用于将序列化的数据转换回 PublisherOptions 对象。

这些注释和变量用于配置一个发布者的行为和属性,确保在创建和使用发布者时能够正确处理数据发布和队列管理。

PublisherOptions 示例

#include "iceoryx_posh/popo/publisher.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iceoryx_utils/cxx/optional.hpp"
#include <iostream>

struct MyData
{
    int value;
};

int main()
{
    iox::runtime::PoshRuntime::initRuntime("publisher");

    // 配置 PublisherOptions
    iox::popo::PublisherOptions publisherOptions;
    publisherOptions.historyCapacity = 10; // 设置历史缓冲区容量
    publisherOptions.nodeName = "MyNode";  // 设置节点名称    // 创建 Publisher
    iox::popo::Publisher<MyData> publisher({"Example", "Publisher", "MyData"}, publisherOptions);

    // 发布数据
    for (int i = 0; i < 10; ++i)
    {
        publisher.loan()
            .and_then([&](auto& sample) {
                sample->value = i;
                sample.publish();
            })
            .or_else([](auto& error) {
                std::cerr << "Could not loan sample! Error: " << error << std::endl;
            });
    }    return 0;
}

2、SubscriberOptions

/// @brief This struct is used to configure the subscriber
struct SubscriberOptions
{
    /// @brief The size of the receiver queue where chunks are stored before they are passed to the user
    /// @attention Depending on the underlying queue there can be a different overflow behavior
    uint64_t queueCapacity{SubscriberChunkQueueData_t::MAX_CAPACITY};

    /// @brief The max number of chunks received after subscription if chunks are available
    uint64_t historyRequest{0U};

    /// @brief The name of the node where the subscriber should belong to
    iox::NodeName_t nodeName{""};

    /// @brief The option whether the subscriber shall try to subscribe when creating it
    bool subscribeOnCreate{true};

    /// @brief The option whether the publisher should block when the subscriber queue is full
    QueueFullPolicy queueFullPolicy{QueueFullPolicy::DISCARD_OLDEST_DATA};

    /// @brief Indicates whether to enforce history support of the publisher,
    ///        i.e. require historyCapacity > 0 to be eligible to be connected
    bool requiresPublisherHistorySupport{false};

    /// @brief serialization of the SubscriberOptions
    cxx::Serialization serialize() const noexcept;
    /// @brief deserialization of the SubscriberOptions
    static cxx::expected<SubscriberOptions, cxx::Serialization::Error>
    deserialize(const cxx::Serialization& serialized) noexcept;
};

详细解释

  1. queueCapacity:接收队列的大小。在数据块传递给用户之前,它们会存储在这个队列中。注意,不同的底层队列可能会有不同的溢出行为。

  2. historyRequest:订阅后接收的最大数据块数量(如果有可用数据块)。

  3. nodeName:订阅者所属节点的名称。

  4. subscribeOnCreate:一个布尔选项,用于指示订阅者在创建时是否尝试订阅。

  5. queueFullPolicy:当订阅者队列已满时,发布者是否应该阻塞的选项。默认行为是丢弃最旧的数据。

  6. requiresPublisherHistorySupport:指示是否强制要求发布者支持历史记录,即要求 historyCapacity 大于 0 才有资格连接。

  7. serialize():订阅者选项的序列化方法,用于将 SubscriberOptions 转换为可存储或传输的格式。

  8. deserialize():订阅者选项的反序列化方法,用于将序列化的数据转换回 SubscriberOptions 对象。

这些注释和变量用于配置一个订阅者的行为和属性,确保在创建和使用订阅者时能够正确处理数据传递和队列管理。

SubscriberOptions 示例

#include "iceoryx_posh/popo/subscriber.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include <iostream>

struct MyData
{
    int value;
};

int main()
{
    iox::runtime::PoshRuntime::initRuntime("subscriber");

    // 配置 SubscriberOptions
    iox::popo::SubscriberOptions subscriberOptions;
    subscriberOptions.queueCapacity = 10; // 设置队列容量
    subscriberOptions.nodeName = "MyNode"; // 设置节点名称

    // 创建 Subscriber
    iox::popo::Subscriber<MyData> subscriber({"Example", "Publisher", "MyData"}, subscriberOptions);

    // 订阅数据
    subscriber.subscribe();

    while (true)
    {
        subscriber.take()
            .and_then([](auto& sample) {
                std::cout << "Received value: " << sample->value << std::endl;
            })
            .or_else([](auto& error) {
                std::cerr << "Could not take sample! Error: " << error << std::endl;
            });

        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    return 0;
}

二、ClientOptions & ServerOptions

1、ClientOptions

/// @brief This struct is used to configure the client
struct ClientOptions
{
    /// @brief The size of the response queue where chunks are stored before they are passed to the user
    /// @attention Depending on the underlying queue there can be a different overflow behavior
    uint64_t responseQueueCapacity{ClientChunkQueueData_t::MAX_CAPACITY};

    /// @brief The name of the node where the client should belong to
    iox::NodeName_t nodeName{""};

    /// @brief The option whether the client shall try to connect when creating it
    bool connectOnCreate{true};

    /// @brief The option whether the server should block when the response queue is full
    /// @note Corresponds with ServerOptions::clientTooSlowPolicy
    QueueFullPolicy responseQueueFullPolicy{QueueFullPolicy::DISCARD_OLDEST_DATA};

    /// @brief The option whether the client should block when the request queue is full
    /// @note Corresponds with ServerOptions::requestQueueFullPolicy
    ConsumerTooSlowPolicy serverTooSlowPolicy{ConsumerTooSlowPolicy::DISCARD_OLDEST_DATA};

    /// @brief serialization of the ClientOptions
    cxx::Serialization serialize() const noexcept;
    /// @brief deserialization of the ClientOptions
    static cxx::expected<ClientOptions, cxx::Serialization::Error>
    deserialize(const cxx::Serialization& serialized) noexcept;

    /// @brief comparison operator
    /// @param[in] rhs the right hand side of the comparison
    bool operator==(const ClientOptions& rhs) const noexcept;
};

详细解释

  1. responseQueueCapacity:响应队列的大小。在数据块传递给用户之前,它们会存储在这个队列中。注意,不同的底层队列可能会有不同的溢出行为。

  2. nodeName:客户端所属节点的名称。

  3. connectOnCreate:一个布尔选项,用于指示客户端在创建时是否尝试连接。

  4. responseQueueFullPolicy:当响应队列已满时,服务器是否应该阻塞的选项。注意,这对应于 ServerOptions::clientTooSlowPolicy

  5. serverTooSlowPolicy:当请求队列已满时,客户端是否应该阻塞的选项。注意,这对应于 ServerOptions::requestQueueFullPolicy

  6. serialize():客户端选项的序列化方法,用于将 ClientOptions 转换为可存储或传输的格式。

  7. deserialize():客户端选项的反序列化方法,用于将序列化的数据转换回 ClientOptions 对象。

  8. operator==:比较运算符,用于比较两个 ClientOptions 对象是否相等。

这些注释和变量用于配置一个客户端的行为和属性,确保在创建和使用客户端时能够正确处理数据传递和队列管理。

ClientOptions 示例

#include "iceoryx_posh/popo/client.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iceoryx_utils/cxx/optional.hpp"
#include <iostream>

struct Request
{
    int number;
};

struct Response
{
    int result;
};

int main()
{
    iox::runtime::PoshRuntime::initRuntime("client");

    // 配置 ClientOptions
    iox::popo::ClientOptions clientOptions;
    clientOptions.responseQueueCapacity = 10; // 设置响应队列容量
    clientOptions.nodeName = "MyClientNode";  // 设置节点名称

    // 创建 Client
    iox::popo::Client<Request, Response> client({"Example", "Service", "RequestResponse"}, clientOptions);

    // 发送请求
    client.loan()
        .and_then([&](auto& request) {
            request->number = 42;
            request.send();
        })
        .or_else([](auto& error) {
            std::cerr << "Could not loan request! Error: " << error << std::endl;
        });    // 接收响应
    client.take()
        .and_then([](auto& response) {
            std::cout << "Received result: " << response->result << std::endl;
        })
        .or_else([](auto& error) {
            std::cerr << "Could not take response! Error: " << error << std::endl;
        });

    return 0;
}

2、ServerOptions

/// @brief This struct is used to configure the server
struct ServerOptions
{
    /// @brief The size of the request queue where chunks are stored before they are passed to the user
    /// @attention Depending on the underlying queue there can be a different overflow behavior
    uint64_t requestQueueCapacity{ServerChunkQueueData_t::MAX_CAPACITY};

    /// @brief The name of the node where the server should belong to
    iox::NodeName_t nodeName{""};

    /// @brief The option whether the server should already be offered when creating it
    bool offerOnCreate{true};

    /// @brief The option whether the client should block when the request queue is full
    /// @note Corresponds with ClientOptions::serverTooSlowPolicy
    QueueFullPolicy requestQueueFullPolicy{QueueFullPolicy::DISCARD_OLDEST_DATA};

    /// @brief The option whether the server should block when the response queue is full
    /// @note Corresponds with ClientOptions::responseQueueFullPolicy
    ConsumerTooSlowPolicy clientTooSlowPolicy{ConsumerTooSlowPolicy::DISCARD_OLDEST_DATA};

    /// @brief serialization of the ServerOptions
    cxx::Serialization serialize() const noexcept;
    /// @brief deserialization of the ServerOptions
    static cxx::expected<ServerOptions, cxx::Serialization::Error>
    deserialize(const cxx::Serialization& serialized) noexcept;

    /// @brief comparison operator
    /// @param[in] rhs the right hand side of the comparison
    bool operator==(const ServerOptions& rhs) const noexcept;
};

详细解释

  1. requestQueueCapacity:请求队列的大小。在数据块传递给用户之前,它们会存储在这个队列中。注意,不同的底层队列可能会有不同的溢出行为。

  2. nodeName:服务器所属节点的名称。

  3. offerOnCreate:一个布尔选项,用于指示服务器在创建时是否已经提供服务。

  4. requestQueueFullPolicy:当请求队列已满时,客户端是否应该阻塞的选项。注意,这对应于 ClientOptions::serverTooSlowPolicy

  5. clientTooSlowPolicy:当响应队列已满时,服务器是否应该阻塞的选项。注意,这对应于 ClientOptions::responseQueueFullPolicy

  6. serialize():服务器选项的序列化方法,用于将 ServerOptions 转换为可存储或传输的格式。

  7. deserialize():服务器选项的反序列化方法,用于将序列化的数据转换回 ServerOptions 对象。

  8. operator==:比较运算符,用于比较两个 ServerOptions 对象是否相等。

这些注释和变量用于配置一个服务器的行为和属性,确保在创建和使用服务器时能够正确处理数据传递和队列管理。

ServerOptions 示例

#include "iceoryx_posh/popo/server.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include <iostream>struct Request
{
    int number;
};

struct Response
{
    int result;
};

int main()
{
    iox::runtime::PoshRuntime::initRuntime("server");

    // 配置 ServerOptions
    iox::popo::ServerOptions serverOptions;
    serverOptions.requestQueueCapacity = 10; // 设置请求队列容量
    serverOptions.nodeName = "MyServerNode"; // 设置节点名称

    // 创建 Server
    iox::popo::Server<Request, Response> server({"Example", "Service", "RequestResponse"}, serverOptions);

    // 处理请求并发送响应
    while (true)
    {
        server.take()
            .and_then([&](auto& request) {
                server.loan()
                    .and_then([&](auto& response) {
                        response->result = request->number * 2;
                        response.send();
                    })
                    .or_else([](auto& error) {
                        std::cerr << "Could not loan response! Error: " << error << std::endl;
                    });
            })
            .or_else([](auto& error) {
                std::cerr << "Could not take request! Error: " << error << std::endl;
            });        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值