text:
#pragma once

#include "feature_set.h"
#include "training_data.h"
#include "mldb/utils/distribution.h"
#include "mldb/arch/demangle.h"
#include <map>
#include "mldb/utils/any.h"
#include <boost/multi_array.hpp>
#include <string>
#include "mldb/utils/pure_bool.h"
#include "mldb/plugins/jml/jml/label.h"

namespace ML {

extern const distribution<float> UNIFORM_WEIGHTS;

/*****************************************************************************/
/* OUTPUT_ENCODING                                                           */
/*****************************************************************************/

/** This enumeration gives the output encoding for a classifier.   There are
    currently three different possibilities.
*/

enum Output_Encoding {
    OE_PROB,     ///< Output encoding is a probability between 0 and 1
    OE_PM_INF,   ///< Output encoding is a value between -inf and inf
    OE_PM_ONE    ///< Output encoding is a value between -1 and 1
};

BYTE_PERSISTENT_ENUM_DECL(Output_Encoding);

std::string print(Output_Encoding encoding);

inline std::ostream & operator << (std::ostream & stream, Output_Encoding e)
{
    return stream << print(e);
}

typedef distribution<float> Label_Dist;
//typedef distribution<float, compact_vector<float, 3> > Label_Dist;

/*****************************************************************************/
/* OPTIMIZATION_INFO                                                         */
/*****************************************************************************/

/** A structure that provides information on optimization to a classifier. */

struct Optimization_Info {
    Optimization_Info() : initialized(false) {}

    std::vector<Feature> from_features;
    std::vector<Feature> to_features;
    std::vector<int> indexes;
    bool initialized;

    std::map<Feature, int> feature_to_optimized_index;

    int features_in() const
    {
        if (!initialized)
            throw Exception("using uninitialized feature map");
        return from_features.size();
    }

    int features_out() const
    {
        if (!initialized)
            throw Exception("using uninitialized feature map");
        return to_features.size();
    }

    void apply(const Feature_Set & fset, float * output) const;
    void apply(const std::vector<float> & fset, float * output) const;
    void apply(const float * fset, float * output) const;

    /** Given a feature, return the index in the dense feature vector that
        it corresponds to.  If there is none, an exception will be thrown. */
    int get_optimized_index(const Feature & feature) const;

    MLDB_IMPLEMENT_OPERATOR_BOOL(initialized);
};

/*****************************************************************************/
/* EXPLANATION                                                               */
/*****************************************************************************/

/** This structure is used to accumulate the effect of various features on
    the prediction. */

struct Explanation {
    Explanation()
    {
    }

    Explanation(std::shared_ptr<const Feature_Space> fspace,
                double weight);

    /** Add the weights from another explanation. */
    void add(const Explanation & other, double weight = 1.0);

    /** Explain how a single prediction was made. */
    std::string explain(int nfeatures,
                        const Feature_Set & fset,
                        int = 0/* ununsed */) const;

    /** Explain how the whole set of predictions were made. */
    std::string explain(int nfeatures = -1) const;

    /** Raw explain; the list is a ranked list of (feature, type) pairs.
        MISSING_FEATURE deals with the bias.
    */
    std::vector<std::pair<Feature, float> >
    explainRaw(int nfeatures = -1, bool includeBias = true) const;

    /** Divide all of the feature weights by the weight so that the effect
        of feature set size is removed.
    */
    void normalize();

    double bias;
    double weight;

    typedef std::map<Feature, double> Feature_Weights;
    Feature_Weights feature_weights;
    std::shared_ptr<const Feature_Space> fspace;
};

/*****************************************************************************/
/* PREDICTION CONTEXT                                                        */
/*****************************************************************************/

/** This structure provides context for a prediction.  It's designed to
    provide extra information that a classifier may need that doesn't neatly
    fit within its arguments.
*/

struct PredictionContext {
    std::any seed;  ///< Seed data for the prediction
    std::map<std::string, std::any> entries;
};

/*****************************************************************************/
/* CLASSIFIER_IMPL                                                           */
/*****************************************************************************/

/** The base class upon which classifiers are built.

    This class provides the base framework upon which all of the classifier
    classes are built.  It is designed to be general enough to allow for most
    operations, including boosting, to be performed without requiring that the
    exact underlying class be known.  It also provides the framework necessary
    for persistence.

    Note that user code will often use the Classifier class (which is always
    concrete) than the Classifier_Impl.  It is mainly implementors of
    classifiers, or algorithms which need special treatment, that will
    concern themselves with this class.

    Note that the word "label" is used where the word "class" would normally
    be used in the literature to describe the range of the classifier (the
    discreet outputs).  This is due to a name clash with the C++ reserved
    word "class".
*/

class Classifier_Impl {
public:
    /** Standard virtual destructor.  Does nothing as all variables are smart
        pointers. */
    virtual ~Classifier_Impl();

    /** \name Domain and range
        These functions allow the domain and range of the classifier to be
        queried.  They describe the arity and interpretation of its input
        and output spaces.
        @{
    */

    /** Returns the number of classes that this classifier is trying to
        classify into.  Non-virtual: simply reads the protected variable
        label_count_.

        \returns                  the number of labels in the range of the
                                  classifier.
    */
    size_t label_count() const { return label_count_; }
    ///@}

    /** Returns the feature space being used.  This describes the range of
        the classifier, and also the interpretation of the features in its
        input.

        \returns                  a shared pointer to the feature space
                                  being used.

        Note that this method is non-virtual and simply returns the value
        of the protected variables feature_space_.  Thus, it cannot be
        overridden to change the behaviour; the correct feature space will
        have to be passed in at construction.
    */
    std::shared_ptr<const Feature_Space> feature_space() const
    {
        return feature_space_;
    }

    /** Returns the feature space, dynamic cast as specified. */
    template<class Target_FS>
    std::shared_ptr<const Target_FS> feature_space() const
    {
        std::shared_ptr<const Target_FS> result
            = std::dynamic_pointer_cast<const Target_FS>(feature_space());
        if (!result) {
            auto fs = feature_space();
            auto & fsr = *fs;
            throw Exception("Couldn't cast feature space of type "
                            + MLDB::demangle(typeid(fsr).name())
                            + " to "
                            + MLDB::demangle(typeid(Target_FS).name()));
        }
        return result;
    }

    /** Change the feature space.  This method should be used with caution,
        as it is quite possible that there will have been initialisation done
        assuming the old feature space.

        \param new_feature_space  the new feature space to use
    */
    void set_feature_space(const std::shared_ptr<const Feature_Space> &
                               new_feature_space)
    {
        feature_space_ = new_feature_space;
    }
    ///@}

    /** \name Prediction
        These methods are used when applying an already learned classifier.
        They attempt to make a classification based upon a feature set.
        @{
    */

    /** Predict the score for a single label.
        \param label              label to predict.
        \param features           features to use to make the prediction.
        \returns                  the prediction for the given label, based
                                  upon the features given.

        \pre label >= 0 && label < label_count()
        \post result == predict(features).at(label)

        Throws an out of range exception if label was not in range, or
        an Exception if there is a problem in performing the
        classification.

        The default implementation is the following:
        \code
        float Classifier_Impl::
        predict(int label, const Feature_Set & features) const
        {
            return predict(features).at(label);
        }
        \endcode

        The only reason that this method is provided is that it may be
        quicker (more efficient) to calculate a score for one feature only
        rather than all at once.
     */
    virtual float predict(int label, const Feature_Set & features,
                          PredictionContext * context = 0) const;

    /** Predict the highest class.  This method returns the label which has
        the highest class.  Ties may be handled in any way.  Normally, it
        is better to use the predict method than this one.

        \param features           the features to use to make the prediction.
        \returns                  an integer 0 <= result < label_count() with
                                  the number of the highest label.

        The default implementation simply finds the maximum value in the
        output of \c predict(features), and returns the index of the first
        one which is equal to this value.

        Throws an exception if label_count() == 0, or if there was a problem
        performing the classification.
    */
    virtual int predict_highest(const Feature_Set & features,
                                PredictionContext * context = 0) const;

    /** Predict the score for all labels.  This method needs to be overridden
        at the very minimum.

        \param features           the features used to make the prediction.
        \returns                  a distribution over all labels of the
                                  classifier output.
        \post                     result.size() == label_count()

        If there is an error in classification, the implementation should throw
        an exception.
     */
    virtual Label_Dist
    predict(const Feature_Set & features,
            PredictionContext * context = 0) const = 0;

    /** Optimize the classifier to be called optimally with the given list of
        features.  The Optimization_Info structure can then be passed to the
        optimized predict method.  Note that the feature vector passed to the
        optimized predict method MUST have EXACTLY the features indicated.
    */
    virtual Optimization_Info optimize(const std::vector<Feature> & features);
    virtual Optimization_Info optimize(const Feature_Set & features);

    /** Is optimization supported by the classifier? */
    virtual bool optimization_supported() const;

    /** Is predict optimized?  Default returns false; those classifiers which
        a) support optimized predict and b) have had optimize_predict() called
        will override to return true in this case.
    */
    virtual bool predict_is_optimized() const;

    /** Methods to call for the optimized predict.  Will check if
        predict_is_optimized() and if true, will call the optimized methods.
        Otherwise, they fall back to the non-optimized versions. */
    virtual Label_Dist predict(const Feature_Set & features,
                               const Optimization_Info & info,
                               PredictionContext * context = 0) const;
    virtual Label_Dist predict(const std::vector<float> & features,
                               const Optimization_Info & info,
                               PredictionContext * context = 0) const;
    virtual Label_Dist predict(const float * features,
                               const Optimization_Info & info,
                               PredictionContext * context = 0) const;

    virtual float predict(int label,
                          const Feature_Set & features,
                          const Optimization_Info & info,
                          PredictionContext * context = 0) const;
    virtual float predict(int label,
                          const std::vector<float> & features,
                          const Optimization_Info & info,
                          PredictionContext * context = 0) const;
    virtual float predict(int label,
                          const float * features,
                          const Optimization_Info & info,
                          PredictionContext * context = 0) const;

    //protected:

    /** Function to override to perform the optimization.  Default will
        simply modify the optimization info to indicate that optimization
        had failed.
    */
    virtual bool
    optimize_impl(Optimization_Info & info);

    /** Optimized predict for a dense feature vector.
        This is the worker function that all classifiers that implement the
        optimized predict should override.  The default implementation will
        convert to a Feature_Set and will call the non-optimized predict.
    */
    virtual Label_Dist
    optimized_predict_impl(const float * features,
                           const Optimization_Info & info,
                           PredictionContext * context = 0) const;

    virtual void
    optimized_predict_impl(const float * features,
                           const Optimization_Info & info,
                           double * accum,
                           double weight = 1.0,
                           PredictionContext * context = 0) const;

