text:
// Copyright 2022 PingCAP, Ltd.
//
// 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 <Common/UnifiedLogPatternFormatter.h>
#include <Encryption/MockKeyManager.h>
#include <Poco/ConsoleChannel.h>
#include <Poco/FormattingChannel.h>
#include <Poco/Logger.h>
#include <Poco/PatternFormatter.h>
#include <Poco/Runnable.h>
#include <Poco/ThreadPool.h>
#include <Poco/Timer.h>
#include <Storages/Page/V3/PageDirectory.h>
#include <Storages/Page/V3/PageDirectoryFactory.h>
#include <Storages/Page/V3/PageStorageImpl.h>
#include <Storages/Page/WriteBatch.h>
#include <Storages/PathPool.h>
#include <TestUtils/MockDiskDelegator.h>

#include <boost/program_options.hpp>

namespace DB::PS::V3
{
struct ControlOptions
{
    enum DisplayType
    {
        DISPLAY_SUMMARY_INFO = 1,
        DISPLAY_DIRECTORY_INFO = 2,
        DISPLAY_BLOBS_INFO = 3,
        CHECK_ALL_DATA_CRC = 4,
    };

    std::vector<std::string> paths;
    int display_mode = DisplayType::DISPLAY_SUMMARY_INFO;
    UInt64 query_page_id = UINT64_MAX;
    UInt32 query_blob_id = UINT32_MAX;
    UInt64 query_ns_id = DB::TEST_NAMESPACE_ID;
    UInt64 check_page_id = UINT64_MAX;
    bool enable_fo_check = true;

    static ControlOptions parse(int argc, char ** argv);
};

ControlOptions ControlOptions::parse(int argc, char ** argv)
{
    namespace po = boost::program_options;
    using po::value;

    po::options_description desc("Allowed options");
    desc.add_options()("help,h", "produce help message") //
        ("paths,P", value<std::vector<std::string>>(), "store path(s)") //
        ("display_mode,D", value<int>()->default_value(1), "Display Mode: 1 is summary information,\n 2 is display all of stored page and version chaim(will be very long),\n 3 is display all blobs(in disk) data distribution. \n 4 is check every data is valid.") //
        ("enable_fo_check,E", value<bool>()->default_value(true), "Also check the evert field offsets. This options only works when `display_mode` is 4.") //
        ("query_ns_id,N", value<UInt64>()->default_value(DB::TEST_NAMESPACE_ID), "When used `check_page_id`/`query_page_id`/`query_blob_id` to query results. You can specify a namespace id.")("check_page_id,C", value<UInt64>()->default_value(UINT64_MAX), "Check a single Page id, display the exception if meet. And also will check the field offsets.") //
        ("query_page_id,W", value<UInt64>()->default_value(UINT64_MAX), "Quert a single Page id, and print its version chaim.") //
        ("query_blob_id,B", value<UInt32>()->default_value(UINT32_MAX), "Quert a single Blob id, and print its data distribution.");

    static_assert(sizeof(DB::PageId) == sizeof(UInt64));
    static_assert(sizeof(DB::BlobFileId) == sizeof(UInt32));

    po::variables_map options;
    po::store(po::parse_command_line(argc, argv, desc), options);
    po::notify(options);

    if (options.count("help") > 0)
    {
        std::cerr << desc << std::endl;
        exit(0);
    }

    ControlOptions opt;

    if (options.count("paths") == 0)
    {
        std::cerr << "Invalid arg paths." << std::endl;
        std::cerr << desc << std::endl;
        exit(0);
    }
    opt.paths = options["paths"].as<std::vector<std::string>>();
    opt.display_mode = options["display_mode"].as<int>();
    opt.query_page_id = options["query_page_id"].as<UInt64>();
    opt.query_blob_id = options["query_blob_id"].as<UInt32>();
    opt.enable_fo_check = options["enable_fo_check"].as<bool>();
    opt.check_page_id = options["check_page_id"].as<UInt64>();
    opt.query_ns_id = options["query_ns_id"].as<UInt64>();

    if (opt.display_mode < DisplayType::DISPLAY_SUMMARY_INFO || opt.display_mode > DisplayType::CHECK_ALL_DATA_CRC)
    {
        std::cerr << "Invalid display mode: " << opt.display_mode << std::endl;
        std::cerr << desc << std::endl;
        exit(0);
    }

    return opt;
}

class PageStorageControl
{
public:
    explicit PageStorageControl(const ControlOptions & options_)
        : options(options_)
    {
    }

    void run()
    {
        DB::PSDiskDelegatorPtr delegator;
        if (options.paths.size() == 1)
        {
            delegator = std::make_shared<DB::tests::MockDiskDelegatorSingle>(options.paths[0]);
        }
        else
        {
            delegator = std::make_shared<DB::tests::MockDiskDelegatorMulti>(options.paths);
        }

        auto key_manager = std::make_shared<DB::MockKeyManager>(false);
        auto file_provider = std::make_shared<DB::FileProvider>(key_manager, false);

        BlobStore::Config blob_config;

        PageStorage::Config config;
        PageStorageImpl ps_v3("PageStorageControl", delegator, config, file_provider);
        ps_v3.restore();
        PageDirectory::MVCCMapType & mvcc_table_directory = ps_v3.page_directory->mvcc_table_directory;

        switch (options.display_mode)
        {
        case ControlOptions::DisplayType::DISPLAY_SUMMARY_INFO:
        {
            std::cout << getSummaryInfo(mvcc_table_directory, ps_v3.blob_store) << std::endl;
            break;
        }
        case ControlOptions::DisplayType::DISPLAY_DIRECTORY_INFO:
        {
            std::cout << getDirectoryInfo(mvcc_table_directory, options.query_ns_id, options.query_page_id) << std::endl;
            break;
        }
        case ControlOptions::DisplayType::DISPLAY_BLOBS_INFO:
        {
            std::cout << getBlobsInfo(ps_v3.blob_store, options.query_blob_id) << std::endl;
            break;
        }
        case ControlOptions::DisplayType::CHECK_ALL_DATA_CRC:
        {
            if (options.check_page_id != UINT64_MAX)
            {
                std::cout << checkSinglePage(mvcc_table_directory, ps_v3.blob_store, options.query_ns_id, options.check_page_id) << std::endl;
            }
            else
            {
                std::cout << checkAllDatasCrc(mvcc_table_directory, ps_v3.blob_store, options.enable_fo_check) << std::endl;
            }
            break;
        }
        default:
            std::cout << "Invalid display mode." << std::endl;
            break;
        }
    }

private:
    static String getBlobsInfo(BlobStore & blob_store, UInt32 blob_id)
    {
        auto stat_info = [](const BlobStore::BlobStats::BlobStatPtr & stat, const String & path) {
            FmtBuffer stat_str;
            stat_str.fmtAppend("    stat id: {}\n"
                               "     path: {}\n"
                               "     total size: {}\n"
                               "     valid size: {}\n"
                               "     valid rate: {}\n"
                               "     max cap: {}\n", //
                               stat->id, //
                               path,
                               stat->sm_total_size, //
                               stat->sm_valid_size, //
                               stat->sm_valid_rate, //
                               stat->sm_max_caps);

            stat_str.append(stat->smap->toDebugString());
            stat_str.append("\n");
            return stat_str.toString();
        };

        FmtBuffer stats_info;
        stats_info.append("  Blobs specific info: \n\n");

        for (const auto & [path, stats] : blob_store.blob_stats.getStats())
        {
            for (const auto & stat : stats)
            {
                if (blob_id != UINT32_MAX)
                {
                    if (stat->id == blob_id)
                    {
                        stats_info.append(stat_info(stat, path));
                        return stats_info.toString();
                    }
                    continue;
                }

                stats_info.append(stat_info(stat, path));
            }
        }

        if (blob_id != UINT32_MAX)
        {
            stats_info.fmtAppend("    no found blob {}", blob_id);
        }
        return stats_info.toString();
    }

    static String getDirectoryInfo(PageDirectory::MVCCMapType & mvcc_table_directory, UInt64 ns_id, UInt64 page_id)
    {
        auto page_info = [](UInt128 page_internal_id_, const VersionedPageEntriesPtr & versioned_entries) {
            FmtBuffer page_str;
            page_str.fmtAppend("    page id {}\n", page_internal_id_);
            page_str.fmtAppend("      {}\n", versioned_entries->toDebugString());

            size_t count = 0;
            for (const auto & [version, entry_or_del] : versioned_entries->entries)
            {
                const auto & entry = entry_or_del.entry;
                page_str.fmtAppend("      entry {}\n"
                                   "       sequence: {}\n"
                                   "       epoch: {}\n"
                                   "       is del: {}\n"
                                   "       blob id: {}\n"
                                   "       offset: {}\n"
                                   "       size: {}\n"
                                   "       crc: {}\n", //
                                   count++, //
                                   version.sequence, //
                                   version.epoch, //
                                   entry_or_del.isDelete(), //
                                   entry.file_id, //
                                   entry.offset, //
                                   entry.size, //
                                   entry.checksum, //
                                   entry.field_offsets.size() //
                );
                if (!entry.field_offsets.empty())
                {
                    page_str.append("          field offset:\n");
                    for (const auto & [offset, crc] : entry.field_offsets)
                    {
                        page_str.fmtAppend("            offset: {} crc: 0x{:X}\n", offset, crc);
                    }
                    page_str.append("\n");
                }
            }
            return page_str.toString();
        };

        FmtBuffer directory_info;
        directory_info.append("  Directory specific info: \n\n");
        for (const auto & [internal_id, versioned_entries] : mvcc_table_directory)
        {
            if (page_id != UINT64_MAX)
            {
                if (internal_id.low == page_id && internal_id.high == ns_id)
                {
                    directory_info.append(page_info(internal_id, versioned_entries));
                    return directory_info.toString();
                }
                continue;
            }
            directory_info.append(page_info(internal_id, versioned_entries));
        }

        if (page_id != UINT64_MAX)
        {
            directory_info.fmtAppend("    no found page {}", page_id);
        }
        return directory_info.toString();
    }

    static String getSummaryInfo(PageDirectory::MVCCMapType & mvcc_table_directory, BlobStore & blob_store)
    {
        UInt64 longest_version_chaim = 0;
        UInt64 shortest_version_chaim = UINT64_MAX;
        FmtBuffer dir_summary_info;

        dir_summary_info.append("  Directory summary info: \n");

        for (const auto & [internal_id, versioned_entries] : mvcc_table_directory)
        {
            (void)internal_id;
            longest_version_chaim = std::max(longest_version_chaim, versioned_entries->size());
            shortest_version_chaim = std::min(shortest_version_chaim, versioned_entries->size());
        }

        dir_summary_info.fmtAppend("    total pages: {}, longest version chaim: {} , shortest version chaim: {} \n\n",
                                   mvcc_table_directory.size(),
                                   longest_version_chaim,
                                   shortest_version_chaim);

        dir_summary_info.append("  Blobs summary info: \n");
        const auto & blob_stats = blob_store.blob_stats.getStats();
        dir_summary_info.joinStr(
            blob_stats.begin(),
            blob_stats.end(),
            [](const auto arg, FmtBuffer & fb) {
                for (const auto & stat : arg.second)
                {
                    fb.fmtAppend("   stat id: {}\n"
                                 "     path: {}\n"
                                 "     total size: {}\n"
                                 "     valid size: {}\n"
                                 "     valid rate: {}\n"
                                 "     max cap: {}\n",
                                 stat->id,
                                 arg.first,
                                 stat->sm_total_size,
                                 stat->sm_valid_size,
                                 stat->sm_valid_rate,
                                 stat->sm_max_caps);
                }
            },
            "");

        return dir_summary_info.toString();
    }

