<commit_before>// Copyright 2015 Open Source Robotics Foundation, Inc.
//
// 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.

#include <chrono>
#include <iostream>
#include <string>

#include <rclcpp/rclcpp.hpp>

#include "message_fixtures.hpp"

template<typename T>
void publish(
  rclcpp::Node::SharedPtr node,
  const std::string & message_type,
  std::vector<typename T::SharedPtr> messages,
  size_t number_of_cycles = 5)
{
  auto publisher = node->create_publisher<T>(
    std::string("test_message_") + message_type, messages.size());

  rclcpp::WallRate time_between_cycles(1);
  rclcpp::WallRate time_between_messages(10);
  size_t cycle_index = 0;
  // publish all messages up to number_of_cycles times, longer sleep between each cycle
  while (rclcpp::ok() && cycle_index < number_of_cycles) {
    size_t message_index = 0;
    // publish all messages one by one, shorter sleep between each message
    while (rclcpp::ok() && message_index < messages.size()) {
      std::cout << "publishing message #" << (message_index + 1) << std::endl;
      publisher->publish(messages[message_index]);
      ++message_index;
      time_between_messages.sleep();
      rclcpp::spin_some(node);
    }
    ++cycle_index;
    time_between_cycles.sleep();
    rclcpp::spin_some(node);
  }
}

template<typename T>
rclcpp::subscription::SubscriptionBase::SharedPtr subscribe(
  rclcpp::Node::SharedPtr node,
  const std::string & message_type,
  std::vector<typename T::SharedPtr> & expected_messages,
  std::vector<bool> & received_messages)
{
  received_messages.assign(expected_messages.size(), false);

  auto callback =
    [&expected_messages, &received_messages](const typename T::SharedPtr received_message) -> void
    {
      // find received message in vector of expected messages
      auto received = received_messages.begin();
      bool known_message = false;
      size_t index = 0;
      for (auto expected_message : expected_messages) {
        if (*received_message == *expected_message) {
          *received = true;
          std::cout << "received message #" << (index + 1) << " of " <<
            expected_messages.size() << std::endl;
          known_message = true;
          break;
        }
        ++received;
        ++index;
      }
      if (!known_message) {
        fprintf(stderr, "received message does not match any expected message\n");
        rclcpp::shutdown();
        throw std::runtime_error("received message does not match any expected message");
      }

      // shutdown node when all expected messages have been received
      for (auto received : received_messages) {
        if (!received) {
          return;
        }
      }
      rclcpp::shutdown();
    };

  auto subscriber = node->create_subscription<T>(
    std::string("test_message_") + message_type, expected_messages.size(), callback);
  return subscriber;
}