    virtual float
    optimized_predict_impl(int label,
                           const float * features,
                           const Optimization_Info & info,
                           PredictionContext * context = 0) const;

public:
    /** Run the classifier over the entire dataset, calling the predict
        function on each of them.  The output function will be called once
        for each example number (from 0 to data.example_count() - 1) pointing
        to a vector of floats giving the number of labels.
    */
    typedef std::function<void (int example_num, const float * predictions)>
        Predict_All_Output_Func;
    virtual void predict(const Training_Data & data,
                         Predict_All_Output_Func output,
                         const Optimization_Info * opt_info = 0) const;

    /** Run the classifier over the entire dataset, calling the predict
        function on each of them, to predict a single label.  The output
        function will be called once for each example number (from 0 to
        data.example_count() - 1) with the value of the prediction.
    */
    typedef std::function<void (int example_num, float prediction)>
        Predict_One_Output_Func;
    virtual void predict(const Training_Data & data,
                         int label,
                         Predict_One_Output_Func output,
                         const Optimization_Info * opt_info = 0) const;

    ///@}

    /** Perform a prediction and explain why the prediction was done in the
        given way.  If not implemented, an exception will be thrown.
    */
    virtual Explanation explain(const Feature_Set & feature_set,
                                const ML::Label & label,
                                double weight = 1.0,
                                PredictionContext * context = 0) const;

    virtual Explanation explainContext(const Feature_Set & feature_set,
                                       const ML::Label & label,
                                       const PredictionContext & context,
                                       double weight = 1.0) const
    {
        return explain(feature_set, label, weight);
    }

    /** \name Accuracy
        These methods are all ways of returning the accuracy of the classifier
        over a set of training data.  They vary in how the output of the
        classifier is represented.
     */

    /** Calculate the prediction accuracy over a training set, using the
        predict() to determine the predictions.

        \param data               the training data used to calculate the
                                  accuracy over.
        \param example_weights    a weighting of the examples.  The examples
                                  will count as if they were duplicated this
                                  number of times.  An empty distribution is
                                  the same as all ones.
        \returns                  first: a number between 0 and 1, giving the
                                  proportion of the examples in data that were
                                  correct (for categorical classifiers), or the
                                  RMSE (for regression).
                                  second: the weighted average margin (for
                                  classification) or the RMSE (for regression).

        Note that this method cannot be overridden.
    */
    virtual std::pair<float, float>
    accuracy(const Training_Data & data,
             const distribution<float> & example_weights
                 = UNIFORM_WEIGHTS,
             const Optimization_Info * opt_info = 0) const;

    ///@}

    /** Provides a list of all features used by this classifier. */
    virtual std::vector<Feature> all_features() const = 0;

    /** Dump a representation of the classifier to a string. */
    virtual std::string print() const = 0;

    /** Dump a short compact representation.  Default is just the class
        name. */
    virtual std::string summary() const;

    /** \name Persistence
        These functions are concerned with allowing serialization and
        reconstitution without needing to know the details of what is the
        exact type of classifier that is being used.
        @{
    */

    /** Perform polymorphic reconstitution.  Can't be done with the normal
        reconstitution operator since the feature space needs to be passed
        in to it.
    */
    static std::shared_ptr<Classifier_Impl>
    poly_reconstitute(DB::Store_Reader & store,
                      const std::shared_ptr<const Feature_Space> & features);

    /** Perform polymorphic reconstitution, including the feature space. */
    static std::shared_ptr<Classifier_Impl>
    poly_reconstitute(DB::Store_Reader & store);

    /** Perform polymorphic serialization.  This is different to the virtual
        methods that are overridden elsewhere, as this one also writes the
        class metadata to the stream.
    */
    void poly_serialize(DB::Store_Writer & store, bool write_fs = true) const;

    /** Return the ID of this class.  This needs to be unique for each
        class in order to allow the persistence framework to function.
    */
    virtual std::string class_id() const = 0;

    /** Serialization and reconstitution. */
    virtual void serialize(DB::Store_Writer & store) const = 0;
    virtual void reconstitute(DB::Store_Reader & store,
                              const std::shared_ptr<const Feature_Space>
                                  & feature_space) = 0;

    /** Allow polymorphic copying. */
    virtual Classifier_Impl * make_copy() const = 0;
    ///@}

    /** Return the feature that this classifier is predicting. */
    const Feature & predicted() const { return predicted_; }

    virtual bool merge_into(const Classifier_Impl & other, float weight = 1.0);

    virtual Classifier_Impl *
    merge(const Classifier_Impl & other, float weight = 1.0) const;

    virtual Output_Encoding output_encoding() const = 0;

protected:
    /** Default constructor.  Must be initialised or assigned to before
        anything useful can be done. */
    Classifier_Impl();

    /** Construct for the given feature space, to classify into the given
        number of labels.

        \param fs                the feature space.  This specifies the
                                 domain of the classifier (what it takes in
                                 as its input).
        \param predicted         the feature that the classifier is trying
                                 to predict.
    */
    Classifier_Impl(const std::shared_ptr<const Feature_Space> & fs,
                    const Feature & predicted);

    /** Construct for the given feature space, to classify into the given
        number of labels.

        \param fs                the feature space.  This specifies the
                                 domain of the classifier (what it takes in
                                 as its input).
        \param predicted         the feature that the classifier is trying
                                 to predict.
        \param label_count       the number of labels (classes) that the
                                 classifier will predict into.
    */
    Classifier_Impl(const std::shared_ptr<const Feature_Space> & fs,
                    const Feature & predicted, size_t label_count);

    /** Two-stage initialisation.  Fills in the feature space and the label
        count. */
    void init(const std::shared_ptr<const Feature_Space> & feature_space,
              const Feature & predicted);

    /** Two-stage initialisation.  Fills in the feature space and the label
        count. */
    void init(const std::shared_ptr<const Feature_Space> & feature_space,
              const Feature & predicted, size_t label_count);

    /** Swap the class with another one.  This method is protected as it is
        dangerous: if it were public it might get called on two classifiers
        of a derived class, which would then swap only the Classifier_Impl
        members and not the derived class's members.

        Normally, you will want to swap two pointers that point to something
        derived from a Classifier_Impl rather then the objects themselves.
    */
    void swap(Classifier_Impl & other)
    {
        std::swap(label_count_, other.label_count_);
        feature_space_.swap(other.feature_space_);
        std::swap(predicted_, other.predicted_);
    }

protected:
    /** The feature space to use.  Be careful when changing it that something
        doesn't already depend upon the other value. */
    std::shared_ptr<const Feature_Space> feature_space_;

    /** The feature that we are trying to predict. */
    Feature predicted_;

    /** The number of labels.  Be careful when changing it that something
        doesn't already depend upon the other value. */
    size_t label_count_;
};

/*****************************************************************************/
/* POLYMORPHIA                                                               */
/*****************************************************************************/

class FS_Context {
public:
    FS_Context(const std::shared_ptr<const Feature_Space> & feature_space);
    ~FS_Context();

    static const std::shared_ptr<const Feature_Space> & inner();
};

DB::Store_Writer &
operator << (DB::Store_Writer & store,
             const std::shared_ptr<const Classifier_Impl> & classifier);

DB::Store_Reader &
operator >> (DB::Store_Reader & store,
             std::shared_ptr<Classifier_Impl> & classifier);

/*****************************************************************************/
/* CLASSIFIER                                                                */
/*****************************************************************************/

class Classifier {
public:
    /** Default constructor.  Must be initialised or assigned to before
        anything useful can be done. */
    Classifier();

    /** Construct from given implementation. */
    Classifier(const std::shared_ptr<Classifier_Impl> & impl);

    /** Construct from implementation pointer. */
    Classifier(Classifier_Impl * impl, bool take_copy = false);

    /** Construct by copying an existing implementation. */
    explicit Classifier(const Classifier_Impl & impl);

    /** Construct by instantiating a new object of the given type. */
    template<class Impl>
    Classifier(const std::shared_ptr<const Feature_Space> & feature_space)
        : impl(new Impl(feature_space)) {}

    /** Allow the existence to be queried. */
    operator bool () const { return !!impl; }

    /** Construct by instantiating a new object of the given registered
        name. */
    Classifier(const std::string & name,
               const std::shared_ptr<const Feature_Space> & feature_space);

    /** Construct by reconstitution. */
    Classifier(const std::shared_ptr<const Feature_Space> & feature_space,
               DB::Store_Reader & store);

    /** Copy constructor. */
    Classifier(const Classifier & other);

    /** Assignment operator. */
    Classifier & operator = (const Classifier & other);

    /** Allow efficient swapping. */
    void swap(Classifier & other)
    {
        impl.swap(other.impl);
    }

    /** Returns the number of classes that this classifier is trying to
        classify into.  */
    size_t label_count() const
    {
        return impl->label_count();
    }

    /** Returns the feature space. */
    std::shared_ptr<const Feature_Space> feature_space() const
    {
        return impl->feature_space();
    }

    /** Returns the feature space, dynamic cast as specified. */
    template<class Target_FS>
    std::shared_ptr<const Target_FS> feature_space() const
    {
        std::shared_ptr<const Target_FS> result
            = std::dynamic_pointer_cast<const Target_FS>(feature_space());
        if (!result) {
            auto fs = feature_space();
            auto & fsr = *fs;
            throw Exception("Couldn't cast feature space of type "
                            + MLDB::demangle(typeid(fsr).name())
                            + " to "
                            + MLDB::demangle(typeid(Target_FS).name()));
        }
        return result;
    }

    /** Predict the score for a single class. */
    float predict(int label, const Feature_Set & features,
                  PredictionContext * context = 0) const
    {
        return impl->predict(label, features, context);
    }

    /** Predict the highest class. */
    int predict_highest(const Feature_Set & features,
                        PredictionContext * context = 0) const
    {
        return impl->predict_highest(features, context);
    }

    /** Predict the score for all classes. */
    Label_Dist
    predict(const Feature_Set & features,
            PredictionContext * context = 0) const
    {
        return impl->predict(features, context);
    }

    /** Calculate the prediction accuracy over a training set. */
    std::pair<float, float>
    accuracy(const Training_Data & data,
             const distribution<float> & example_weights
                   = UNIFORM_WEIGHTS) const
    {
        return impl->accuracy(data);
    }

    /** Dump it to a string. */
    std::string print() const
    {
        return impl->print();
    }

    /** Provides a list of all features used by this classifier. */
    std::vector<Feature> all_features() const
    {
        return impl->all_features();
    }

    /** Return the feature that this classifier is predicting. */
    const Feature & predicted() const
    {
        return impl->predicted();
    }

    /** Serialization and reconstitution. */
    void serialize(DB::Store_Writer & store, bool write_fs = true) const
    {
        impl->poly_serialize(store, write_fs);
    }

    /** Reconstitute into the given feature space. */
    void reconstitute(DB::Store_Reader & store,
                      const std::shared_ptr<const Feature_Space>
                              & feature_space)
    {
        impl = Classifier_Impl::poly_reconstitute(store, feature_space);
    }

    /** Reconstitute. */
    void reconstitute(DB::Store_Reader & store)
    {
        impl = Classifier_Impl::poly_reconstitute(store);
    }