    static String checkSinglePage(PageDirectory::MVCCMapType & mvcc_table_directory, BlobStore & blob_store, UInt64 ns_id, UInt64 page_id)
    {
        const auto & page_internal_id = buildV3Id(ns_id, page_id);
        const auto & it = mvcc_table_directory.find(page_internal_id);
        if (it == mvcc_table_directory.end())
        {
            return fmt::format("Can't find {}", page_internal_id);
        }

        FmtBuffer error_msg;
        size_t error_count = 0;
        for (const auto & [version, entry_or_del] : it->second->entries)
        {
            if (entry_or_del.isEntry() && it->second->type == EditRecordType::VAR_ENTRY)
            {
                (void)blob_store;
                try
                {
                    PageIDAndEntryV3 to_read_entry;
                    const PageEntryV3 & entry = entry_or_del.entry;
                    PageIDAndEntriesV3 to_read;
                    to_read_entry.first = page_internal_id;
                    to_read_entry.second = entry;

                    to_read.emplace_back(to_read_entry);
                    blob_store.read(to_read);

                    if (!entry.field_offsets.empty())
                    {
                        DB::PageStorage::FieldIndices indices(entry.field_offsets.size());
                        std::iota(std::begin(indices), std::end(indices), 0);

                        BlobStore::FieldReadInfos infos;
                        BlobStore::FieldReadInfo info(page_internal_id, entry, indices);
                        infos.emplace_back(info);
                        blob_store.read(infos);
                    }
                }
                catch (DB::Exception & e)
                {
                    error_count++;
                    error_msg.append(e.displayText());
                    error_msg.append("\n");
                }
            }
        }

        if (error_count == 0)
        {
            return fmt::format("Checked {} without any error.", page_internal_id);
        }

        error_msg.fmtAppend("Check {} meet {} errors!", page_internal_id, error_count);
        return error_msg.toString();
    }

    static String checkAllDatasCrc(PageDirectory::MVCCMapType & mvcc_table_directory, BlobStore & blob_store, bool enable_fo_check)
    {
        size_t total_pages = mvcc_table_directory.size();
        size_t cut_index = 0;
        size_t index = 0;
        std::cout << fmt::format("Begin to check all of datas CRC. enable_fo_check={}", static_cast<int>(enable_fo_check)) << std::endl;

        std::list<std::pair<UInt128, PageVersionType>> error_versioned_pages;
        for (const auto & [internal_id, versioned_entries] : mvcc_table_directory)
        {
            if (index == total_pages / 10 * cut_index)
            {
                std::cout << fmt::format("processing : {}%", cut_index * 10) << std::endl;
                cut_index++;
            }

            // TODO : need replace by getLastEntry();
            for (const auto & [version, entry_or_del] : versioned_entries->entries)
            {
                if (entry_or_del.isEntry() && versioned_entries->type == EditRecordType::VAR_ENTRY)
                {
                    (void)blob_store;
                    try
                    {
                        PageIDAndEntryV3 to_read_entry;
                        const PageEntryV3 & entry = entry_or_del.entry;
                        PageIDAndEntriesV3 to_read;
                        to_read_entry.first = internal_id;
                        to_read_entry.second = entry;

                        to_read.emplace_back(to_read_entry);
                        blob_store.read(to_read);

                        if (enable_fo_check && !entry.field_offsets.empty())
                        {
                            DB::PageStorage::FieldIndices indices(entry.field_offsets.size());
                            std::iota(std::begin(indices), std::end(indices), 0);

                            BlobStore::FieldReadInfos infos;
                            BlobStore::FieldReadInfo info(internal_id, entry, indices);
                            infos.emplace_back(info);
                            blob_store.read(infos);
                        }
                    }
                    catch (DB::Exception & e)
                    {
                        error_versioned_pages.emplace_back(std::make_pair(internal_id, version));
                    }
                }
            }
            index++;
        }

        if (error_versioned_pages.empty())
        {
            return "All of data checked. All passed.";
        }

        FmtBuffer error_msg;
        error_msg.append("Found error in these pages: ");
        for (const auto & [internal_id, versioned] : error_versioned_pages)
        {
            error_msg.fmtAppend("id: {}, sequence: {}, epoch: {} \n", internal_id, versioned.sequence, versioned.epoch);
        }
        error_msg.append("Please use `--query_table_id` + `--check_page_id` to get the more error info.");

        return error_msg.toString();
    }

private:
    ControlOptions options;
};

} // namespace DB::PS::V3

using namespace DB::PS::V3;
int main(int argc, char ** argv)
{
    const auto & options = ControlOptions::parse(argc, argv);
    PageStorageControl(options).run();
    return 0;
}

max_stars_repo_name: solotzg/tiflash

max_issues_repo_name: solotzg/tiflash

max_forks_repo_name: solotzg/tiflash


text:
// Copyright 2020 Advanced Remanufacturing and Technology Centre
// Copyright 2020 ROS-Industrial Consortium Asia Pacific Team
//
// 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 PLANNING_FUNCTIONS_HPP_
#define PLANNING_FUNCTIONS_HPP_

#include <boost/filesystem.hpp>
#include <ament_index_cpp/get_package_share_directory.hpp>
#include <opencv2/opencv.hpp>
#include <cv_bridge/cv_bridge.h>

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <algorithm>
#include "cmath"
#include "sensor_msgs/msg/camera_info.hpp"
#include "sensor_msgs/image_encodings.hpp"
#include "yaml-cpp/yaml.h"

float PI = 3.14159265;
/**
 * Calculate length of two points in 2D space
 */
float length(int x1, int y1, int x2, int y2)
{
  return sqrt(pow(static_cast<float>(x1 - x2), 2) + pow(static_cast<float>(y1 - y2), 2));
}

/**
 * Checks if a point is in a circle.
 */
bool in_circle(float radius, std::vector<int> centre, std::vector<int> point)
{
  if (pow((point[0] - centre[0]), 2) + pow((point[1] - centre[1]), 2) - pow(radius, 2) < 0) {
    return true;
  } else {
    return false;
  }
}

/**
 * Quadratic equation solver
 */
std::vector<float> quadratic_equation(float a, float b, float c)
{
  std::vector<float> result;
  float discriminant = pow(b, 2) - (4 * a * c);
  if (discriminant < 0) {
    return result;
  } else if (discriminant == 0) {
    result.push_back(-b / (2 * a));
  } else {
    result.push_back(((-b + sqrt(discriminant)) / (2 * a)));
    result.push_back(((-b - sqrt(discriminant)) / (2 * a)));
  }
  return result;
}

/**
 * Get the intersecting coordinates between a circle and a line
 */
std::vector<std::vector<int>> circle_line_intersect(
  float gradient,
  float intersect,
  float radius,
  std::vector<float> circle_centre)
{
  std::vector<std::vector<int>> result;

  if (circle_centre.size() == 2 && radius > 0) {
    // Get the quadratic equation to determine the x coordinates of point
    float a = 1 + pow(gradient, 2);
    float b = 2 * ((gradient * (intersect - circle_centre[1])) - circle_centre[0]);
    float c = pow(circle_centre[0], 2) +
      pow((intersect - circle_centre[1]), 2) - pow(radius, 2);

    std::vector<float> x_coords = quadratic_equation(a, b, c);

    // Solve the equation to get the x coordinates
    if (static_cast<int>(x_coords.size()) == 2) {
      for (int i = 0; i < static_cast<int>(x_coords.size()); i++) {
        std::vector<int> temp_coords {static_cast<int>(round(x_coords[i])),
          static_cast<int>(round(gradient * x_coords[i] + intersect))};
        result.push_back(temp_coords);
      }
    } else {
      return result;
    }
  }

  return result;
}

/**
 * Get a bounding box based on coodinate centre and radius of box area.
 */

std::vector<std::vector<int>> get_border_corners(std::vector<int> centre, float radius)
{
  std::vector<std::vector<int>> result;
  if (centre.size() == 2 && radius > 0 && centre[0] >= 0 && centre[1] >= 0) {
    std::vector<int> top_left;
    std::vector<int> bottom_right;
    if (centre[0] - radius < 0) {
      top_left.push_back(0);
    } else {
      top_left.push_back(static_cast<int>(round(centre[0] - radius)));
    }
    if (centre[1] - radius < 0) {
      top_left.push_back(0);
    } else {
      top_left.push_back(static_cast<int>(round(centre[1] - radius)));
    }
    bottom_right = {static_cast<int>(round(centre[0] + radius)),
      static_cast<int>(round(centre[1] + radius))};
    result = {top_left, bottom_right};
  } else {
    return result;
  }
  return result;
}

/**
 * Make sure input is within 0 to Pi
 */

float keep_angle_in_bounds(float input)
{
  if (input < 0 || input >= PI) {
    if (input < 0) {
      while (input < 0) {
        input += PI;
      }
    }
    if (input >= PI) {
      while (input >= PI) {
        input -= PI;
      }
    }
  }
  return input;
}

#endif  // PLANNING_FUNCTIONS_HPP_

ext: hpp


text:
/******************************************************************************
*       SOFA, Simulation Open-Framework Architecture, development version     *
*                (c) 2006-2017 INRIA, USTL, UJF, CNRS, MGH                    *
*                                                                             *
* This program is free software; you can redistribute it and/or modify it     *
* under the terms of the GNU Lesser General Public License as published by    *
* the Free Software Foundation; either version 2.1 of the License, or (at     *
* your option) any later version.                                             *
*                                                                             *
* This program is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
* for more details.                                                           *
*                                                                             *
* You should have received a copy of the GNU Lesser General Public License    *
* along with this program. If not, see <http://www.gnu.org/licenses/>.        *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt)          *
*                                                                             *
* Contact information: contact@sofa-framework.org                             *
******************************************************************************/
/*****************************************************************************
* User of this library should read the documentation
* in the messaging.h file.
******************************************************************************/

#include "RichConsoleStyleMessageFormatter.h"
#include "Message.h"

#include <sofa/helper/system/console.h>
#include <sofa/helper/fixed_array.h>

#include <boost/tokenizer.hpp>
#include <boost/token_functions.hpp>
#include <boost/token_iterator.hpp>

