-
Notifications
You must be signed in to change notification settings - Fork 12
/
iox_automotive_proxy.cpp
188 lines (168 loc) · 7.16 KB
/
iox_automotive_proxy.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2022 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 "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_watcher.hpp"
//! [include proxy]
#include "minimal_proxy.hpp"
//! [include proxy]
#include "owl/runtime.hpp"
#include <iostream>
using namespace owl;
using namespace iox::cxx;
constexpr char APP_NAME[] = "iox-cpp-automotive-proxy";
int main()
{
//! [create runtime]
Runtime::GetInstance(APP_NAME);
//! [create runtime]
//! [wrap proxy]
iox::concurrent::smart_lock<optional<MinimalProxy>> maybeProxy;
//! [wrap proxy]
optional<kom::FindServiceCallbackHandle> maybeHandle;
//! [synchronous discovery]
kom::InstanceIdentifier exampleInstanceSearchQuery(TruncateToCapacity, "ExampleInstance");
std::cout << "Searching for instances of '" << MinimalProxy::SERVICE_IDENTIFIER << "' called '"
<< exampleInstanceSearchQuery.c_str() << "':" << std::endl;
auto handleContainer = MinimalProxy::FindService(exampleInstanceSearchQuery);
//! [synchronous discovery]
if (!handleContainer.empty())
{
//! [create proxy]
for (auto& handle : handleContainer)
{
std::cout << " Found instance of service: '" << MinimalProxy::SERVICE_IDENTIFIER << "', '"
<< handle.GetInstanceId().c_str() << "'" << std::endl;
maybeProxy->emplace(handle);
break;
}
//! [create proxy]
}
else
{
std::cout << " Found no service(s), setting up asynchronous search with 'EnableFindServiceCallback'!"
<< std::endl;
auto callback = [&](kom::ServiceHandleContainer<owl::kom::ProxyHandleType> container,
kom::FindServiceCallbackHandle) -> void {
//! [destroy proxy asynchronously]
if (container.empty())
{
std::cout << " Instance '" << maybeProxy->value().m_instanceIdentifier.c_str() << "' of service '"
<< MinimalProxy::SERVICE_IDENTIFIER << "' has disappeared." << std::endl;
maybeProxy->value().m_event.UnsetReceiveCallback();
maybeProxy->reset();
return;
}
//! [destroy proxy asynchronously]
//! [create proxy asynchronously]
for (auto& proxyHandle : container)
{
if (!maybeProxy->has_value())
{
std::cout << " Found instance of service: '" << MinimalProxy::SERVICE_IDENTIFIER << "', '"
<< proxyHandle.GetInstanceId().c_str() << "'" << std::endl;
maybeProxy->emplace(proxyHandle);
break;
}
}
//! [create proxy asynchronously]
};
//! [set up asynchronous search]
auto handle = MinimalProxy::EnableFindServiceCallback(callback, exampleInstanceSearchQuery);
//! [set up asynchronous search]
maybeHandle.emplace(handle);
std::cout << " Waiting for instance called '" << exampleInstanceSearchQuery.c_str()
<< "' to become available.." << std::endl;
}
//! [Field: create ints for computeSum]
uint64_t addend1{0};
uint64_t addend2{0};
//! [Field: create ints for computeSum]
while (!iox::posix::hasTerminationRequested())
{
{
//! [gain exclusive access to proxy]
auto proxyGuard = maybeProxy.getScopeGuard();
if (proxyGuard->has_value())
{
auto& proxy = proxyGuard->value();
//! [gain exclusive access to proxy]
//! [Event: receiveCallback]
auto onReceive = [&]() -> void {
proxy.m_event.TakeNewSamples([](const auto& topic) {
auto finish = std::chrono::steady_clock::now();
std::cout << "Event: value is " << topic->counter << std::endl;
auto duration =
std::chrono::duration_cast<std::chrono::nanoseconds>(finish - topic->sendTimestamp);
std::cout << "Event: latency (ns) is " << duration.count() << std::endl;
});
};
//! [Event: receiveCallback]
//! [Event: set receiveCallback]
if (!proxy.m_event.HasReceiveCallback())
{
proxy.m_event.Subscribe(10U);
proxy.m_event.SetReceiveCallback(onReceive);
}
//! [Event: set receiveCallback]
//! [Field: get and set data]
auto fieldFuture = proxy.m_field.Get();
try
{
auto result = fieldFuture.get();
std::cout << "Field: value is " << result.counter << std::endl;
if (result.counter >= 4242)
{
result.counter++;
proxy.m_field.Set(result);
std::cout << "Field: value set to " << result.counter << std::endl;
}
}
catch (const std::future_error&)
{
std::cout << "Empty future from field received, please start the 'iox-cpp-automotive-skeleton'."
<< std::endl;
}
//! [Field: get and set data]
//! [Method: call computeSum remotely]
auto methodFuture = proxy.computeSum(addend1, addend2);
try
{
auto result = methodFuture.get();
std::cout << "Method: result of " << std::to_string(addend1) << " + " << std::to_string(addend2)
<< " = " << result.sum << std::endl;
}
catch (const std::future_error&)
{
std::cout << "Empty future from method received, please start the 'iox-cpp-automotive-skeleton'."
<< std::endl;
}
//! [Method: call computeSum remotely]
addend1 += addend2 + addend2;
addend2++;
std::cout << std::endl;
}
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
//! [stop find service]
if (maybeHandle.has_value())
{
MinimalProxy::DisableFindServiceCallback(maybeHandle.value());
}
//! [stop find service]
return (EXIT_SUCCESS);
}