forked from eclipse-iceoryx/iceoryx
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsingle_process.cpp
108 lines (88 loc) · 3.46 KB
/
single_process.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright (c) 2020 by Robert Bosch GmbH. 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.
#include "iceoryx_posh/iceoryx_posh_config.hpp"
#include "iceoryx_posh/internal/roudi/roudi_multi_process.hpp"
#include "iceoryx_posh/popo/publisher.hpp"
#include "iceoryx_posh/popo/subscriber.hpp"
#include "iceoryx_posh/roudi/iceoryx_roudi_components.hpp"
#include "iceoryx_posh/runtime/posh_runtime_single_process.hpp"
#include "iceoryx_utils/log/logmanager.hpp"
#include <atomic>
#include <chrono>
#include <cstdint>
#include <iostream>
#include <mutex>
#include <thread>
std::atomic_bool keepRunning{true};
struct TransmissionData_t
{
uint64_t counter;
};
void consoleOutput(const std::string& output)
{
static std::mutex consoleOutputMutex;
std::lock_guard<std::mutex> lock(consoleOutputMutex);
std::cout << output << std::endl;
}
void sender()
{
iox::popo::Publisher publisher({"Single", "Process", "Demo"});
publisher.offer();
uint64_t counter{0};
while (keepRunning.load())
{
auto sample = static_cast<TransmissionData_t*>(publisher.allocateChunk(sizeof(TransmissionData_t)));
sample->counter = counter++;
consoleOutput(std::string("Sending: " + std::to_string(sample->counter)));
publisher.sendChunk(sample);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
void receiver()
{
iox::popo::Subscriber subscriber({"Single", "Process", "Demo"});
uint64_t cacheQueueSize = 10;
subscriber.subscribe(cacheQueueSize);
while (keepRunning.load())
{
if (iox::popo::SubscriptionState::SUBSCRIBED == subscriber.getSubscriptionState())
{
const void* rawSample = nullptr;
while (subscriber.getChunk(&rawSample))
{
auto sample = static_cast<const TransmissionData_t*>(rawSample);
consoleOutput(std::string("Receiving : " + std::to_string(sample->counter)));
subscriber.releaseChunk(rawSample);
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
int main()
{
// set the log level to error to see the essence of the example
iox::log::LogManager::GetLogManager().SetDefaultLogLevel(iox::log::LogLevel::kError);
iox::RouDiConfig_t defaultRouDiConfig = iox::RouDiConfig_t().setDefaults();
iox::roudi::IceOryxRouDiComponents roudiComponents(defaultRouDiConfig);
iox::roudi::RouDiMultiProcess roudi(
roudiComponents.m_rouDiMemoryManager, roudiComponents.m_portManager, iox::roudi::MonitoringMode::OFF, false);
// create a single process runtime for inter thread communication
iox::runtime::PoshRuntimeSingleProcess runtime("/singleProcessDemo");
std::thread receiverThread(receiver), senderThread(sender);
// communicate for 2 seconds and then stop the example
std::this_thread::sleep_for(std::chrono::seconds(2));
keepRunning.store(false);
senderThread.join();
receiverThread.join();
}