namespace sofa
{

namespace helper
{

namespace logging
{

namespace richconsolestylemessageformater
{
/////////////////////////////// STATIC ELEMENT SPECIFIC TO RichConsoleStyleMessage /////////////////
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
helper::fixed_array<std::string,Message::TypeCount> s_messageTypePrefixes;
helper::fixed_array<Console::ColorType,Message::TypeCount> s_messageTypeColors;

int initColors(){
    s_messageTypePrefixes[Message::Advice]      = "[SUGGESTION] ";
    s_messageTypePrefixes[Message::Info]        = "[INFO]    ";
    s_messageTypePrefixes[Message::Deprecated]  = "[DEPRECATED] ";
    s_messageTypePrefixes[Message::Warning]     = "[WARNING] ";
    s_messageTypePrefixes[Message::Error]       = "[ERROR]   ";
    s_messageTypePrefixes[Message::Fatal]       = "[FATAL]   ";
    s_messageTypePrefixes[Message::TEmpty]      = "[EMPTY]   ";

    s_messageTypeColors[Message::Advice]       = Console::BRIGHT_GREEN;
    s_messageTypeColors[Message::Info]       = Console::BRIGHT_GREEN;
    s_messageTypeColors[Message::Deprecated] = Console::BRIGHT_YELLOW;
    s_messageTypeColors[Message::Warning]    = Console::BRIGHT_CYAN;
    s_messageTypeColors[Message::Error]      = Console::BRIGHT_RED;
    s_messageTypeColors[Message::Fatal]      = Console::BRIGHT_PURPLE;
    s_messageTypeColors[Message::TEmpty]     = Console::DEFAULT_COLOR;
    return 1;
}

int s_isInited = initColors();

///
/// \brief simpleFormat a text containing our markdown 'tags'
/// \param jsize size of the line prefix to fill with space (for left side alignment)
/// \param text  the text to format
/// \param line_length number of column to render to to
/// \param wrapped the destination stream where to write the formatted text.
///
void simpleFormat(int jsize, const std::string& text, size_t line_length,
                  std::ostream& wrapped)
{
    //TODO(dmarchal): All that code is a mess...need to be done for real.

    /// space and * are separator that are returned in the token flow
    /// while "\n" is a 'hidden' separator.
    static boost::char_separator<char> sep("\n", "* '");

    std::string emptyspace(jsize, ' ') ;

    tokenizer tokens(text, sep) ;

    int numspaces = 0 ;
    bool beginOfLine = false ;
    bool isInItalic = false ;

    size_t space_left = line_length;
    for (tokenizer::iterator tok_iter = tokens.begin();tok_iter != tokens.end(); ++tok_iter)
    {
        const std::string& word = *tok_iter;
        if(word=="'" || word=="*")
        {
            if(isInItalic)
            {
                isInItalic=false;
                wrapped << Console::DEFAULT_CODE ;
                wrapped << "'";
                continue;
            }
            else
            {
                isInItalic=true;

                if(numspaces==1){
                    if(!beginOfLine){
                        wrapped << " ";
                        numspaces = 0;
                        space_left--;
                    }else{
                        wrapped << Console::DEFAULT_CODE << Console::DEFAULT_COLOR << emptyspace ;
                    }
                }

                wrapped << "'";
                wrapped << Console::ITALIC ;
                wrapped << Console::UNDERLINE ;
                continue;
            }
        }else if(word==" ")
        {
            if(numspaces==1)
            {
                wrapped << "\n"  ;
                numspaces=0;
                space_left = line_length;
                beginOfLine=true;
                continue;
            }else
            {
                numspaces=1;
                continue;
            }
        }else{
            if(numspaces==1){
                if(!beginOfLine){
                    wrapped << " ";
                    numspaces = 0;
                    space_left--;
                }else{
                    wrapped << Console::DEFAULT_CODE;
                    wrapped << Console::DEFAULT_COLOR;
                    wrapped << emptyspace ;
                    if(isInItalic){
                        wrapped << Console::ITALIC;
                        wrapped << Console::UNDERLINE;
                    }
                }
            }
        }

        if (space_left < word.length() + 1)
        {
            if(word.length()>line_length)
            {
                std::string first;
                size_t curidx=0;
                size_t endidx=std::min(word.length(), space_left-1);

                while(curidx < word.length())
                {
                    first=word.substr(curidx,endidx);

                    if(beginOfLine){
                        wrapped << Console::DEFAULT_CODE;
                        wrapped << Console::DEFAULT_COLOR;
                        wrapped << emptyspace ;
                        if(isInItalic){
                            wrapped << Console::ITALIC;
                            wrapped << Console::UNDERLINE;
                        }
                    }
                    beginOfLine=false;
                    wrapped << first ;

                    curidx+=endidx;
                    endidx=std::min(word.length()-curidx, line_length-1);

                    if(curidx < word.length())
                    {
                        wrapped << "\n" ;
                        beginOfLine=true;
                    }
                }
                space_left = line_length - first.length();
            }
            else
            {
                wrapped << "\n";
                wrapped << Console::DEFAULT_CODE;
                wrapped << Console::DEFAULT_COLOR;
                wrapped << emptyspace ;
                if(isInItalic){
                    wrapped << Console::ITALIC;
                    wrapped << Console::UNDERLINE;
                }
                wrapped << word ;
                space_left = line_length-word.length();
            }
        }
        else
        {
            if(beginOfLine){
                wrapped << Console::DEFAULT_CODE;
                wrapped << Console::DEFAULT_COLOR;
                wrapped << emptyspace ;
                if(isInItalic){
                    wrapped << Console::ITALIC;
                    wrapped << Console::UNDERLINE;
                }
            }
            beginOfLine=false;
            wrapped << word;
            space_left -= word.length() ;
        }
    }
}

////////////////////////////// RichConsoleStyleMessageFormatter Implementation /////////////////////
RichConsoleStyleMessageFormatter::RichConsoleStyleMessageFormatter(){}

void RichConsoleStyleMessageFormatter::formatMessage(const Message& m, std::ostream& out)
{
    int psize = s_messageTypePrefixes[m.type()].size() ;

    out << s_messageTypeColors[m.type()] << s_messageTypePrefixes[m.type()];

    if (!m.sender().empty())
    {
        if( m.componentInfo() )
        {
            psize +=m.sender().size()+m.componentInfo()->m_name.size()+5 ;
            out << Console::BLUE << "[" << m.sender()<< "(" << m.componentInfo()->m_name << ")] ";
        }
        else
        {
            psize +=m.sender().size()+3 ;
            out << Console::BLUE << "[" << m.sender()<< "] ";
        }
    }
    out << Console::DEFAULT_COLOR;

    /// Format & align the text and write the result into 'out'.
    simpleFormat(psize , m.message().str(), Console::getColumnCount()-psize, out) ;

    if(m.fileInfo()){
        std::stringstream buf;
        std::string emptyspace(psize, ' ') ;
        buf << "Emitted from '" << m.fileInfo()->filename << "' line " << m.fileInfo()->line ;
        out << "\n" << Console::DEFAULT_CODE << Console::DEFAULT_COLOR << emptyspace ;
        simpleFormat(psize , buf.str(), Console::getColumnCount()-psize, out) ;
    }

    ///Restore the console rendering attribute.
    out << Console::DEFAULT_COLOR;
    out << Console::DEFAULT_CODE;
    out << std::endl ;
}

} // richconsolestylmessageformatter
} // logging
} // helper
} // sofa

ext: cpp

max_stars_repo_name: sofa-framework/issofa

max_issues_repo_name: sofa-framework/issofa

max_forks_repo_name: sofa-framework/issofa


text:
/*
@copyright Louis Dionne 2015
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
 */

#include <boost/hana/assert.hpp>
#include <boost/hana/config.hpp>
#include <boost/hana/core/datatype.hpp>
#include <boost/hana/integral_constant.hpp>
#include <boost/hana/maybe.hpp>
#include <boost/hana/string.hpp>

#include <type_traits>
using namespace boost::hana;

int main() {

{

//! [BOOST_HANA_STRING]
BOOST_HANA_CONSTEXPR_LAMBDA auto str = BOOST_HANA_STRING("abcdef");
static_assert(std::is_same<datatype_t<decltype(str)>, String>{}, "");
//! [BOOST_HANA_STRING]

}{

//! [string]
constexpr auto str = string<'a', 'b', 'c', 'd', 'e', 'f'>;
static_assert(std::is_same<datatype_t<decltype(str)>, String>{}, "");
//! [string]

}{

//! [comparable]
BOOST_HANA_CONSTANT_CHECK(
    BOOST_HANA_STRING("abcdef") == BOOST_HANA_STRING("abcdef")
);

BOOST_HANA_CONSTANT_CHECK(
    BOOST_HANA_STRING("abcdef") != BOOST_HANA_STRING("abef")
);
//! [comparable]

}{

//! [orderable]
BOOST_HANA_CONSTANT_CHECK(
    BOOST_HANA_STRING("abc") < BOOST_HANA_STRING("bcd")
);

BOOST_HANA_CONSTANT_CHECK(
    BOOST_HANA_STRING("abcd") > BOOST_HANA_STRING("abc")
);
//! [orderable]

}{

//! [foldable]
auto sum_string = [](auto str) {
    return foldl(str, int_<0>, [](auto sum, auto c) {
        constexpr int i = value(c) - 48; // convert character to decimal
        return sum + int_<i>;
    });
};

BOOST_HANA_CONSTANT_CHECK(
    sum_string(BOOST_HANA_STRING("1234")) == int_<1 + 2 + 3 + 4>
);
//! [foldable]

}{

//! [iterable]
BOOST_HANA_CONSTANT_CHECK(!is_empty(BOOST_HANA_STRING("abcd")));
BOOST_HANA_CONSTANT_CHECK(is_empty(BOOST_HANA_STRING("")));

BOOST_HANA_CONSTANT_CHECK(BOOST_HANA_STRING("abcd")[int_<2>] == char_<'c'>);

auto is_vowel = [](auto c) {
    return c ^in^ BOOST_HANA_STRING("aeiouy");
};
BOOST_HANA_CONSTANT_CHECK(
    drop_while(BOOST_HANA_STRING("aioubcd"), is_vowel) == BOOST_HANA_STRING("bcd")
);
//! [iterable]

}{

//! [searchable]
BOOST_HANA_CONSTANT_CHECK(char_<'c'> ^in^ BOOST_HANA_STRING("abcde"));
BOOST_HANA_CONSTANT_CHECK(!(char_<'z'> ^in^ BOOST_HANA_STRING("abcde")));

BOOST_HANA_CONSTANT_CHECK(
    find(BOOST_HANA_STRING("abcxefg"), char_<'x'>) == just(char_<'x'>)
);
//! [searchable]

}

}

max_stars_repo_name: josephwinston/hana

max_issues_repo_name: josephwinston/hana

max_forks_repo_name: josephwinston/hana


text:
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
// Licensed under the MIT License (MIT). See License.txt in the repo root for license information.
// ------------------------------------------------------------

#include "stdafx.h"

#include <boost/test/unit_test.hpp>
#include "Common/boost-taef.h"

Common::StringLiteral LinkableTraceType("LinkableAsyncOperationTest");

namespace Common
{
    using namespace std;

    class AsyncOpContainer : public ComponentRoot
    {
        DENY_COPY(AsyncOpContainer)

    public:
        AsyncOpContainer()
            : ComponentRoot()
            , primary_(nullptr)
            , lock_()
            , pendingOps_()
            , allOpsCompletedEvent_()
        {
        }

        virtual ~AsyncOpContainer() {}

        AsyncOperationSPtr BeginOp(
            wstring const & id,
            TimeSpan const timeToWait,
            TimeSpan const timeout,
            bool hasRetryableErrors,
            ErrorCode const expectedEndResult)
        {
            AsyncOperationSPtr operation;
            {
                AcquireExclusiveLock lock(lock_);
                ++pendingOps_;
                if (primary_)
                {
                    Trace.WriteInfo(LinkableTraceType, "Create secondary {0}. {1} pending ops", id, pendingOps_);
                    operation = AsyncOperation::CreateAndStart<MyLinkableAsyncOperation>(
                        *this,
                        id,
                        timeToWait,
                        timeout,
                        hasRetryableErrors,
                        primary_,
                        [this, expectedEndResult](AsyncOperationSPtr const & op) { OnOpCompleted(op, false, expectedEndResult); },
                        this->CreateAsyncOperationRoot());
                }
                else
                {
                    VERIFY_ARE_EQUAL(pendingOps_, 1, L"Primary is the first operation");
                    Trace.WriteInfo(LinkableTraceType, "Create primary {0}.", id);
                    operation = AsyncOperation::CreateAndStart<MyLinkableAsyncOperation>(
                        *this,
                        id,
                        timeToWait,
                        timeout,
                        hasRetryableErrors,
                        [this, expectedEndResult](AsyncOperationSPtr const & op) { OnOpCompleted(op, false, expectedEndResult); },
                        this->CreateAsyncOperationRoot());
                    primary_ = operation;
                }
            }

            AsyncOperation::Get<MyLinkableAsyncOperation>(operation)->ResumeOutsideLock(operation);
            OnOpCompleted(operation, true, expectedEndResult);

            return operation;
        }

