Skip to content

Commit

Permalink
iox-eclipse-iceoryx#218 Move common publisher logic into base class.
Browse files Browse the repository at this point in the history
Signed-off-by: Ithier Jeff (CC-AD/EYF1) <[email protected]>
  • Loading branch information
orecham committed Aug 31, 2020
1 parent 7bb3f94 commit f60ade1
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 64 deletions.
20 changes: 14 additions & 6 deletions iceoryx_examples/icedelivery/iox_publisher_modern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "iceoryx_posh/experimental/popo/publisher.hpp"
#include "iceoryx_posh/experimental/popo/typed_publisher.hpp"
#include "iceoryx_posh/runtime/posh_runtime.hpp"

#include <iostream>
#include <chrono>
Expand Down Expand Up @@ -42,22 +43,29 @@ void getVehiclePosition(Position* allocation)
int main(int argc, char *argv[])
{
iox::runtime::PoshRuntime::getInstance("/iox-ex-publisher-modern");
auto publisher = iox::popo::Publisher<Position>({"Odometry", "Position", "Vehicle"});
publisher.offer();

auto typedPublisher = iox::popo::TypedPublisher<Position>({"Odometry", "Position", "Vehicle"});
//auto untypedPublisher = iox::popo::Publisher<iox::popo::Untyped>({"Odometry", "Position", "Vehicle"});

typedPublisher.offer();
//untypedPublisher.offer();

float_t ct = 0.0;
while(!killswitch)
{
publisher.loan()
typedPublisher.loan()
.and_then([&](iox::popo::Sample<Position>& sample){
std::cout << "Got sample" << std::endl;
++ct;
sample.emplace(ct, ct, ct);
sample.publish();
});
std::this_thread::sleep_for(std::chrono::seconds(1));

publisher.publishResultOf(getVehiclePosition);
// typedPublisher.publishResultOf(getVehiclePosition);

//untypedPublisher.loan();
std::this_thread::sleep_for(std::chrono::seconds(1));

}

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef IOX_EXPERIMENTAL_POSH_POPO_PUBLISHER_INL
#define IOX_EXPERIMENTAL_POSH_POPO_PUBLISHER_INL
#ifndef IOX_EXPERIMENTAL_POSH_POPO_BASE_PUBLISHER_INL
#define IOX_EXPERIMENTAL_POSH_POPO_BASE_PUBLISHER_INL

#include "iceoryx_posh/experimental/popo/publisher.hpp"
#include "iceoryx_posh/experimental/popo/base_publisher.hpp"

#include "iceoryx_posh/runtime/posh_runtime.hpp"

Expand All @@ -32,29 +32,29 @@ using SamplePtr = iox::cxx::unique_ptr<T>;
using uid_t = uint64_t;

template<typename T, typename port_t>
Publisher<T, port_t>::Publisher(const capro::ServiceDescription& service)
BasePublisher<T, port_t>::BasePublisher(const capro::ServiceDescription& service)
: m_port(iox::runtime::PoshRuntime::getInstance().getMiddlewareSender(service, ""))
{}

template<typename T, typename port_t>
inline uid_t
Publisher<T, port_t>::uid() const noexcept
BasePublisher<T, port_t>::uid() const noexcept
{
std::cout << "uid()" << std::endl;
return 0u;
}

template<typename T, typename port_t>
inline cxx::expected<Sample<T>, AllocationError>
Publisher<T, port_t>::loan() noexcept
BasePublisher<T, port_t>::loan(uint64_t size) noexcept
{
auto header = m_port.reserveChunk(sizeof(T), m_useDynamicPayloadSize);
auto header = m_port.reserveChunk(size, m_useDynamicPayloadSize);
if (header == nullptr)
{
// Old API does not provide error handling, so return unknown error.
return iox::cxx::error<AllocationError>(AllocationError::UNKNOWN);
return cxx::error<AllocationError>(AllocationError::UNKNOWN);
}
return iox::cxx::success<Sample<T>>(
return cxx::success<Sample<T>>(
cxx::unique_ptr<T>(
header->payload(),
[this](T* const p){
Expand All @@ -66,18 +66,19 @@ Publisher<T, port_t>::loan() noexcept
);
}

// Need a specialization where T = untyped to return a void sample

template<typename T, typename port_t>
inline cxx::expected<AllocationError>
Publisher<T, port_t>::release(Sample<T>& sample) noexcept
inline void
BasePublisher<T, port_t>::release(Sample<T>& sample) noexcept
{
auto header = iox::mepoo::convertPayloadPointerToChunkHeader(sample.allocation());
m_port.freeChunk(header);
return iox::cxx::success<>();
}

template<typename T, typename port_t>
inline cxx::expected<AllocationError>
Publisher<T, port_t>::publish(Sample<T>& sample) noexcept
BasePublisher<T, port_t>::publish(Sample<T>& sample) noexcept
{
/// @todo - ensure sample points to valid shared memory location
auto header = iox::mepoo::convertPayloadPointerToChunkHeader(reinterpret_cast<void* const>(sample.allocation()));
Expand All @@ -87,7 +88,7 @@ Publisher<T, port_t>::publish(Sample<T>& sample) noexcept

template<typename T, typename port_t>
inline cxx::expected<AllocationError>
Publisher<T, port_t>::publishResultOf(cxx::function_ref<void(T*)> f) noexcept
BasePublisher<T, port_t>::publishResultOf(cxx::function_ref<void(T*)> f) noexcept
{
loan()
.and_then([&](Sample<T>& sample){
Expand All @@ -98,7 +99,7 @@ Publisher<T, port_t>::publishResultOf(cxx::function_ref<void(T*)> f) noexcept

template<typename T, typename port_t>
inline cxx::expected<AllocationError>
Publisher<T, port_t>::publishCopyOf(const T& val) noexcept
BasePublisher<T, port_t>::publishCopyOf(const T& val) noexcept
{
loan()
.and_then([&](Sample<T>& sample){
Expand All @@ -109,41 +110,41 @@ Publisher<T, port_t>::publishCopyOf(const T& val) noexcept

template<typename T, typename port_t>
inline cxx::expected<SampleRecallError>
Publisher<T, port_t>::previousSample() const noexcept
BasePublisher<T, port_t>::previousSample() const noexcept
{
assert(false && "Not yet supported");
return iox::cxx::error<SampleRecallError>(SampleRecallError::NO_PREVIOUS_CHUNK);
}

template<typename T, typename port_t>
inline void
Publisher<T, port_t>::offer() noexcept
BasePublisher<T, port_t>::offer() noexcept
{
m_port.activate();
}

template<typename T, typename port_t>
inline void
Publisher<T, port_t>::stopOffer() noexcept
BasePublisher<T, port_t>::stopOffer() noexcept
{
m_port.deactivate();
}

template<typename T, typename port_t>
inline bool
Publisher<T, port_t>::isOffered() noexcept
BasePublisher<T, port_t>::isOffered() noexcept
{
assert(false && "Not yet supported");
}

template<typename T, typename port_t>
inline bool
Publisher<T, port_t>::hasSubscribers() noexcept
BasePublisher<T, port_t>::hasSubscribers() noexcept
{
return m_port.hasSubscribers();
}

} // namespace popo
} // namespace iox

#endif // IOX_EXPERIMENTAL_POSH_POPO_PUBLISHER_INL
#endif // IOX_EXPERIMENTAL_POSH_POPO_BASE_PUBLISHER_INL
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// 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.

#ifndef IOX_EXPERIMENTAL_POSH_POPO_TYPED_PUBLISHER_INL
#define IOX_EXPERIMENTAL_POSH_POPO_TYPED_PUBLISHER_INL

#include "iceoryx_posh/runtime/posh_runtime.hpp"
#include "iceoryx_utils/cxx/expected.hpp"

#include <iostream>

namespace iox
{
namespace popo
{

template<typename T>
TypedPublisher<T>::TypedPublisher(const capro::ServiceDescription& service)
: BasePublisher<T>(service)
{}

template<typename T>
inline cxx::expected<Sample<T>, AllocationError>
TypedPublisher<T>::loan() noexcept
{
return BasePublisher<T>::loan(sizeof(T));
}

template<typename T>
inline void
TypedPublisher<T>::release(Sample<T>& sample) noexcept
{
return BasePublisher<T>::release(sample);
}

template<typename T>
inline cxx::expected<AllocationError>
TypedPublisher<T>::publish(Sample<T>& sample) noexcept
{
return BasePublisher<T>::publish(sample);
}

template<typename T>
inline void
TypedPublisher<T>::offer() noexcept
{
return BasePublisher<T>::offer();
}

template<typename T>
inline void
TypedPublisher<T>::stopOffer() noexcept
{
return BasePublisher<T>::stopOffer();
}

template<typename T>
inline bool
TypedPublisher<T>::isOffered() noexcept
{
return BasePublisher<T>::isOffered();
}

template<typename T>
inline bool
TypedPublisher<T>::hasSubscribers() noexcept
{
return BasePublisher<T>::hasSubscribers();
}

} // namespace popo
} // namespace iox

#endif // IOX_EXPERIMENTAL_POSH_POPO_TYPED_PUBLISHER_INL
Empty file.
Loading

0 comments on commit f60ade1

Please sign in to comment.