forked from eclipse-iceoryx/iceoryx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiox_publisher_with_options.cpp
90 lines (69 loc) · 2.97 KB
/
iox_publisher_with_options.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2020 - 2021 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#include "topic_data.hpp"
#include "iceoryx_dust/posix_wrapper/signal_watcher.hpp"
#include "iceoryx_posh/popo/publisher.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include <iostream>
constexpr char APP_NAME[] = "iox-cpp-publisher-with-options";
int main()
{
iox::runtime::PoshRuntime::initRuntime(APP_NAME);
// create publisher with some options set
iox::popo::PublisherOptions publisherOptions;
// the publishers stores the last 10 samples for possible late joiners
//! [history capacity]
publisherOptions.historyCapacity = 10U;
//! [history capacity]
// when the publisher is created, it is not yet visible
//! [offer on create]
publisherOptions.offerOnCreate = false;
//! [offer on create]
// grouping of publishers and subscribers within a process
//! [node name]
publisherOptions.nodeName = "Pub_Node_With_Options";
//! [node name]
// we allow the subscribers to block the publisher if they want to ensure that no samples are lost
//! [too slow policy]
publisherOptions.subscriberTooSlowPolicy = iox::popo::ConsumerTooSlowPolicy::WAIT_FOR_CONSUMER;
//! [too slow policy]
//! [create publisher with options]
iox::popo::Publisher<RadarObject> publisher({"Radar", "FrontLeft", "Object"}, publisherOptions);
//! [create publisher with options]
// we have to explicitely offer the publisher for making it visible to subscribers
//! [offer]
publisher.offer();
//! [offer]
double ct = 0.0;
std::thread publishData([&]() {
while (!iox::posix::hasTerminationRequested())
{
++ct;
// Retrieve a sample, construct it with the given arguments and publish it via a lambda.
publisher.loan(ct, ct, ct).and_then([](auto& sample) { sample.publish(); });
std::cout << APP_NAME << " sent value: " << ct << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(400));
}
});
iox::posix::waitForTerminationRequest();
// this is optional, but since the iox::popo::ConsumerTooSlowPolicy::WAIT_FOR_CONSUMER option is used,
// a slow subscriber might block the shutdown and this call unblocks the publisher
//! [shutdown]
iox::runtime::PoshRuntime::getInstance().shutdown();
//! [shutdown]
publishData.join();
return 0;
}