        void OnOpCompleted(
            AsyncOperationSPtr const& operation,
            bool expectedCompletedSynchronously,
            ErrorCode const expectedEndResult)
        {
            if (operation->CompletedSynchronously != expectedCompletedSynchronously)
            {
                return;
            }

            auto error = MyLinkableAsyncOperation::End(operation);
            Trace.WriteInfo(LinkableTraceType, "Operation completed with {0}, expected {1}", error, expectedEndResult);
            VERIFY_ARE_EQUAL(
                error.ReadValue(),
                expectedEndResult.ReadValue(),
                L"Operation completed with expected error");

            {
                AcquireExclusiveLock lock(lock_);
                --pendingOps_;
                if (pendingOps_ == 0)
                {
                    primary_.reset();
                    Trace.WriteInfo(LinkableTraceType, "All operations completed");
                    allOpsCompletedEvent_.Set();
                }
            }
        }

        void WaitForOpsCompleted()
        {
            auto error = allOpsCompletedEvent_.Wait(TimeSpan::FromSeconds(10));
            Trace.WriteInfo(LinkableTraceType, "AllOpsCompleted.waitOne returned {0}", error);
            VERIFY_IS_TRUE(error.IsSuccess(), L"All operations completed");
            VERIFY_ARE_EQUAL(pendingOps_, 0, L"All operations completed");
        }

    private:
        class MyLinkableAsyncOperation : public LinkableAsyncOperation
        {
        public:
            MyLinkableAsyncOperation(
                __in AsyncOpContainer & owner,
                wstring const & id,
                TimeSpan const timeToWait,
                TimeSpan const timeout,
                bool hasRetryableErrors,
                AsyncOperationSPtr const & primary,
                AsyncCallback const & callback,
                AsyncOperationSPtr const & parent)
              : LinkableAsyncOperation(primary, callback, parent)
              , owner_(owner)
              , id_(id)
              , hasRetryableErrors_(hasRetryableErrors)
              , waitEvent_()
              , timeToWait_(timeToWait)
              , timeoutHelper_(timeout)
              , timerActivity_()
              , timerTimeout_()
            {
            }

            MyLinkableAsyncOperation(
                __in AsyncOpContainer & owner,
                wstring const & id,
                TimeSpan const timeToWait,
                TimeSpan const timeout,
                bool hasRetryableErrors,
                AsyncCallback const & callback,
                AsyncOperationSPtr const & parent)
              : LinkableAsyncOperation(callback, parent)
              , owner_(owner)
              , id_(id)
              , hasRetryableErrors_(hasRetryableErrors)
              , waitEvent_()
              , timeToWait_(timeToWait)
              , timeoutHelper_(timeout)
              , timerActivity_()
              , timerTimeout_()
            {
            }

            static ErrorCode End(AsyncOperationSPtr const & asyncOperation)
            {
                auto casted = AsyncOperation::End<MyLinkableAsyncOperation>(asyncOperation);
                if (casted->timerActivity_)
                {
                    casted->timerActivity_->Cancel();
                    casted->timerActivity_.reset();
                }

                if (casted->timerTimeout_)
                {
                    casted->timerTimeout_->Cancel();
                    casted->timerTimeout_.reset();
                }

                return casted->Error;
            }

        protected:
            void OnResumeOutsideLockPrimary(AsyncOperationSPtr const & thisSPtr)
            {
                timerActivity_ = Timer::Create("LinkableAsyncOperationTest1", [this](TimerSPtr const & )
                {
                    Trace.WriteInfo(LinkableTraceType, "Primary {0}: TimerActivity fired", id_);
                    this->waitEvent_.Set();
                });
                timerActivity_->Change(timeToWait_);

                timerTimeout_ = Timer::Create("LinkableAsyncOperationTest2", [this, thisSPtr](TimerSPtr const &)
                {
                    Trace.WriteInfo(LinkableTraceType, "Primary {0}: TimerTimeout fired", id_);
                    thisSPtr->TryComplete(thisSPtr, ErrorCode(ErrorCodeValue::Timeout));
                });
                timerTimeout_->Change(timeoutHelper_.GetRemainingTime());

                Trace.WriteInfo(LinkableTraceType, "Primary {0}: OnResumeOutsideLock", id_);
                auto inner = waitEvent_.BeginWaitOne(
                    timeToWait_.AddWithMaxAndMinValueCheck(TimeSpan::FromMilliseconds(100)),
                    [this] (AsyncOperationSPtr const & operation) { this->OnWaitCompleted(operation, false); },
                    thisSPtr);
                OnWaitCompleted(inner, true);
            }

            bool IsErrorRetryable(ErrorCode const error)
            {
                bool retry = false;
                if (hasRetryableErrors_ && error.IsError(ErrorCodeValue::Timeout))
                {
                    retry = !timeoutHelper_.IsExpired;
                }

                Trace.WriteInfo(LinkableTraceType, "Error {0} is retryable {1}", error, retry);
                return retry;
            }

        void OnCompleted()
        {
            LinkableAsyncOperation::OnCompleted();
            if (!IsSecondary)
            {
                Trace.WriteInfo(LinkableTraceType, "Primary {0} completed", id_);
                timerActivity_->Cancel();
                timerTimeout_->Cancel();
                if (PromotedPrimary)
                {
                    Trace.WriteInfo(LinkableTraceType, "Old primary {0} completed, but new primary chosen", id_);
                    AcquireExclusiveLock lock(owner_.lock_);
                    owner_.primary_ = this->PromotedPrimary;
                }
            }
        }

        private:
            void OnWaitCompleted(
                AsyncOperationSPtr const & operation,
                bool expectedCompletedSynchronously)
            {
                if(operation->CompletedSynchronously != expectedCompletedSynchronously)
                {
                    return;
                }

                auto error = waitEvent_.EndWaitOne(operation);
                this->TryComplete(operation->Parent, error);
            }

            AsyncOpContainer & owner_;
            wstring id_;
            bool hasRetryableErrors_;
            TimeoutHelper timeoutHelper_;
            TimeSpan timeToWait_;
            AsyncManualResetEvent waitEvent_;
            TimerSPtr timerActivity_;
            TimerSPtr timerTimeout_;
        };
        typedef shared_ptr<MyLinkableAsyncOperation> MyLinkableAsyncOperationSPtr;

        AsyncOperationSPtr primary_;
        ExclusiveLock lock_;
        int pendingOps_;
        AsyncManualResetEvent allOpsCompletedEvent_;
    };

    class TestLinkableAsyncOperation
    {
    };

    // Start 2 operations - primary and secondary
    // The primary finishes with success, so the secondary must finish with success also
    BOOST_FIXTURE_TEST_SUITE(TestLinkableAsyncOperationSuite,TestLinkableAsyncOperation)

    BOOST_AUTO_TEST_CASE(TestSuccess)
    {
        shared_ptr<AsyncOpContainer> root = make_shared<AsyncOpContainer>();
        root->BeginOp(
            L"TestSuccess0",
            TimeSpan::FromMilliseconds(100),
            TimeSpan::FromSeconds(1),
            false,
            ErrorCode(ErrorCodeValue::Success));

        root->BeginOp(
            L"TestSuccess1",
            TimeSpan::FromSeconds(10),
            TimeSpan::FromSeconds(1),
            false,
            ErrorCode(ErrorCodeValue::Success));

        root->WaitForOpsCompleted();
    }

    // Start 2 operations - primary and secondary
    // The primary finishes with Timeout, so the secondary must finish with Timeout also
    BOOST_AUTO_TEST_CASE(TestTimeoutNoRetryableErrors)
    {
        shared_ptr<AsyncOpContainer> root = make_shared<AsyncOpContainer>();
        root->BeginOp(
            L"TestTimeoutNoRetryableErrors0",
            TimeSpan::FromSeconds(1),
            TimeSpan::FromMilliseconds(100),
            false,
            ErrorCode(ErrorCodeValue::Timeout));

        root->BeginOp(
            L"TestTimeoutNoRetryableErrors1",
            TimeSpan::FromMilliseconds(100),
            TimeSpan::FromSeconds(1),
            false,
            ErrorCode(ErrorCodeValue::Timeout));

        root->WaitForOpsCompleted();
    }

    // Start 3 operations - primary, S1 and S2
    // The primary finishes with Timeout,
    // so the first secondary is promoted to primary
    // It completes with success, so the third secondary also succeeds
    BOOST_AUTO_TEST_CASE(TestTimeoutWithRetryableErrors)
    {
        shared_ptr<AsyncOpContainer> root = make_shared<AsyncOpContainer>();
        // Wait time is longer than timeout, so op times out
        // Before timing out, the other operations are linked to it as secondaries
        root->BeginOp(
            L"TestTimeoutWithRetryableErrors0",
            TimeSpan::FromSeconds(1),
            TimeSpan::FromMilliseconds(100),
            true,
            ErrorCode(ErrorCodeValue::Timeout));

        root->BeginOp(
            L"TestTimeoutWithRetryableErrors1",
            TimeSpan::FromMilliseconds(100),
            TimeSpan::FromSeconds(1),
            true,
            ErrorCode(ErrorCodeValue::Success));

        root->BeginOp(
            L"TestTimeoutWithRetryableErrors2",
            TimeSpan::FromMilliseconds(500),
            TimeSpan::FromSeconds(1),
            true,
            ErrorCode(ErrorCodeValue::Success));
        root->WaitForOpsCompleted();
    }

    // Start 3 operations: primary and 2 secondaries,
    // with secondary timeouts larger than primary timeout.
    // Cancel the first secondary - make sure it completes with the correct error.
    // When primary completes, the second secondary should be promoted to primary.
    BOOST_AUTO_TEST_CASE(TestCancelSecondary)
    {
        shared_ptr<AsyncOpContainer> root = make_shared<AsyncOpContainer>();
        // Wait time is longer than timeout, so op times out
        // Before timing out, the other operations are linked to it as secondaries
        root->BeginOp(
            L"TestCancelSecondary0",
            TimeSpan::FromSeconds(10),
            TimeSpan::FromSeconds(2),
            true,
            ErrorCode(ErrorCodeValue::Timeout));

        auto secondary = root->BeginOp(
            L"TestCancelSecondary1",
            TimeSpan::FromSeconds(5),
            TimeSpan::FromSeconds(7),
            true,
            ErrorCode(ErrorCodeValue::OperationCanceled));
        secondary->Cancel(true);

        root->BeginOp(
            L"TestCancelSecondary2",
            TimeSpan::FromSeconds(1),
            TimeSpan::FromSeconds(5),
            true,
            ErrorCode(ErrorCodeValue::Success));

        root->WaitForOpsCompleted();
    }

    BOOST_AUTO_TEST_SUITE_END()
}


text:
// Copyright (c) 2011-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <policy/fees.h>
#include <policy/policy.h>

#include <txmempool.h>
#include <uint256.h>
#include <util/time.h>

#include <test/util/setup_common.h>

#include <boost/test/unit_test.hpp>

BOOST_FIXTURE_TEST_SUITE(policyestimator_tests, TestingSetup)

