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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
|
// Copyright (c) 2019 - 2021 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 - 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_posh/internal/runtime/posh_runtime_impl.hpp"
#include "iceoryx_hoofs/cxx/algorithm.hpp"
#include "iceoryx_hoofs/cxx/convert.hpp"
#include "iceoryx_hoofs/cxx/helplets.hpp"
#include "iceoryx_hoofs/cxx/variant.hpp"
#include "iceoryx_hoofs/internal/relocatable_pointer/base_relative_pointer.hpp"
#include "iceoryx_posh/iceoryx_posh_types.hpp"
#include "iceoryx_posh/internal/log/posh_logging.hpp"
#include "iceoryx_posh/internal/runtime/ipc_message.hpp"
#include "iceoryx_posh/runtime/node.hpp"
#include "iceoryx_posh/runtime/port_config_info.hpp"
#include <cstdint>
namespace iox
{
namespace runtime
{
PoshRuntimeImpl::PoshRuntimeImpl(cxx::optional<const RuntimeName_t*> name, const RuntimeLocation location) noexcept
: PoshRuntime(name)
, m_ipcChannelInterface(roudi::IPC_CHANNEL_ROUDI_NAME, *name.value(), runtime::PROCESS_WAITING_FOR_ROUDI_TIMEOUT)
, m_ShmInterface([&] {
// in case the runtime is located in the same process like RouDi the shm is already opened;
// also in case of the RouDiEnvironment this would close the shm on destruction of the runstime which is also
// not desired
return location == RuntimeLocation::SAME_PROCESS_LIKE_ROUDI
? cxx::nullopt
: cxx::optional<SharedMemoryUser>({m_ipcChannelInterface.getShmTopicSize(),
m_ipcChannelInterface.getSegmentId(),
m_ipcChannelInterface.getSegmentManagerAddressOffset()});
}())
{
}
PoshRuntimeImpl::~PoshRuntimeImpl() noexcept
{
// Inform RouDi that we're shutting down
IpcMessage sendBuffer;
sendBuffer << IpcMessageTypeToString(IpcMessageType::TERMINATION) << m_appName;
IpcMessage receiveBuffer;
if (m_ipcChannelInterface.sendRequestToRouDi(sendBuffer, receiveBuffer)
&& (1U == receiveBuffer.getNumberOfElements()))
{
std::string IpcMessage = receiveBuffer.getElementAtIndex(0U);
if (stringToIpcMessageType(IpcMessage.c_str()) == IpcMessageType::TERMINATION_ACK)
{
LogVerbose() << "RouDi cleaned up resources of " << m_appName << ". Shutting down gracefully.";
}
else
{
LogError() << "Got wrong response from IPC channel for IpcMessageType::TERMINATION:'"
<< receiveBuffer.getMessage() << "'";
}
}
else
{
LogError() << "Sending IpcMessageType::TERMINATION to RouDi failed:'" << receiveBuffer.getMessage() << "'";
}
}
PublisherPortUserType::MemberType_t*
PoshRuntimeImpl::getMiddlewarePublisher(const capro::ServiceDescription& service,
const popo::PublisherOptions& publisherOptions,
const PortConfigInfo& portConfigInfo) noexcept
{
constexpr uint64_t MAX_HISTORY_CAPACITY =
PublisherPortUserType::MemberType_t::ChunkSenderData_t::ChunkDistributorDataProperties_t::MAX_HISTORY_CAPACITY;
auto options = publisherOptions;
if (options.historyCapacity > MAX_HISTORY_CAPACITY)
{
LogWarn() << "Requested history capacity " << options.historyCapacity
<< " exceeds the maximum possible one for this publisher"
<< ", limiting from " << publisherOptions.historyCapacity << " to " << MAX_HISTORY_CAPACITY;
options.historyCapacity = MAX_HISTORY_CAPACITY;
}
if (options.nodeName.empty())
{
options.nodeName = m_appName;
}
IpcMessage sendBuffer;
sendBuffer << IpcMessageTypeToString(IpcMessageType::CREATE_PUBLISHER) << m_appName
<< static_cast<cxx::Serialization>(service).toString() << publisherOptions.serialize().toString()
<< static_cast<cxx::Serialization>(portConfigInfo).toString();
auto maybePublisher = requestPublisherFromRoudi(sendBuffer);
if (maybePublisher.has_error())
{
switch (maybePublisher.get_error())
{
case IpcMessageErrorType::NO_UNIQUE_CREATED:
LogWarn() << "Service '" << service << "' already in use by another process.";
errorHandler(Error::kPOSH__RUNTIME_PUBLISHER_PORT_NOT_UNIQUE, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::INTERNAL_SERVICE_DESCRIPTION_IS_FORBIDDEN:
LogWarn() << "Usage of internal service '" << service << "' is forbidden.";
errorHandler(Error::kPOSH__RUNTIME_SERVICE_DESCRIPTION_FORBIDDEN, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::PUBLISHER_LIST_FULL:
LogWarn() << "Service '" << service << "' could not be created since we are out of memory for publishers.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_PUBLISHER_LIST_FULL, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_PUBLISHER_INVALID_RESPONSE:
LogWarn() << "Service '" << service << "' could not be created. Request publisher got invalid response.";
errorHandler(
Error::kPOSH__RUNTIME_ROUDI_REQUEST_PUBLISHER_INVALID_RESPONSE, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_PUBLISHER_WRONG_IPC_MESSAGE_RESPONSE:
LogWarn() << "Service '" << service
<< "' could not be created. Request publisher got wrong IPC channel response.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_REQUEST_PUBLISHER_WRONG_IPC_MESSAGE_RESPONSE,
nullptr,
iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_PUBLISHER_NO_WRITABLE_SHM_SEGMENT:
LogWarn() << "Service '" << service
<< "' could not be created. RouDi did not find a writable shared memory segment for the current "
"user. Try using another user or adapt RouDi's config.";
errorHandler(Error::kPOSH__RUNTIME_NO_WRITABLE_SHM_SEGMENT, nullptr, iox::ErrorLevel::SEVERE);
break;
default:
LogWarn() << "Unknown error occurred while creating service '" << service << "'.";
errorHandler(Error::kPOSH__RUNTIME_PUBLISHER_PORT_CREATION_UNKNOWN_ERROR, nullptr, iox::ErrorLevel::SEVERE);
break;
}
return nullptr;
}
return maybePublisher.value();
}
cxx::expected<PublisherPortUserType::MemberType_t*, IpcMessageErrorType>
PoshRuntimeImpl::requestPublisherFromRoudi(const IpcMessage& sendBuffer) noexcept
{
IpcMessage receiveBuffer;
if (sendRequestToRouDi(sendBuffer, receiveBuffer) == false)
{
LogError() << "Request publisher got invalid response!";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_PUBLISHER_INVALID_RESPONSE);
}
else if (receiveBuffer.getNumberOfElements() == 3U)
{
std::string IpcMessage = receiveBuffer.getElementAtIndex(0U);
if (stringToIpcMessageType(IpcMessage.c_str()) == IpcMessageType::CREATE_PUBLISHER_ACK)
{
rp::BaseRelativePointer::id_t segmentId{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(2U).c_str(), segmentId);
rp::BaseRelativePointer::offset_t offset{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(1U).c_str(), offset);
auto ptr = rp::BaseRelativePointer::getPtr(segmentId, offset);
return cxx::success<PublisherPortUserType::MemberType_t*>(
reinterpret_cast<PublisherPortUserType::MemberType_t*>(ptr));
}
}
else if (receiveBuffer.getNumberOfElements() == 2U)
{
std::string IpcMessage1 = receiveBuffer.getElementAtIndex(0U);
std::string IpcMessage2 = receiveBuffer.getElementAtIndex(1U);
if (stringToIpcMessageType(IpcMessage1.c_str()) == IpcMessageType::ERROR)
{
LogError() << "Request publisher received no valid publisher port from RouDi.";
return cxx::error<IpcMessageErrorType>(stringToIpcMessageErrorType(IpcMessage2.c_str()));
}
}
LogError() << "Request publisher got wrong response from IPC channel :'" << receiveBuffer.getMessage() << "'";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_PUBLISHER_WRONG_IPC_MESSAGE_RESPONSE);
}
SubscriberPortUserType::MemberType_t*
PoshRuntimeImpl::getMiddlewareSubscriber(const capro::ServiceDescription& service,
const popo::SubscriberOptions& subscriberOptions,
const PortConfigInfo& portConfigInfo) noexcept
{
constexpr uint64_t MAX_QUEUE_CAPACITY = SubscriberPortUserType::MemberType_t::ChunkQueueData_t::MAX_CAPACITY;
auto options = subscriberOptions;
if (options.queueCapacity > MAX_QUEUE_CAPACITY)
{
LogWarn() << "Requested queue capacity " << options.queueCapacity
<< " exceeds the maximum possible one for this subscriber"
<< ", limiting from " << subscriberOptions.queueCapacity << " to " << MAX_QUEUE_CAPACITY;
options.queueCapacity = MAX_QUEUE_CAPACITY;
}
else if (0U == options.queueCapacity)
{
LogWarn() << "Requested queue capacity of 0 doesn't make sense as no data would be received,"
<< " the capacity is set to 1";
options.queueCapacity = 1U;
}
if (subscriberOptions.historyRequest > subscriberOptions.queueCapacity)
{
LogWarn() << "Requested historyRequest for " << service
<< " is larger than queueCapacity. Clamping historyRequest to queueCapacity!";
options.historyRequest = subscriberOptions.queueCapacity;
}
if (options.nodeName.empty())
{
options.nodeName = m_appName;
}
IpcMessage sendBuffer;
sendBuffer << IpcMessageTypeToString(IpcMessageType::CREATE_SUBSCRIBER) << m_appName
<< static_cast<cxx::Serialization>(service).toString() << options.serialize().toString()
<< static_cast<cxx::Serialization>(portConfigInfo).toString();
auto maybeSubscriber = requestSubscriberFromRoudi(sendBuffer);
if (maybeSubscriber.has_error())
{
switch (maybeSubscriber.get_error())
{
case IpcMessageErrorType::SUBSCRIBER_LIST_FULL:
LogWarn() << "Service '" << service << "' could not be created since we are out of memory for subscribers.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_SUBSCRIBER_LIST_FULL, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_SUBSCRIBER_INVALID_RESPONSE:
LogWarn() << "Service '" << service << "' could not be created. Request subscriber got invalid response.";
errorHandler(
Error::kPOSH__RUNTIME_ROUDI_REQUEST_SUBSCRIBER_INVALID_RESPONSE, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_SUBSCRIBER_WRONG_IPC_MESSAGE_RESPONSE:
LogWarn() << "Service '" << service
<< "' could not be created. Request subscriber got wrong IPC channel response.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_REQUEST_SUBSCRIBER_WRONG_IPC_MESSAGE_RESPONSE,
nullptr,
iox::ErrorLevel::SEVERE);
break;
default:
LogWarn() << "Unknown error occurred while creating service '" << service << "'.";
errorHandler(
Error::kPOSH__RUNTIME_SUBSCRIBER_PORT_CREATION_UNKNOWN_ERROR, nullptr, iox::ErrorLevel::SEVERE);
break;
}
return nullptr;
}
return maybeSubscriber.value();
}
cxx::expected<SubscriberPortUserType::MemberType_t*, IpcMessageErrorType>
PoshRuntimeImpl::requestSubscriberFromRoudi(const IpcMessage& sendBuffer) noexcept
{
IpcMessage receiveBuffer;
if (sendRequestToRouDi(sendBuffer, receiveBuffer) == false)
{
LogError() << "Request subscriber got invalid response!";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_SUBSCRIBER_INVALID_RESPONSE);
}
else if (receiveBuffer.getNumberOfElements() == 3U)
{
std::string IpcMessage = receiveBuffer.getElementAtIndex(0U);
if (stringToIpcMessageType(IpcMessage.c_str()) == IpcMessageType::CREATE_SUBSCRIBER_ACK)
{
rp::BaseRelativePointer::id_t segmentId{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(2U).c_str(), segmentId);
rp::BaseRelativePointer::offset_t offset{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(1U).c_str(), offset);
auto ptr = rp::BaseRelativePointer::getPtr(segmentId, offset);
return cxx::success<SubscriberPortUserType::MemberType_t*>(
reinterpret_cast<SubscriberPortUserType::MemberType_t*>(ptr));
}
}
else if (receiveBuffer.getNumberOfElements() == 2U)
{
std::string IpcMessage1 = receiveBuffer.getElementAtIndex(0U);
std::string IpcMessage2 = receiveBuffer.getElementAtIndex(1U);
if (stringToIpcMessageType(IpcMessage1.c_str()) == IpcMessageType::ERROR)
{
LogError() << "Request subscriber received no valid subscriber port from RouDi.";
return cxx::error<IpcMessageErrorType>(stringToIpcMessageErrorType(IpcMessage2.c_str()));
}
}
LogError() << "Request subscriber got wrong response from IPC channel :'" << receiveBuffer.getMessage() << "'";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_SUBSCRIBER_WRONG_IPC_MESSAGE_RESPONSE);
}
popo::ClientPortUser::MemberType_t* PoshRuntimeImpl::getMiddlewareClient(const capro::ServiceDescription& service,
const popo::ClientOptions& clientOptions,
const PortConfigInfo& portConfigInfo) noexcept
{
constexpr uint64_t MAX_QUEUE_CAPACITY = iox::popo::ClientChunkQueueConfig::MAX_QUEUE_CAPACITY;
auto options = clientOptions;
if (options.responseQueueCapacity > MAX_QUEUE_CAPACITY)
{
LogWarn() << "Requested response queue capacity " << options.responseQueueCapacity
<< " exceeds the maximum possible one for this client"
<< ", limiting from " << options.responseQueueCapacity << " to " << MAX_QUEUE_CAPACITY;
options.responseQueueCapacity = MAX_QUEUE_CAPACITY;
}
else if (options.responseQueueCapacity == 0U)
{
LogWarn() << "Requested response queue capacity of 0 doesn't make sense as no data would be received,"
<< " the capacity is set to 1";
options.responseQueueCapacity = 1U;
}
IpcMessage sendBuffer;
sendBuffer << IpcMessageTypeToString(IpcMessageType::CREATE_CLIENT) << m_appName
<< static_cast<cxx::Serialization>(service).toString() << options.serialize().toString()
<< static_cast<cxx::Serialization>(portConfigInfo).toString();
auto maybeClient = requestClientFromRoudi(sendBuffer);
if (maybeClient.has_error())
{
switch (maybeClient.get_error())
{
case IpcMessageErrorType::CLIENT_LIST_FULL:
LogWarn() << "Could not create client with service description '" << service
<< "' as we are out of memory for clients.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_OUT_OF_CLIENTS, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_CLIENT_INVALID_RESPONSE:
LogWarn() << "Could not create client with service description '" << service
<< "'; received invalid response.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_REQUEST_CLIENT_INVALID_RESPONSE, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_CLIENT_WRONG_IPC_MESSAGE_RESPONSE:
LogWarn() << "Could not create client with service description '" << service
<< "'; received wrong IPC channel response.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_REQUEST_CLIENT_WRONG_IPC_MESSAGE_RESPONSE,
nullptr,
iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_CLIENT_NO_WRITABLE_SHM_SEGMENT:
LogWarn() << "Service '" << service
<< "' could not be created. RouDi did not find a writable shared memory segment for the current "
"user. Try using another user or adapt RouDi's config.";
errorHandler(Error::kPOSH__RUNTIME_NO_WRITABLE_SHM_SEGMENT, nullptr, iox::ErrorLevel::SEVERE);
break;
default:
LogWarn() << "Unknown error occurred while creating client with service description '" << service << "'";
errorHandler(Error::kPOSH__RUNTIME_CLIENT_PORT_CREATION_UNKNOWN_ERROR, nullptr, iox::ErrorLevel::SEVERE);
break;
}
return nullptr;
}
return maybeClient.value();
}
cxx::expected<popo::ClientPortUser::MemberType_t*, IpcMessageErrorType>
PoshRuntimeImpl::requestClientFromRoudi(const IpcMessage& sendBuffer) noexcept
{
IpcMessage receiveBuffer;
if (sendRequestToRouDi(sendBuffer, receiveBuffer) == false)
{
LogError() << "Request client got invalid response!";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_CLIENT_INVALID_RESPONSE);
}
else if (receiveBuffer.getNumberOfElements() == 3U)
{
std::string IpcMessage = receiveBuffer.getElementAtIndex(0U);
if (stringToIpcMessageType(IpcMessage.c_str()) == IpcMessageType::CREATE_CLIENT_ACK)
{
rp::BaseRelativePointer::id_t segmentId{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(2U).c_str(), segmentId);
rp::BaseRelativePointer::offset_t offset{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(1U).c_str(), offset);
auto ptr = rp::BaseRelativePointer::getPtr(segmentId, offset);
return cxx::success<popo::ClientPortUser::MemberType_t*>(
reinterpret_cast<popo::ClientPortUser::MemberType_t*>(ptr));
}
}
else if (receiveBuffer.getNumberOfElements() == 2U)
{
std::string IpcMessage1 = receiveBuffer.getElementAtIndex(0U);
std::string IpcMessage2 = receiveBuffer.getElementAtIndex(1U);
if (stringToIpcMessageType(IpcMessage1.c_str()) == IpcMessageType::ERROR)
{
LogError() << "Request client received no valid client port from RouDi.";
return cxx::error<IpcMessageErrorType>(stringToIpcMessageErrorType(IpcMessage2.c_str()));
}
}
LogError() << "Request client got wrong response from IPC channel :'" << receiveBuffer.getMessage() << "'";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_CLIENT_WRONG_IPC_MESSAGE_RESPONSE);
}
popo::ServerPortUser::MemberType_t* PoshRuntimeImpl::getMiddlewareServer(const capro::ServiceDescription& service,
const popo::ServerOptions& serverOptions,
const PortConfigInfo& portConfigInfo) noexcept
{
constexpr uint64_t MAX_QUEUE_CAPACITY = iox::popo::ServerChunkQueueConfig::MAX_QUEUE_CAPACITY;
auto options = serverOptions;
if (options.requestQueueCapacity > MAX_QUEUE_CAPACITY)
{
LogWarn() << "Requested request queue capacity " << options.requestQueueCapacity
<< " exceeds the maximum possible one for this server"
<< ", limiting from " << options.requestQueueCapacity << " to " << MAX_QUEUE_CAPACITY;
options.requestQueueCapacity = MAX_QUEUE_CAPACITY;
}
else if (options.requestQueueCapacity == 0U)
{
LogWarn() << "Requested request queue capacity of 0 doesn't make sense as no data would be received,"
<< " the capacity is set to 1";
options.requestQueueCapacity = 1U;
}
IpcMessage sendBuffer;
sendBuffer << IpcMessageTypeToString(IpcMessageType::CREATE_SERVER) << m_appName
<< static_cast<cxx::Serialization>(service).toString() << options.serialize().toString()
<< static_cast<cxx::Serialization>(portConfigInfo).toString();
auto maybeServer = requestServerFromRoudi(sendBuffer);
if (maybeServer.has_error())
{
switch (maybeServer.get_error())
{
case IpcMessageErrorType::SERVER_LIST_FULL:
LogWarn() << "Could not create server with service description '" << service
<< "' as we are out of memory for servers.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_OUT_OF_SERVERS, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_SERVER_INVALID_RESPONSE:
LogWarn() << "Could not create server with service description '" << service
<< "'; received invalid response.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_REQUEST_SERVER_INVALID_RESPONSE, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_SERVER_WRONG_IPC_MESSAGE_RESPONSE:
LogWarn() << "Could not create server with service description '" << service
<< "'; received wrong IPC channel response.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_REQUEST_SERVER_WRONG_IPC_MESSAGE_RESPONSE,
nullptr,
iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_SERVER_NO_WRITABLE_SHM_SEGMENT:
LogWarn() << "Service '" << service
<< "' could not be created. RouDi did not find a writable shared memory segment for the current "
"user. Try using another user or adapt RouDi's config.";
errorHandler(Error::kPOSH__RUNTIME_NO_WRITABLE_SHM_SEGMENT, nullptr, iox::ErrorLevel::SEVERE);
break;
default:
LogWarn() << "Unknown error occurred while creating server with service description '" << service << "'";
errorHandler(Error::kPOSH__RUNTIME_SERVER_PORT_CREATION_UNKNOWN_ERROR, nullptr, iox::ErrorLevel::SEVERE);
break;
}
return nullptr;
}
return maybeServer.value();
}
cxx::expected<popo::ServerPortUser::MemberType_t*, IpcMessageErrorType>
PoshRuntimeImpl::requestServerFromRoudi(const IpcMessage& sendBuffer) noexcept
{
IpcMessage receiveBuffer;
if (sendRequestToRouDi(sendBuffer, receiveBuffer) == false)
{
LogError() << "Request server got invalid response!";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_SERVER_INVALID_RESPONSE);
}
else if (receiveBuffer.getNumberOfElements() == 3U)
{
std::string IpcMessage = receiveBuffer.getElementAtIndex(0U);
if (stringToIpcMessageType(IpcMessage.c_str()) == IpcMessageType::CREATE_SERVER_ACK)
{
rp::BaseRelativePointer::id_t segmentId{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(2U).c_str(), segmentId);
rp::BaseRelativePointer::offset_t offset{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(1U).c_str(), offset);
auto ptr = rp::BaseRelativePointer::getPtr(segmentId, offset);
return cxx::success<popo::ServerPortUser::MemberType_t*>(
reinterpret_cast<popo::ServerPortUser::MemberType_t*>(ptr));
}
}
else if (receiveBuffer.getNumberOfElements() == 2U)
{
std::string IpcMessage1 = receiveBuffer.getElementAtIndex(0U);
std::string IpcMessage2 = receiveBuffer.getElementAtIndex(1U);
if (stringToIpcMessageType(IpcMessage1.c_str()) == IpcMessageType::ERROR)
{
LogError() << "Request server received no valid server port from RouDi.";
return cxx::error<IpcMessageErrorType>(stringToIpcMessageErrorType(IpcMessage2.c_str()));
}
}
LogError() << "Request server got wrong response from IPC channel :'" << receiveBuffer.getMessage() << "'";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_SERVER_WRONG_IPC_MESSAGE_RESPONSE);
}
popo::InterfacePortData* PoshRuntimeImpl::getMiddlewareInterface(const capro::Interfaces interface,
const NodeName_t& nodeName) noexcept
{
IpcMessage sendBuffer;
sendBuffer << IpcMessageTypeToString(IpcMessageType::CREATE_INTERFACE) << m_appName
<< static_cast<uint32_t>(interface) << nodeName;
IpcMessage receiveBuffer;
if (sendRequestToRouDi(sendBuffer, receiveBuffer) == false)
{
LogError() << "Request interface got invalid response!";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_GET_MW_INTERFACE_INVALID_RESPONSE, nullptr, iox::ErrorLevel::SEVERE);
return nullptr;
}
else if (receiveBuffer.getNumberOfElements() == 3U)
{
std::string IpcMessage = receiveBuffer.getElementAtIndex(0U);
if (stringToIpcMessageType(IpcMessage.c_str()) == IpcMessageType::CREATE_INTERFACE_ACK)
{
rp::BaseRelativePointer::id_t segmentId{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(2U).c_str(), segmentId);
rp::BaseRelativePointer::offset_t offset{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(1U).c_str(), offset);
auto ptr = rp::BaseRelativePointer::getPtr(segmentId, offset);
return reinterpret_cast<popo::InterfacePortData*>(ptr);
}
}
LogError() << "Get mw interface got wrong response from IPC channel :'" << receiveBuffer.getMessage() << "'";
errorHandler(
Error::kPOSH__RUNTIME_ROUDI_GET_MW_INTERFACE_WRONG_IPC_MESSAGE_RESPONSE, nullptr, iox::ErrorLevel::SEVERE);
return nullptr;
}
NodeData* PoshRuntimeImpl::createNode(const NodeProperty& nodeProperty) noexcept
{
IpcMessage sendBuffer;
sendBuffer << IpcMessageTypeToString(IpcMessageType::CREATE_NODE) << m_appName
<< static_cast<cxx::Serialization>(nodeProperty).toString();
IpcMessage receiveBuffer;
if (sendRequestToRouDi(sendBuffer, receiveBuffer) == false)
{
LogError() << "Request node got invalid response!";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_CREATE_NODE_INVALID_RESPONSE, nullptr, iox::ErrorLevel::SEVERE);
return nullptr;
}
else if (receiveBuffer.getNumberOfElements() == 3U)
{
std::string IpcMessage = receiveBuffer.getElementAtIndex(0U);
if (stringToIpcMessageType(IpcMessage.c_str()) == IpcMessageType::CREATE_NODE_ACK)
{
rp::BaseRelativePointer::id_t segmentId{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(2U).c_str(), segmentId);
rp::BaseRelativePointer::offset_t offset{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(1U).c_str(), offset);
auto ptr = rp::BaseRelativePointer::getPtr(segmentId, offset);
return reinterpret_cast<NodeData*>(ptr);
}
}
LogError() << "Got wrong response from RouDi while creating node:'" << receiveBuffer.getMessage() << "'";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_CREATE_NODE_WRONG_IPC_MESSAGE_RESPONSE, nullptr, iox::ErrorLevel::SEVERE);
return nullptr;
}
cxx::expected<popo::ConditionVariableData*, IpcMessageErrorType>
PoshRuntimeImpl::requestConditionVariableFromRoudi(const IpcMessage& sendBuffer) noexcept
{
IpcMessage receiveBuffer;
if (sendRequestToRouDi(sendBuffer, receiveBuffer) == false)
{
LogError() << "Request condition variable got invalid response!";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_CONDITION_VARIABLE_INVALID_RESPONSE);
}
else if (receiveBuffer.getNumberOfElements() == 3U)
{
std::string IpcMessage = receiveBuffer.getElementAtIndex(0U);
if (stringToIpcMessageType(IpcMessage.c_str()) == IpcMessageType::CREATE_CONDITION_VARIABLE_ACK)
{
rp::BaseRelativePointer::id_t segmentId{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(2U).c_str(), segmentId);
rp::BaseRelativePointer::offset_t offset{0U};
cxx::convert::fromString(receiveBuffer.getElementAtIndex(1U).c_str(), offset);
auto ptr = rp::BaseRelativePointer::getPtr(segmentId, offset);
return cxx::success<popo::ConditionVariableData*>(reinterpret_cast<popo::ConditionVariableData*>(ptr));
}
}
else if (receiveBuffer.getNumberOfElements() == 2U)
{
std::string IpcMessage1 = receiveBuffer.getElementAtIndex(0U);
std::string IpcMessage2 = receiveBuffer.getElementAtIndex(1U);
if (stringToIpcMessageType(IpcMessage1.c_str()) == IpcMessageType::ERROR)
{
LogError() << "Request condition variable received no valid condition variable port from RouDi.";
return cxx::error<IpcMessageErrorType>(stringToIpcMessageErrorType(IpcMessage2.c_str()));
}
}
LogError() << "Request condition variable got wrong response from IPC channel :'" << receiveBuffer.getMessage()
<< "'";
return cxx::error<IpcMessageErrorType>(IpcMessageErrorType::REQUEST_CONDITION_VARIABLE_WRONG_IPC_MESSAGE_RESPONSE);
}
popo::ConditionVariableData* PoshRuntimeImpl::getMiddlewareConditionVariable() noexcept
{
IpcMessage sendBuffer;
sendBuffer << IpcMessageTypeToString(IpcMessageType::CREATE_CONDITION_VARIABLE) << m_appName;
auto maybeConditionVariable = requestConditionVariableFromRoudi(sendBuffer);
if (maybeConditionVariable.has_error())
{
switch (maybeConditionVariable.get_error())
{
case IpcMessageErrorType::CONDITION_VARIABLE_LIST_FULL:
LogWarn() << "Could not create condition variable as we are out of memory for condition variables.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_CONDITION_VARIABLE_LIST_FULL, nullptr, iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_CONDITION_VARIABLE_INVALID_RESPONSE:
LogWarn() << "Could not create condition variables; received invalid IPC channel response.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_REQUEST_CONDITION_VARIABLE_INVALID_RESPONSE,
nullptr,
iox::ErrorLevel::SEVERE);
break;
case IpcMessageErrorType::REQUEST_CONDITION_VARIABLE_WRONG_IPC_MESSAGE_RESPONSE:
LogWarn() << "Could not create condition variables; received wrong IPC channel response.";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_REQUEST_CONDITION_VARIABLE_WRONG_IPC_MESSAGE_RESPONSE,
nullptr,
iox::ErrorLevel::SEVERE);
break;
default:
LogWarn() << "Unknown error occurred while creating condition variable";
errorHandler(Error::kPOSH__RUNTIME_ROUDI_CONDITION_VARIABLE_CREATION_UNKNOWN_ERROR,
nullptr,
iox::ErrorLevel::SEVERE);
break;
}
return nullptr;
}
return maybeConditionVariable.value();
}
bool PoshRuntimeImpl::sendRequestToRouDi(const IpcMessage& msg, IpcMessage& answer) noexcept
{
// runtime must be thread safe
std::lock_guard<posix::mutex> g(m_appIpcRequestMutex);
return m_ipcChannelInterface.sendRequestToRouDi(msg, answer);
}
// this is the callback for the m_keepAliveTimer
void PoshRuntimeImpl::sendKeepAliveAndHandleShutdownPreparation() noexcept
{
if (!m_ipcChannelInterface.sendKeepalive())
{
LogWarn() << "Error in sending keep alive";
}
// this is not the nicest solution, but we cannot send this in the signal handler where m_shutdownRequested is
// usually set; luckily the runtime already has a thread running and therefore this thread is used to unblock the
// application shutdown from a potentially blocking publisher with the
// ConsumerTooSlowPolicy::WAIT_FOR_CONSUMER option set
if (m_shutdownRequested.exchange(false, std::memory_order_relaxed))
{
// Inform RouDi to prepare for app shutdown
IpcMessage sendBuffer;
sendBuffer << IpcMessageTypeToString(IpcMessageType::PREPARE_APP_TERMINATION) << m_appName;
IpcMessage receiveBuffer;
if (m_ipcChannelInterface.sendRequestToRouDi(sendBuffer, receiveBuffer)
&& (1U == receiveBuffer.getNumberOfElements()))
{
std::string IpcMessage = receiveBuffer.getElementAtIndex(0U);
if (stringToIpcMessageType(IpcMessage.c_str()) == IpcMessageType::PREPARE_APP_TERMINATION_ACK)
{
LogVerbose() << "RouDi unblocked shutdown of " << m_appName << ".";
}
else
{
LogError() << "Got wrong response from IPC channel for PREPARE_APP_TERMINATION:'"
<< receiveBuffer.getMessage() << "'";
}
}
else
{
LogError() << "Sending IpcMessageType::PREPARE_APP_TERMINATION to RouDi failed:'"
<< receiveBuffer.getMessage() << "'";
}
}
}
} // namespace runtime
} // namespace iox
|