Skip to content

Commit b74bb56

Browse files
committed
iox-#27 Add typed API for client and server
1 parent 7d1265d commit b74bb56

File tree

6 files changed

+369
-0
lines changed

6 files changed

+369
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2022 by Apex.AI Inc. 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+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
#ifndef IOX_POSH_POPO_CLIENT_IMPL_HPP
18+
#define IOX_POSH_POPO_CLIENT_IMPL_HPP
19+
20+
#include "iceoryx_posh/capro/service_description.hpp"
21+
#include "iceoryx_posh/internal/popo/base_client.hpp"
22+
#include "iceoryx_posh/internal/popo/request_deleter.hpp"
23+
#include "iceoryx_posh/internal/popo/response_deleter.hpp"
24+
#include "iceoryx_posh/internal/popo/rpc_interface.hpp"
25+
#include "iceoryx_posh/popo/client_options.hpp"
26+
#include "iceoryx_posh/popo/request.hpp"
27+
#include "iceoryx_posh/popo/response.hpp"
28+
#include "iceoryx_posh/popo/trigger_handle.hpp"
29+
#include "iceoryx_posh/runtime/posh_runtime.hpp"
30+
31+
namespace iox
32+
{
33+
namespace popo
34+
{
35+
template <typename Req, typename Res, typename BaseClientT = BaseClient<>>
36+
class ClientImpl : public BaseClientT, public RpcInterface<Request<Req>>
37+
{
38+
public:
39+
explicit ClientImpl(const capro::ServiceDescription& service, const ClientOptions& clientOptions = {}) noexcept;
40+
41+
template <typename... Args>
42+
cxx::expected<Request<Req>, AllocationError> loan(Args&&... args) noexcept;
43+
44+
void send(Request<Req>&& request) noexcept override;
45+
46+
cxx::expected<Response<const Res>, ChunkReceiveResult> take() noexcept;
47+
48+
private:
49+
using BaseClientT::port;
50+
51+
cxx::expected<Request<Req>, AllocationError> loanUninitialized() noexcept;
52+
53+
using RequestSampleDeleter = RequestDeleter<typename BaseClientT::PortType>;
54+
RequestSampleDeleter m_requestDeleter{port()};
55+
using ResponseSampleDeleter = ResponseDeleter<typename BaseClientT::PortType>;
56+
ResponseSampleDeleter m_responseDeleter{port()};
57+
};
58+
} // namespace popo
59+
} // namespace iox
60+
61+
#include "iceoryx_posh/internal/popo/client_impl.inl"
62+
63+
#endif // IOX_POSH_POPO_CLIENT_IMPL_HPP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) 2021 by Apex.AI Inc. 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+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
#ifndef IOX_POSH_POPO_CLIENT_IMPL_INL
18+
#define IOX_POSH_POPO_CLIENT_IMPL_INL
19+
20+
#include "iceoryx_posh/internal/popo/client_impl.hpp"
21+
22+
namespace iox
23+
{
24+
namespace popo
25+
{
26+
template <typename Req, typename Res, typename BaseClientT>
27+
ClientImpl<Req, Res, BaseClientT>::ClientImpl(const capro::ServiceDescription& service,
28+
const ClientOptions& clientOptions) noexcept
29+
: BaseClientT(service, clientOptions)
30+
{
31+
}
32+
33+
template <typename Req, typename Res, typename BaseClientT>
34+
cxx::expected<Request<Req>, AllocationError> ClientImpl<Req, Res, BaseClientT>::loanUninitialized() noexcept
35+
{
36+
auto result = port().allocateRequest(sizeof(Req), alignof(Req));
37+
if (result.has_error())
38+
{
39+
return cxx::error<AllocationError>(result.get_error());
40+
}
41+
else
42+
{
43+
auto requestHeader = result.value();
44+
auto payload = mepoo::ChunkHeader::fromUserHeader(requestHeader)->userPayload();
45+
auto request = cxx::unique_ptr<Req>(reinterpret_cast<Req*>(payload), m_requestDeleter);
46+
return cxx::success<Request<Req>>(Request<Req>{std::move(request), *this});
47+
}
48+
}
49+
50+
template <typename Req, typename Res, typename BaseClientT>
51+
template <typename... Args>
52+
cxx::expected<Request<Req>, AllocationError> ClientImpl<Req, Res, BaseClientT>::loan(Args&&... args) noexcept
53+
{
54+
return std::move(
55+
loanUninitialized().and_then([&](auto& request) { new (request.get()) Req(std::forward<Args>(args)...); }));
56+
}
57+
58+
template <typename Req, typename Res, typename BaseClientT>
59+
void ClientImpl<Req, Res, BaseClientT>::send(Request<Req>&& request) noexcept
60+
{
61+
auto payload = request.release(); // release the Request ownership of the chunk before publishing
62+
auto* requestHeader = static_cast<RequestHeader*>(mepoo::ChunkHeader::fromUserPayload(payload)->userHeader());
63+
port().sendRequest(requestHeader);
64+
}
65+
66+
template <typename Req, typename Res, typename BaseClientT>
67+
cxx::expected<Response<const Res>, ChunkReceiveResult> ClientImpl<Req, Res, BaseClientT>::take() noexcept
68+
{
69+
auto result = port().getResponse();
70+
if (result.has_error())
71+
{
72+
return cxx::error<ChunkReceiveResult>(result.get_error());
73+
}
74+
auto responseHeader = result.value();
75+
auto payload = mepoo::ChunkHeader::fromUserHeader(responseHeader)->userPayload();
76+
auto response = cxx::unique_ptr<const Res>(static_cast<const Res*>(payload), m_responseDeleter);
77+
return cxx::success<Response<const Res>>(Response<const Res>{std::move(response)});
78+
}
79+
80+
} // namespace popo
81+
} // namespace iox
82+
83+
#endif // IOX_POSH_POPO_CLIENT_IMPL_INL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2022 by Apex.AI Inc. 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+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
#ifndef IOX_POSH_POPO_SERVER_IMPL_HPP
18+
#define IOX_POSH_POPO_SERVER_IMPL_HPP
19+
20+
#include "iceoryx_posh/capro/service_description.hpp"
21+
#include "iceoryx_posh/internal/popo/base_server.hpp"
22+
#include "iceoryx_posh/internal/popo/request_deleter.hpp"
23+
#include "iceoryx_posh/internal/popo/response_deleter.hpp"
24+
#include "iceoryx_posh/internal/popo/rpc_interface.hpp"
25+
#include "iceoryx_posh/popo/request.hpp"
26+
#include "iceoryx_posh/popo/response.hpp"
27+
#include "iceoryx_posh/popo/server_options.hpp"
28+
#include "iceoryx_posh/popo/trigger_handle.hpp"
29+
#include "iceoryx_posh/runtime/posh_runtime.hpp"
30+
31+
namespace iox
32+
{
33+
namespace popo
34+
{
35+
template <typename Req, typename Res, typename BaseServerT = BaseServer<>>
36+
class ServerImpl : public BaseServerT, public RpcInterface<Response<Res>>
37+
{
38+
public:
39+
explicit ServerImpl(const capro::ServiceDescription& service, const ServerOptions& serverOptions = {}) noexcept;
40+
41+
cxx::expected<Request<const Req>, ServerRequestResult> take() noexcept;
42+
43+
template <typename... Args>
44+
cxx::expected<Response<Res>, AllocationError> loan(const Request<const Req>& request, Args&&... args) noexcept;
45+
46+
void send(Response<Res>&& response) noexcept override;
47+
48+
private:
49+
using BaseServerT::port;
50+
51+
cxx::expected<Response<Res>, AllocationError> loanUninitialized(const Request<const Req>& request) noexcept;
52+
53+
using RequestSampleDeleter = RequestDeleter<typename BaseServerT::PortType>;
54+
RequestSampleDeleter m_requestDeleter{port()};
55+
using ResponseSampleDeleter = ResponseDeleter<typename BaseServerT::PortType>;
56+
ResponseSampleDeleter m_responseDeleter{port()};
57+
};
58+
} // namespace popo
59+
} // namespace iox
60+
61+
#include "iceoryx_posh/internal/popo/server_impl.inl"
62+
63+
#endif // IOX_POSH_POPO_SERVER_IMPL_HPP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// Copyright (c) 2022 by Apex.AI Inc. 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+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
#ifndef IOX_POSH_POPO_SERVER_IMPL_INL
18+
#define IOX_POSH_POPO_SERVER_IMPL_INL
19+
20+
#include "iceoryx_posh/internal/popo/server_impl.hpp"
21+
22+
namespace iox
23+
{
24+
namespace popo
25+
{
26+
template <typename Req, typename Res, typename BaseServerT>
27+
inline ServerImpl<Req, Res, BaseServerT>::ServerImpl(const capro::ServiceDescription& service,
28+
const ServerOptions& serverOptions) noexcept
29+
: BaseServerT(service, serverOptions)
30+
{
31+
}
32+
33+
template <typename Req, typename Res, typename BaseServerT>
34+
cxx::expected<Request<const Req>, ServerRequestResult> ServerImpl<Req, Res, BaseServerT>::take() noexcept
35+
{
36+
auto result = port().getRequest();
37+
if (result.has_error())
38+
{
39+
return cxx::error<ServerRequestResult>(result.get_error());
40+
}
41+
auto requestHeader = result.value();
42+
auto payload = mepoo::ChunkHeader::fromUserHeader(requestHeader)->userPayload();
43+
auto request = cxx::unique_ptr<const Req>(static_cast<const Req*>(payload), m_requestDeleter);
44+
return cxx::success<Request<const Req>>(Request<const Req>{std::move(request)});
45+
}
46+
47+
template <typename Req, typename Res, typename BaseServerT>
48+
cxx::expected<Response<Res>, AllocationError>
49+
ServerImpl<Req, Res, BaseServerT>::loanUninitialized(const Request<const Req>& request) noexcept
50+
{
51+
const auto* requestHeader = &request.getRequestHeader();
52+
auto result = port().allocateResponse(requestHeader, sizeof(Req), alignof(Req));
53+
if (result.has_error())
54+
{
55+
return cxx::error<AllocationError>(result.get_error());
56+
}
57+
else
58+
{
59+
auto responseHeader = result.value();
60+
auto payload = mepoo::ChunkHeader::fromUserHeader(responseHeader)->userPayload();
61+
auto response = cxx::unique_ptr<Res>(reinterpret_cast<Res*>(payload), m_responseDeleter);
62+
return cxx::success<Response<Res>>(Response<Res>{std::move(response), *this});
63+
}
64+
}
65+
66+
template <typename Req, typename Res, typename BaseServerT>
67+
template <typename... Args>
68+
cxx::expected<Response<Res>, AllocationError> ServerImpl<Req, Res, BaseServerT>::loan(const Request<const Req>& request,
69+
Args&&... args) noexcept
70+
{
71+
return std::move(loanUninitialized(request).and_then(
72+
[&](auto& response) { new (response.get()) Res(std::forward<Args>(args)...); }));
73+
}
74+
75+
template <typename Req, typename Res, typename BaseServerT>
76+
void ServerImpl<Req, Res, BaseServerT>::send(Response<Res>&& response) noexcept
77+
{
78+
auto payload = response.release(); // release the Request ownership of the chunk before publishing
79+
auto* responseHeader = static_cast<ResponseHeader*>(mepoo::ChunkHeader::fromUserPayload(payload)->userHeader());
80+
port().sendResponse(responseHeader);
81+
}
82+
83+
} // namespace popo
84+
} // namespace iox
85+
86+
#endif // IOX_POSH_POPO_SERVER_IMPL_INL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) 2022 by Apex.AI Inc. 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+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
#ifndef IOX_POSH_POPO_CLIENT_HPP
18+
#define IOX_POSH_POPO_CLIENT_HPP
19+
20+
#include "iceoryx_posh/capro/service_description.hpp"
21+
#include "iceoryx_posh/internal/popo/client_impl.hpp"
22+
#include "iceoryx_posh/popo/client_options.hpp"
23+
#include "iceoryx_posh/popo/trigger_handle.hpp"
24+
#include "iceoryx_posh/runtime/posh_runtime.hpp"
25+
26+
namespace iox
27+
{
28+
namespace popo
29+
{
30+
template <typename Req, typename Res>
31+
class Client : public ClientImpl<Req, Res>
32+
{
33+
public:
34+
using ClientImpl<Req, Res>::ClientImpl;
35+
};
36+
} // namespace popo
37+
} // namespace iox
38+
39+
#endif // IOX_POSH_POPO_CLIENT_HPP
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) 2022 by Apex.AI Inc. 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+
//
15+
// SPDX-License-Identifier: Apache-2.0
16+
17+
#ifndef IOX_POSH_POPO_SERVER_HPP
18+
#define IOX_POSH_POPO_SERVER_HPP
19+
20+
#include "iceoryx_posh/internal/popo/server_impl.hpp"
21+
22+
namespace iox
23+
{
24+
namespace popo
25+
{
26+
template <typename Req, typename Res>
27+
class Server : public ServerImpl<Req, Res>
28+
{
29+
public:
30+
using ServerImpl<Req, Res>::ServerImpl;
31+
};
32+
} // namespace popo
33+
} // namespace iox
34+
35+
#endif // IOX_POSH_POPO_SERVER_HPP

0 commit comments

Comments
 (0)