BOOST_AUTO_TEST_CASE(MempoolMinimumFeeEstimate) {
    CTxMemPool mpool;
    LOCK2(cs_main, mpool.cs);
    TestMemPoolEntryHelper entry;

    // Create a transaction template
    CScript garbage;
    for (unsigned int i = 0; i < 128; i++) {
        garbage.push_back('X');
    }

    CMutableTransaction tx;
    tx.vin.resize(1);
    tx.vin[0].scriptSig = garbage;
    tx.vout.resize(1);
    tx.vout[0].nValue = Amount::zero();

    // Create a fake block
    std::vector<CTransactionRef> block;
    int blocknum = 0;

    // Loop through 200 blocks adding transactions so we have a estimateFee
    // that is calculable.
    while (blocknum < 200) {
        for (int64_t j = 0; j < 100; j++) {
            // make transaction unique
            tx.vin[0].nSequence = 10000 * blocknum + j;
            TxId txid = tx.GetId();
            mpool.addUnchecked(
                entry.Fee((j + 1) * DEFAULT_BLOCK_MIN_TX_FEE_PER_KB)
                    .Time(GetTime())
                    .Height(blocknum)
                    .FromTx(tx));
            CTransactionRef ptx = mpool.get(txid);
            block.push_back(ptx);
        }
        mpool.removeForBlock(block, ++blocknum);
        block.clear();
    }

    // Check that the estimate is above the rolling minimum fee. This should be
    // true since we have not trimmed the mempool.
    BOOST_CHECK(mpool.GetMinFee(1) <= mpool.estimateFee());

    // Check that estimateFee returns the minimum rolling fee even when the
    // mempool grows very quickly and no blocks have been mined.

    // Add a bunch of low fee transactions which are not in the mempool
    // And have zero fees.
    CMutableTransaction mtx;
    tx.vin.resize(1);
    tx.vin[0].scriptSig = garbage;
    tx.vout.resize(1);
    block.clear();

    // Add tons of transactions to the mempool,
    // but don't mine them.
    for (int64_t i = 0; i < 10000; i++) {
        // Mutate the hash
        tx.vin[0].nSequence = 10000 * blocknum + i;
        // Add new transaction to the mempool with a increasing fee
        // The average should end up as 1/2 * 100 *
        // DEFAULT_BLOCK_MIN_TX_FEE_PER_KB
        mpool.addUnchecked(entry.Fee((i + 1) * DEFAULT_BLOCK_MIN_TX_FEE_PER_KB)
                               .Time(GetTime())
                               .Height(blocknum)
                               .FromTx(tx));
    }

    // Trim to size. GetMinFee should be more than 10000 *
    // DEFAULT_BLOCK_MIN_TX_FEE_PER_KB, but the estimateFee should remain
    // unchanged.
    mpool.TrimToSize(1);
    BOOST_CHECK(mpool.GetMinFee(1) >=
                CFeeRate(10000 * DEFAULT_BLOCK_MIN_TX_FEE_PER_KB,
                         CTransaction(tx).GetTotalSize()));
    BOOST_CHECK_MESSAGE(mpool.estimateFee() == mpool.GetMinFee(1),
                        "Confirm blocks has failed");
}

BOOST_AUTO_TEST_SUITE_END()

max_stars_repo_name: syedrizwanmy/bitcoin-abc

max_issues_repo_name: EGYVOICE/bitcoin-abc-avalanche

max_forks_repo_name: EGYVOICE/bitcoin-abc-avalanche


text:
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (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.openairinterface.org/?page_id=698
 *
 * 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.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/*! \file nssf_http2-server.h
 \brief
 \author  Rohan Kharade
 \company Openairinterface Software Allianse
 \date 2021
 \email: rohan.kharade@openairinterface.org
 */

#include "nssf-http2-server.h"
#include <boost/algorithm/string.hpp>
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
#include <nlohmann/json.hpp>
#include <regex>
#include <string>

#include "3gpp_29.500.h"
#include "logger.hpp"
#include "nssf.h"
#include "nssf_config.hpp"

using namespace nghttp2::asio_http2;
using namespace nghttp2::asio_http2::server;
using namespace oai::nssf_server::model;

extern nssf::nssf_config nssf_cfg;

//------------------------------------------------------------------------------
void nssf_http2_server::start() {
  boost::system::error_code ec;

  Logger::nssf_sbi().info("HTTP2 server started");
  std::string nfId                            = {};
  std::string subscriptionID                  = {};
  NssaiAvailabilityInfo nssaiAvailabilityInfo = {};

  // NSSF NS Selection - Network Slice Information (Document)
  server.handle(
      NSSF_NSS_BASE + nssf_cfg.sbi_api_version + NSSF_NS_INFO_URL,
      [&](const request& request, const response& response) {
        request.on_data([&](const uint8_t* data, std::size_t len) {
          try {
            Logger::nssf_sbi().debug(
                "Received request for NS Selection - (HTTP_VERSION 2)");
            if (request.method().compare("GET") == 0) {
              // Initialise query parametres
              SliceInfoForUEConfigurationUpdate slice_info_ue_cu;
              SliceInfoForPDUSession slice_info_pdu_sess;
              SliceInfoForRegistration slice_info_reg;
              PlmnId home_plmnid;
              Tai tai;

              // Parse URI
              std::string qs = request.uri().raw_query;
              Logger::nssf_sbi().debug("QueryString: %s", qs.c_str());

              // Parse mandatory query parametrs
              std::string nfType = util::get_query_param(qs, NF_TYPE);
              std::string nfId   = util::get_query_param(qs, NF_ID);
              Logger::nssf_sbi().info(
                  " Query_PARAM::NF_TYPE - %s", nfType.c_str());
              Logger::nssf_sbi().info(" Query_PARAM::NF_ID - %s", nfId.c_str());

              // Parse optional query parametrs and API calbacks
              std::string slice_infoReg =
                  util::get_query_param(qs, SLICE_INFO_REGISTRATION);
              std::string slice_infoPduSession =
                  util::get_query_param(qs, SLICE_INFO_PDU_SESSION);
              std::string slice_infoUeCu =
                  util::get_query_param(qs, SLICE_INFO_UE_CU);
              std::string home_plmn_id =
                  util::get_query_param(qs, HOME_PLMN_ID);
              std::string supported_features =
                  util::get_query_param(qs, SUPPORTED_FEATURES);
              std::string tai_info = util::get_query_param(qs, TAI);

              if (!home_plmn_id.empty()) {
                nlohmann::json::parse(home_plmn_id.c_str()).get_to(home_plmnid);
                Logger::nssf_sbi().info(
                    " Query_PARAM::HOME_PLMN_ID - %s", home_plmn_id.c_str());
              }
              if (!tai_info.empty()) {
                nlohmann::json::parse(tai_info.c_str()).get_to(tai);
                Logger::nssf_sbi().info(
                    " Query_PARAM::TAI - %s", tai_info.c_str());
              }
              if (!supported_features.empty()) {
                Logger::nssf_sbi().info(
                    " Query_PARAM::SUPPORTED_FEATURES - %s",
                    supported_features.c_str());
              }
              if (!slice_infoReg.empty()) {
                nlohmann::json::parse(slice_infoReg.c_str())
                    .get_to(slice_info_reg);
                Logger::nssf_sbi().info(
                    " Query_PARAM::SLICE_INFO_REGISTRATION - %s",
                    slice_infoReg.c_str());
                this->get_slice_info_for_registration_handler(
                    nfType, nfId, slice_info_reg, tai, home_plmnid,
                    supported_features, response);
                return;
              }
              if (!slice_infoPduSession.empty()) {
                nlohmann::json::parse(slice_infoPduSession.c_str())
                    .get_to(slice_info_pdu_sess);
                Logger::nssf_sbi().info(
                    " Query_PARAM::SLICE_INFO_PDU_SESSION - %s",
                    slice_infoPduSession.c_str());
                this->get_slice_info_for_pdu_session_handler(
                    nfType, nfId, slice_info_pdu_sess, tai, home_plmnid,
                    supported_features, response);
                return;
              }
              if (!slice_infoUeCu.empty()) {
                nlohmann::json::parse(slice_infoUeCu.c_str())
                    .get_to(slice_info_ue_cu);
                Logger::nssf_sbi().info(
                    " Query_PARAM::SLICE_INFO_UE_CU - %s",
                    slice_infoUeCu.c_str());
                this->get_slice_info_for_ue_cu_handler(
                    nfType, nfId, slice_info_ue_cu, tai, home_plmnid,
                    supported_features, response);
                return;
              } else {
                this->get_slice_info_default_handler(
                    nfType, nfId, tai, home_plmnid, supported_features,
                    response);
              }
            } else {
              Logger::nssf_sbi().warn("Method not supported");
            }
          } catch (nlohmann::detail::exception& e) {
            Logger::nssf_sbi().warn(
                "Can not parse the json data (error: %s)!", e.what());
            response.write_head(
                http_status_code_e::HTTP_STATUS_CODE_400_BAD_REQUEST);
            response.end();
            return;
          }
        });
      });

  // NSSF NSSAI Availability - NF Instance ID (Document)
  server.handle(
      NSSF_NSSAI_AVAILABILITY_BASE + nssf_cfg.sbi_api_version +
          NSSF_NSSAI_AVAILABILITY_URL,
      [&](const request& request, const response& response) {
        request.on_data([&](const uint8_t* data, std::size_t len) {
          std::string msg((char*) data, len);
          try {
            std::vector<std::string> split_result;
            boost::split(
                split_result, request.uri().path, boost::is_any_of("/"));
            nfId = split_result[split_result.size() - 1].c_str();
            if (!nfId.empty()) {
              Logger::nssf_sbi().debug(
                  "Received request for NSSAI Availability - (HTTP_VERSION 2)");

              if (request.method().compare("PUT") == 0 && len > 0) {
                nlohmann::json::parse(msg.c_str())
                    .get_to(nssaiAvailabilityInfo);
                this->create_n_ssai_availability_handler(
                    nfId, nssaiAvailabilityInfo, response);
              }

              if (request.method().compare("PATCH") == 0 && len > 0) {
                std::vector<PatchItem> patchItem;
                nlohmann::json::parse(msg.c_str()).get_to(patchItem);
                // this->create_n_ssai_availability_handler(nfId,
                // nssaiAvailabilityInfo, response);
              }

              if (request.method().compare("DELETE") == 0 && len > 0) {
                // this->create_n_ssai_availability_handler(nfId,
                // nssaiAvailabilityInfo, response);
              }
            }
          } catch (nlohmann::detail::exception& e) {
            Logger::nssf_sbi().warn(
                "Can not parse the json data (error: %s)!", e.what());
            response.write_head(
                http_status_code_e::HTTP_STATUS_CODE_400_BAD_REQUEST);
            response.end();
            return;
          }
        });
      });

  // NSSF NSSAI Availability - Subscription ID (Collection/Document)
  server.handle(
      NSSF_NSSAI_AVAILABILITY_BASE + nssf_cfg.sbi_api_version +
          NSSF_NSSAI_AVAILABILITY_SUBSCRIPTION_URL,
      [&](const request& request, const response& response) {
        request.on_data([&](const uint8_t* data, std::size_t len) {
          // ToDo
        });
      });

  //###### NSSF Custom APIs ######
  // Get default slice config
  server.handle(
      "/nnssf-slice-config",
      [&](const request& request, const response& response) {
        request.on_data([&](const uint8_t* data, std::size_t len) {
          if (request.method().compare("GET") == 0) {
            this->get_slice_config(response);
          }
        });
      });

  // Get list of supported APIs
  server.handle("/", [&](const request& request, const response& response) {
    request.on_data([&](const uint8_t* data, std::size_t len) {
      if (request.method().compare("GET") == 0) {
        this->get_api_list(response);
      }
    });
  });

  if (server.listen_and_serve(ec, m_address, std::to_string(m_port))) {
    std::cerr << "HTTP Server error: " << ec.message() << std::endl;
  }
}

//------------------------------------------------------------------------------
void nssf_http2_server::get_slice_info_for_registration_handler(
    const std::string& nf_type, std::string& nf_id,
    const SliceInfoForRegistration slice_info, const Tai& tai,
    const PlmnId& home_plmnid, const std::string& features,
    const response& response) {
  Logger::nssf_sbi().info(
      "NS Selection: Got a request with slice info for Registration, "
      "Instance ID: %s",
      nf_id.c_str());
  // ToDo
}

//------------------------------------------------------------------------------
void nssf_http2_server::get_slice_info_for_pdu_session_handler(
    const std::string& nf_type, std::string& nf_id,
    const SliceInfoForPDUSession& slice_info, const Tai& tai,
    const PlmnId& home_plmnid, const std::string& features,
    const response& response) {
  Logger::nssf_app().info("");
  Logger::nssf_sbi().info(
      "NS Selection: Got a request with slice info for PDU Session, "
      "Instance ID: %s",
      nf_id.c_str());

  int http_code                  = 0;
  ProblemDetails problem_details = {};
  nlohmann::json json_data       = {};
  std::string content_type       = "application/json";
  std::string json_format;
  AuthorizedNetworkSliceInfo auth_slice_info;
  header_map h;
  h.emplace("content-type", header_value{content_type});

  if (nf_type.compare(NF_TYPE_AMF) == 0 || nf_type.compare(NF_TYPE_NSSF) == 0) {
    // ToDo - Check seperately first if TAI, HomePlmnId and features are
    // supported
    m_nssf_app->handle_slice_info_for_pdu_session(
        slice_info, tai, home_plmnid, features, http_code, 2, problem_details,
        auth_slice_info);
    if (http_code == HTTP_STATUS_CODE_200_OK) {
      to_json(json_data, auth_slice_info);

      response.write_head(http_code, h);
      response.end(json_data.dump().c_str());
    } else {
      response.write_head(http_code, h);
      to_json(json_data, problem_details);
      response.end(json_data.dump().c_str());
    }
  } else {
    Logger::nssf_sbi().error(
        "Invalid NF_Type (Valid NF_Type is AMF, NSSF, NWDAP, SMF)");
    Logger::nssf_app().info(
        "//---------------------------------------------------------");
    Logger::nssf_app().info("");
    http_code = HTTP_STATUS_CODE_400_BAD_REQUEST;
    response.write_head(http_code, h);
    to_json(json_data, problem_details);
    response.end();
  }
}

//------------------------------------------------------------------------------
void nssf_http2_server::get_slice_info_for_ue_cu_handler(
    const std::string& nf_type, std::string& nf_id,
    const SliceInfoForUEConfigurationUpdate& slice_info, const Tai& tai,
    const PlmnId& home_plmnid, const std::string& features,
    const response& response) {
  Logger::nssf_sbi().info(
      "NS Selection: Got a request with slice info for UE Config Update, "
      "Instance ID: %s",
      nf_id.c_str());
  // ToDo
}

//------------------------------------------------------------------------------
void nssf_http2_server::get_slice_info_default_handler(
    const std::string& nf_type, std::string& nf_id, const Tai& tai,
    const PlmnId& home_plmnid, const std::string& features,
    const response& response) {
  Logger::nssf_sbi().info(
      "NS Selection: Got a request with default query parameters, "
      "Instance ID: %s",
      nf_id.c_str());
  // ToDo and TBD
}

//------------------------------------------------------------------------------
void nssf_http2_server::create_n_ssai_availability_handler(
    const std::string& nfId, const NssaiAvailabilityInfo& nssaiAvailInfo,
    const response& response) {
  Logger::nssf_sbi().info(
      "NSSAI Availability: Got a request to "
      "Updates/replaces the NSSF with the S-NSSAIs");
  Logger::nssf_sbi().info("NSSAI Availability: Instance ID: %s", nfId.c_str());
}

//------------------------------------------------------------------------------
void nssf_http2_server::get_slice_config(const response& response) {
  Logger::nssf_sbi().info(
      "OAI-NSSF:: Default Slice Config is requested (HTTP Version 2)!!!");
  int http_code            = 0;
  nlohmann::json json_data = {};
  std::string content_type = "application/json";
  header_map h;
  h.emplace("content-type", header_value{content_type});
  if (nssf_cfg.get_slice_config(json_data)) {
    http_code = HTTP_STATUS_CODE_200_OK;
    response.write_head(http_code, h);
    response.end(json_data.dump(4).c_str());
  } else {
    http_code = HTTP_STATUS_CODE_503_SERVICE_UNAVAILABLE;
    response.write_head(http_code, h);
    response.end();
  }
}
//------------------------------------------------------------------------------
void nssf_http2_server::get_api_list(const response& response) {
  int http_code            = 0;
  nlohmann::json json_data = {};
  std::string content_type = "application/json";
  header_map h;
  h.emplace("content-type", header_value{content_type});
  if (nssf_cfg.get_api_list(json_data)) {
    http_code = HTTP_STATUS_CODE_200_OK;
    response.write_head(http_code, h);
    response.end(json_data.dump(4).c_str());
  } else {
    http_code = HTTP_STATUS_CODE_503_SERVICE_UNAVAILABLE;
    response.write_head(http_code, h);
    response.end();
  }
}
//------------------------------------------------------------------------------
void nssf_http2_server::stop() {
  server.stop();
}


text:
//
// strand.cpp
// ~~~~~~~~~~
//
// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//

// Disable autolinking for unit tests.
#if !defined(BOOST_ALL_NO_LIB)
#define BOOST_ALL_NO_LIB 1
#endif // !defined(BOOST_ALL_NO_LIB)

// Test that header file is self-contained.
#include <boost/asio/strand.hpp>

#include <sstream>
#include <boost/asio/io_context.hpp>
#include <boost/asio/dispatch.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/detail/thread.hpp>
#include "unit_test.hpp"

#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
# include <boost/asio/deadline_timer.hpp>
#else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
# include <boost/asio/steady_timer.hpp>
#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)

#if defined(BOOST_ASIO_HAS_BOOST_BIND)
# include <boost/bind.hpp>
#else // defined(BOOST_ASIO_HAS_BOOST_BIND)
# include <functional>
#endif // defined(BOOST_ASIO_HAS_BOOST_BIND)

using namespace boost::asio;

#if defined(BOOST_ASIO_HAS_BOOST_BIND)
namespace bindns = boost;
#else // defined(BOOST_ASIO_HAS_BOOST_BIND)
namespace bindns = std;
#endif

#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
typedef deadline_timer timer;
namespace chronons = boost::posix_time;
#elif defined(BOOST_ASIO_HAS_CHRONO)
typedef steady_timer timer;
namespace chronons = boost::asio::chrono;
#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)

void increment(int* count)
{
  ++(*count);
}

void increment_without_lock(io_context::strand* s, int* count)
{
  BOOST_ASIO_CHECK(!s->running_in_this_thread());

  int original_count = *count;

  dispatch(*s, bindns::bind(increment, count));

  // No other functions are currently executing through the locking dispatcher,
  // so the previous call to dispatch should have successfully nested.
  BOOST_ASIO_CHECK(*count == original_count + 1);
}

void increment_with_lock(io_context::strand* s, int* count)
{
  BOOST_ASIO_CHECK(s->running_in_this_thread());

  int original_count = *count;

  dispatch(*s, bindns::bind(increment, count));

  // The current function already holds the strand's lock, so the
  // previous call to dispatch should have successfully nested.
  BOOST_ASIO_CHECK(*count == original_count + 1);
}

void sleep_increment(io_context* ioc, int* count)
{
  timer t(*ioc, chronons::seconds(2));
  t.wait();

  ++(*count);
}

void increment_by_a(int* count, int a)
{
  (*count) += a;
}

void increment_by_a_b(int* count, int a, int b)
{
  (*count) += a + b;
}

void increment_by_a_b_c(int* count, int a, int b, int c)
{
  (*count) += a + b + c;
}

void increment_by_a_b_c_d(int* count, int a, int b, int c, int d)
{
  (*count) += a + b + c + d;
}

void start_sleep_increments(io_context* ioc, io_context::strand* s, int* count)
{
  // Give all threads a chance to start.
  timer t(*ioc, chronons::seconds(2));
  t.wait();

  // Start three increments.
  post(*s, bindns::bind(sleep_increment, ioc, count));
  post(*s, bindns::bind(sleep_increment, ioc, count));
  post(*s, bindns::bind(sleep_increment, ioc, count));
}

void throw_exception()
{
  throw 1;
}

void io_context_run(io_context* ioc)
{
  ioc->run();
}

void strand_test()
{
  io_context ioc;
  io_context::strand s(ioc);
  int count = 0;

  post(ioc, bindns::bind(increment_without_lock, &s, &count));

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);

  ioc.run();

  // The run() call will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 1);

  count = 0;
  ioc.restart();
  post(s, bindns::bind(increment_with_lock, &s, &count));

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);

  ioc.run();

  // The run() call will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 1);

  count = 0;
  ioc.restart();
  post(ioc, bindns::bind(start_sleep_increments, &ioc, &s, &count));
  boost::asio::detail::thread thread1(bindns::bind(io_context_run, &ioc));
  boost::asio::detail::thread thread2(bindns::bind(io_context_run, &ioc));

  // Check all events run one after another even though there are two threads.
  timer timer1(ioc, chronons::seconds(3));
  timer1.wait();
  BOOST_ASIO_CHECK(count == 0);