    /** Return the class ID of the given classifier. */
    std::string class_id() const
    {
        if (!impl) return "NONE";
        else return impl->class_id();
    }

    Output_Encoding output_encoding() const
    {
        return impl->output_encoding();
    }

    void load(const std::string & filename);
    void load(const std::string & filename,
              std::shared_ptr<const Feature_Space> fs);
    void save(const std::string & filename, bool write_fs = true) const;

    std::shared_ptr<Classifier_Impl> impl;
};

DB::Store_Writer &
operator << (DB::Store_Writer & store, const Classifier & classifier);

DB::Store_Reader &
operator >> (DB::Store_Reader & store, Classifier & classifier);

} // namespace ML


text:
#ifdef HAVE_CONFIG_H
#  include <dtn-config.h>
#endif

#include "bundling/BundleProtocol.h"
#include "bundling/BundleDaemon.h"
#include <oasys/thread/Lock.h>

#include "prophet/QueuePolicy.h"
#include "ProphetRouter.h"

namespace dtn
{

void prophet_router_shutdown(void*)
{
    BundleDaemon::instance()->router()->shutdown();
}

prophet::ProphetParams ProphetRouter::params_;

bool ProphetRouter::is_init_ = false;

ProphetRouter::ProphetRouter()
    : BundleRouter("ProphetRouter","prophet"),
      core_(NULL), oracle_(NULL),
      lock_(new oasys::SpinLock("ProphetRouter"))
{
}

ProphetRouter::~ProphetRouter()
{
    delete oracle_;
    delete core_;
    delete lock_;
}

void
ProphetRouter::initialize()
{
    ASSERT( is_init_ == false );

    // create local instance of ProphetBundleCore,
    // prophet::Repository, and prophet::Controller
    std::string local_eid(BundleDaemon::instance()->local_eid().str());

    core_ = new ProphetBundleCore(local_eid,actions_,lock_);
    oracle_ = new prophet::Controller(core_,core_->bundles(),&params_);

    // register the global shutdown function
    BundleDaemon::instance()->set_rtr_shutdown(
            prophet_router_shutdown, (void *) 0);

    // deserialize any routes from permanent storage
    core_->load_prophet_nodes(oracle_->nodes(),&params_);

    is_init_ = true;
    log_info("ProphetRouter initialization complete");
}

void
ProphetRouter::shutdown()
{
    log_info("ProphetRouter shutdown");
    oasys::ScopeLock l(lock_, "shutdown");
    oracle_->shutdown();
    core_->shutdown();
}

void
ProphetRouter::handle_event(BundleEvent* e)
{
    dispatch_event(e);
}

void
ProphetRouter::get_routing_state(oasys::StringBuffer* buf)
{
    log_info("ProphetRouter get_routing_state");
    oasys::ScopeLock l(lock_, "get_routing_state");

    // summarize current number of routes, misc. statistics
    buf->appendf("ProphetRouter:\n"
            "  %zu routes, %zu queued bundles, %zu ACKs, %zu active sessions\n",
            oracle_->nodes()->size(),
            core_->bundles()->size(),
            oracle_->acks()->size(),
            oracle_->size());
    // iterate over Encounters and query their status
    buf->appendf("Active Sessions\n");
    for (prophet::Controller::List::const_iterator i = oracle_->begin();
            i != oracle_->end(); i++)
    {
        buf->appendf(" %4d: %-30s %s timeout %u\n",
                (*i)->local_instance(),
                (*i)->nexthop()->remote_eid(),
                (*i)->state_str(),
                (*i)->time_remaining());
    }
    buf->appendf("Routes\n");
    for (prophet::Table::const_iterator i = oracle_->nodes()->begin();
            i != oracle_->nodes()->end(); i++)
    {
        buf->appendf("       %-30s: %.2f %s%s%s %lu s old\n",
                i->second->dest_id(), i->second->p_value(),
                i->second->relay() ? "R" : " ",
                i->second->custody() ? "C" : " ",
                i->second->internet_gw() ? "I" : " ",
                (time(0) - i->second->age()));
    }
    buf->appendf("\n R - relay   C - custody   I - internet gateway \n\n");

    //XXX/wilson debug
    buf->appendf("Bundles:\n");
    prophet::BundleList bundles = core_->bundles()->get_bundles();
    for (prophet::BundleList::iterator i = bundles.begin();
            i != bundles.end(); i++)
    {
        buf->appendf("%s -> %s (%u:%u)\n",
                (*i)->source_id().c_str(),
                (*i)->destination_id().c_str(),
                (*i)->creation_ts(),
                (*i)->sequence_num());
    }
}

bool
ProphetRouter::accept_bundle(Bundle* bundle, int* errp)
{
    log_info("ProphetRouter accept_bundle");

    // first ask base class
    if (!BundleRouter::accept_bundle(bundle,errp))
    {
        log_debug("BundleRouter rejects *%p",bundle);
        return false;
    }

    BundleRef tmp("accept_bundle");
    tmp = bundle;

    oasys::ScopeLock l(lock_, "accept_bundle");
    // retrieve temp prophet handle to Bundle metadata
    const prophet::Bundle* b = core_->get_temp_bundle(tmp);
    if (errp != NULL) errp = (int) BundleProtocol::REASON_NO_ADDTL_INFO;
    // ask controller's opinion on this bundle
    bool ok = oracle_->accept_bundle(b);
    // clean up memory used by temporary wrapper
    delete b;
    log_debug("do%saccept bundle *%p", ok ? " " : " not ", bundle);
    return ok;
}

void
ProphetRouter::handle_bundle_received(BundleReceivedEvent* e)
{
    log_info("ProphetRouter handle_bundle_received");

    // should not be reached, but somehow still is
    if (e->source_ == EVENTSRC_STORE)
        return;

    const prophet::Link* l = NULL;

    oasys::ScopeLock sl(lock_, "handle_bundle_received");
    // Locally generated files do not have a link specified either
    // The ping reflector generates bundles with EVENTSRC_ADMIN
    // [Note from Elwyn Davies: Maybe using a special link might be useful]
    if ((e->source_ != EVENTSRC_APP) && (e->source_ != EVENTSRC_ADMIN))
    {
	// The external CL does not set this field, which the Prophet
	// implementation needs. We want to fail quickly if we're
	// running with the ECL.
	ASSERT(e->link_ != NULL);

        // add DTN's Link to BundleCore facade
        core_->add(e->link_);

        // retrieve prophet's handle to Link metadata
        l = core_->get_link(e->link_.object());

        if (l == NULL) return;
    }

    // create temporary prophet handle to Bundle metadata
    const prophet::Bundle* b = core_->get_temp_bundle(e->bundleref_);

    if (b == NULL)
    {
        log_err("failed to retrieve prophet handle for *%p",
                e->bundleref_.object());
        return;
    }

    core_->bundles_.add(b);

    // inform Controller that a new bundle has arrived on this link
    oracle_->handle_bundle_received(b,l);
}

void
ProphetRouter::handle_bundle_delivered(BundleDeliveredEvent* e)
{
    log_info("ProphetRouter handle_bundle_delivered");
    oasys::ScopeLock l(lock_, "handle_bundle_delivered");

    Bundle* bundle = e->bundleref_.object();
    if (bundle == NULL) return;

    // retrieve prophet's handle to Bundle metadata
    const prophet::Bundle* b = core_->get_bundle(bundle);
    if (b == NULL)
    {
        log_err("Failed to convert *%p to prophet object",bundle);
        return;
    }
    // BundleDeliveredEvent means prophet::Ack, which kicks Bundle out of Prophet
    oracle_->ack(b);
}

void
ProphetRouter::handle_bundle_expired(BundleExpiredEvent* e)
{
    log_info("ProphetRouter handle_bundle_expired");
    oasys::ScopeLock l(lock_, "handle_bundle_expired");

    const prophet::Bundle* b = NULL;
    Bundle* bundle = e->bundleref_.object();
    if (bundle != NULL && ((b = core_->get_bundle(bundle)) != NULL))
    {
        // drop Prophet stats on this bundle
        oracle_->stats()->drop_bundle(b);
    }

    core_->del(e->bundleref_);
}

void
ProphetRouter::handle_bundle_transmitted(BundleTransmittedEvent* e)
{
    const prophet::Bundle* bundle = core_->get_bundle(e->bundleref_.object());
    const prophet::Link* link = core_->get_link(e->link_.object());
    if (bundle != NULL && link != NULL)
        oracle_->handle_bundle_transmitted(bundle,link);
}

void
ProphetRouter::handle_contact_up(ContactUpEvent* e)
{
    log_info("ProphetRouter handle_contact_up");
    oasys::ScopeLock lk(lock_, "handle_contact_up");

    Link* link = e->contact_->link().object();
    if (link == NULL) return;

    // add DTN's Link to BundleCore facade
    core_->add(e->contact_->link());
    // retrieve prophet's handle to Link metadata
    const prophet::Link* l = core_->get_link(link);
    if (l == NULL) return;
    // tell Controller about our new friend
    oracle_->new_neighbor(l);
}

void
ProphetRouter::handle_contact_down(ContactDownEvent* e)
{
    log_info("ProphetRouter handle_contact_down");
    oasys::ScopeLock lk(lock_, "handle_contact_down");

    Link* link = e->contact_->link().object();

    // retrieve prophet's handle to Link metadata
    const prophet::Link* l = NULL;
    if (link != NULL &&
            (l = core_->get_link(e->contact_->link().object())) != NULL)
        // inform Controller about the loss
        oracle_->neighbor_gone(l);
    // drop BundleCore's knowledge about this Link
    core_->del(e->contact_->link());
}

void
ProphetRouter::handle_link_available(LinkAvailableEvent* e)
{
    LinkRef next_hop = e->link_;
    ASSERT(next_hop != NULL);
    ASSERT(!next_hop->isdeleted());

    // Prophet initiates its protocol based on handle_contact_up,
    // which fires upon success link open ... so poke it and see
    // what happens
    if (!next_hop->isopen())
    {
        // request to open link
        actions_->open_link(next_hop);
    }
}

void
ProphetRouter::set_queue_policy()
{
    log_info("ProphetRouter set_queue_policy");
    oasys::ScopeLock l(lock_, "set_queue_policy");
    // tell Controller to reorganize internal bundle policy based on new
    // parameters written to params_ by ProphetCommand
    oracle_->set_queue_policy();
}

void
ProphetRouter::set_hello_interval()
{
    log_info("ProphetRouter set_hello_interval");
    oasys::ScopeLock l(lock_, "set_hello_interval");
    // tell Controller to change internal protocol timeouts based on new
    // parameters written to params_ by ProphetCommand
    oracle_->set_hello_interval();
}

void
ProphetRouter::set_max_route()
{
    log_info("ProphetRouter set_max_route");
    oasys::ScopeLock l(lock_, "set_max_route");
    // tell Controller to change internal limit on number of routes
    // to retain, based on changes made to params_ by ProphetCommand
    oracle_->set_max_route();
}

}; // namespace dtn