int main(int argc, char ** argv)
{
  rclcpp::init(argc, argv);
  if (argc != 2) {
    fprintf(stderr, "Wrong number of arguments, pass one message type\n");
    return 1;
  }

  auto start = std::chrono::steady_clock::now();

  std::string message = argv[1];
  auto node = rclcpp::Node::make_shared(std::string("test_publisher_subscriber_") + message);

  rclcpp::subscription::SubscriptionBase::SharedPtr subscriber;
  std::vector<bool> received_messages;  // collect flags about received messages

  auto messages_empty = get_messages_empty();
  auto messages_primitives = get_messages_primitives();
  auto messages_static_array_primitives = get_messages_static_array_primitives();
  auto messages_dynamic_array_primitives = get_messages_dynamic_array_primitives();
  auto messages_nested = get_messages_nested();
  auto messages_dynamic_array_nested = get_messages_dynamic_array_nested();
  auto messages_static_array_nested = get_messages_static_array_nested();
  auto messages_builtins = get_messages_builtins();

  if (message == "empty") {
    subscriber = subscribe<test_communication::msg::Empty>(
      node, message, messages_empty, received_messages);
    publish<test_communication::msg::Empty>(node, message, messages_empty);
  } else if (message == "primitives") {
    subscriber = subscribe<test_communication::msg::Primitives>(
      node, message, messages_primitives, received_messages);
    publish<test_communication::msg::Primitives>(node, message, messages_primitives);
  } else if (message == "staticarrayprimitives") {
    subscriber = subscribe<test_communication::msg::StaticArrayPrimitives>(
      node, message, messages_static_array_primitives, received_messages);
    publish<test_communication::msg::StaticArrayPrimitives>(node, message, messages_static_array_primitives);
  } else if (message == "dynamicarrayprimitives") {
    subscriber = subscribe<test_communication::msg::DynamicArrayPrimitives>(
      node, message, messages_dynamic_array_primitives, received_messages);
    publish<test_communication::msg::DynamicArrayPrimitives>(node, message, messages_dynamic_array_primitives);
  } else if (message == "nested") {
    subscriber = subscribe<test_communication::msg::Nested>(
      node, message, messages_nested, received_messages);
    publish<test_communication::msg::Nested>(node, message, messages_nested);
  } else if (message == "dynamicarraynested") {
    subscriber = subscribe<test_communication::msg::DynamicArrayNested>(
      node, message, messages_dynamic_array_nested, received_messages);
    publish<test_communication::msg::DynamicArrayNested>(node, message, messages_dynamic_array_nested);
  } else if (message == "staticarraynested") {
    subscriber = subscribe<test_communication::msg::StaticArrayNested>(
      node, message, messages_static_array_nested, received_messages);
    publish<test_communication::msg::StaticArrayNested>(node, message, messages_static_array_nested);
  } else if (message == "builtins") {
    subscriber = subscribe<test_communication::msg::Builtins>(
      node, message, messages_builtins, received_messages);
    publish<test_communication::msg::Builtins>(node, message, messages_builtins);
  } else {
    fprintf(stderr, "Unknown message argument '%s'\n", message.c_str());
    return 1;
  }

  rclcpp::spin_some(node);

  auto end = std::chrono::steady_clock::now();
  std::chrono::duration<float> diff = (end - start);
  std::cout << "published and subscribed for " << diff.count() << " seconds" << std::endl;

  for (auto received : received_messages) {
    if (!received) {
      return 1;
    }
  }

  return 0;
}
<commit_msg>[style] limit line length to 100 chars.<commit_after>// Copyright 2015 Open Source Robotics Foundation, Inc.
//
// 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.

#include <chrono>
#include <iostream>
#include <string>

#include <rclcpp/rclcpp.hpp>

#include "message_fixtures.hpp"

template<typename T>
void publish(
  rclcpp::Node::SharedPtr node,
  const std::string & message_type,
  std::vector<typename T::SharedPtr> messages,
  size_t number_of_cycles = 5)
{
  auto publisher = node->create_publisher<T>(
    std::string("test_message_") + message_type, messages.size());

  rclcpp::WallRate time_between_cycles(1);
  rclcpp::WallRate time_between_messages(10);
  size_t cycle_index = 0;
  // publish all messages up to number_of_cycles times, longer sleep between each cycle
  while (rclcpp::ok() && cycle_index < number_of_cycles) {
    size_t message_index = 0;
    // publish all messages one by one, shorter sleep between each message
    while (rclcpp::ok() && message_index < messages.size()) {
      std::cout << "publishing message #" << (message_index + 1) << std::endl;
      publisher->publish(messages[message_index]);
      ++message_index;
      time_between_messages.sleep();
      rclcpp::spin_some(node);
    }
    ++cycle_index;
    time_between_cycles.sleep();
    rclcpp::spin_some(node);
  }
}

template<typename T>
rclcpp::subscription::SubscriptionBase::SharedPtr subscribe(
  rclcpp::Node::SharedPtr node,
  const std::string & message_type,
  std::vector<typename T::SharedPtr> & expected_messages,
  std::vector<bool> & received_messages)
{
  received_messages.assign(expected_messages.size(), false);

  auto callback =
    [&expected_messages, &received_messages](const typename T::SharedPtr received_message) -> void
    {
      // find received message in vector of expected messages
      auto received = received_messages.begin();
      bool known_message = false;
      size_t index = 0;
      for (auto expected_message : expected_messages) {
        if (*received_message == *expected_message) {
          *received = true;
          std::cout << "received message #" << (index + 1) << " of " <<
            expected_messages.size() << std::endl;
          known_message = true;
          break;
        }
        ++received;
        ++index;
      }
      if (!known_message) {
        fprintf(stderr, "received message does not match any expected message\n");
        rclcpp::shutdown();
        throw std::runtime_error("received message does not match any expected message");
      }

      // shutdown node when all expected messages have been received
      for (auto received : received_messages) {
        if (!received) {
          return;
        }
      }
      rclcpp::shutdown();
    };

  auto subscriber = node->create_subscription<T>(
    std::string("test_message_") + message_type, expected_messages.size(), callback);
  return subscriber;
}