#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  timer1.expires_at(timer1.expires_at() + chronons::seconds(2));
#else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  timer1.expires_at(timer1.expiry() + chronons::seconds(2));
#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  timer1.wait();
  BOOST_ASIO_CHECK(count == 1);
#if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  timer1.expires_at(timer1.expires_at() + chronons::seconds(2));
#else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  timer1.expires_at(timer1.expiry() + chronons::seconds(2));
#endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
  timer1.wait();
  BOOST_ASIO_CHECK(count == 2);

  thread1.join();
  thread2.join();

  // The run() calls will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 3);

  count = 0;
  int exception_count = 0;
  ioc.restart();
  post(s, throw_exception);
  post(s, bindns::bind(increment, &count));
  post(s, bindns::bind(increment, &count));
  post(s, throw_exception);
  post(s, bindns::bind(increment, &count));

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);
  BOOST_ASIO_CHECK(exception_count == 0);

  for (;;)
  {
    try
    {
      ioc.run();
      break;
    }
    catch (int)
    {
      ++exception_count;
    }
  }

  // The run() calls will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 3);
  BOOST_ASIO_CHECK(exception_count == 2);

  count = 0;
  ioc.restart();

  // Check for clean shutdown when handlers posted through an orphaned strand
  // are abandoned.
  {
    io_context::strand s2(ioc);
    post(s2, bindns::bind(increment, &count));
    post(s2, bindns::bind(increment, &count));
    post(s2, bindns::bind(increment, &count));
  }

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);
}

void strand_wrap_test()
{
#if !defined(BOOST_ASIO_NO_DEPRECATED)
  io_context ioc;
  io_context::strand s(ioc);
  int count = 0;

  s.wrap(bindns::bind(increment, &count))();

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);

  ioc.restart();
  ioc.run();

  // The run() calls will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 1);

  count = 0;
  s.wrap(increment)(&count);

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);

  ioc.restart();
  ioc.run();

  // The run() calls will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 1);

  count = 0;
  s.wrap(increment_by_a)(&count, 1);

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);

  ioc.restart();
  ioc.run();

  // The run() calls will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 1);

  count = 0;
  s.wrap(increment_by_a_b)(&count, 1, 2);

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);

  ioc.restart();
  ioc.run();

  // The run() calls will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 3);

  count = 0;
  s.wrap(increment_by_a_b_c)(&count, 1, 2, 3);

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);

  ioc.restart();
  ioc.run();

  // The run() calls will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 6);

  count = 0;
  s.wrap(increment_by_a_b_c_d)(&count, 1, 2, 3, 4);

  // No handlers can be called until run() is called.
  BOOST_ASIO_CHECK(count == 0);

  ioc.restart();
  ioc.run();

  // The run() calls will not return until all work has finished.
  BOOST_ASIO_CHECK(count == 10);
#endif // !defined(BOOST_ASIO_NO_DEPRECATED)
}