redpajama_set_name: RedPajamaGithub


text:
'use strict';

var LinkedList = require('./linked-list');

describe('Linked List', function () {

    it('should be able to create empty linked list', function () {
        var linkedList = new LinkedList();
        expect(linkedList).toBeDefined();
        expect(linkedList.head).toBe(null);
    });

});


text:
import PropTypes from 'prop-types';
import React from 'react';

export default class Document extends React.Component {

  componentDidMount() {
    // eslint-disable-next-line
    console.warn('The Document component in Fervor is now deprecated and will be removed in the future. Please use `Meta` instead. See https://github.com/fervorous/fervor/blob/master/CHANGELOG.md#110---2018-06-02 for more details.');
    document.querySelector('title').innerText = this.props.title;
  }

  render() {
    return this.props.children;
  }
}

Document.propTypes = {
  children: PropTypes.node.isRequired,
  title: PropTypes.string.isRequired,
};


text:
{-# LANGUAGE TemplateHaskell #-}

module System.Hapistrano.Maintenance
  ( writeMaintenanceFile
  , deleteMaintenanceFile
  ) where

import Path (Abs, Dir, File, Path, Rel, (</>))
import System.Hapistrano.Commands
import System.Hapistrano.Core
import System.Hapistrano.Types

-- | It writes an HTML page in the given directory with a given name
writeMaintenanceFile ::
     Path Abs Dir -> Path Rel Dir -> Path Rel File -> Hapistrano ()
writeMaintenanceFile deployPath relDir fileName =
  let foo = deployPath </> relDir
      fullpath = relDir </> fileName
      root = deployPath </> fullpath
   in do exec (MkDir foo) Nothing
         exec (Touch root) Nothing
         exec (BasicWrite root maintenancePageContent) Nothing

-- | It deletes the file in the given directory with the given name
deleteMaintenanceFile ::
     Path Abs Dir -> Path Rel Dir -> Path Rel File -> Hapistrano ()
deleteMaintenanceFile deployPath relDir fileName =
  let fullpath = relDir </> fileName
      root = deployPath </> fullpath
   in exec (Rm root) Nothing

maintenancePageContent :: String
maintenancePageContent =
  "<!DOCTYPE html> \n\
  \\n\
  \ \n\
  \ <title>Maintenance</title>\n\
  \ \n\
  \ \n\
  \ \n\
  \
Maintenance
\n\
  \
The system is down for maintenance
\n\
  \
It'll be back shortly
\n\
  \ \n\
  \"


text:
layout: archive
title: "Contact"
permalink: /contact/
author_profile: true
redirect_from:
  - /contacts
---

{% include base_path %}

### Email
[sdowning@missouri.edu](mailto:sdowning@missouri.edu "Email: sdowning@missouri.edu")

### Department Affiliation
Robert J. Trulaske, Sr. College of Business
University of Missouri
700 Tiger Ave.
Columbia, MO 65211


text:
"""
    Conjunto de modelos relacionais para o controle da app (Usuarios, auditorias, logs, etc)
    Os modelos do catálogo do OPAC (periódicos, números, artigos) estão definidos na
    lib: opac_schema (ver requirements.txt)
"""

import os
from sqlalchemy.event import listens_for
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy_utils.types.choice import ChoiceType
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from flask import current_app
from webapp.utils import thumbgen_filename
from . import dbsql as db
from . import login_manager
from . import notifications

LANGUAGES_CHOICES = [
    ('pt', 'Português'),
    ('en', 'English'),
    ('es', 'Español'),
]

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(120), nullable=False, unique=True)
    _password = db.Column(db.String(128), nullable=True)  # deve ser possível add novo user sem setar senha
    email_confirmed = db.Column(db.Boolean, nullable=False, default=False)

    @hybrid_property
    def password(self):
        return self._password

    @password.setter
    def _set_password(self, plaintext):
        self._password = generate_password_hash(plaintext)

    def is_correct_password(self, plaintext):
        """
        Compara a string ``plaintext`` com a senha "hasheada" armazenada para este usuário.
        """
        if not self._password:
            return False
        else:
            return check_password_hash(self._password, plaintext)

    def send_confirmation_email(self):
        if not self._check_valid_email():
            raise ValueError('Usuário deve ter email válido para realizar o envío')
        else:
            return notifications.send_confirmation_email(self.email)

    def send_reset_password_email(self):
        if not self._check_valid_email():
            raise ValueError('Usuário deve ter email válido para realizar o envío')
        else:
            return notifications.send_reset_password_email(self.email)

    def _check_valid_email(self):
        """
        retorna True quando a instância (self) do usuário, tem um email válido.
        retorna False em outro caso.
        """
        from webapp.admin.forms import EmailForm
        if not self.email or self.email == '' or self.email == '':
            return False
        else:
            form = EmailForm(data={'email': self.email})
            return form.validate()

    # Required for administrative interface
    def __unicode__(self):
        return self.email

@login_manager.user_loader
def load_user(user_id):
    """
        Retora usuário pelo id.
        Necessário para o login manager.
    """
    return User.query.get(int(user_id))

class File(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(64), nullable=False)
    path = db.Column(db.Unicode(256), nullable=False)
    language = db.Column(ChoiceType(LANGUAGES_CHOICES), nullable=True)

    def __unicode__(self):
        return self.name

    @property
    def get_absolute_url(self):
        media_url = current_app.config['MEDIA_URL']
        return '%s/%s' % (media_url, self.path)

# Delete hooks: remove arquivos quando o modelo é apagado
@listens_for(File, 'after_delete')
def delelte_file_hook(mapper, connection, target):
    if target.path:
        media_root = current_app.config['MEDIA_ROOT']
        try:
            os.remove(os.path.join(media_root, target.path))
        except OSError:
            pass  # Se der erro não importa, o arquivo já não existe

class Image(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Unicode(64), nullable=False)
    path = db.Column(db.Unicode(256), nullable=False)
    language = db.Column(ChoiceType(LANGUAGES_CHOICES), nullable=True)

    def __unicode__(self):
        return self.name

    @property
    def get_absolute_url(self):
        media_url = current_app.config['MEDIA_URL']
        return '%s/%s' % (media_url, self.path)

    @property
    def get_thumbnail_absolute_url(self):
        media_url = current_app.config['MEDIA_URL']
        thumb_path = thumbgen_filename(self.path)
        return '%s/%s' % (media_url, thumb_path)

# Delete hooks: remove arquivos quando o modelo é apagado
@listens_for(Image, 'after_delete')
def delelte_image_hook(mapper, connection, target):
    if target.path:
        media_root = current_app.config['MEDIA_ROOT']
        # Remover a imagem
        try:
            os.remove(os.path.join(media_root, target.path))
        except OSError:
            pass  # Se der erro não importa, o arquivo já não existe

        # Remover o thumbnail
        try:
            thumb_path = thumbgen_filename(target.path)
            os.remove(os.path.join(media_root, thumb_path))
        except OSError:
            pass  # Se der erro não importa, o arquivo já não existe


text:
Pattern: Use of space after method declaration name

Issue: -

## Description

Checks whether method declarations do not contain unnecessary whitespace between method name and the opening parenthesis for parameter list.

## Examples

```java
class ClassWithWhitespaceInConstructorDeclaration {

	ClassWithWhitespaceInConstructorDeclaration () { //violation
	}

	void methodWithWhitespaceInDeclaration () { //violation
	}
}
```

## Further Reading

