|
| 1 | +// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +#ifndef IOX_POSH_POPO_PORTS_CLIENT_PORT_USER_HPP |
| 15 | +#define IOX_POSH_POPO_PORTS_CLIENT_PORT_USER_HPP |
| 16 | + |
| 17 | +#include "iceoryx_posh/internal/popo/building_blocks/chunk_receiver.hpp" |
| 18 | +#include "iceoryx_posh/internal/popo/building_blocks/chunk_sender.hpp" |
| 19 | +#include "iceoryx_posh/internal/popo/ports/base_port.hpp" |
| 20 | +#include "iceoryx_posh/internal/popo/ports/client_port_data.hpp" |
| 21 | +#include "iceoryx_posh/mepoo/chunk_header.hpp" |
| 22 | +#include "iceoryx_utils/cxx/expected.hpp" |
| 23 | +#include "iceoryx_utils/cxx/helplets.hpp" |
| 24 | +#include "iceoryx_utils/cxx/optional.hpp" |
| 25 | +#include "iceoryx_utils/error_handling/error_handling.hpp" |
| 26 | + |
| 27 | +namespace iox |
| 28 | +{ |
| 29 | +namespace popo |
| 30 | +{ |
| 31 | +/// @brief The ClientPortUser provides the API for accessing a client port from the user side. The client port |
| 32 | +/// is divided in the three parts ClientPortData, ClientPortRouDi and ClientPortUser. The ClientPortUser |
| 33 | +/// uses the functionality of a ChunkSender and ChunReceiver for sending requests and receiving responses. |
| 34 | +/// Additionally it provides the connect / disconnect API which controls whether the client port shall connect to the |
| 35 | +/// server |
| 36 | +class ClientPortUser : public BasePort |
| 37 | +{ |
| 38 | + public: |
| 39 | + using MemberType_t = ClientPortData; |
| 40 | + |
| 41 | + explicit ClientPortUser(cxx::not_null<MemberType_t* const> clientPortDataPtr) noexcept; |
| 42 | + |
| 43 | + ClientPortUser(const ClientPortUser& other) = delete; |
| 44 | + ClientPortUser& operator=(const ClientPortUser&) = delete; |
| 45 | + ClientPortUser(ClientPortUser&& rhs) = default; |
| 46 | + ClientPortUser& operator=(ClientPortUser&& rhs) = default; |
| 47 | + ~ClientPortUser() = default; |
| 48 | + |
| 49 | + /// @brief Allocate a chunk, the ownerhip of the SharedChunk remains in the ClientPortUser for being able to |
| 50 | + /// cleanup if the user process disappears |
| 51 | + /// @param[in] payloadSize, size of the user paylaod without additional headers |
| 52 | + /// @return on success pointer to a ChunkHeader which can be used to access the payload and header fields, error if |
| 53 | + /// not |
| 54 | + cxx::expected<mepoo::ChunkHeader*, AllocationError> allocateRequestChunk(const uint32_t payloadSize) noexcept; |
| 55 | + |
| 56 | + /// @brief Free an allocated chunk without sending it |
| 57 | + /// @param[in] chunkHeader, pointer to the ChunkHeader to free |
| 58 | + void freeRequestChunk(mepoo::ChunkHeader* const chunkHeader) noexcept; |
| 59 | + |
| 60 | + /// @brief Send an allocated request chunk to the server port |
| 61 | + /// @param[in] chunkHeader, pointer to the ChunkHeader to send |
| 62 | + void sendRequestChunk(mepoo::ChunkHeader* const chunkHeader) noexcept; |
| 63 | + |
| 64 | + /// @brief try to connect to the server |
| 65 | + void connect() noexcept; |
| 66 | + |
| 67 | + /// @brief disconnect from the server |
| 68 | + void disconnect() noexcept; |
| 69 | + |
| 70 | + /// @brief get the current connection state. Caution: There can be delays between calling connect and a change |
| 71 | + /// in the conenction state. The connection state can also change without user interaction if the server comes |
| 72 | + /// and goes |
| 73 | + /// @return ConnectionState |
| 74 | + ConnectionState getConnectionState() const noexcept; |
| 75 | + |
| 76 | + /// @brief Tries to get the next response from the queue. If there is a new one, the ChunkHeader of the oldest |
| 77 | + /// response in the queue is returned (FiFo queue) |
| 78 | + /// @return optional that has a new chunk header or no value if there are no new responses in the underlying queue, |
| 79 | + /// ChunkReceiveError on error |
| 80 | + cxx::expected<cxx::optional<const mepoo::ChunkHeader*>, ChunkReceiveError> getResponseChunk() noexcept; |
| 81 | + |
| 82 | + /// @brief Release a response that was obtained with getResponseChunk |
| 83 | + /// @param[in] chunkHeader, pointer to the ChunkHeader to release |
| 84 | + void releaseResponseChunk(const mepoo::ChunkHeader* chunkHeader) noexcept; |
| 85 | + |
| 86 | + /// @brief check if there are responses in the queue |
| 87 | + /// @return if there are responses in the queue return true, otherwise false |
| 88 | + bool hasNewResponseChunks() noexcept; |
| 89 | + |
| 90 | + /// @brief check if there was a queue overflow since the last call of hasLostResponseChunks |
| 91 | + /// @return true if the underlying queue overflowed since last call of this method, otherwise false |
| 92 | + bool hasLostResponseChunks() noexcept; |
| 93 | + |
| 94 | + /// @brief set a condition variable (via its pointer) to the client |
| 95 | + /// @return true if attachment worked, otherwise false |
| 96 | + bool setConditionVariable(ConditionVariableData* conditionVariableDataPtr) noexcept; |
| 97 | + |
| 98 | + /// @brief unset a condition variable from the client |
| 99 | + /// @return true if detachment worked, otherwise false |
| 100 | + bool unsetConditionVariable() noexcept; |
| 101 | + |
| 102 | + /// @brief check if there's a condition variable set |
| 103 | + /// @return true if a condition variable attached, otherwise false |
| 104 | + bool isConditionVariableSet() noexcept; |
| 105 | + |
| 106 | + private: |
| 107 | + const MemberType_t* getMembers() const noexcept; |
| 108 | + MemberType_t* getMembers() noexcept; |
| 109 | + |
| 110 | + ChunkSender<ClientChunkSenderData_t> m_chunkSender; |
| 111 | + ChunkReceiver<ClientChunkReceiverData_t> m_chunkReceiver; |
| 112 | +}; |
| 113 | + |
| 114 | +} // namespace popo |
| 115 | +} // namespace iox |
| 116 | + |
| 117 | +#endif // IOX_POSH_POPO_PORTS_PUBLISHER_PORT_USER_HPP |
0 commit comments