Commit 33b0830f authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add initial parsing of user settings

parent 3eb7b32a
Loading
Loading
Loading
Loading
+9 −6
Original line number Diff line number Diff line
@@ -30,8 +30,10 @@ DIST_SUBDIRS = lib examples
bin_PROGRAMS = src/urd

src_urd_SOURCES = 			\
	src/main.cpp			\
	src/ipc-listener.hpp	\
	src/main.cpp			\
	src/settings.cpp		\
	src/settings.hpp		\
	src/urd.cpp				\
	src/urd.hpp

@@ -50,6 +52,7 @@ src_urd_LDFLAGS = \
	@BOOST_LDFLAGS@				\
    @BOOST_SYSTEM_LIB@			\
    @BOOST_ASIO_LIB@			\
    @BOOST_PROGRAM_OPTIONS_LIB@	\
	-pthread

src_urd_LDADD = 	\
+1 −0
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ AX_BOOST_BASE([1.53],, [
AX_BOOST_SYSTEM
AX_BOOST_FILESYSTEM
AX_BOOST_ASIO
AX_BOOST_PROGRAM_OPTIONS


# check that Intel's TBB
+108 −0
Original line number Diff line number Diff line
# =============================================================================
#  https://www.gnu.org/software/autoconf-archive/ax_boost_program_options.html
# =============================================================================
#
# SYNOPSIS
#
#   AX_BOOST_PROGRAM_OPTIONS
#
# DESCRIPTION
#
#   Test for program options library from the Boost C++ libraries. The macro
#   requires a preceding call to AX_BOOST_BASE. Further documentation is
#   available at <http://randspringer.de/boost/index.html>.
#
#   This macro calls:
#
#     AC_SUBST(BOOST_PROGRAM_OPTIONS_LIB)
#
#   And sets:
#
#     HAVE_BOOST_PROGRAM_OPTIONS
#
# LICENSE
#
#   Copyright (c) 2009 Thomas Porschberg <thomas@randspringer.de>
#
#   Copying and distribution of this file, with or without modification, are
#   permitted in any medium without royalty provided the copyright notice
#   and this notice are preserved. This file is offered as-is, without any
#   warranty.

#serial 25

AC_DEFUN([AX_BOOST_PROGRAM_OPTIONS],
[
    AC_ARG_WITH([boost-program-options],
        AS_HELP_STRING([--with-boost-program-options@<:@=special-lib@:>@],
                       [use the program options library from boost - it is possible to specify a certain library for the linker
                        e.g. --with-boost-program-options=boost_program_options-gcc-mt-1_33_1 ]),
        [
        if test "$withval" = "no"; then
            want_boost="no"
        elif test "$withval" = "yes"; then
            want_boost="yes"
            ax_boost_user_program_options_lib=""
        else
            want_boost="yes"
        ax_boost_user_program_options_lib="$withval"
        fi
        ],
        [want_boost="yes"]
    )

    if test "x$want_boost" = "xyes"; then
        AC_REQUIRE([AC_PROG_CC])
        export want_boost
        CPPFLAGS_SAVED="$CPPFLAGS"
        CPPFLAGS="$CPPFLAGS $BOOST_CPPFLAGS"
        export CPPFLAGS
        LDFLAGS_SAVED="$LDFLAGS"
        LDFLAGS="$LDFLAGS $BOOST_LDFLAGS"
        export LDFLAGS
        AC_CACHE_CHECK([whether the Boost::Program_Options library is available],
                       ax_cv_boost_program_options,
                       [AC_LANG_PUSH(C++)
                AC_COMPILE_IFELSE([AC_LANG_PROGRAM([[@%:@include <boost/program_options/errors.hpp>
                                                          ]],
                                  [[boost::program_options::error err("Error message");
                                   return 0;]])],
                           ax_cv_boost_program_options=yes, ax_cv_boost_program_options=no)
                    AC_LANG_POP([C++])
        ])
        if test "$ax_cv_boost_program_options" = yes; then
                AC_DEFINE(HAVE_BOOST_PROGRAM_OPTIONS,,[define if the Boost::PROGRAM_OPTIONS library is available])
                  BOOSTLIBDIR=`echo $BOOST_LDFLAGS | sed -e 's/@<:@^\/@:>@*//'`
                if test "x$ax_boost_user_program_options_lib" = "x"; then
                for libextension in `ls $BOOSTLIBDIR/libboost_program_options*.so* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_program_options.*\)\.so.*$;\1;'` `ls $BOOSTLIBDIR/libboost_program_options*.dylib* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_program_options.*\)\.dylib.*$;\1;'` `ls $BOOSTLIBDIR/libboost_program_options*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^lib\(boost_program_options.*\)\.a.*$;\1;'` ; do
                     ax_lib=${libextension}
                    AC_CHECK_LIB($ax_lib, exit,
                                 [BOOST_PROGRAM_OPTIONS_LIB="-l$ax_lib"; AC_SUBST(BOOST_PROGRAM_OPTIONS_LIB) link_program_options="yes"; break],
                                 [link_program_options="no"])
                done
                if test "x$link_program_options" != "xyes"; then
                for libextension in `ls $BOOSTLIBDIR/boost_program_options*.dll* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^\(boost_program_options.*\)\.dll.*$;\1;'` `ls $BOOSTLIBDIR/boost_program_options*.a* 2>/dev/null | sed 's,.*/,,' | sed -e 's;^\(boost_program_options.*\)\.a.*$;\1;'` ; do
                     ax_lib=${libextension}
                    AC_CHECK_LIB($ax_lib, exit,
                                 [BOOST_PROGRAM_OPTIONS_LIB="-l$ax_lib"; AC_SUBST(BOOST_PROGRAM_OPTIONS_LIB) link_program_options="yes"; break],
                                 [link_program_options="no"])
                done
                fi
                else
                  for ax_lib in $ax_boost_user_program_options_lib boost_program_options-$ax_boost_user_program_options_lib; do
                      AC_CHECK_LIB($ax_lib, main,
                                   [BOOST_PROGRAM_OPTIONS_LIB="-l$ax_lib"; AC_SUBST(BOOST_PROGRAM_OPTIONS_LIB) link_program_options="yes"; break],
                                   [link_program_options="no"])
                  done
                fi
            if test "x$ax_lib" = "x"; then
                AC_MSG_ERROR(Could not find a version of the library!)
            fi
                if test "x$link_program_options" != "xyes"; then
                    AC_MSG_ERROR([Could not link against [$ax_lib] !])
                fi
        fi
        CPPFLAGS="$CPPFLAGS_SAVED"
    LDFLAGS="$LDFLAGS_SAVED"
    fi
])
+65 −0
Original line number Diff line number Diff line
//
// Copyright (C) 2017 Barcelona Supercomputing Center
//                    Centro Nacional de Supercomputacion
//
// This file is part of the Data Scheduler, a daemon for tracking and managing
// requests for asynchronous data transfer in a hierarchical storage environment.
//
// See AUTHORS file in the top level directory for information
// regarding developers and contributors.
//
// The Data Scheduler 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 3 of the License, or
// (at your option) any later version.
//
// Data Scheduler 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 Data Scheduler.  If not, see <http://www.gnu.org/licenses/>.
//

#include <unistd.h>
#include <boost/program_options.hpp>

#include "settings.hpp"
#include "urd.hpp"

namespace bpo = boost::program_options;

int main(int argc, char* argv[]){
    
    bool run_in_foreground = true;

    // declare a group of options that will be allowed only on the command line
    bpo::options_description generic("Allowed options");
    generic.add_options()
        (",f", bpo::bool_switch(&run_in_foreground), "foreground operation") // check how to do flags
        ("version,v",                                "print version string")
        ("help,h",                                   "produce help message")
    ;

    // declare a group of options that will be allowed in a config file
    bpo::variables_map vm;
    bpo::store(bpo::parse_command_line(argc, argv, generic), vm);
    bpo::notify(vm);    

    if (vm.count("help")) {
        std::cout << generic << "\n";
        exit(EXIT_SUCCESS);
    }

    config_settings settings;

    settings.load("norns.json");

//    if (vm.count("f")) {
//        std::cout << "Compression level was set to " << vm["foreground"].as<bool>() << ".\n";
        std::cout << "Compression level was set to " << run_in_foreground << ".\n";
//    } else {
//        std::cout << "Compression level was not set.\n";
//    }

    exit(0);



    ::unlink(urd::SOCKET_FILE);

    urd().run();

src/settings.cpp

0 → 100644
+161 −0
Original line number Diff line number Diff line
//
// Copyright (C) 2017 Barcelona Supercomputing Center
//                    Centro Nacional de Supercomputacion
//
// This file is part of the Data Scheduler, a daemon for tracking and managing
// requests for asynchronous data transfer in a hierarchical storage environment.
//
// See AUTHORS file in the top level directory for information
// regarding developers and contributors.
//
// The Data Scheduler 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 3 of the License, or
// (at your option) any later version.
//
// Data Scheduler 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 Data Scheduler.  If not, see <http://www.gnu.org/licenses/>.
//

#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include "settings.hpp"

static uint64_t parse_size(const std::string& str);

void config_settings::load(const std::string& filename) {

    bpt::ptree pt;

    bpt::read_json(filename, pt);

    m_storage_path = pt.get<std::string>("settings.storage.path", "");

    if(m_storage_path == "") {
        std::cerr << "Missing storage.path setting in configuration file";
        exit(EXIT_FAILURE);
    }

    auto capacity_str = pt.get<std::string>("settings.storage.capacity", "");

    if(capacity_str == "") {
        std::cerr << "Missing storage.capacity setting in configuration file";
        exit(EXIT_FAILURE);
    }

    try {
        m_storage_capacity = parse_size(capacity_str);
    } catch(std::invalid_argument ex) {
        std::cerr << "Unable to parse storage.capacity parameter '" << capacity_str << "': " << ex.what() << "\n";
        exit(EXIT_FAILURE);
    }

    /* parse storage backends */
    BOOST_FOREACH(bpt::ptree::value_type& bend, pt.get_child("backends")){
        // bend.first == ""
        const auto& name = bend.second.get<std::string>("name", "");

        if(name == ""){
            std::cerr << "Missing 'name' in backend description";
            exit(EXIT_FAILURE);
        }

        const auto& path = bend.second.get<std::string>("path", "");

        if(path == ""){
            std::cerr << "Missing 'path' in backend description";
            exit(EXIT_FAILURE);
        }

        const auto& capacity_str = bend.second.get<std::string>("capacity", "");

        if(capacity_str == ""){
            std::cerr << "Missing 'capacity' in backend description";
            exit(EXIT_FAILURE);
        }

        uint64_t capacity;

        try {
            capacity = parse_size(capacity_str);
        } catch(std::invalid_argument ex) {
            std::cerr << "Error parsing backend 'capacity' parameter: '" << capacity_str << "': " << ex.what() << "\n";
            exit(EXIT_FAILURE);
        }

        m_backends.emplace_back(name, path, capacity);
    }
}

uint64_t parse_size(const std::string& str){

    const uint64_t B_FACTOR = 1;
    const uint64_t KB_FACTOR = 1e3;
    const uint64_t KiB_FACTOR = (1 << 10);
    const uint64_t MB_FACTOR = 1e6;
    const uint64_t MiB_FACTOR = (1 << 20);
    const uint64_t GB_FACTOR = 1e9;
    const uint64_t GiB_FACTOR = (1 << 30);

    std::pair<std::string, uint64_t> conversions[] = {
        {"GiB", GiB_FACTOR},
        {"GB",  GB_FACTOR},
        {"G",   GB_FACTOR},
        {"MiB", MiB_FACTOR},
        {"MB",  MB_FACTOR},
        {"M",   MB_FACTOR},
        {"KiB", KiB_FACTOR},
        {"KB",  KB_FACTOR},
        {"K",   KB_FACTOR},
        {"B",   B_FACTOR},
    };

    std::string scopy(str);

    /* remove whitespaces from the string */
    scopy.erase(std::remove_if(scopy.begin(), scopy.end(), 
                                [](char ch){
                                    return std::isspace<char>(ch, std::locale::classic()); 
                                }), 
                scopy.end() );

    /* determine the units */
    std::size_t pos = std::string::npos;
    uint64_t factor = B_FACTOR;

    for(const auto& c: conversions){
        const std::string& suffix = c.first;

        if((pos = scopy.find(suffix)) != std::string::npos){
            /* check that the candidate is using the suffix EXACTLY 
             * to prevent accepting something like "GBfoo" as a valid Gigabyte unit */
            if(suffix != scopy.substr(pos)){
                pos = std::string::npos;
                continue;
            }

            factor = c.second;
            break;
        }
    }

    /* this works as expected because if pos == std::string::npos, the standard
     * states that all characters until the end of the string should be included.*/
    std::string number_str = scopy.substr(0, pos);

    /* check if it's a valid number */
    if(number_str == "" || !std::all_of(number_str.begin(), number_str.end(), ::isdigit)){
        throw std::invalid_argument("Not a number");
    }

    double value = std::stod(number_str);

    return std::round(value*factor);

}
Loading