BOOST_ASIO_TEST_SUITE
(
  "strand",
  BOOST_ASIO_TEST_CASE(strand_test)
  BOOST_ASIO_TEST_CASE(strand_wrap_test)
)

max_stars_repo_name: alexhenrie/poedit

max_issues_repo_name: alexhenrie/poedit

max_forks_repo_name: alexhenrie/poedit


text: #include <boost/spirit/repository/include/qi_primitive.hpp>

max_stars_repo_name: miathedev/BoostForArduino

max_issues_repo_name: miathedev/BoostForArduino

max_forks_repo_name: miathedev/BoostForArduino


text:
#include "i2cserver.hpp"

#include <boost/asio.hpp>
#include <iostream>
#include <memory>
#include <cstdlib>

using namespace boost;

// set up socket, currently one to one, should be one to many
I2CServer::I2CServer() :
    I2C("I2C"),
    m_acceptor(m_io_service, asio::ip::tcp::endpoint(asio::ip::tcp::v6(), 4444)),
    m_socket(m_io_service),
    m_stop(false)
    {

    m_thread.reset(new std::thread([this]() {
        for (;;) {
            if (!m_stop.load()) {
                std::cout << "WAIT FOR I2C CLIENT" << std::endl;
                m_acceptor.accept(m_socket);
                std::cout << "CONNECTED TO I2C CLIENT" << std::endl;
                m_stop.store(true);
            }
        }
    }));
}

I2CServer::~I2CServer() {
}

uint8_t I2CServer::process_i2c(uint8_t f_reg, uint8_t f_in) {

    uint8_t l_transmission[2] {f_reg, f_in};
    uint8_t l_return[1];
    system::error_code ec;

    // send signal using crsr
    if (f_reg == I2C::CRSR_REG_ADDR)
    {
        if (f_in & (1 << I2C_CMD_STA))
        {
            l_transmission[1] &= (1 << I2C_CMD_STA);
        }
        else if (f_in & (1 << I2C_CMD_ACK))
        {
            l_transmission[1] &= (1 << I2C_CMD_ACK);
        }
        else if (f_in & (1 << I2C_CMD_STO))
        {
            l_transmission[1] &= (1 << I2C_CMD_STO);
        }
    }

    // data value, init for case of no connection
    if (f_reg == I2C::TXRX_REG_ADDR)
    {
        l_return[0] = f_in;
    }
    else
    {
        l_return[0] = 0;
    }

    // no client, return
    if (!m_stop.load()) {
        return l_return[0];
    }

    // client, send transmisstion and receive result
    asio::write(m_socket, asio::buffer(l_transmission), ec);
    if (ec)
    {
        std::cout << ec.message() << std::endl;
        if ((asio::error::eof == ec) || (asio::error::connection_reset == ec))
        {
            m_socket.close();
            m_stop.store(false);
        }
    }
    asio::read(m_socket, asio::buffer(l_return, 1), ec);
    if (ec)
    {
        std::cout << ec.message() << std::endl;
        if ((asio::error::eof == ec) || (asio::error::connection_reset == ec))
        {
            m_socket.close();
            m_stop.store(false);
        }
    }

    // if nothing worked, nothing is happend.
    return l_return[0];
}


text:
#include "Client_UDP.h"

#include "engine.h"
#include <boost/bind.hpp>
#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>

#include "ClientUtils.h"
#include "EntityExplosiveBarrel.h"
#include "EntityClientCam.h"
#include "Util.h"

using boost::asio::ip::udp;

namespace
{

	ClientCommand defaultCommand = [](std::string) {};
}

Client_UDP::Client_UDP(Engine* engine_)
	:
	engine(engine_),
	recvbuffer(),
	socket_(io_service),
	sendClientUpdate(defaultCommand)
{

	sendClientUpdate = [&](std::string) {
		nlohmann::json j;
		j["type"] = "clientUpdate";

		nlohmann::json data;
		data["pos"] = Util::printVec3(engine->camera->pos);
		data["ang"] = Util::printVec3(engine->camera->lookVec);

		j["data"] = data;

		if (socket_.is_open())
		{
			socket_.send_to(boost::asio::buffer(j.dump()), remoteEndpoint);
		}
	};

	onCommands["welcome"] = [&](nlohmann::json msg) {
		std::cout << msg.dump(2) << std::endl;
	};

	onCommands["ping"] = [&](nlohmann::json msg) {
		//sendJson(msg);
	};

	onCommands["cping"] = [&](nlohmann::json msg) {
		//uint64_t ts = 0;
		//json_get_object(msg, "ts", ts)
		//{
		//	uint64_t ms = std::chrono::duration_cast<std::chrono::milliseconds>(
		//		std::chrono::system_clock::now().time_since_epoch()
		//		).count();
		//	std::stringstream ss;
		//	ss << (ms - ts) / 2 << "ms";
		//	*engine->console << "Ping returned in " << ss.str() << std::endl;
		//}
	};

	onCommands["entityUpdate"] = [&](nlohmann::json msg) {
		nlohmann::json j;

		//std::cout << msg.dump(2) << std::endl;

		json_get_object(msg, "data", j)
		{
			//engine->entityManger->clear();
			try
			{

				for (auto& i : j)
				{
					std::string type = i["type"];
					//if (i["active"] == 0)
					//	continue;

					//if (type == "EntityPhysicsProp")
					//{
					//	EntityPhysicsProp* e = (EntityPhysicsProp*)engine->entityManger->addEntity(
					//		new EntityPhysicsProp(i["modelName"], Util::vec3FromString(i["origin"]), i["mass"])
					//	);
					//	e->setTransform(Util::mat4FromString(i["transform"]));
					//}
					if (type == "EntityExplosiveBarrel")
					{
						int id = i["id"];

						engine->netEvents->pushInstruction(
							[=]() {

								Entity* foundEntity = engine->entityManger->getEntityByID(id);
								if (foundEntity == nullptr)
								{
									std::cout << "Creating new entity " << id << std::endl;
									EntityExplosiveBarrel* newEntity = (EntityExplosiveBarrel*)engine->entityManger->addEntity(
										new EntityExplosiveBarrel(Util::vec3FromString(i["origin"]))
									);
									if (newEntity != nullptr)
									{
										json_get_string(i, "transform", transform)
										{
											newEntity->setTransform(Util::mat4FromString(transform));
											newEntity->id = id;
										}
									}
								}
								else
								{
									std::cout << "Found entity " << id << std::endl;
									auto e = (EntityExplosiveBarrel*)foundEntity;
									if (e != nullptr && e->body != nullptr)
									{
										json_get_string(i, "transform", transform)
										{
											e->setTransform(Util::mat4FromString(transform));
										}

										json_get_string(i, "velocity", velocity)
										{
											glm::vec3 vec = Util::vec3FromString(velocity);
											btVector3 btVec = Util::vec3Conv(vec);
											e->body->setLinearVelocity(btVec);
										}

										json_get_string(i, "angularVelocity", angularVelocity)
										{
											e->body->setAngularVelocity(Util::vec3Conv(Util::vec3FromString(angularVelocity)));
										}

										int activeState = false;
										json_get_object(i, "active", activeState)
										{
											if (!activeState)
												e->remove();
											e->active = activeState;
										}

										e->body->activate();
									}
								}

								//std::cout << "Created Barrel" << std::endl;
							}
						);
					}
				}
			}
			catch (nlohmann::json::exception& e)
			{
				*engine->console << "message: " << e.what() << '\n'
					<< "exception id: " << e.id << std::endl;
			}
		}
	};

	onCommands["clients"] = [&](nlohmann::json msg) {
		nlohmann::json data;
		json_get_object(msg, "data", data)
		{
			for (auto& clientJson : data)
			{
				json_get_string(clientJson, "ip", username)
				{
					std::cout << username << std::endl;
					auto& clientEntityItor = clientEntities.find(username);
					if (clientEntityItor != clientEntities.end())
					{
						auto& client = clientEntities[username];

						if (client != nullptr)
						{
							json_get_string(clientJson, "pos", pos)
							{

								json_get_string(clientJson, "ang", ang)
								{
									engine->netEvents->pushInstruction(
										[=]() {
											if (client != nullptr)
											{
												client->pos = Util::vec3FromString(pos);
												client->lookVec = Util::vec3FromString(ang);
											}
										}
									);
								}
							}
						}
					}
					else
					{
						EntityClientCam* newClient = new EntityClientCam(glm::vec3(0));
						json_get_string(clientJson, "pos", pos)
						{
							json_get_string(clientJson, "ang", ang)
							{
								if (newClient != nullptr)
								{
									newClient->pos = Util::vec3FromString(pos);
									newClient->lookVec = Util::vec3FromString(ang);
								}
							}
						}
						clientEntities[username] = newClient;

						engine->netEvents->pushInstruction(
							[newClient, this]() {
								if (newClient != nullptr)
									this->engine->entityManger->addEntity(newClient);
							}
						);
					}
				}
			}
		}
	};

}

Client_UDP::~Client_UDP()
{
}

void Client_UDP::connect(std::string address)
{
	engine->entityManger->clear();
	clientEntities.clear();

	std::thread clientThread;

	lastServer = address;
	port = "32500";
	if (address == "")
	{
		lastServer = "81.147.31.211";
	}

	clientThread = std::thread([this]()
		{
			try
			{
				boost::asio::ip::udp::resolver resolver(io_service);
				boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), lastServer, port);
				remoteEndpoint = *resolver.resolve(query);

				std::cout << "Created endpoint" << std::endl;

				if (socket_.is_open())
				{
					std::cout << "Closing opened socket" << std::endl;
					io_service.stop();
					socket_.close();
				}

				socket_.open(boost::asio::ip::udp::v4());

				std::cout << "Opened new socket" << std::endl;

				//io_service->stop();

				std::cout << "Started io_service" << std::endl;

				socket_.send_to(boost::asio::buffer(nlohmann::json({ { "type", "test" }, { "data", "hello" } }).dump()), remoteEndpoint);
				//socket_.send_to(boost::asio::buffer("This is a second test message."), remoteEndpoint);

				std::cout << "Sent initial message" << std::endl;

				start_receive();

				std::cout << "After start_receive" << std::endl;

				io_service.run();

			}
			catch (std::exception& e)
			{
				std::cerr << "Socket error: " << e.what() << std::endl;
			}
		}
	);

	clientThread.detach();
}

void Client_UDP::start_receive()
{
	socket_.async_receive_from(
		boost::asio::buffer(recvbuffer), remoteEndpoint,
		boost::bind(&Client_UDP::handle_receive, this,
			boost::asio::placeholders::error,
			boost::asio::placeholders::bytes_transferred));
}

void Client_UDP::handle_receive(const boost::system::error_code& error, std::size_t bytes)
{
	if (!error || error == boost::asio::error::message_size)
	{
		//std::cout << "Data: " << recvbuffer.data() << std::endl;
		processMessage(recvbuffer.data());
		recvbuffer = boost::array<char, 65536>();
	}
	else// if (error != boost::asio::error::invalid_argument)
	{
		std::cout << "Receive error: " << error.message() << std::endl;
	}

	start_receive();
}

void Client_UDP::tick(float deltaTime)
{
	updateTimer += deltaTime;

	if (updateTimer > 1.f / engine->variables->getVarOrCreate("client_tickrate", "64.0", Variable::valFloat)->getFloat()) //64 ticks a second
	{
		updateTimer = 0;
		if (active)
		{
			sendClientUpdate("");
		}
	}
}