int main(int argc, char ** argv)
{
  rclcpp::init(argc, argv);
  if (argc != 2) {
    fprintf(stderr, "Wrong number of arguments, pass one message type\n");
    return 1;
  }

  auto start = std::chrono::steady_clock::now();

  std::string message = argv[1];
  auto node = rclcpp::Node::make_shared(std::string("test_publisher_subscriber_") + message);

  rclcpp::subscription::SubscriptionBase::SharedPtr subscriber;
  std::vector<bool> received_messages;  // collect flags about received messages

  auto messages_empty = get_messages_empty();
  auto messages_primitives = get_messages_primitives();
  auto messages_static_array_primitives = get_messages_static_array_primitives();
  auto messages_dynamic_array_primitives = get_messages_dynamic_array_primitives();
  auto messages_nested = get_messages_nested();
  auto messages_dynamic_array_nested = get_messages_dynamic_array_nested();
  auto messages_static_array_nested = get_messages_static_array_nested();
  auto messages_builtins = get_messages_builtins();

  if (message == "empty") {
    subscriber = subscribe<test_communication::msg::Empty>(
      node, message, messages_empty, received_messages);
    publish<test_communication::msg::Empty>(node, message, messages_empty);
  } else if (message == "primitives") {
    subscriber = subscribe<test_communication::msg::Primitives>(
      node, message, messages_primitives, received_messages);
    publish<test_communication::msg::Primitives>(node, message, messages_primitives);
  } else if (message == "staticarrayprimitives") {
    subscriber = subscribe<test_communication::msg::StaticArrayPrimitives>(
      node, message, messages_static_array_primitives, received_messages);
    publish<test_communication::msg::StaticArrayPrimitives>(node, message,
      messages_static_array_primitives);
  } else if (message == "dynamicarrayprimitives") {
    subscriber = subscribe<test_communication::msg::DynamicArrayPrimitives>(
      node, message, messages_dynamic_array_primitives, received_messages);
    publish<test_communication::msg::DynamicArrayPrimitives>(node, message,
      messages_dynamic_array_primitives);
  } else if (message == "nested") {
    subscriber = subscribe<test_communication::msg::Nested>(
      node, message, messages_nested, received_messages);
    publish<test_communication::msg::Nested>(node, message, messages_nested);
  } else if (message == "dynamicarraynested") {
    subscriber = subscribe<test_communication::msg::DynamicArrayNested>(
      node, message, messages_dynamic_array_nested, received_messages);
    publish<test_communication::msg::DynamicArrayNested>(node, message,
      messages_dynamic_array_nested);
  } else if (message == "staticarraynested") {
    subscriber = subscribe<test_communication::msg::StaticArrayNested>(
      node, message, messages_static_array_nested, received_messages);
    publish<test_communication::msg::StaticArrayNested>(node, message,
      messages_static_array_nested);
  } else if (message == "builtins") {
    subscriber = subscribe<test_communication::msg::Builtins>(
      node, message, messages_builtins, received_messages);
    publish<test_communication::msg::Builtins>(node, message, messages_builtins);
  } else {
    fprintf(stderr, "Unknown message argument '%s'\n", message.c_str());
    return 1;
  }

  rclcpp::spin_some(node);

  auto end = std::chrono::steady_clock::now();
  std::chrono::duration<float> diff = (end - start);
  std::cout << "published and subscribed for " << diff.count() << " seconds" << std::endl;

  for (auto received : received_messages) {
    if (!received) {
      return 1;
    }
  }

  return 0;
}

{'fraction_non_alphanumeric': 0.11781036446469248, 'fraction_numerical': 0.0034880410022779043, 'mean_word_length': 4.548578199052133, 'pattern_counts': {'":': 0, '<': 113, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2491310', 'n_tokens_mistral': 4133, 'n_tokens_neox': 3953, 'n_words': 972}