* [CodeNarc - SpaceAfterMethodDeclarationName](https://codenarc.org/codenarc-rules-formatting.html#spaceaftermethoddeclarationname-rule)


text:
function GHCND_clustered = cluster_on_extremes(GHCND_QC, maxclust, summer_doy, makeplots, ...
	station_latlims, station_lonlims, fig_folder, cacheDir)
% Cluster station data based on whether days are extreme on the same day
%
% Input: GHCND_QC = structure containing info about stations that passed QC
%		 note that anom contains all data, not just summer
%        maxclust = number of clusters to use. Note that some stations end
%        up being grouped into their own cluster, so this is larger than
%        the actual numer of clusters being used in the end
%		 summer_doy = days to use for summer
% Output: GHCND_clustered = structure amended to include the cluster id

% anomalies from baseline during summer
summer_anom = GHCND_QC.anom(:, ismember(GHCND_QC.doy, summer_doy));

% Calculate binary field
binary_temperature = summer_anom;
binary_temperature(binary_temperature <= 0) = 0;
binary_temperature(binary_temperature > 0) = 1;

% cluster on above or below baseline
rng('default');
cluster_input.binary_temperature = binary_temperature;
cluster_input.maxclust = maxclust;
cluster_input.dist_method = 'jaccard';
cluster_input.linkage_method = 'average';

% Hash to see if this has already been done...
cluster_hash = DataHash(cluster_input);
plot_dendro = 0;
plot_silhouette = 0;

figpath = fig_folder;
if exist([cacheDir '/cluster' cluster_hash '.mat'],'file') % load existing?
    disp('Clustering already computed... loading')
    load([cacheDir '/cluster' cluster_hash '.mat'])
else % If not, cluster and save:
    disp('First time clustering this data... will take a while.')
    tic
    [clusters,c,Z] = cluster_qdata(binary_temperature, cluster_input.maxclust, ...
    	cluster_input.dist_method, cluster_input.linkage_method, plot_dendro, ...
    	plot_silhouette, figpath);
    toc
    timestamp = datestr(now);
    save([cacheDir '/cluster' cluster_hash '.mat'],'clusters','c','Z','timestamp')
end

% make plot of different numbers of clusters?

clusterInd = maxclust;

if makeplots
	for ii = 1:maxclust
		cluster_temp = clusters(:, ii);
		cluster_temp = relabel_clusters(cluster_temp);
		map_clustered_stations(GHCND_QC, cluster_temp, fig_folder, ...
			maxclust, station_latlims, station_lonlims)
	end
end

clusters = clusters(:,clusterInd);
[clusters,cluster_counts] = relabel_clusters(clusters);

GHCND_clustered = GHCND_QC;
GHCND_clustered.clusterid = clusters;

return


text:
Request which commands the device to switch all light relays on and then queries the device for the status of the relays. OGSP checks if the status of the relays is as expected \(on in this case\).

Response returns the result of the request.

## IEC61850 Fields

| **ATTRIBUTE** | **FC** | **SUB ATTRIBUTE** | **DATATYPE** | **DESCRIPTION** |
| :--- | :--- | :--- | :--- | :--- |
| XSWC2.Pos | CO | Oper.ctlVal | BOOLEAN | Flag which, if set to true, immediately switches relay 2 on. |
| XSWC3.Pos | CO | Oper.ctlVal | BOOLEAN | Flag which, if set to true, immediately switches relay 3 on. |

## Example

Soap requests and responses sent to and from platform:

```markup
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns="http://www.opensmartgridplatform.org/schemas/common/2014/10"
    xmlns:ns1="http://www.opensmartgridplatform.org/schemas/deviceinstallation/2014/10">
   <soapenv:Header>
      <ns:ApplicationName>SoapUI</ns:ApplicationName>
      <ns:UserName>Sander</ns:UserName>
      <ns:OrganisationIdentification>LianderNetManagement</ns:OrganisationIdentification>
   </soapenv:Header>
   <soapenv:Body>
      <ns1:StartDeviceTestRequest>
         <ns1:DeviceIdentification>KAI-0000000053</ns1:DeviceIdentification>
      </ns1:StartDeviceTestRequest>
   </soapenv:Body>
</soapenv:Envelope>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns2:StartDeviceTestAsyncResponse
          xmlns:ns2="http://www.opensmartgridplatform.org/schemas/deviceinstallation/2014/10"
          xmlns:ns3="http://www.opensmartgridplatform.org/schemas/common/2014/10">
         <ns2:AsyncResponse>
            <ns3:CorrelationUid>LianderNetManagement|||KAI-0000000053|||20180925142331454</ns3:CorrelationUid>
            <ns3:DeviceId>KAI-0000000053</ns3:DeviceId>
         </ns2:AsyncResponse>
      </ns2:StartDeviceTestAsyncResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ns="http://www.opensmartgridplatform.org/schemas/common/2014/10"
    xmlns:ns1="http://www.opensmartgridplatform.org/schemas/deviceinstallation/2014/10">
   <soapenv:Header>
      <ns:ApplicationName>SoapUI</ns:ApplicationName>
      <ns:UserName>Sander</ns:UserName>
      <ns:OrganisationIdentification>LianderNetManagement</ns:OrganisationIdentification>
   </soapenv:Header>
   <soapenv:Body>
      <ns1:StartDeviceTestAsyncRequest>
         <ns1:AsyncRequest>
            <ns:CorrelationUid>LianderNetManagement|||KAI-0000000053|||20180925142331454</ns:CorrelationUid>
            <ns:DeviceId>KAI-0000000053</ns:DeviceId>
         </ns1:AsyncRequest>
      </ns1:StartDeviceTestAsyncRequest>
   </soapenv:Body>
</soapenv:Envelope>

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <ns2:StartDeviceTestResponse
          xmlns:ns2="http://www.opensmartgridplatform.org/schemas/deviceinstallation/2014/10"
          xmlns:ns3="http://www.opensmartgridplatform.org/schemas/common/2014/10">
         <ns2:Result>OK</ns2:Result>
      </ns2:StartDeviceTestResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>
```

IEC61850 platform message of the data written to the device:

```javascript
LogicalDevice: SWDeviceGenericIO
messageType: StartSelfTest {
  XSWC2.Pos[CO].Oper.ctlVal: true
  XSWC3.Pos[CO].Oper.ctlVal: true
}
```

IEC61850 platform message of the data read from the device:

```javascript
LogicalDevice: SWDeviceGenericIO
messageType: GetStatus {
  XSWC2.Pos[ST].stVal: true
  CSLC.SWCf[CF].LT: RELAY
  XSWC3.Pos[ST].stVal: true
  XSWC1.Pos[ST].stVal: false
  CSLC.EvnBuf[CF].enbEvnType: 1FFFFFF
}
```

IEC61850 protocol adapter logging:

```text
2018-09-25 14:23:31.994] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.m.DeviceRequestMessageListener@onMessage:61 - Received message of type: START_SELF_TEST with message priority: 4
2018-09-25 14:23:31.994] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.m.BaseMessageProcessor@printDomainInfo:53 - Calling DeviceService function: START_SELF_TEST for domain: CORE 1.0
2018-09-25 14:23:31.995] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.m.BaseMessageProcessor@getJmsXdeliveryCount:64 - jmsXdeliveryCount: 1
2018-09-25 14:23:31.995] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850DeviceConnectionService@connect:115 - Trying to connect to deviceIdentification: KAI-0000000053 at IP address 84.30.69.148 using response time-out: 10000
2018-09-25 14:23:32.002] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.r.Iec61850ClientSSLDEventListener@buildExternalByInternalIndexMap:80 - Retrieved internal to external index map for device KAI-0000000053: {0=0, 1=1, 2=2, 3=3}
2018-09-25 14:23:32.005] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.Iec61850Client@connect:97 - Attempting to connect to server: 84.30.69.148 on port: 102, max redelivery count: 3 and max retry count: 3
2018-09-25 14:23:32.056] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.Iec61850Client@connect:113 - Connected to device: KAI-0000000053
2018-09-25 14:23:32.056] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850DeviceConnectionService@readServerModelFromConfiguredIcdFile:316 - Reading ServerModel from SCL / ICD file: /etc/osp/kaifa-server-model/SWDevice-010805.icd
2018-09-25 14:23:32.111] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850DeviceConnectionService@connect:159 - Connected to device: KAI-0000000053, fetched server model. Start time: 2018-09-25T14:23:31.995Z, end time: 2018-09-25T14:23:32.111Z, total time in milliseconds: 116
2018-09-25 14:23:32.114] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@runSelfTest:339 - Turning all lights relays on
2018-09-25 14:23:32.114] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@createListOfInternalIndicesToSwitch:220 - creating list of internal indices using device output settings
2018-09-25 14:23:32.114] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850SetLightCommand@apply:66 - Trying to switch light relay with internal index: 2 on: true for device: KAI-0000000053
2018-09-25 14:23:32.115] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.h.DeviceConnection@createObjectReference:94 - Device: KAI-0000000053, ObjectReference: SWDeviceGenericIO/XSWC2.CfSt
2018-09-25 14:23:32.128] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850Commands@enableOperationOfRelay:56 - masterControl.enbOper is true, switching of relay 2 is enabled
2018-09-25 14:23:32.129] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.h.DeviceConnection@createObjectReference:94 - Device: KAI-0000000053, ObjectReference: SWDeviceGenericIO/XSWC2.Pos
2018-09-25 14:23:32.144] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850SetLightCommand@switchLightRelay:108 - Switching relay 2 on
2018-09-25 14:23:32.162] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850SetLightCommand@apply:66 - Trying to switch light relay with internal index: 3 on: true for device: KAI-0000000053
2018-09-25 14:23:32.162] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.h.DeviceConnection@createObjectReference:94 - Device: KAI-0000000053, ObjectReference: SWDeviceGenericIO/XSWC3.CfSt
2018-09-25 14:23:32.258] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850Commands@enableOperationOfRelay:56 - masterControl.enbOper is true, switching of relay 3 is enabled
2018-09-25 14:23:32.258] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.h.DeviceConnection@createObjectReference:94 - Device: KAI-0000000053, ObjectReference: SWDeviceGenericIO/XSWC3.Pos
2018-09-25 14:23:32.480] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850SetLightCommand@switchLightRelay:108 - Switching relay 3 on
2018-09-25 14:23:32.523] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.s.DeviceMessageLoggingService@logMessage:52 - Sending iec61850LogItemRequestMessage for device: KAI-0000000053
2018-09-25 14:23:32.523] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@selfTestSleep:391 - Waiting 5000 milliseconds before getting the device status
2018-09-25 14:23:37.524] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.h.DeviceConnection@createObjectReference:94 - Device: KAI-0000000053, ObjectReference: SWDeviceGenericIO/XSWC1.Pos
2018-09-25 14:23:39.899] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850GetStatusCommand@apply:63 - Got status of relay 1 => off
2018-09-25 14:23:39.900] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.h.DeviceConnection@createObjectReference:94 - Device: KAI-0000000053, ObjectReference: SWDeviceGenericIO/XSWC2.Pos
2018-09-25 14:23:40.148] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850GetStatusCommand@apply:63 - Got status of relay 2 => on
2018-09-25 14:23:40.148] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.h.DeviceConnection@createObjectReference:94 - Device: KAI-0000000053, ObjectReference: SWDeviceGenericIO/XSWC3.Pos
2018-09-25 14:23:40.287] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850GetStatusCommand@apply:63 - Got status of relay 3 => on
2018-09-25 14:23:40.287] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.h.DeviceConnection@createObjectReference:94 - Device: KAI-0000000053, ObjectReference: SWDeviceGenericIO/CSLC.EvnBuf
2018-09-25 14:23:41.762] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.helper.NodeContainer@getString:88 - Device: KAI-0000000053, enbEvnType has value 1FFFFFF
2018-09-25 14:23:41.762] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.c.Iec61850GetStatusCommand@apply:74 - Got EvnBuf.enbEvnType filter 1FFFFFF
2018-09-25 14:23:41.763] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.h.DeviceConnection@createObjectReference:94 - Device: KAI-0000000053, ObjectReference: SWDeviceGenericIO/CSLC.SWCf
2018-09-25 14:23:41.778] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.helper.NodeContainer@getString:88 - Device: KAI-0000000053, LT has value RELAY
2018-09-25 14:23:41.778] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.s.DeviceMessageLoggingService@logMessage:52 - Sending iec61850LogItemRequestMessage for device: KAI-0000000053
2018-09-25 14:23:41.778] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@runSelfTest:354 - Fetching and checking the devicestatus
2018-09-25 14:23:41.779] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@runSelfTest:359 - relaysWithInternalIdToSwitch.getIndex().equals(lightValue.getIndex()): false for lightValue.getIndex(): 1 and lightValueDto.getIndex(): 2
2018-09-25 14:23:41.779] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@runSelfTest:359 - relaysWithInternalIdToSwitch.getIndex().equals(lightValue.getIndex()): false for lightValue.getIndex(): 1 and lightValueDto.getIndex(): 3
2018-09-25 14:23:41.779] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@runSelfTest:359 - relaysWithInternalIdToSwitch.getIndex().equals(lightValue.getIndex()): true for lightValue.getIndex(): 2 and lightValueDto.getIndex(): 2
2018-09-25 14:23:41.779] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@runSelfTest:359 - relaysWithInternalIdToSwitch.getIndex().equals(lightValue.getIndex()): false for lightValue.getIndex(): 2 and lightValueDto.getIndex(): 3
2018-09-25 14:23:41.779] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@runSelfTest:359 - relaysWithInternalIdToSwitch.getIndex().equals(lightValue.getIndex()): false for lightValue.getIndex(): 3 and lightValueDto.getIndex(): 2
2018-09-25 14:23:41.779] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@runSelfTest:359 - relaysWithInternalIdToSwitch.getIndex().equals(lightValue.getIndex()): true for lightValue.getIndex(): 3 and lightValueDto.getIndex(): 3
2018-09-25 14:23:41.779] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850SsldDeviceService@runSelfTest:372 - All lights relays are on, returning OK
2018-09-25 14:23:41.779] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.s.DeviceResponseService@handleDeviceMessageStatus:42 - OK device message status received: OK
2018-09-25 14:23:41.779] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.m.DeviceResponseMessageSender@sendMessage:111 - Sending protocol response message for device: KAI-0000000053 of message type: START_SELF_TEST with message priority: 4
2018-09-25 14:23:41.780] [osgp-tst-03] [iec61850RequestsMessageListenerContainer-11] INFO o.o.a.p.i.i.n.s.Iec61850DeviceConnectionService@logDuration:355 - Device: KAI-0000000053, messageType: START_SELF_TEST, Start time: 2018-09-25T14:23:31.995Z, end time: 2018-09-25T14:23:41.780Z, total time in milliseconds: 9785
```


text:
#include <QtCore/qglobal.h>

#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
	#include <QtGui/QApplication>
#else
	#include <QtWidgets/QApplication>
#endif

#include <QtCore/QDebug>

#include <trikKernel/applicationInitHelper.h>
#include <trikKernel/configurer.h>
#include <trikKernel/paths.h>
#include <trikCommunicator/trikCommunicator.h>
#include <trikControl/brickFactory.h>
#include <trikControl/brickInterface.h>
#include <trikNetwork/mailboxFactory.h>
#include <trikNetwork/mailboxInterface.h>

#include <QsLog.h>

int main(int argc, char *argv[])
{
	const int port = 8888;

	QApplication app(argc, argv);
	app.setApplicationName("TrikServer");
	trikKernel::ApplicationInitHelper initHelper(app);

	if (!initHelper.parseCommandLine()) {
		return 0;
	}

	initHelper.init();

	QLOG_INFO() << "TrikServer started on port" << port;

	QScopedPointer<trikControl::BrickInterface> brick(trikControl::BrickFactory::create(
			initHelper.configPath(), trikKernel::Paths::mediaPath()));

	trikKernel::Configurer configurer(
			initHelper.configPath() + "/system-config.xml"
			, initHelper.configPath() + "/model-config.xml"
			);

	QScopedPointer<trikNetwork::MailboxInterface> mailbox(trikNetwork::MailboxFactory::create(configurer));

	trikCommunicator::TrikCommunicator communicator(*brick, mailbox.data());
	communicator.startServer(port);

	return app.exec();
}


text:
module DPLibrary
  class Collection
    attr_accessor :collection_items

    def initialize(collection_response)
      set_values(collection_response)
    end

    def set_values(collection_response)
      @collection_items = []
      if (collection_response.is_a?(Array))
        @collection_items = collection_response.map {|h| create_collection_item(h)}
      else
        @collection_items << CollectionItem.new(collection_response)
      end
      @collection_items
    end

    def create_collection_item(collection_item_hash)
      CollectionItem.new(collection_item_hash)
    end
  end
end


text:
/* @flow */

import { cloneVNode, cloneVNodes } from 'core/vdom/vnode'

/**
 * Runtime helper for rendering static trees.
 */
export function renderStatic (
  index: number,
  isInFor?: boolean
): VNode | Array<VNode> {
  let tree = this._staticTrees[index]
  // if has already-rendered static tree and not inside v-for,
  // we can reuse the same tree by doing a shallow clone.
  if (tree && !isInFor) {
    return Array.isArray(tree)
      ? cloneVNodes(tree)
      : cloneVNode(tree)
  }
  // otherwise, render a fresh tree.
  tree = this._staticTrees[index] =
    this.$options.staticRenderFns[index].call(this._renderProxy)
  markStatic(tree, `__static__${index}`, false)
  return tree
}

/**
 * Runtime helper for v-once.
 * Effectively it means marking the node as static with a unique key.
 */
export function markOnce (
  tree: VNode | Array<VNode>,
  index: number,
  key: string
) {
  markStatic(tree, `__once__${index}${key ? `_${key}` : ``}`, true)
  return tree
}

function markStatic (
  tree: VNode | Array<VNode>,
  key: string,
  isOnce: boolean
) {
  if (Array.isArray(tree)) {
    for (let i = 0; i < tree.length; i++) {
      if (tree[i] && typeof tree[i] !== 'string') {
        markStaticNode(tree[i], `${key}_${i}`, isOnce)
      }
    }
  } else {
    markStaticNode(tree, key, isOnce)
  }
}

function markStaticNode (node, key, isOnce) {
  node.isStatic = true
  node.key = key
  node.isOnce = isOnce
}


text:
"use strict";

var Promise = require('pouchdb-promise');
var utils = require('./utils');

var resolved = Promise.resolve();

function DaemonManager() {
  // There are a few things express-pouchdb needs to do outside of
  // requests. These things can't get a PouchDB object from the request
  // like other code does. Instead, they register themselves here and
  // get an object passed in. By providing both a start and stop
  // function, it is possible to switch PouchDB objects on the fly.
  this._daemons = [];
}

DaemonManager.prototype.registerDaemon = function (daemon) {
  this._daemons.push(daemon);
};

['start', 'stop'].forEach(function (name) {
  DaemonManager.prototype[name] = function (PouchDB) {
    var funcs = this._daemons.map(function (daemon) {
      return daemon[name];
    });
    return utils.callAsyncRecursive(funcs, function (func, next) {
      return resolved.then(function () {
        return func(PouchDB);
      }).then(next);
    });
  };
});

module.exports = DaemonManager;


text:
layout: docs
title:  "Algorithms"
section: "algorithms"
position: 2
---
# Algorithms

{% for x in site.pages %}
  {% if x.section == 'algorithms' and x.title != page.title %}
- [{{x.title}}]({{site.baseurl}}{{x.url}})
  {% endif %}
{% endfor %}


text:
[https://github.com/mdn/data](https://github.com/mdn/data)

Maintained by the [MDN team at Mozilla](https://wiki.mozilla.org/MDN).

This repository contains general data for Web technologies.

This data is used in MDN documentation, to build
[information boxes](https://developer.mozilla.org/en-US/docs/Web/CSS/background)
or [sidebar navigation](https://developer.mozilla.org/en-US/docs/Web/API/Window).
External tools have started to make use of this data as well.
For example, the [CSSTree](https://github.com/csstree/csstree/) CSS parser.

[![NPM version](https://img.shields.io/npm/v/mdn-data.svg)](https://www.npmjs.com/package/mdn-data)
[![Build Status](https://travis-ci.org/mdn/data.svg?branch=master)](https://travis-ci.org/mdn/data)

## Repository contents

There's a top-level directory for each broad area covered: for example, "api",
"css", "svg". Inside each of these directories is one or more
JSON files containing the data.

### api
Contains data about Web APIs:
* API inheritance (interface inheritance and mixin implementations)

### css
 Contains data about:
* CSS at-rules
* CSS properties
* CSS selectors
* CSS syntaxes
* CSS types
* CSS units

Read more about [CSS data](https://github.com/mdn/data/blob/master/css/readme.md) and the format of the files.

### l10n
The l10n folder contains localization strings that are used in the various
json files throughout this repository.

## Problems?

If you find a problem, please [file an issue](https://github.com/mdn/data/issues/new).

## Contributing

We're very happy to accept contributions to this data. Please familiarize yourself
with the schema for the data you're editing, and send us a pull request. See also the [Contributing file](https://github.com/mdn/data/blob/master/CONTRIBUTING.md) for more information.

## See also
* [https://github.com/mdn/browser-compat-data](https://github.com/mdn/browser-compat-data)
for compatibility data for Web technologies.


text:
import time
from data.utils.indicator_mapper import Indicator_Data_Map
from data.influx import InfluxDB

INFLUX_HOST = "ledmatrix"
INFLUX_DB = "home"

class Indoor_Mapper(Indicator_Data_Map):
  """ Mapper for inside conditions """

  def __init__(self):

    query = """ SELECT
                        "temp_f" AS "temp_f",
                        "humidity" as "humidity"
                    FROM "home"."autogen"."SI7201 Temp+Humidity"
                    ORDER BY DESC
                    LIMIT 1
        """

    self._data = InfluxDB(query, INFLUX_HOST, INFLUX_DB)

    self.init()

    text = lambda: "%iF %i%% RH" % (
        self._data["temp_f"],
        self._data["humidity"]
    )

    self["icon_name"] = "house"
    self["icon_color"] = "#85144B"
    self["line1"] = ""
    self["line2"] = lambda: time.strftime(
        "%I:%M", time.localtime()).lstrip("0")
    self["line3"] = text


text:
<!DOCTYPE html>
<html>
<title>Test MediaSource object and handle creation, with MediaSource in dedicated worker</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="mediasource-message-util.js"></script>
<script>

async_test(t => {
  // Fail fast if MSE-in-Workers is not supported.
  assert_true(MediaSource.hasOwnProperty("canConstructInDedicatedWorker"), "MediaSource hasOwnProperty 'canConstructInDedicatedWorker'");
  assert_true(MediaSource.canConstructInDedicatedWorker, "MediaSource.canConstructInDedicatedWorker");
  assert_true(window.hasOwnProperty("MediaSourceHandle"), "window must have MediaSourceHandle visibility");

  let worker = new Worker("mediasource-worker-play.js");
  worker.onmessage = t.step_func(e => {
    let subject = e.data.subject;
    assert_true(subject != undefined, "message must have a subject field");
    switch (subject) {
      case messageSubject.ERROR:
        assert_unreached("Worker error: " + e.data.info);
        break;
      case messageSubject.HANDLE:
        const handle = e.data.info;
        assert_not_equals(handle, null, "must have a non-null MediaSourceHandle");
        assert_true(handle instanceof MediaSourceHandle, "must be a MediaSourceHandle");
        t.done();
        break;
      default:
        assert_unreached("Unexpected message subject: " + subject);

    }
  });
}, "Test main context receipt of postMessage'd MediaSourceHandle from DedicatedWorker MediaSource");

test(t => {
  assert_true(window.hasOwnProperty("MediaSourceHandle"), "window must have MediaSourceHandle visibility");

  // Note, MSE spec may eventually describe how a main-thread MediaSource can
  // attach to an HTMLMediaElement using a MediaSourceHandle. For now, we
  // ensure that the implementation of this is not available.
  assert_throws_dom('NotSupportedError', function() {
    let h = new MediaSource().handle;
  }, 'main thread MediaSource instance cannot (yet) create a usable MediaSourceHandle');
}, "Test main-thread-owned MediaSource instance cannot create a MediaSourceHandle");

if (MediaSource.hasOwnProperty("canConstructInDedicatedWorker") && MediaSource.canConstructInDedicatedWorker === true) {
  // If implementation claims support for MSE-in-Workers, then fetch and run
  // some tests directly in another dedicated worker and get their results
  // merged into those from this page.
  fetch_tests_from_worker(new Worker("mediasource-worker-handle.js"));
} else {
  // Otherwise, fetch and run a test that verifies lack of support of
  // MediaSource construction in another dedicated worker.
  fetch_tests_from_worker(new Worker("mediasource-worker-must-fail-if-unsupported.js"));
}

</script>
</html>


text:
//
//  Copyright 2012 Alin Dobra and Christopher Jermaine
//
//  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 _GLA_HELPER_H_
#define _GLA_HELPER_H_

#include "ID.h"
#include "Bitstring.h"
#include "Bitmap.h"
#include "Chunk.h"
#include "Bitstring.h"
#include <vector>
#include "Errors.h"

/** This file contains helper class GLAChunkCache to allow the GLAWayPoint to keep track of all the chunks produced by it.
 *  In case if a chunk is dropped and GLAWayPoint receives request for the same chunk again, it is sent from this data structure
 *  and not produced again as that particular GLAState has already been destroyed in the Finalize method.
 */

class QueryFragmentMap {

    private:
        std::vector<Bitstring> qc;

    public:
        QueryFragmentMap ();

        Bitstring GetBits (int fragmentNo);

        void Clear (int chunkNo);
        void Clear (int chunkNo, Bitstring queries);

        void ORAll (Bitstring newQ, int tillFragmentNo);
        void OROne (int fragmentNo, Bitstring newQ);

        int FindFirstSet (int _start);

        void Debugg(void);

};

inline
QueryFragmentMap::QueryFragmentMap() {
}

inline
Bitstring QueryFragmentMap::GetBits (int fragmentNo) {
    return qc[fragmentNo];
}

inline
void QueryFragmentMap::Clear (int fragmentNo) {
    qc[fragmentNo] = Bitstring(0,true);
}

inline
void QueryFragmentMap::Clear (int fragmentNo, Bitstring queries) {
    qc[fragmentNo].Difference(queries);
}

inline
void QueryFragmentMap::ORAll (Bitstring newQ, int tillFragmentNo) {
    if (tillFragmentNo>qc.size())
        qc.resize(tillFragmentNo, Bitstring(0,true));

    for (int i = 0; i < tillFragmentNo; i++) {
        qc[i].Union(newQ);
    }
}

inline
void QueryFragmentMap::OROne (int fragmentNo, Bitstring newQ) {
    newQ.Union(GetBits(fragmentNo));
    qc[fragmentNo] = newQ;
}

inline
int QueryFragmentMap::FindFirstSet (int _start) {

    for (int i = _start; i < qc.size(); i++) {
        if (!qc[i].IsEmpty()) {
            return i;
        }
    }

    for (int i = 0; i < _start; i++) {
        if (!qc[i].IsEmpty()) {
            return i;
        }
    }

    return -1;
}

inline
void QueryFragmentMap::Debugg(void){
    for (int i = 0; i < qc.size(); i++) {
        if (!qc[i].IsEmpty()) {
            printf("TS: %d\n", i);
        }
    }

}

#endif //  _GLA_HELPER_H_


text:
The *fly* stage1 is an alternative stage1 that runs a single-application ACI with only `chroot`-isolation.

## Motivation

The motivation of the fly feature is to add the ability to run applications with full privileges on the host but still benefit from the image management and discovery from rkt.
The Kubernetes `kubelet` is one candidate for rkt fly.

## How does it work?

In comparison to the default stage1, there is no process manager involved in the stage1.
This a visual illustration for the differences in the process tree between the default and the fly stage1:

stage1-coreos.aci:

```
host OS
  └─ rkt
    └─ systemd-nspawn
      └─ systemd
        └─ chroot
          └─ user-app1
```

stage1-fly.aci:

```
host OS
  └─ rkt
    └─ chroot
      └─ user-app1
```

The rkt application sets up bind mounts for `/dev`, `/proc`, `/sys`, and the user-provided volumes.
In addition to the bind mounts, an additional *tmpfs* mount is done at `/tmp`.
After the mounts are set up, rkt `chroot`s to the application's RootFS and finally executes the application.

### Mount propagation modes

The *fly* stage1 makes use of Linux [mount propagation modes](https://www.kernel.org/doc/Documentation/filesystems/sharedsubtree.txt).
If a volume source path is a mountpoint on the host, this mountpoint is made recursively shared before the host path is mounted on the target path in the container.
Hence, changes to the mounts inside the container will be propagated back to the host.

The bind mounts for `/dev`, `/proc`, and `/sys` are done automatically and are recursive, because their hierarchy contains mounts which also need to be available for the container to function properly.
User-provided volumes are not mounted recursively.
This is a safety measure to prevent system crashes when multiple containers are started that mount `/` into the container.

## Getting started

You can either use `stage1-fly.aci` from the official release, or build rkt yourself with the right options:

```
$ ./autogen.sh && ./configure --with-stage1-flavors=fly && make
```

For more details about configure parameters, see the [configure script parameters documentation](build-configure.md).
This will build the rkt binary and the stage1-fly.aci in `build-rkt-1.15.0+git/bin/`.

### Selecting stage1 at runtime

Here is a quick example of how to use a container with the official fly stage1:

```
# rkt run --stage1-name=coreos.com/rkt/stage1-fly:1.15.0 coreos.com/etcd:v2.2.5
```

If the image is not in the store, `--stage1-name` will perform discovery and fetch the image.

## Notes on isolation and security

By design, the *fly* stage1 does not provide the same isolaton and security features as the default stage1.

Specifically, the following constraints are not available when using the *fly* stage1:

- network namespace isolation
- CPU isolators
- Memory isolators
- CAPABILITY bounding
- SELinux

### Providing additional isolation with systemd

When using systemd on the host it is possible to [wrap rkt with a systemd unit file](using-rkt-with-systemd.md#advanced-unit-file) to provide additional isolation.
For more information please consult the systemd manual.
* [systemd.resource-control](http://www.freedesktop.org/software/systemd/man/systemd.resource-control.html)
* [systemd.directives](http://www.freedesktop.org/software/systemd/man/systemd.directives.html)


text:
using System;
using System.Collections.Generic;
using System.IO;
using BrokenShoeLeague.Domain;
using BrokenShoeLeague.Services.Helpers;
using BrokenShoeLeague.Services.Seasons.Models;
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.FileIO;

namespace BrokenShoeLeague.Services.Importers
{
    public class StatsImporter : IStatsImporter
    {
        public PlayerStats[] GetStats(string path)
        {
            var stats = new List<PlayerStats> { };

            foreach (var line in Common.ParseCsv(path))
            {
                if (line[1] == "" || line[1] == "0")
                    continue;

                var stat = new PlayerStats
                {
                    Wins = int.Parse(line[2]),
                    Draws = int.Parse(line[3]),
                    Losts = int.Parse(line[4]),
                    Goals = int.Parse(line[5]),
                    Assists = int.Parse(line[6]),
                };
                if (stat.IsValid())
                    stats.Add(stat);
                else
                    throw new Exception(string.Format("The stats of {0} are invalid: {1}!={2}+{3}+{4}", stat.PlayerName, stat.PlayedGames, stat.Wins, stat.Draws, stat.Losts));
            }

            return stats.ToArray();
        }
    }
}


text:
package panda.utilities;

import org.jetbrains.annotations.Nullable;
import panda.std.Option;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public final class ClassUtils {

    public static final Map<Class<?>, Class<?>> PRIMITIVE_EQUIVALENT = new HashMap<Class<?>, Class<?>>() {{
        put(void.class, Void.class);

        put(byte.class, Byte.class);
        put(short.class, Short.class);
        put(int.class, Integer.class);
        put(long.class, Long.class);

        put(float.class, Float.class);
        put(double.class, Double.class);

        put(boolean.class, Boolean.class);
        put(char.class, Character.class);
    }};

    private ClassUtils() { }

    /**
     * Get non primitive class
     *
     * @param clazz the class to get
     * @return non primitive class
     */
    public static Class<?> getNonPrimitiveClass(Class<?> clazz) {
        if (!clazz.isPrimitive()) {
            return clazz;
        }

        return PRIMITIVE_EQUIVALENT.get(clazz);
    }

    /**
     * Select the most related class from the specified collection
     *
     * @param classes the collection to search in
     * @param to the class to compare with
     * @return the most related class from collection (may not exist)
     */
    public static Option<Class<?>> selectMostRelated(Collection<? extends Class<?>> classes, Class<?> to) {
        Class<?> selected = null;

        for (Class<?> clazz : classes) {
            if (clazz.isAssignableFrom(to)) {
                if (selected != null && clazz.isAssignableFrom(selected)) {
                    continue;
                }

                selected = clazz;
            }
        }

        return Option.of(selected);
    }

    /**
     * Check if the class is assignable to another with primitive types support.
     * Utility command for nullable fromObject.
     *
     * @see ClassUtils#isAssignableFrom(Class, Class)
     */
    public static boolean isAssignableFrom(Class<?> toClass, @Nullable Object fromObject) {
        return fromObject != null && isAssignableFrom(toClass, fromObject.getClass());
    }

    /**
     * Check if the class is assignable to another with primitive types support
     *
     * @param toClass the root class
     * @param fromClass the {@link java.lang.Class} object to be checked
     * @return true if toClass is assignable to fromClass
     */
    public static boolean isAssignableFrom(@Nullable Class<?> toClass, @Nullable Class<?> fromClass) {
        if (toClass == null || fromClass == null) {
            return false;
        }

        if (toClass.isPrimitive()) {
            toClass = PRIMITIVE_EQUIVALENT.get(toClass);
        }

        if (fromClass.isPrimitive()) {
            fromClass = PRIMITIVE_EQUIVALENT.get(fromClass);
        }

        return toClass.isAssignableFrom(fromClass);

    }

    /**
     * Get classes of the given values
     *
     * @param values the values to check
     * @return array of classes
     */
    public static Class<?>[] getClasses(Object... values) {
        //noinspection ReturnOfNull
        return Arrays.stream(values)
                       .map(object -> object != null ? object.getClass() : null)
                       .toArray(Class[]::new);
    }

    /**
     * Check if the class exists using {@link ClassUtils#forName(String)}
     *
     * @param name the name of class
     * @return true if class exists, otherwise false
     */
    public static boolean exists(String name) {
        return forName(name).isPresent();
    }

    /**
     * Get class using {@link java.lang.Class#forName(String)} as {@link panda.std.Option}
     *
     * @param name the name of class
     * @return optional with class (with null instead of {@link java.lang.ClassNotFoundException})
     */
    @SuppressWarnings("unchecked")
    public static <T> Option<Class<? extends T>> forName(String name) {
        try {
            Class<T> clazz = (Class<T>) Class.forName(name);
            return Option.of(clazz);
        } catch (ClassNotFoundException classNotFoundException) {
            return Option.none();
        }
    }

}


text:
/*
 * 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.
 */
package com.facebook.presto.connector;

import com.facebook.presto.metadata.ForMetadata;
import com.facebook.presto.metadata.NativeHandleResolver;
import com.facebook.presto.metadata.NativeMetadata;
import com.facebook.presto.spi.Connector;
import com.facebook.presto.spi.ConnectorFactory;
import com.facebook.presto.spi.ConnectorHandleResolver;
import com.facebook.presto.spi.ConnectorMetadata;
import com.facebook.presto.spi.ConnectorSplitManager;
import com.facebook.presto.split.ConnectorDataStreamProvider;
import com.facebook.presto.split.NativeDataStreamProvider;
import com.facebook.presto.split.NativeSplitManager;
import com.google.common.collect.ImmutableClassToInstanceMap;
import org.skife.jdbi.v2.IDBI;

import javax.inject.Inject;

import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import static com.google.common.base.Preconditions.checkNotNull;

public class NativeConnectorFactory
        implements ConnectorFactory
{
    private static final NativeHandleResolver HANDLE_RESOLVER = new NativeHandleResolver();

    private final AtomicReference<IDBI> dbi = new AtomicReference<>();
    private final AtomicReference<NativeSplitManager> splitManager = new AtomicReference<>();
    private final NativeDataStreamProvider dataStreamProvider;

    @Inject
    public NativeConnectorFactory(NativeDataStreamProvider dataStreamProvider)
    {
        this.dataStreamProvider = checkNotNull(dataStreamProvider, "dataStreamProvider is null");
    }

    @Inject
    public void setDbi(@ForMetadata IDBI dbi)
    {
        this.dbi.set(checkNotNull(dbi, "dbi is null"));
    }

    @Inject
    public void setSplitManager(NativeSplitManager splitManager)
    {
        this.splitManager.set(checkNotNull(splitManager, "splitManager is null"));
    }

    @Override
    public String getName()
    {
        return "native";
    }

    @Override
    public Connector create(String connectorId, Map<String, String> properties)
    {
        ImmutableClassToInstanceMap.Builder<Object> builder = ImmutableClassToInstanceMap.builder();

        IDBI dbi = this.dbi.get();
        if (dbi != null) {
            builder.put(ConnectorMetadata.class, new NativeMetadata(connectorId, dbi));
        }

        NativeSplitManager splitManager = this.splitManager.get();
        if (splitManager != null) {
            builder.put(ConnectorSplitManager.class, splitManager);
        }

        builder.put(ConnectorDataStreamProvider.class, dataStreamProvider);
        builder.put(ConnectorHandleResolver.class, HANDLE_RESOLVER);

        return new StaticConnector(builder.build());
    }
}


text:
<?php

namespace Webit\Shipment\Feature;

use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Collections\ArrayCollection;
use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\SerializerBuilder;
use JMS\Serializer\SerializerInterface;
use Webit\Shipment\Consignment\ConsignmentInterface;
use Webit\Shipment\Serializer\VendorOptionCollectionHandler;
use Webit\Shipment\Serializer\VendorOptionCollectionNormaliser;
use Webit\Shipment\Serializer\VendorOptionValueCollectionNormaliser;
use Webit\Shipment\Vendor\VendorInterface;
use Webit\Shipment\Vendor\VendorOptionCollection;
use Webit\Shipment\Vendor\VendorOptionValue;
use Webit\Shipment\Vendor\VendorOptionValueCollection;
use Webit\Tools\Serializer\MixedTypeHandler;
use Webit\Tools\Serializer\TypeAliasHandler;

/**
 * Defines application features from the specific context.
 */
class MappingFeatureContext implements Context, SnippetAcceptingContext
{
    /**
     * @var array
     */
    private $keyMap = array();

    /**
     * @var SerializerInterface
     */
    private $serializer;

    /**
     * @var string
     */
    private $json;

    /**
     * @var mixed
     */
    private $result;

    /**
     * @Given there is following serializer mapping:
     */
    public function thereIsFollowingSerializerMapping(TableNode $table)
    {
        AnnotationRegistry::registerAutoloadNamespace('JMS\\Serializer\\Annotation\\', __DIR__.'/../../vendor/jms/serializer/src');

        $builder = SerializerBuilder::create();
        $builder->addMetadataDir(__DIR__.'/../../src/Resources/serializer', 'Webit\Shipment');
        $builder->addDefaultHandlers();
        $builder->configureHandlers(function (HandlerRegistryInterface $handlerRegistry) {
            $handler = new VendorOptionCollectionHandler(
                new VendorOptionCollectionNormaliser(),
                new VendorOptionValueCollectionNormaliser()
            );
            $methods = VendorOptionCollectionHandler::getSubscribingMethods();
            foreach ($methods as $method) {
                $handlerRegistry->registerHandler($method['direction'], $method['type'], $method['format'], array($handler, $method['method']));
            }
        });

        $map = array();
        foreach ($table as $row) {
            $map[$row['interface']] = $row['class'];
            $this->keyMap[$row['key']] = $row['class'];
        }

        $builder->configureHandlers(function (HandlerRegistryInterface $handlerRegistry) use ($map) {
            $handler = new TypeAliasHandler($map);
            $methods = TypeAliasHandler::getSubscribingMethods();
            foreach ($methods as $method) {
                $handlerRegistry->registerHandler($method['direction'], $method['type'], $method['format'], array($handler, $method['method']));
            }
        });

        $builder->configureHandlers(function (HandlerRegistryInterface $handlerRegistry) use ($map) {
            $handler = new MixedTypeHandler();
            $methods = MixedTypeHandler::getSubscribingMethods();
            foreach ($methods as $method) {
                $handlerRegistry->registerHandler($method['direction'], $method['type'], $method['format'], array($handler, $method['method']));
            }
        });

        $this->serializer = $builder->build();
    }

    /**
     * @Given json representation like:
     */
    public function jsonRepresentationLike(PyStringNode $string)
    {
        $this->json = $string->getRaw();
    }

    /**
     * @When I deserialize it to :arg1
     * @param $type
     */
    public function iDeserializeItTo($type)
    {
        $this->result = $this->serializer->deserialize($this->json, $type, 'json');
    }

    /**
     * @Then I should get instance of :arg1
     */
    public function iShouldGetInstanceOf($type)
    {
        \PHPUnit_Framework_Assert::assertInstanceOf($type, $this->result);
    }

    /**
     * @Then I should get valid :arg1 with properties like:
     */
    public function iShouldGetValidWithPropertiesLike($key, TableNode $table)
    {
        \PHPUnit_Framework_Assert::assertInstanceOf($this->keyMap[$key], $this->result);

        $this->checkProperties($this->result, $this->getProperties($table));
    }

    private function getProperties(TableNode $table)
    {
        $properties = array();
        foreach ($table as $row) {
            $properties[$row['property']] = $row['value'];
        }

        return $properties;
    }

    /**
     * @Then Instance should have properties like:
     */
    private function checkProperties($result, $properties)
    {
        foreach ($properties as $property => $expectedValue) {
            $getter = 'get'.ucfirst($property);
            if (! method_exists($result, $getter)) {
                $getter = 'is'.ucfirst($property);
            }

            if (preg_match('/^is/', $getter)) {
                $expectedValue = $expectedValue == 'true' ? true : false;
            }

            $value = $result->{$getter}();
            if ($value instanceof \DateTime) {
                \PHPUnit_Framework_Assert::assertEquals($expectedValue, $value->format('Y-m-d H:i:s'));
            } else {
                \PHPUnit_Framework_Assert::assertEquals($expectedValue, $value);
            }
        }
    }

    /**
     * @Then vendor should have following consignment options:
     */
    public function vendorShouldHaveFollowingConsignmentOptions(TableNode $table)
    {
        /** @var VendorInterface $vendor */
        $vendor = $this->result;
        $this->checkOptions($vendor->getConsignmentOptions(), $table);
    }

    /**
     * @Then vendor should have following parcel options:
     * @param TableNode $table
     */
    public function vendorShouldHaveFollowingParcelOptions(TableNode $table)
    {
        /** @var VendorInterface $vendor */
        $vendor = $this->result;
        $this->checkOptions($vendor->getParcelOptions(), $table);
    }

    private function checkOptions(VendorOptionCollection $collection, TableNode $table) {
        \PHPUnit_Framework_Assert::assertInstanceOf('Webit\Shipment\Vendor\VendorOptionCollection', $collection);
        foreach ($table as $row) {
            $option = $collection->getOption($row['code']);
            \PHPUnit_Framework_Assert::assertNotNull($option);
            $properties = array(
                'code' => $row['code'],
                'name' => $row['name'],
                'description' => $row['description'],
                'allowedValues' => new ArrayCollection(explode(',',$row['allowedValues']))
            );

            $this->checkProperties($option, $properties);
        }
    }

    /**
     * @Then vendor should have following :arg1 print modes:
     */
    public function vendorShouldHaveFollowingPrintModes($printType, TableNode $table)
    {
        /** @var VendorInterface $result */
        $result = $this->result;
        $modes = null;
        switch($printType) {
            case 'label':
                $modes = $result->getLabelPrintModes();
                break;
            case 'dispatch_confirmation':
                $modes = $result->getDispatchConfirmationPrintModes();
                break;
            default:
                throw new \UnexpectedValueException('Unsupported print type: '.$printType);
        }

        $expectedModes = new ArrayCollection();
        foreach ($table as $row) {
            $expectedModes->add($row['mode']);
        }

        \PHPUnit_Framework_Assert::assertEquals($expectedModes, $modes);
    }

    /**
     * @Then consignment should have :arg1 like:
     */
    public function consignmentShouldHaveLike($type, TableNode $table)
    {
        switch ($type) {
            case 'sender_address':
                $this->checkProperties($this->result->getSenderAddress(), $this->getProperties($table));
                break;
            case 'delivery_address':
                $this->checkProperties($this->result->getDeliveryAddress(), $this->getProperties($table));
                break;
            case 'parcels':
                $this->checkParcels($this->result, $table);
                break;
            case 'vendor_options':
                $this->checkVendorOptions($this->result, $table);
                break;
            default:
                throw new \UnexpectedValueException('Unsupported type: '. $type);
        }

    }

    private function checkParcels(ConsignmentInterface $consignment, TableNode $table)
    {
        $parcels = $consignment->getParcels();
        foreach ($table as $key => $parcel) {
            $currentParcel = $parcels->get($key);
            \PHPUnit_Framework_Assert::assertNotNull($currentParcel);
            $this->checkProperties($currentParcel, $parcel);
        }
    }

    private function checkVendorOptions(ConsignmentInterface $consignmentInterface, TableNode $table)
    {
        $expected = new VendorOptionValueCollection();
        foreach ($table as $key => $properties) {
            $option = new VendorOptionValue($properties['optionCode']);

            if ($properties['value'] == 'true') {
                $properties['value'] = true;
            }

            if (preg_match('/,/', $properties['value'])) {
                $properties['value'] = explode(',', $properties['value']);
            }
            $option->setValue($properties['value']);
            $expected->addValue($option);
        }

        \PHPUnit_Framework_Assert::assertEquals(
            $expected,
            $consignmentInterface->getVendorOptions()
        );
    }
}


text:
package com.assylias.jbloomberg;

import com.bloomberglp.blpapi.CorrelationID;

/**
 *
 * This class is use to hold the data returned by subscriptions.
 */
final class Data {
    private final CorrelationID correlationId;
    private final String field;
    private final Object value;

    Data(CorrelationID correlationId, String field, Object value) {
        this.correlationId = correlationId;
        this.field = field;
        this.value = value;
    }

    public CorrelationID getCorrelationId() {
        return correlationId;
    }

    public String getField() {
        return field;
    }

    public Object getValue() {
        return value;
    }

    @Override
    public String toString() {
        return "{id=" + correlationId + ", " + field + "=" + value + '}';
    }
}