void Client_UDP::processMessage(std::string messageData)
{
	try
	{
		//const std::lock_guard<std::mutex> lock(engine->engineLock);

		nlohmann::json msg = nlohmann::json::parse(messageData);

		//std::cout << "NetworkClient::processMessage() - msg = " << msg.dump(2) << std::endl;

		int tick = 0;
		json_get_object(msg, "tick", tick)
		{
			std::cout << "tick: " << tick << std::endl;
		}

		json_get_string(msg, "type", type)
		{
			auto onCommand = onCommands.find(type);
			if (onCommand != onCommands.end())
			{
				//*engine->console << "[Received] " << type << std::endl;
				onCommands[type](msg);
			}
			else
			{
				*engine->console << "Invalid type recieved: " << type << std::endl;
			}
		}

	}
	catch (nlohmann::json::exception& e)
	{
		*engine->console << e.what() << std::endl;
		*engine->console << messageData << std::endl;
	}
}


text:
#include <toynet/examples/diff2/diff2.h>
#include <toynet/stlio.h>
#include <toynet/ublas/io.h>
#include <toynet/ublas/convert.h>
#include <iostream>
#include <sstream>
#include <boost/program_options.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using namespace boost::program_options;
using namespace toynet;

void print(const boost::property_tree::ptree& pt)
{
    boost::property_tree::ptree::const_iterator end = pt.end();
    for (boost::property_tree::ptree::const_iterator it = pt.begin(); it != end; ++it) {
        std::cout << it->first << ": " << it->second.get_value<std::string>() << std::endl;
        print(it->second);
    }
}

std::vector<diff2::Tensor1D> parse_json_examples(const std::string& s)
{
    std::vector<diff2::Tensor1D> ret;
    if (!s.empty()) {
        std::stringstream ss(s);
        boost::property_tree::ptree pt;
        boost::property_tree::read_json(ss, pt);
        for (const auto& e1 : pt) {
            diff2::Tensor1D v;
            for (const auto& e2 : e1.second) {
                v.resize(v.size() + 1);
                v[v.size() - 1] = e2.second.get_value<double>();
            }
            ret.push_back(v);
        }
    }
    return ret;
}

struct Options
{
    options_description desc;
    variables_map vm;
    bool help;
    int hidden;
    int width;
    int inputs;
    int outputs;
    int epochs;
    double lr;
    double alpha;
    std::string trainingx;
    std::string trainingy;
    std::string testing;
    bool progress;
    std::string loss;
    std::string optimizer;
    std::string initializer;
    double seed;

    Options(int argc, char* argv[])
        : desc("Allowed options")
        , help(false)
        , hidden(1)
        , width(2)
        , inputs(2)
        , outputs(1)
        , epochs(10)
        , lr(0.01)
        , alpha(0.5)
        , trainingx("[[4.0, 3.0]]")
        , trainingy("[[1.0]]")
        , testing("")
        , progress(true)
        , loss("mse")
        , optimizer("gradient")
        , initializer("fixed")
        , seed(0.0)
    {
        desc.add_options()
            // First parameter describes option name/short name
            // The second is parameter to option
            // The third is description
            ("help,h", bool_switch(&help), "print help message")
            ("hidden,H", value(&hidden), "number of hidden layers")
            ("width,W", value(&width), "width of hidden layers")
            ("inputs,I", value(&inputs), "width of input layer")
            ("outputs,O", value(&outputs), "width of output layer")
            ("epochs,e", value(&epochs), "number of training epochs")
            ("lr", value(&lr), "learning rate")
            ("alpha", value(&alpha), "momentum's alpha")
            ("trainingx", value(&trainingx), "training inputs")
            ("trainingy", value(&trainingy), "training outputs")
            ("testing", value(&testing), "testing X")
            ("progress", value(&progress), "show loss at each epoch")
            ("loss", value(&loss), "loss function")
            ("optimizer", value(&optimizer), "optimizer")
            ("initializer", value(&initializer), "weight initializer")
            ("seed", value(&seed), "rng seed")
            ;
        store(parse_command_line(argc, argv, desc), vm);
        notify(vm);
    }
};

std::unique_ptr<Loss> get_loss(const std::string& name)
{
    if (name == "mse")
        return std::make_unique<MSELoss>();
    if (name == "softmax")
        return std::make_unique<SoftmaxLoss>();
    throw std::runtime_error("unknown loss function: " + name);
}

std::unique_ptr<diff2::Optimizer> get_optimizer(const std::string& name, double lr, double alpha)
{
    if (name == "gradient")
        return std::make_unique<diff2::GradientOptimizer>(lr);
    if (name == "momentum")
        return std::make_unique<diff2::MomentumOptimizer>(lr, alpha);
    throw std::runtime_error("unknown optimizer: " + name);
}

std::unique_ptr<diff2::WeightInitializer> get_initializer(const std::string& name, double seed)
{
    if (name == "fixed")
        return std::make_unique<diff2::FixedWeightInitializer>();
    if (name == "glorot")
        return std::make_unique<diff2::GlorotBengio2010Initializer>(seed);
    throw std::runtime_error("unknown initializer: " + name);
}

int main(int argc, char* argv[])
{
    Options opts(argc, argv);

    if (opts.help) {
        std::cout << opts.desc << std::endl;
        return 0;
    }

    const std::vector<diff2::Tensor1D> training_X = parse_json_examples(opts.trainingx);
    const std::vector<diff2::Tensor1D> training_Y = parse_json_examples(opts.trainingy);
    const std::vector<diff2::Tensor1D> testing = parse_json_examples(opts.testing);

    // Make sure the dimensions of each training example's X is equal to opts.inputs
    for (const auto&v : training_X) {
        if (v.size() != opts.inputs) {
            std::cerr << "Size mismatch: the network has input size "
                      << opts.inputs << " but a training input has size "
                      << v.size() << ": " << v << std::endl;
            return 1;
        }
    }

    // Make sure the dimensions of each training example's Y is equal to opts.outputs
    for (const auto&v : training_Y) {
        if (v.size() != opts.outputs) {
            std::cerr << "Size mismatch: the network has output size "
                      << opts.inputs << " but a training output has size "
                      << v.size() << ": " << v << std::endl;
            return 1;
        }
    }

    // Make sure the dimensions of each testing example is equal to opts.inputs
    for (const auto&v : testing) {
        if (v.size() != opts.inputs) {
            std::cerr << "Size mismatch: the network has input size "
                      << opts.inputs << " but a testing input has size "
                      << v.size() << ": " << v << std::endl;
            return 1;
        }
    }

    std::unique_ptr<diff2::WeightInitializer> initializer = get_initializer(opts.initializer, opts.seed);
    std::unique_ptr<Loss> loss = get_loss(opts.loss);
    std::unique_ptr<diff2::Optimizer> opt = get_optimizer(opts.optimizer, opts.lr, opts.alpha);
    diff2::Network network(opts.hidden, opts.width, opts.inputs, opts.outputs, initializer.get());
    diff2::Trainer trainer(network, *loss, *opt);
    for (int e = 1;  e <= opts.epochs;  ++e) {
        trainer.train(e, training_X, training_Y);
        if (opts.progress)
            std::cout << e << " " << trainer.workspace.loss << std::endl;
    }
    std::cout << "loss " << trainer.workspace.loss << " weights " << network.W << std::endl;

    for (const auto&v : testing)
        std::cout << v << " -> " << network.predict(v) << std::endl;

    return 0;
}

max_forks_repo_name: pbrunelle/toynet


text:
/*    Copyright (c) 2010-2019, Delft University of Technology
 *    All rigths reserved
 *
 *    This file is part of the Tudat. Redistribution and use in source and
 *    binary forms, with or without modification, are permitted exclusively
 *    under the terms of the Modified BSD license. You should have received
 *    a copy of the license with this file. If not, please or visit:
 *    http://tudat.tudelft.nl/LICENSE.
 *
 *    References
 *      Easy calculation. Newton's Law of Gravity Tutorial,
 *          http://easycalculation.com/physics/classical-physics/learn-newtons-law.php, last
 *          accessed: 12th February, 2012.
 *
 */

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN

#include <limits>

#include <boost/test/tools/floating_point_comparison.hpp>
#include <boost/test/unit_test.hpp>

#include <Eigen/Core>

#include "tudat/astro/gravitation/centralGravityModel.h"

namespace tudat
{
namespace unit_tests
{

BOOST_AUTO_TEST_SUITE( test_gravitational_force )

//! Test if gravitational force is computed correctly.
BOOST_AUTO_TEST_CASE( testGravitationalForce )
{
    // Case 1: Compute gravitational force exerted on boy, due to Earth (Easy calculation, 2012).
    {
        // Set gravitational parameter of Earth [m^3 s^-2].
        double gravitationalParameterOfEarth = 6.6726e-11 * 5.98e24;

        // Set position vector of Earth [m].
        Eigen::Vector3d positionOfEarth = Eigen::Vector3d::Zero( );

        // Set mass of boy [kg]
        double massOfBoy = 70.0;

        // Set position vector of boy [m].
        Eigen::Vector3d positionOfBoy( 6.38e6, 0.0, 0.0 );

        // Compute gravitational force acting on boy [N].
        Eigen::Vector3d gravitationalForceExertedOnBoy
                = gravitation::computeGravitationalForce(
                    massOfBoy, positionOfBoy,
                    gravitationalParameterOfEarth, positionOfEarth );

        // Check if computed gravitational force matches expected value.
        BOOST_CHECK_CLOSE_FRACTION( 685.54, gravitationalForceExertedOnBoy.norm( ), 1.0e-3 );
    }

    // Case 2: Compute gravitational force exerted on arbitrary body1, due to arbitrary body2
    //         (Easy calculation, 2012).
    {
        // Set universal gravitational constant [m^3 kg^-1 s^-2].
        double universalGravitationalConstant = 6.6726e-11;

        // Set mass of body1 [kg].
        double massOfBody1 = 1.0e4;

        // Set position vector of body1 [m].
        Eigen::Vector3d positionOfBody1( 0.0, 5.0, 0.0 );

        // Set mass of body2 [kg].
        double massOfBody2 = 2.0e4;

        // Set position vector of body2 [m].
        Eigen::Vector3d positionOfBody2( 0.0, -5.0, 0.0 );

        // Compute gravitational force acting on body1 [N].
        Eigen::Vector3d gravitationalForceExertedOnBody1
                = gravitation::computeGravitationalForce(
                    universalGravitationalConstant, massOfBody1,
                    positionOfBody1, massOfBody2, positionOfBody2 );

        // Check if computed gravitational force matches expected value.
        BOOST_CHECK_CLOSE_FRACTION( 13345200.0e-11, gravitationalForceExertedOnBody1.norm( ),
                                    std::numeric_limits< double >::epsilon( ) );
    }
}

BOOST_AUTO_TEST_SUITE_END( )

} // namespace unit_tests
} // namespace tudat


text:
#pragma once

#include <boost/config.hpp>

#ifdef BOOST_WINDOWS

#include <malloc.h>
#define XUL_ALLOCA(t, n) static_cast<t*>(_alloca(sizeof(t) * (n)))

#else

#ifdef __FreeBSD__
#include <stdlib.h>
#else
#include <alloca.h>
#endif

#define XUL_ALLOCA(t, n) static_cast<t*>(alloca(sizeof(t) * (n)))

#endif

max_stars_repo_name: hindsights/xul

max_issues_repo_name: hindsights/xul

max_forks_repo_name: hindsights/xul


text:
#ifndef Z_PLANE_HPP
#define Z_PLANE_HPP

//STL
#include <vector>

//Eigen
#include <Eigen/Dense>

struct Plane
{
    Eigen::Vector3f n;
    float d;

    Plane()
        : n(Eigen::Vector3f::Zero()), d(0.f)
    {   }

    Plane(const Eigen::Vector3f &n, float d)
        : n(n), d(d)
    {    }
};

typedef std::vector<Plane> PlanesList;

#endif