<commit_before>#include "NoncentralTRand.h"
#include "NormalRand.h"
#include <functional>

NoncentralTRand::NoncentralTRand(double degree, double noncentrality) :
    T(degree)
{
    SetParameters(degree, noncentrality);
}

std::string NoncentralTRand::Name() const
{
    return "Noncentral-t(" + toStringWithPrecision(GetDegree()) + ", "
            + toStringWithPrecision(GetNoncentrality()) + ")";
}

void NoncentralTRand::SetParameters(double degree, double noncentrality)
{
    nu = (degree > 0.0) ? degree : 1.0;
    T.SetDegree(nu);
    sqrt1p2oNu = std::exp(0.5 * std::log1p(2.0 / nu));

    mu = noncentrality;
    PhiMu = 0.5 * std::erfc(-M_SQRT1_2 * mu); /// φ(μ)
    PhimMu = 0.5 * std::erfc(M_SQRT1_2 * mu); /// φ(-μ)

    /// precalculate values for pdf/cdf integration
    static constexpr double epsilon = 1e-16;
    ChiSquaredRand X(nu);
    nuCoefs.halfNu = 0.5 * nu;
    nuCoefs.qEpsCoef = std::sqrt(X.Quantile(epsilon) / nu);
    nuCoefs.q1mEpsCoef = std::sqrt(X.Quantile1m(epsilon) / nu);
    nuCoefs.logHalfNu = std::log(nuCoefs.halfNu);
    nuCoefs.lgammaHalfNu = std::lgamma(nuCoefs.halfNu);
    double nup2 = nu + 2;
    X.SetDegree(nup2);
    nup2Coefs.halfNu = nuCoefs.halfNu + 1.0;
    nup2Coefs.qEpsCoef = std::sqrt(X.Quantile(epsilon) / nup2);
    nup2Coefs.q1mEpsCoef = std::sqrt(X.Quantile1m(epsilon) / nup2);
    nup2Coefs.logHalfNu = std::log1p(nuCoefs.halfNu);
    nup2Coefs.lgammaHalfNu = std::lgamma(nup2Coefs.halfNu);
}

DoublePair NoncentralTRand::getIntegrationLimits(double x, double muAux, const NoncentralTRand::nuStruct &nuAuxCoef) const
{
    static constexpr double normQuant = -37.5194;
    double A0 = std::max(-muAux, normQuant), B0 = -normQuant;
    double qEps = x * nuAuxCoef.qEpsCoef - muAux;
    double q1mEps = x * nuAuxCoef.q1mEpsCoef - muAux;
    return std::make_pair(std::max(qEps, A0), std::min(q1mEps, B0));
}

double NoncentralTRand::cdf(const double &x, const NoncentralTRand::nuStruct &nuAuxCoef, bool isCompl) const
{
    if (x < -mu)
        return upperTail(-x, -mu, nuAuxCoef, !isCompl);
    if (x < 0.0)
        return lowerTail(-x, -mu, nuAuxCoef, !isCompl);
    return (x < mu) ? lowerTail(x, mu, nuAuxCoef, isCompl) : upperTail(x, mu, nuAuxCoef, isCompl);
}

double NoncentralTRand::g(double z, const double &x, const nuStruct &nuAuxCoef, double muAux, bool lower) const
{
    if (z == -muAux)
        return 0.0;
    double y = -0.5 * z * z;
    y -= 0.5 * (M_LN2 + M_LNPI);
    double b = (z + muAux) / x;
    b *= b;
    b *= nuAuxCoef.halfNu;
    /// 'lower' stands for lower tail P(X < x),
    /// however in that case we need to call upper incomplete gamma function
    /// and vice versa
    y += lower ? RandMath::lqgamma(nuAuxCoef.halfNu, b, nuAuxCoef.logHalfNu, nuAuxCoef.lgammaHalfNu) :
                 RandMath::lpgamma(nuAuxCoef.halfNu, b, nuAuxCoef.logHalfNu, nuAuxCoef.lgammaHalfNu);
    return std::exp(y);
}

double NoncentralTRand::findMode(const double &x, double halfNuAux, double muAux, double A, double B) const
{
    /// find approximate value of mode
    double temp = (halfNuAux > 1) ? 8 * halfNuAux - 8 : 4.0;
    double mode = muAux * muAux + temp;
    double xSq = x * x;
    mode *= xSq;
    mode += 2 * temp * halfNuAux;
    mode = x * std::sqrt(mode);
    mode -= muAux * (xSq + 4 * halfNuAux);
    mode /= 2 * xSq + 4 * halfNuAux;
    /// sanity check
    if (mode <= A || mode >= B)
        mode = 0.5 * (A + B);
    return mode;
}

double NoncentralTRand::lowerTail(const double &x, double muAux, const nuStruct &nuAuxCoef, bool isCompl) const
{
    DoublePair intLimits = getIntegrationLimits(x, muAux, nuAuxCoef);
    double A = intLimits.first, B = intLimits.second;
    /// find peak of the integrand
    double mode = findMode(x, nuAuxCoef.halfNu, muAux, A, B);
    /// calculate two integrals
    std::function<double (double)> integrandPtr = std::bind(&NoncentralTRand::g, this, std::placeholders::_1, x, nuAuxCoef, muAux, true);
    double I1 = RandMath::integral(integrandPtr, A, mode);
    double I2 = RandMath::integral(integrandPtr, mode, B);
    double I = I1 + I2;
    /// for S(x) return 1 - F(x) w/o losing precision
    if (isCompl) {
        double PhimA = (A == -mu) ? PhiMu : 0.5 * std::erfc(M_SQRT1_2 * A); /// φ(-A)
        return PhimA - I;
    }
    double PhiA = (A == -mu) ? PhimMu : 0.5 * std::erfc(-M_SQRT1_2 * A); /// φ(A)
    return PhiA + I;
}

double NoncentralTRand::upperTail(const double &x, double muAux, const nuStruct &nuAuxCoef, bool isCompl) const
{
    DoublePair intLimits = getIntegrationLimits(x, muAux, nuAuxCoef);
    double A = intLimits.first, B = intLimits.second;
    /// find peak of the integrand
    double mode = findMode(x, nuAuxCoef.halfNu, muAux, A, B);
    /// calculate two integrals
    std::function<double (double)> integrandPtr = std::bind(&NoncentralTRand::g, this, std::placeholders::_1, x, nuAuxCoef, muAux, false);
    double I1 = RandMath::integral(integrandPtr, A, mode);
    double I2 = RandMath::integral(integrandPtr, mode, B);
    double I = I1 + I2;
    /// for S(x) return 1 - F(x) w/o losing precision
    if (isCompl) {
        double PhimB = (B == -mu) ? PhiMu : 0.5 * std::erfc(M_SQRT1_2 * B); /// φ(-B)
        return PhimB + I;
    }
    double PhiB = (B == -mu) ? PhimMu : 0.5 * std::erfc(-M_SQRT1_2 * B); /// φ(B)
    return PhiB - I;
}

double NoncentralTRand::f(const double & x) const
{
    if (mu == 0.0)
        return T.f(x);
    if (x == 0.0) {
        double y = std::lgamma(0.5 * nu + 0.5);
        double z = mu * mu + M_LNPI + M_LN2 + nuCoefs.logHalfNu;
        y -= T.Y.GetLogGammaFunction();
        return std::exp(y - 0.5 * z);
    }
    double y = cdf(x * sqrt1p2oNu, nup2Coefs, false);
    y -= cdf(x, nuCoefs, false);
    return nu * y / x;
}

double NoncentralTRand::logf(const double & x) const
{
    return std::log(f(x));
}

double NoncentralTRand::F(const double & x) const
{
    if (mu == 0.0)
        return T.F(x);
    if (x == 0.0)
        return PhimMu;
    return cdf(x, nuCoefs, false);
}

double NoncentralTRand::S(const double &x) const
{
    if (mu == 0.0)
        return T.S(x);
    if (x == 0.0)
        return PhiMu;
    return cdf(x, nuCoefs, true);
}

double NoncentralTRand::Variate() const
{
    double X = NormalRand::StandardVariate() + mu;
    X /= T.Y.Variate();
    return X;
}

void NoncentralTRand::Sample(std::vector<double> &outputData) const
{
    if (mu == 0.0)
        return T.Sample(outputData);
    T.Y.Sample(outputData);
    for (double &var : outputData)
        var = (mu + NormalRand::StandardVariate()) / var;
}

double NoncentralTRand::Mean() const
{
    if (nu <= 1)
        return NAN;
    if (mu == 0.0)
        return 0.0;
    double mean = std::lgamma(0.5 * nu - 0.5);
    mean -= T.Y.GetLogGammaFunction();
    mean += 0.5 * (nuCoefs.logHalfNu);
    return mu * std::exp(mean);
}

double NoncentralTRand::Variance() const
{
    if (nu <= 2)
        return (nu > 1) ? INFINITY : NAN;
    double mean = Mean();
    return nu * (1.0 + mu * mu) / (nu - 2) + mean * mean;
}

double NoncentralTRand::Mode() const
{
    if (mu == 0.0)
        return 0.0;
    double left = std::sqrt(nu / (nu + 2.5)); /// left boundary for mode / μ
    double right = std::sqrt(nu / (nu + 1.0)); /// right boundary for mode / μ
    double guess = 0.5 * mu * (left + right);
    double root = 0;
    RandMath::findMin([this] (double x)
    {
        return -f(x);
    }, guess, root);
    return root;
}

double NoncentralTRand::Skewness() const
{
    if (nu <= 3)
        return NAN;
    double mean = Mean();
    double var = nu * (1.0 + mu * mu) / (nu - 2) + mean * mean;
    double thirdMoment = std::lgamma(0.5 * nu - 1.5);
    thirdMoment -= T.Y.GetLogGammaFunction();
    thirdMoment += 1.5 * (nuCoefs.logHalfNu + M_LN2);
    thirdMoment = 0.25 * std::exp(thirdMoment);
    thirdMoment *= mu * (3.0 + mu * mu);
    double denominator = std::pow(var, 1.5);
    return (thirdMoment - 3 * mean * var - std::pow(mean, 3)) / denominator;
}

double NoncentralTRand::ExcessKurtosis() const
{
    if (nu <= 4)
        return (nu > 2) ? INFINITY : NAN;
    double fourthMoment = nu * nu / ((nu - 2) * (nu - 4));
    fourthMoment *= (std::pow(mu, 4) + 6 * mu * mu + 3);
    double thirdMoment = std::lgamma(0.5 * nu - 1.5);
    thirdMoment -= T.Y.GetLogGammaFunction();
    thirdMoment += 1.5 * (nuCoefs.logHalfNu + M_LN2);
    thirdMoment = 0.25 * std::exp(thirdMoment);
    thirdMoment *= mu * (3.0 + mu * mu);
    double mean = Mean();
    double kurtosis = fourthMoment - 4 * mean * thirdMoment;
    kurtosis += 6 * mean * mean * nu * (1.0 + mu * mu) / (nu - 2);
    kurtosis -= 3 * std::pow(mean, 4);
    double var = Variance();
    return kurtosis / (var * var);
}
<commit_msg>Update NoncentralTRand.cpp<commit_after>#include "NoncentralTRand.h"
#include "NormalRand.h"
#include <functional>

NoncentralTRand::NoncentralTRand(double degree, double noncentrality) :
    T(degree)
{
    SetParameters(degree, noncentrality);
}

std::string NoncentralTRand::Name() const
{
    return "Noncentral-t(" + toStringWithPrecision(GetDegree()) + ", "
            + toStringWithPrecision(GetNoncentrality()) + ")";
}

void NoncentralTRand::SetParameters(double degree, double noncentrality)
{
    nu = (degree > 0.0) ? degree : 1.0;
    T.SetDegree(nu);
    sqrt1p2oNu = std::exp(0.5 * std::log1p(2.0 / nu));

    mu = noncentrality;
    PhiMu = 0.5 * std::erfc(-M_SQRT1_2 * mu); /// φ(μ)
    PhimMu = 0.5 * std::erfc(M_SQRT1_2 * mu); /// φ(-μ)

    /// precalculate values for pdf/cdf integration
    static constexpr double epsilon = 1e-16;
    ChiSquaredRand X(nu);
    nuCoefs.halfNu = 0.5 * nu;
    nuCoefs.qEpsCoef = std::sqrt(X.Quantile(epsilon) / nu);
    nuCoefs.q1mEpsCoef = std::sqrt(X.Quantile1m(epsilon) / nu);
    nuCoefs.logHalfNu = std::log(nuCoefs.halfNu);
    nuCoefs.lgammaHalfNu = Y.GetLogGammaFunction();
    double nup2 = nu + 2;
    X.SetDegree(nup2);
    nup2Coefs.halfNu = nuCoefs.halfNu + 1.0;
    nup2Coefs.qEpsCoef = std::sqrt(X.Quantile(epsilon) / nup2);
    nup2Coefs.q1mEpsCoef = std::sqrt(X.Quantile1m(epsilon) / nup2);
    nup2Coefs.logHalfNu = std::log1p(nuCoefs.halfNu);
    nup2Coefs.lgammaHalfNu = std::lgamma(nup2Coefs.halfNu);
}

DoublePair NoncentralTRand::getIntegrationLimits(double x, double muAux, const NoncentralTRand::nuStruct &nuAuxCoef) const
{
    static constexpr double normQuant = -37.5194;
    double A0 = std::max(-muAux, normQuant), B0 = -normQuant;
    double qEps = x * nuAuxCoef.qEpsCoef - muAux;
    double q1mEps = x * nuAuxCoef.q1mEpsCoef - muAux;
    return std::make_pair(std::max(qEps, A0), std::min(q1mEps, B0));
}

double NoncentralTRand::cdf(const double &x, const NoncentralTRand::nuStruct &nuAuxCoef, bool isCompl) const
{
    if (x < -mu)
        return upperTail(-x, -mu, nuAuxCoef, !isCompl);
    if (x < 0.0)
        return lowerTail(-x, -mu, nuAuxCoef, !isCompl);
    return (x < mu) ? lowerTail(x, mu, nuAuxCoef, isCompl) : upperTail(x, mu, nuAuxCoef, isCompl);
}

double NoncentralTRand::g(double z, const double &x, const nuStruct &nuAuxCoef, double muAux, bool lower) const
{
    if (z == -muAux)
        return 0.0;
    double y = -0.5 * z * z;
    y -= 0.5 * (M_LN2 + M_LNPI);
    double b = (z + muAux) / x;
    b *= b;
    b *= nuAuxCoef.halfNu;
    /// 'lower' stands for lower tail P(X < x),
    /// however in that case we need to call upper incomplete gamma function
    /// and vice versa
    y += lower ? RandMath::lqgamma(nuAuxCoef.halfNu, b, nuAuxCoef.logHalfNu, nuAuxCoef.lgammaHalfNu) :
                 RandMath::lpgamma(nuAuxCoef.halfNu, b, nuAuxCoef.logHalfNu, nuAuxCoef.lgammaHalfNu);
    return std::exp(y);
}

double NoncentralTRand::findMode(const double &x, double halfNuAux, double muAux, double A, double B) const
{
    /// find approximate value of mode
    double temp = (halfNuAux > 1) ? 8 * halfNuAux - 8 : 4.0;
    double mode = muAux * muAux + temp;
    double xSq = x * x;
    mode *= xSq;
    mode += 2 * temp * halfNuAux;
    mode = x * std::sqrt(mode);
    mode -= muAux * (xSq + 4 * halfNuAux);
    mode /= 2 * xSq + 4 * halfNuAux;
    /// sanity check
    if (mode <= A || mode >= B)
        mode = 0.5 * (A + B);
    return mode;
}

double NoncentralTRand::lowerTail(const double &x, double muAux, const nuStruct &nuAuxCoef, bool isCompl) const
{
    DoublePair intLimits = getIntegrationLimits(x, muAux, nuAuxCoef);
    double A = intLimits.first, B = intLimits.second;
    /// find peak of the integrand
    double mode = findMode(x, nuAuxCoef.halfNu, muAux, A, B);
    /// calculate two integrals
    std::function<double (double)> integrandPtr = std::bind(&NoncentralTRand::g, this, std::placeholders::_1, x, nuAuxCoef, muAux, true);
    double I1 = RandMath::integral(integrandPtr, A, mode);
    double I2 = RandMath::integral(integrandPtr, mode, B);
    double I = I1 + I2;
    /// for S(x) return 1 - F(x) w/o losing precision
    if (isCompl) {
        double PhimA = (A == -mu) ? PhiMu : 0.5 * std::erfc(M_SQRT1_2 * A); /// φ(-A)
        return PhimA - I;
    }
    double PhiA = (A == -mu) ? PhimMu : 0.5 * std::erfc(-M_SQRT1_2 * A); /// φ(A)
    return PhiA + I;
}

double NoncentralTRand::upperTail(const double &x, double muAux, const nuStruct &nuAuxCoef, bool isCompl) const
{
    DoublePair intLimits = getIntegrationLimits(x, muAux, nuAuxCoef);
    double A = intLimits.first, B = intLimits.second;
    /// find peak of the integrand
    double mode = findMode(x, nuAuxCoef.halfNu, muAux, A, B);
    /// calculate two integrals
    std::function<double (double)> integrandPtr = std::bind(&NoncentralTRand::g, this, std::placeholders::_1, x, nuAuxCoef, muAux, false);
    double I1 = RandMath::integral(integrandPtr, A, mode);
    double I2 = RandMath::integral(integrandPtr, mode, B);
    double I = I1 + I2;
    /// for S(x) return 1 - F(x) w/o losing precision
    if (isCompl) {
        double PhimB = (B == -mu) ? PhiMu : 0.5 * std::erfc(M_SQRT1_2 * B); /// φ(-B)
        return PhimB + I;
    }
    double PhiB = (B == -mu) ? PhimMu : 0.5 * std::erfc(-M_SQRT1_2 * B); /// φ(B)
    return PhiB - I;
}

double NoncentralTRand::f(const double & x) const
{
    if (mu == 0.0)
        return T.f(x);
    if (x == 0.0) {
        double y = std::lgamma(0.5 * nu + 0.5);
        double z = mu * mu + M_LNPI + M_LN2 + nuCoefs.logHalfNu;
        y -= T.Y.GetLogGammaFunction();
        return std::exp(y - 0.5 * z);
    }
    double y = cdf(x * sqrt1p2oNu, nup2Coefs, false);
    y -= cdf(x, nuCoefs, false);
    return nu * y / x;
}

double NoncentralTRand::logf(const double & x) const
{
    return std::log(f(x));
}

double NoncentralTRand::F(const double & x) const
{
    if (mu == 0.0)
        return T.F(x);
    if (x == 0.0)
        return PhimMu;
    return cdf(x, nuCoefs, false);
}

double NoncentralTRand::S(const double &x) const
{
    if (mu == 0.0)
        return T.S(x);
    if (x == 0.0)
        return PhiMu;
    return cdf(x, nuCoefs, true);
}

double NoncentralTRand::Variate() const
{
    double X = NormalRand::StandardVariate() + mu;
    X /= T.Y.Variate();
    return X;
}

void NoncentralTRand::Sample(std::vector<double> &outputData) const
{
    if (mu == 0.0)
        return T.Sample(outputData);
    T.Y.Sample(outputData);
    for (double &var : outputData)
        var = (mu + NormalRand::StandardVariate()) / var;
}

double NoncentralTRand::Mean() const
{
    if (nu <= 1)
        return NAN;
    if (mu == 0.0)
        return 0.0;
    double mean = std::lgamma(0.5 * nu - 0.5);
    mean -= T.Y.GetLogGammaFunction();
    mean += 0.5 * (nuCoefs.logHalfNu);
    return mu * std::exp(mean);
}

double NoncentralTRand::Variance() const
{
    if (nu <= 2)
        return (nu > 1) ? INFINITY : NAN;
    double mean = Mean();
    return nu * (1.0 + mu * mu) / (nu - 2) + mean * mean;
}

double NoncentralTRand::Mode() const
{
    if (mu == 0.0)
        return 0.0;
    double left = std::sqrt(nu / (nu + 2.5)); /// left boundary for mode / μ
    double right = std::sqrt(nu / (nu + 1.0)); /// right boundary for mode / μ
    double guess = 0.5 * mu * (left + right);
    double root = 0;
    RandMath::findMin([this] (double x)
    {
        return -f(x);
    }, guess, root);
    return root;
}

double NoncentralTRand::Skewness() const
{
    if (nu <= 3)
        return NAN;
    double mean = Mean();
    double var = nu * (1.0 + mu * mu) / (nu - 2) + mean * mean;
    double thirdMoment = std::lgamma(0.5 * nu - 1.5);
    thirdMoment -= T.Y.GetLogGammaFunction();
    thirdMoment += 1.5 * (nuCoefs.logHalfNu + M_LN2);
    thirdMoment = 0.25 * std::exp(thirdMoment);
    thirdMoment *= mu * (3.0 + mu * mu);
    double denominator = std::pow(var, 1.5);
    return (thirdMoment - 3 * mean * var - std::pow(mean, 3)) / denominator;
}

double NoncentralTRand::ExcessKurtosis() const
{
    if (nu <= 4)
        return (nu > 2) ? INFINITY : NAN;
    double fourthMoment = nu * nu / ((nu - 2) * (nu - 4));
    fourthMoment *= (std::pow(mu, 4) + 6 * mu * mu + 3);
    double thirdMoment = std::lgamma(0.5 * nu - 1.5);
    thirdMoment -= T.Y.GetLogGammaFunction();
    thirdMoment += 1.5 * (nuCoefs.logHalfNu + M_LN2);
    thirdMoment = 0.25 * std::exp(thirdMoment);
    thirdMoment *= mu * (3.0 + mu * mu);
    double mean = Mean();
    double kurtosis = fourthMoment - 4 * mean * thirdMoment;
    kurtosis += 6 * mean * mean * nu * (1.0 + mu * mu) / (nu - 2);
    kurtosis -= 3 * std::pow(mean, 4);
    double var = Variance();
    return kurtosis / (var * var);
}
<|endoftext|>


<commit_before>/*
* this file is part of the oxygen gtk engine
* Copyright (c) 2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
*
* inspired notably from kdelibs/kdeui/color/kcolorutils.h
* Copyright (C) 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
* Copyright (C) 2007 Zack Rusin <zack@kde.org>
*
* This  library is free  software; you can  redistribute it and/or
* modify it  under  the terms  of the  GNU Lesser  General  Public
* License  as published  by the Free  Software  Foundation; either
* version 2 of the License, or( at your option ) any later version.
*
* This library is distributed  in the hope that it will be useful,
* but  WITHOUT ANY WARRANTY; without even  the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License  along  with  this library;  if not,  write to  the Free
* Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

#include "oxygenapplicationname.h"
#include "oxygengtkutils.h"
#include "config.h"

#include <iostream>

namespace Oxygen
{

    //__________________________________________________________________________
    void ApplicationName::parse( const std::string& appName )
    {

        #if OXYGEN_DEBUG
        std::cout << "ApplicationName::parse - " << appName << std::endl;
        #endif

        if( appName.find("firefox") == 0 ) _name = Firefox;
        else if( appName.find("xulrunner") == 0 ) _name = Xul;
        else if( appName.find("thunderbird") == 0 ) _name = Thunderbird;
        else if( appName.find("seamonkey" ) == 0 ) _name = Seamonkey;
        else if( appName == "soffice" ) _name = OpenOffice;
        else if( appName == "gimp" ) _name = Gimp;
        else if( appName == "chromium" || appName == "chromium-browser" || appName == "google-chrome" ) _name = GoogleChrome;
        else _name = Unknown;
    }

    //__________________________________________________________________________
    bool ApplicationName::isMozilla( GtkWidget* widget ) const
    {
        if( !isMozilla() ) return false;

        GtkWidget* parent( gtk_widget_get_toplevel( widget ) );

        #if OXYGEN_DEBUG
        if( parent ) std::cout << "Oxygen::ApplicationName::isMozilla - parent: " << G_OBJECT_TYPE_NAME( widget ) << std::endl;
        #endif

        if( parent && (
            Gtk::gtk_object_is_a( G_OBJECT( parent ), "GtkFileChooserDialog" ) ||
            Gtk::gtk_object_is_a( G_OBJECT( parent ), "GtkPrintUnixDialog" ) ) )
            { return false; }

        return true;

    }

}
<commit_msg>store mozilla's native widgets in const char* array, for simplicity.<commit_after>/*
* this file is part of the oxygen gtk engine
* Copyright (c) 2010 Hugo Pereira Da Costa <hugo@oxygen-icons.org>
*
* inspired notably from kdelibs/kdeui/color/kcolorutils.h
* Copyright (C) 2007 Matthew Woehlke <mw_triad@users.sourceforge.net>
* Copyright (C) 2007 Thomas Zander <zander@kde.org>
* Copyright (C) 2007 Zack Rusin <zack@kde.org>
*
* This  library is free  software; you can  redistribute it and/or
* modify it  under  the terms  of the  GNU Lesser  General  Public
* License  as published  by the Free  Software  Foundation; either
* version 2 of the License, or( at your option ) any later version.
*
* This library is distributed  in the hope that it will be useful,
* but  WITHOUT ANY WARRANTY; without even  the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License  along  with  this library;  if not,  write to  the Free
* Software Foundation, Inc., 51  Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

#include "oxygenapplicationname.h"
#include "oxygengtkutils.h"
#include "config.h"

#include <iostream>

namespace Oxygen
{

    //__________________________________________________________________________
    void ApplicationName::parse( const std::string& appName )
    {

        #if OXYGEN_DEBUG
        std::cout << "ApplicationName::parse - " << appName << std::endl;
        #endif

        if( appName.find("firefox") == 0 ) _name = Firefox;
        else if( appName.find("xulrunner") == 0 ) _name = Xul;
        else if( appName.find("thunderbird") == 0 ) _name = Thunderbird;
        else if( appName.find("seamonkey" ) == 0 ) _name = Seamonkey;
        else if( appName == "soffice" ) _name = OpenOffice;
        else if( appName == "gimp" ) _name = Gimp;
        else if( appName == "chromium" || appName == "chromium-browser" || appName == "google-chrome" ) _name = GoogleChrome;
        else _name = Unknown;
    }

    //__________________________________________________________________________
    bool ApplicationName::isMozilla( GtkWidget* widget ) const
    {
        if( !isMozilla() ) return false;

        // store class names for which native gtk widgets are used
        static const char* classNames[] =
        {
            "GtkFileChooserDialog",
            "GtkPrintUnixDialog",
            0
        };

        GtkWidget* parent( gtk_widget_get_toplevel( widget ) );

        #if OXYGEN_DEBUG
        if( parent ) std::cout << "Oxygen::ApplicationName::isMozilla - parent: " << G_OBJECT_TYPE_NAME( widget ) << std::endl;
        #endif

        // check parent
        if( !parent ) return true;

        // loop over class names
        for( unsigned int i = 0; classNames[i]; ++i )
        { if( Gtk::gtk_object_is_a( G_OBJECT( parent ), classNames[i] ) ) return false; }

        return true;

    }

}
<|endoftext|>


<commit_before>// Copyright 2013 Velodyne Acoustics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    PacketFileSender.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
// .NAME PacketFileSender -
// .SECTION Description
// This program reads a pcap file and sends the packets using UDP.

#include "vtkPacketFileReader.h"
#include "vvPacketSender.h"

#include <string>
#include <cstdlib>
#include <iostream>

#include <boost/thread/thread.hpp>

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

  if (argc != 2) {
    std::cout << "Usage: " << argv[0] << " <packet file>" << std::endl;
    return 1;
  }
  std::string filename(argv[1]);

  try
    {
    std::string destinationIp = "127.0.0.1";
    int dataPort = 2368;
    vvPacketSender sender(filename, destinationIp, dataPort);
    //socket.connect(destinationEndpoint);

    while (!sender.done())
      {
      sender.pumpPacket();
      if ((sender.packetCount() % 500) == 0)
        {
        printf("total sent packets: %lu\n", sender.packetCount());
        }

      boost::this_thread::sleep(boost::posix_time::microseconds(200));
      }
    }
  catch( std::exception & e )
    {
    std::cout << "Caught Exception: " << e.what() << std::endl;
    return 1;
    }

  return 0;
}
<commit_msg>Enable packet file sender to operator in a loop<commit_after>// Copyright 2013 Velodyne Acoustics, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    PacketFileSender.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
// .NAME PacketFileSender -
// .SECTION Description
// This program reads a pcap file and sends the packets using UDP.

#include "vtkPacketFileReader.h"
#include "vvPacketSender.h"

#include <string>
#include <cstdlib>
#include <iostream>

#include <boost/thread/thread.hpp>

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

  if (argc < 2) {
    std::cout << "Usage: " << argv[0] << " <packet file> [loop]" << std::endl;
    return 1;
  }
  std::string filename(argv[1]);

  int loop = 0;
  if(argc > 2)
    {
    loop = atoi(argv[2]);
    }

  try
    {
    std::string destinationIp = "127.0.0.1";
    int dataPort = 2368;
    do
      {
      vvPacketSender sender(filename, destinationIp, dataPort);
      //socket.connect(destinationEndpoint);

      while (!sender.done())
        {
        sender.pumpPacket();
        if ((sender.packetCount() % 500) == 0)
          {
          printf("total sent packets: %lu\n", sender.packetCount());
          }

        boost::this_thread::sleep(boost::posix_time::microseconds(200));
        }
      } while(loop);
    }
  catch( std::exception & e )
    {
    std::cout << "Caught Exception: " << e.what() << std::endl;
    return 1;
    }

  return 0;
}
<|endoftext|>


<commit_before>/*

Copyright (c) 2006, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef NODE_HPP
#define NODE_HPP

#include <algorithm>
#include <map>
#include <set>

#include <libtorrent/config.hpp>
#include <libtorrent/kademlia/routing_table.hpp>
#include <libtorrent/kademlia/rpc_manager.hpp>
#include <libtorrent/kademlia/node_id.hpp>
#include <libtorrent/kademlia/msg.hpp>
#include <libtorrent/kademlia/find_data.hpp>

#include <libtorrent/io.hpp>
#include <libtorrent/session_settings.hpp>
#include <libtorrent/assert.hpp>
#include <libtorrent/thread.hpp>
#include <libtorrent/bloom_filter.hpp>

#include <boost/cstdint.hpp>
#include <boost/ref.hpp>

#include "libtorrent/socket.hpp"

namespace libtorrent {
	class alert_manager;
	struct alert_dispatcher;
}

namespace libtorrent { namespace dht
{

#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_DECLARE_LOG(node);
#endif

struct traversal_algorithm;

struct key_desc_t
{
	char const* name;
	int type;
	int size;
	int flags;

	enum {
		// this argument is optional, parsing will not
		// fail if it's not present
		optional = 1,
		// for dictionaries, the following entries refer
		// to child nodes to this node, up until and including
		// the next item that has the last_child flag set.
		// these flags are nestable
		parse_children = 2,
		// this is the last item in a child dictionary
		last_child = 4,
		// the size argument refers to that the size
		// has to be divisible by the number, instead
		// of having that exact size
		size_divisible = 8
	};
};

bool TORRENT_EXTRA_EXPORT verify_message(lazy_entry const* msg, key_desc_t const desc[]
	, lazy_entry const* ret[], int size , char* error, int error_size);

// this is the entry for every peer
// the timestamp is there to make it possible
// to remove stale peers
struct peer_entry
{
	tcp::endpoint addr;
	ptime added;
	bool seed;
};

// this is a group. It contains a set of group members
struct torrent_entry
{
	std::string name;
	std::set<peer_entry> peers;
};

struct dht_immutable_item
{
	dht_immutable_item() : value(0), num_announcers(0) {}
	// malloced space for the actual value
	char* value;
	// this counts the number of IPs we have seen
	// announcing this item, this is used to determine
	// popularity if we reach the limit of items to store
	bloom_filter<128> ips;
	// the last time we heard about this
	ptime last_seen;
	// number of IPs in the bloom filter
	int num_announcers;
	// size of malloced space pointed to by value
	int size;
};

struct dht_mutable_item : dht_immutable_item
{
	char sig[256];
	int seq;
};

struct rsa_key { char bytes[268]; };

inline bool operator<(rsa_key const& lhs, rsa_key const& rhs)
{
	return memcmp(lhs.bytes, rhs.bytes, sizeof(lhs.bytes)) < 0;
}

inline bool operator<(peer_entry const& lhs, peer_entry const& rhs)
{
	return lhs.addr.address() == rhs.addr.address()
		? lhs.addr.port() < rhs.addr.port()
		: lhs.addr.address() < rhs.addr.address();
}

struct null_type {};

class announce_observer : public observer
{
public:
	announce_observer(boost::intrusive_ptr<traversal_algorithm> const& algo
		, udp::endpoint const& ep, node_id const& id)
		: observer(algo, ep, id)
	{}

	void reply(msg const&) { flags |= flag_done; }
};

struct count_peers
{
	int& count;
	count_peers(int& c): count(c) {}
	void operator()(std::pair<libtorrent::dht::node_id
		, libtorrent::dht::torrent_entry> const& t)
	{
		count += t.second.peers.size();
	}
};

struct udp_socket_interface
{
	virtual bool send_packet(entry& e, udp::endpoint const& addr, int flags) = 0;
};

class TORRENT_EXTRA_EXPORT node_impl : boost::noncopyable
{
typedef std::map<node_id, torrent_entry> table_t;
typedef std::map<node_id, dht_immutable_item> dht_immutable_table_t;
typedef std::map<rsa_key, dht_mutable_item> dht_mutable_table_t;

public:
	node_impl(alert_dispatcher* alert_disp, udp_socket_interface* sock
		, dht_settings const& settings, node_id nid, address const& external_address
		, dht_observer* observer);

	virtual ~node_impl() {}

	void tick();
	void refresh(node_id const& id, find_data::nodes_callback const& f);
	void bootstrap(std::vector<udp::endpoint> const& nodes
		, find_data::nodes_callback const& f);
	void add_router_node(udp::endpoint router);

	void unreachable(udp::endpoint const& ep);
	void incoming(msg const& m);

	int num_torrents() const { return m_map.size(); }
	int num_peers() const
	{
		int ret = 0;
		std::for_each(m_map.begin(), m_map.end(), count_peers(ret));
		return ret;
	}

	int bucket_size(int bucket);

	node_id const& nid() const { return m_id; }

	boost::tuple<int, int> size() const{ return m_table.size(); }
	size_type num_global_nodes() const
	{ return m_table.num_global_nodes(); }

	int data_size() const { return int(m_map.size()); }

#ifdef TORRENT_DHT_VERBOSE_LOGGING
	void print_state(std::ostream& os) const
	{ m_table.print_state(os); }
#endif

	void announce(sha1_hash const& info_hash, int listen_port, bool seed
		, boost::function<void(std::vector<tcp::endpoint> const&)> f);

	bool verify_token(std::string const& token, char const* info_hash
		, udp::endpoint const& addr);

	std::string generate_token(udp::endpoint const& addr, char const* info_hash);

	// the returned time is the delay until connection_timeout()
	// should be called again the next time
	time_duration connection_timeout();

	// generates a new secret number used to generate write tokens
	void new_write_key();

	// pings the given node, and adds it to
	// the routing table if it respons and if the
	// bucket is not full.
	void add_node(udp::endpoint node);

	void replacement_cache(bucket_t& nodes) const
	{ m_table.replacement_cache(nodes); }

	int branch_factor() const { return m_settings.search_branching; }

	void add_traversal_algorithm(traversal_algorithm* a)
	{
		mutex_t::scoped_lock l(m_mutex);
		m_running_requests.insert(a);
	}

	void remove_traversal_algorithm(traversal_algorithm* a)
	{
		mutex_t::scoped_lock l(m_mutex);
		m_running_requests.erase(a);
	}

	void status(libtorrent::session_status& s);

	dht_settings const& settings() const { return m_settings; }

protected:

	void lookup_peers(sha1_hash const& info_hash, int prefix, entry& reply
		, bool noseed, bool scrape) const;
	bool lookup_torrents(sha1_hash const& target, entry& reply
		, char* tags) const;

	dht_settings const& m_settings;

private:
	typedef libtorrent::mutex mutex_t;
	mutex_t m_mutex;

	// this list must be destructed after the rpc manager
	// since it might have references to it
	std::set<traversal_algorithm*> m_running_requests;

	void incoming_request(msg const& h, entry& e);

	node_id m_id;

public:
	routing_table m_table;
	rpc_manager m_rpc;

private:
	table_t m_map;
	dht_immutable_table_t m_immutable_table;
	dht_mutable_table_t m_mutable_table;

	ptime m_last_tracker_tick;

	// secret random numbers used to create write tokens
	int m_secret[2];

	alert_dispatcher* m_post_alert;
	udp_socket_interface* m_sock;
};

} } // namespace libtorrent::dht

#endif // NODE_HPP

<commit_msg>one more uninitialized member<commit_after>/*

Copyright (c) 2006, Arvid Norberg
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in
      the documentation and/or other materials provided with the distribution.
    * Neither the name of the author nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

*/

#ifndef NODE_HPP
#define NODE_HPP

#include <algorithm>
#include <map>
#include <set>

#include <libtorrent/config.hpp>
#include <libtorrent/kademlia/routing_table.hpp>
#include <libtorrent/kademlia/rpc_manager.hpp>
#include <libtorrent/kademlia/node_id.hpp>
#include <libtorrent/kademlia/msg.hpp>
#include <libtorrent/kademlia/find_data.hpp>

#include <libtorrent/io.hpp>
#include <libtorrent/session_settings.hpp>
#include <libtorrent/assert.hpp>
#include <libtorrent/thread.hpp>
#include <libtorrent/bloom_filter.hpp>

#include <boost/cstdint.hpp>
#include <boost/ref.hpp>

#include "libtorrent/socket.hpp"

namespace libtorrent {
	class alert_manager;
	struct alert_dispatcher;
}

namespace libtorrent { namespace dht
{

#ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_DECLARE_LOG(node);
#endif

struct traversal_algorithm;

struct key_desc_t
{
	char const* name;
	int type;
	int size;
	int flags;

	enum {
		// this argument is optional, parsing will not
		// fail if it's not present
		optional = 1,
		// for dictionaries, the following entries refer
		// to child nodes to this node, up until and including
		// the next item that has the last_child flag set.
		// these flags are nestable
		parse_children = 2,
		// this is the last item in a child dictionary
		last_child = 4,
		// the size argument refers to that the size
		// has to be divisible by the number, instead
		// of having that exact size
		size_divisible = 8
	};
};

bool TORRENT_EXTRA_EXPORT verify_message(lazy_entry const* msg, key_desc_t const desc[]
	, lazy_entry const* ret[], int size , char* error, int error_size);

// this is the entry for every peer
// the timestamp is there to make it possible
// to remove stale peers
struct peer_entry
{
	tcp::endpoint addr;
	ptime added;
	bool seed;
};

// this is a group. It contains a set of group members
struct torrent_entry
{
	std::string name;
	std::set<peer_entry> peers;
};

struct dht_immutable_item
{
	dht_immutable_item() : value(0), num_announcers(0), size(0) {}
	// malloced space for the actual value
	char* value;
	// this counts the number of IPs we have seen
	// announcing this item, this is used to determine
	// popularity if we reach the limit of items to store
	bloom_filter<128> ips;
	// the last time we heard about this
	ptime last_seen;
	// number of IPs in the bloom filter
	int num_announcers;
	// size of malloced space pointed to by value
	int size;
};

struct dht_mutable_item : dht_immutable_item
{
	char sig[256];
	int seq;
};

struct rsa_key { char bytes[268]; };

inline bool operator<(rsa_key const& lhs, rsa_key const& rhs)
{
	return memcmp(lhs.bytes, rhs.bytes, sizeof(lhs.bytes)) < 0;
}

inline bool operator<(peer_entry const& lhs, peer_entry const& rhs)
{
	return lhs.addr.address() == rhs.addr.address()
		? lhs.addr.port() < rhs.addr.port()
		: lhs.addr.address() < rhs.addr.address();
}

struct null_type {};

class announce_observer : public observer
{
public:
	announce_observer(boost::intrusive_ptr<traversal_algorithm> const& algo
		, udp::endpoint const& ep, node_id const& id)
		: observer(algo, ep, id)
	{}

	void reply(msg const&) { flags |= flag_done; }
};

struct count_peers
{
	int& count;
	count_peers(int& c): count(c) {}
	void operator()(std::pair<libtorrent::dht::node_id
		, libtorrent::dht::torrent_entry> const& t)
	{
		count += t.second.peers.size();
	}
};

struct udp_socket_interface
{
	virtual bool send_packet(entry& e, udp::endpoint const& addr, int flags) = 0;
};

class TORRENT_EXTRA_EXPORT node_impl : boost::noncopyable
{
typedef std::map<node_id, torrent_entry> table_t;
typedef std::map<node_id, dht_immutable_item> dht_immutable_table_t;
typedef std::map<rsa_key, dht_mutable_item> dht_mutable_table_t;

public:
	node_impl(alert_dispatcher* alert_disp, udp_socket_interface* sock
		, dht_settings const& settings, node_id nid, address const& external_address
		, dht_observer* observer);

	virtual ~node_impl() {}

	void tick();
	void refresh(node_id const& id, find_data::nodes_callback const& f);
	void bootstrap(std::vector<udp::endpoint> const& nodes
		, find_data::nodes_callback const& f);
	void add_router_node(udp::endpoint router);

	void unreachable(udp::endpoint const& ep);
	void incoming(msg const& m);

	int num_torrents() const { return m_map.size(); }
	int num_peers() const
	{
		int ret = 0;
		std::for_each(m_map.begin(), m_map.end(), count_peers(ret));
		return ret;
	}

	int bucket_size(int bucket);

	node_id const& nid() const { return m_id; }

	boost::tuple<int, int> size() const{ return m_table.size(); }
	size_type num_global_nodes() const
	{ return m_table.num_global_nodes(); }

	int data_size() const { return int(m_map.size()); }

#ifdef TORRENT_DHT_VERBOSE_LOGGING
	void print_state(std::ostream& os) const
	{ m_table.print_state(os); }
#endif

	void announce(sha1_hash const& info_hash, int listen_port, bool seed
		, boost::function<void(std::vector<tcp::endpoint> const&)> f);

	bool verify_token(std::string const& token, char const* info_hash
		, udp::endpoint const& addr);

	std::string generate_token(udp::endpoint const& addr, char const* info_hash);

	// the returned time is the delay until connection_timeout()
	// should be called again the next time
	time_duration connection_timeout();

	// generates a new secret number used to generate write tokens
	void new_write_key();

	// pings the given node, and adds it to
	// the routing table if it respons and if the
	// bucket is not full.
	void add_node(udp::endpoint node);

	void replacement_cache(bucket_t& nodes) const
	{ m_table.replacement_cache(nodes); }

	int branch_factor() const { return m_settings.search_branching; }

	void add_traversal_algorithm(traversal_algorithm* a)
	{
		mutex_t::scoped_lock l(m_mutex);
		m_running_requests.insert(a);
	}

	void remove_traversal_algorithm(traversal_algorithm* a)
	{
		mutex_t::scoped_lock l(m_mutex);
		m_running_requests.erase(a);
	}

	void status(libtorrent::session_status& s);

	dht_settings const& settings() const { return m_settings; }

protected:

	void lookup_peers(sha1_hash const& info_hash, int prefix, entry& reply
		, bool noseed, bool scrape) const;
	bool lookup_torrents(sha1_hash const& target, entry& reply
		, char* tags) const;

	dht_settings const& m_settings;

private:
	typedef libtorrent::mutex mutex_t;
	mutex_t m_mutex;

	// this list must be destructed after the rpc manager
	// since it might have references to it
	std::set<traversal_algorithm*> m_running_requests;

	void incoming_request(msg const& h, entry& e);

	node_id m_id;

public:
	routing_table m_table;
	rpc_manager m_rpc;

private:
	table_t m_map;
	dht_immutable_table_t m_immutable_table;
	dht_mutable_table_t m_mutable_table;

	ptime m_last_tracker_tick;

	// secret random numbers used to create write tokens
	int m_secret[2];

	alert_dispatcher* m_post_alert;
	udp_socket_interface* m_sock;
};

} } // namespace libtorrent::dht

#endif // NODE_HPP

<|endoftext|>


<commit_before>/*!
 * \file engine_robust.cc
 * \brief Robust implementation of AllReduce
 * \author Tianqi, Nacho, Tianyi
 */
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#define NOMINMAX
#include "./utils.h"
#include "./engine_robust.h"

namespace engine {
/*!
 * \brief perform in-place allreduce, on sendrecvbuf
 *        this function is NOT thread-safe
 * \param sendrecvbuf_ buffer for both sending and recving data
 * \param type_nbytes the unit number of bytes the type have
 * \param count number of elements to be reduced
 * \param reducer reduce function
 */
void AllReduceRobust::AllReduce(void *sendrecvbuf_,
                                size_t type_nbytes,
                                size_t count,
                                ReduceFunction reducer) {
  while (true) {
    ReturnType ret = TryAllReduce(sendrecvbuf_, type_nbytes, count, reducer);
    if (ret == kSuccess) return;
    if (ret == kSockError) {
      utils::Error("error occur during all reduce\n");
    }
    utils::LogPrintf("[%d] receive except signal, start reset link\n", rank);
    TryResetLinks();
  }
  // TODO
}
/*!
 * \brief broadcast data from root to all nodes
 * \param sendrecvbuf_ buffer for both sending and recving data
 * \param size the size of the data to be broadcasted
 * \param root the root worker id to broadcast the data
 */
void AllReduceRobust::Broadcast(void *sendrecvbuf_, size_t total_size, int root) {
  utils::Assert(TryBroadcast(sendrecvbuf_, total_size, root) == kSuccess,
                "AllReduce failed");
  // TODO
}
/*!
 * \brief load latest check point
 * \param p_model pointer to the model
 * \return true if there was stored checkpoint and load was successful
 *   false if there was no stored checkpoint, means we are start over gain
 */
bool AllReduceRobust::LoadCheckPoint(utils::ISerializable *p_model) {
  // TODO
  return false;
}
/*!
 * \brief checkpoint the model, meaning we finished a stage of execution
 * \param p_model pointer to the model
 */
void AllReduceRobust::CheckPoint(const utils::ISerializable &model) {
  // TODO
}
/*!
 * \brief reset the all the existing links by sending Out-of-Band message marker
 *  after this function finishes, all the messages received and sent before in all live links are discarded,
 *  This allows us to get a fresh start after error has happened
 *
 * \return this function can return kSuccess or kSockError
 *         when kSockError is returned, it simply means there are bad sockets in the links,
 *         and some link recovery proceduer is needed
 */
AllReduceRobust::ReturnType AllReduceRobust::TryResetLinks(void) {
  // number of links
  const int nlink = static_cast<int>(links.size());
  for (int i = 0; i < nlink; ++i) {
    links[i].InitBuffer(sizeof(int), 1 << 10, reduce_buffer_size);
    links[i].ResetSize();
  }
  // read and discard data from all channels until pass mark
  while (true) {
    for (int i = 0; i < nlink; ++i) {
      if (links[i].sock.BadSocket()) continue;
      if (links[i].size_write == 0) {
        char sig = kOOBReset;
        ssize_t len = links[i].sock.Send(&sig, sizeof(sig), MSG_OOB);
        // error will be filtered in next loop
        if (len == sizeof(sig)) links[i].size_write = 1;
      }
      if (links[i].size_write == 1) {
        char sig = kResetMark;
        ssize_t len = links[i].sock.Send(&sig, sizeof(sig));
        if (len == sizeof(sig)) links[i].size_write = 2;
      }
    }
    utils::SelectHelper rsel;
    bool finished = true;
    for (int i = 0; i < nlink; ++i) {
      if (links[i].size_write != 2 && !links[i].sock.BadSocket()) {
        rsel.WatchWrite(links[i].sock); finished = false;
      }
    }
    if (finished) break;
    // wait to read from the channels to discard data
    rsel.Select();
  }
  for (int i = 0; i < nlink; ++i) {
    if (!links[i].sock.BadSocket()) {
      utils::SelectHelper::WaitExcept(links[i].sock);
    }
  }
  while (true) {
    for (int i = 0; i < nlink; ++i) {
      if (links[i].size_read == 0) {
        int atmark = links[i].sock.AtMark();
        if (atmark < 0) {
          utils::Assert(links[i].sock.BadSocket(), "must already gone bad");
        } else if (atmark > 0) {
          links[i].size_read = 1;
        } else {
          // no at mark, read and discard data
          ssize_t len = links[i].sock.Recv(links[i].buffer_head, links[i].buffer_size);
          if (links[i].sock.AtMark()) links[i].size_read = 1;
          // zero length, remote closed the connection, close socket
          if (len == 0) links[i].sock.Close();
        }
      }
    }
    utils::SelectHelper rsel;
    bool finished = true;
    for (int i = 0; i < nlink; ++i) {
      if (links[i].size_read == 0 && !links[i].sock.BadSocket()) {
        rsel.WatchRead(links[i].sock); finished = false;
      }
    }
    if (finished) break;
    rsel.Select();
  }

  // start synchronization, use blocking I/O to avoid select
  for (int i = 0; i < nlink; ++i) {
    if (!links[i].sock.BadSocket()) {
      char oob_mark;
      links[i].sock.SetNonBlock(false);
      ssize_t len = links[i].sock.Recv(&oob_mark, sizeof(oob_mark), MSG_WAITALL);
      if (len == 0) {
        links[i].sock.Close(); continue;
      } else if (len > 0) {
        utils::Assert(oob_mark == kResetMark, "wrong oob msg");
        utils::Assert(links[i].sock.AtMark() != 1, "should already read past mark");
      } else {
        utils::Assert(errno != EAGAIN|| errno != EWOULDBLOCK, "BUG");
      }
      // send out ack
      char ack = kResetAck;
      while (true) {
        len = links[i].sock.Send(&ack, sizeof(ack));
        if (len == sizeof(ack)) break;
        if (len == -1) {
          if (errno != EAGAIN && errno != EWOULDBLOCK) break;
        }
      }
    }
  }
  // wait all ack
  for (int i = 0; i < nlink; ++i) {
    if (!links[i].sock.BadSocket()) {
      char ack;
      ssize_t len = links[i].sock.Recv(&ack, sizeof(ack), MSG_WAITALL);
      if (len == 0) {
        links[i].sock.Close(); continue;
      } else if (len > 0) {
        utils::Assert(ack == kResetAck, "wrong Ack MSG");
      } else {
        utils::Assert(errno != EAGAIN|| errno != EWOULDBLOCK, "BUG");
      }
      // set back to nonblock mode
      links[i].sock.SetNonBlock(true);
    }
  }
  for (int i = 0; i < nlink; ++i) {
    if (links[i].sock.BadSocket()) return kSockError;
  }
  return kSuccess;
}
/*!
 * \brief try to reconnect the broken links
 * \return this function can kSuccess or kSockError
 */
AllReduceRobust::ReturnType AllReduceRobust::TryReConnectLinks(void) {
  utils::Error("TryReConnectLinks: not implemented");
  return kSuccess;
}
/*!
 * \brief if err_type indicates an error
 *         recover links according to the error type reported
 *        if there is no error, return true
 * \param err_type the type of error happening in the system
 * \return true if err_type is kSuccess, false otherwise
 */
bool AllReduceRobust::CheckAndRecover(ReturnType err_type) {
  if (err_type == kSuccess) return true;
  while(err_type != kSuccess) {
    switch(err_type) {
      case kGetExcept: err_type = TryResetLinks(); break;
      case kSockError: {
        TryResetLinks();
        err_type = TryReConnectLinks();
        break;
      }
      default: utils::Assert(false, "RecoverLinks: cannot reach here");
    }
  }
  return false;
}
/*!
 * \brief try to load check point
 *
 *        This is a collaborative function called by all nodes
 *        only the nodes with requester set to true really needs to load the check point
 *        other nodes acts as collaborative roles to complete this request
 *
 * \param requester whether current node is the requester
 * \return this function can return kSuccess/kSockError/kGetExcept, see ReturnType for details
 * \sa ReturnType
 */
AllReduceRobust::ReturnType AllReduceRobust::TryLoadCheckPoint(bool requester) {
  utils::Error("TryLoadCheckPoint: not implemented");
  return kSuccess;
}
/*!
 * \brief try to get the result of operation specified by seqno
 *
 *        This is a collaborative function called by all nodes
 *        only the nodes with requester set to true really needs to get the result
 *        other nodes acts as collaborative roles to complete this request
 *
 * \param buf the buffer to store the result, this parameter is only use when current node is requester
 * \param size the total size of the buffer, this parameter is only use when current node is requester
 * \param seqno sequence number of the operation, this is unique index of a operation in current iteration
 * \param requester whether current node is the requester
 * \return this function can return kSuccess/kSockError/kGetExcept, see ReturnType for details
 * \sa ReturnType
 */
AllReduceRobust::ReturnType AllReduceRobust::TryGetResult(void *sendrecvbuf, size_t size, int seqno, bool requester) {
  utils::Error("TryGetResult: not implemented");
  return kSuccess;
}
/*!
 * \brief try to run recover execution for a request action described by flag and seqno,
 *        the function will keep blocking to run possible recovery operations before the specified action,
 *        until the requested result is received by a recovering procedure,
 *        or the function discovers that the requested action is not yet executed, and return false
 *
 * \param buf the buffer to store the result
 * \param size the total size of the buffer
 * \param flag flag information about the action \sa ActionSummary
 * \param seqno sequence number of the action, if it is special action with flag set, seqno needs to be set to ActionSummary::kMaxSeq
 *
 * \return if this function can return true or false
 *    - true means buf already set to the
 *           result by recovering procedure, the action is complete, no further action is needed
 *    - false means this is the lastest action that has not yet been executed, need to execute the action
 */
bool AllReduceRobust::RecoverExec(void *buf, size_t size, int flag, int seqno) {
  if (flag != 0) {
    utils::Assert(seqno == ActionSummary::kMaxSeq, "must only set seqno for normal operations");
  }
  // request
  ActionSummary req(flag, seqno);
  while (true) {
    // action
    ActionSummary act = req;
    // get the reduced action
    if (!CheckAndRecover(TryAllReduce(&act, sizeof(act), 1, ActionSummary::Reducer))) continue;
    if (act.check_ack()) {
      if (act.check_point()) {
        // if we also have check_point, do check point first
        utils::Assert(!act.diff_seq(),
                      "check ack & check pt  cannot occur together with normal ops");
        // if we requested checkpoint, we are free to go
        if (req.check_point()) return true;
      } else if (act.load_check()) {
        // if there is only check_ack and load_check, do load_check
        if (!CheckAndRecover(TryLoadCheckPoint(req.load_check()))) continue;
        // if requested load check, then misson complete
        if (req.load_check()) return true;
      } else {
        // there is no check point and no load check, execute check ack
        if (req.check_ack()) return true;
      }
      // if execute to this point
      // this means the action requested has not been completed
      // try next round
    } else {
      if (act.check_point()) {
        if (act.diff_seq()) {
          utils::Assert(act.min_seqno() != ActionSummary::kMaxSeq, "min seq bug");
          bool requester = req.min_seqno() == act.min_seqno();
          if (!CheckAndRecover(TryGetResult(buf, size, act.min_seqno(), requester))) continue;
          if (requester) return true;
        } else  {
          // no difference in seq no, means we are free to check point
          if (req.check_point()) return true;
        }
      } else {
        // no check point
        if (act.load_check()) {
          // load check have higher priority, do load_check
          if (!CheckAndRecover(TryLoadCheckPoint(req.load_check()))) continue;
          // if requested load check, then misson complete
          if (req.load_check()) return true;
        } else {
          // no special flags, no checkpoint, check ack, load_check
          utils::Assert(act.min_seqno() != ActionSummary::kMaxSeq, "min seq bug");
          if (act.diff_seq()) {
            bool requester = req.min_seqno() == act.min_seqno();
            if (!CheckAndRecover(TryGetResult(buf, size, act.min_seqno(), requester))) continue;
            if (requester) return true;
          } else {
            // all the request is same, this is most recent command that is yet to be executed
            return false;
          }
        }
      }
      // something is still incomplete try next round
    }
  }
  utils::Assert(false, "RecoverExec: should not reach here");
  return true;
}
}  // namespace engine
<commit_msg>inline<commit_after>/*!
 * \file engine_robust.cc
 * \brief Robust implementation of AllReduce
 * \author Tianqi, Nacho, Tianyi
 */
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_DEPRECATE
#define NOMINMAX
#include "./utils.h"
#include "./engine_robust.h"

namespace engine {
/*!
 * \brief perform in-place allreduce, on sendrecvbuf
 *        this function is NOT thread-safe
 * \param sendrecvbuf_ buffer for both sending and recving data
 * \param type_nbytes the unit number of bytes the type have
 * \param count number of elements to be reduced
 * \param reducer reduce function
 */
void AllReduceRobust::AllReduce(void *sendrecvbuf_,
                                size_t type_nbytes,
                                size_t count,
                                ReduceFunction reducer) {
  while (true) {
    ReturnType ret = TryAllReduce(sendrecvbuf_, type_nbytes, count, reducer);
    if (ret == kSuccess) return;
    if (ret == kSockError) {
      utils::Error("error occur during all reduce\n");
    }
    utils::LogPrintf("[%d] receive except signal, start reset link\n", rank);
    TryResetLinks();
  }
  // TODO
}
/*!
 * \brief broadcast data from root to all nodes
 * \param sendrecvbuf_ buffer for both sending and recving data
 * \param size the size of the data to be broadcasted
 * \param root the root worker id to broadcast the data
 */
void AllReduceRobust::Broadcast(void *sendrecvbuf_, size_t total_size, int root) {
  utils::Assert(TryBroadcast(sendrecvbuf_, total_size, root) == kSuccess,
                "AllReduce failed");
  // TODO
}
/*!
 * \brief load latest check point
 * \param p_model pointer to the model
 * \return true if there was stored checkpoint and load was successful
 *   false if there was no stored checkpoint, means we are start over gain
 */
bool AllReduceRobust::LoadCheckPoint(utils::ISerializable *p_model) {
  // TODO
  return false;
}
/*!
 * \brief checkpoint the model, meaning we finished a stage of execution
 * \param p_model pointer to the model
 */
void AllReduceRobust::CheckPoint(const utils::ISerializable &model) {
  // TODO
}
/*!
 * \brief reset the all the existing links by sending Out-of-Band message marker
 *  after this function finishes, all the messages received and sent before in all live links are discarded,
 *  This allows us to get a fresh start after error has happened
 *
 * \return this function can return kSuccess or kSockError
 *         when kSockError is returned, it simply means there are bad sockets in the links,
 *         and some link recovery proceduer is needed
 */
AllReduceRobust::ReturnType AllReduceRobust::TryResetLinks(void) {
  // number of links
  const int nlink = static_cast<int>(links.size());
  for (int i = 0; i < nlink; ++i) {
    links[i].InitBuffer(sizeof(int), 1 << 10, reduce_buffer_size);
    links[i].ResetSize();
  }
  // read and discard data from all channels until pass mark
  while (true) {
    for (int i = 0; i < nlink; ++i) {
      if (links[i].sock.BadSocket()) continue;
      if (links[i].size_write == 0) {
        char sig = kOOBReset;
        ssize_t len = links[i].sock.Send(&sig, sizeof(sig), MSG_OOB);
        // error will be filtered in next loop
        if (len == sizeof(sig)) links[i].size_write = 1;
      }
      if (links[i].size_write == 1) {
        char sig = kResetMark;
        ssize_t len = links[i].sock.Send(&sig, sizeof(sig));
        if (len == sizeof(sig)) links[i].size_write = 2;
      }
    }
    utils::SelectHelper rsel;
    bool finished = true;
    for (int i = 0; i < nlink; ++i) {
      if (links[i].size_write != 2 && !links[i].sock.BadSocket()) {
        rsel.WatchWrite(links[i].sock); finished = false;
      }
    }
    if (finished) break;
    // wait to read from the channels to discard data
    rsel.Select();
  }
  for (int i = 0; i < nlink; ++i) {
    if (!links[i].sock.BadSocket()) {
      utils::SelectHelper::WaitExcept(links[i].sock);
    }
  }
  while (true) {
    for (int i = 0; i < nlink; ++i) {
      if (links[i].size_read == 0) {
        int atmark = links[i].sock.AtMark();
        if (atmark < 0) {
          utils::Assert(links[i].sock.BadSocket(), "must already gone bad");
        } else if (atmark > 0) {
          links[i].size_read = 1;
        } else {
          // no at mark, read and discard data
          ssize_t len = links[i].sock.Recv(links[i].buffer_head, links[i].buffer_size);
          if (links[i].sock.AtMark()) links[i].size_read = 1;
          // zero length, remote closed the connection, close socket
          if (len == 0) links[i].sock.Close();
        }
      }
    }
    utils::SelectHelper rsel;
    bool finished = true;
    for (int i = 0; i < nlink; ++i) {
      if (links[i].size_read == 0 && !links[i].sock.BadSocket()) {
        rsel.WatchRead(links[i].sock); finished = false;
      }
    }
    if (finished) break;
    rsel.Select();
  }

  // start synchronization, use blocking I/O to avoid select
  for (int i = 0; i < nlink; ++i) {
    if (!links[i].sock.BadSocket()) {
      char oob_mark;
      links[i].sock.SetNonBlock(false);
      ssize_t len = links[i].sock.Recv(&oob_mark, sizeof(oob_mark), MSG_WAITALL);
      if (len == 0) {
        links[i].sock.Close(); continue;
      } else if (len > 0) {
        utils::Assert(oob_mark == kResetMark, "wrong oob msg");
        utils::Assert(links[i].sock.AtMark() != 1, "should already read past mark");
      } else {
        utils::Assert(errno != EAGAIN|| errno != EWOULDBLOCK, "BUG");
      }
      // send out ack
      char ack = kResetAck;
      while (true) {
        len = links[i].sock.Send(&ack, sizeof(ack));
        if (len == sizeof(ack)) break;
        if (len == -1) {
          if (errno != EAGAIN && errno != EWOULDBLOCK) break;
        }
      }
    }
  }
  // wait all ack
  for (int i = 0; i < nlink; ++i) {
    if (!links[i].sock.BadSocket()) {
      char ack;
      ssize_t len = links[i].sock.Recv(&ack, sizeof(ack), MSG_WAITALL);
      if (len == 0) {
        links[i].sock.Close(); continue;
      } else if (len > 0) {
        utils::Assert(ack == kResetAck, "wrong Ack MSG");
      } else {
        utils::Assert(errno != EAGAIN|| errno != EWOULDBLOCK, "BUG");
      }
      // set back to nonblock mode
      links[i].sock.SetNonBlock(true);
    }
  }
  for (int i = 0; i < nlink; ++i) {
    if (links[i].sock.BadSocket()) return kSockError;
  }
  return kSuccess;
}
/*!
 * \brief try to reconnect the broken links
 * \return this function can kSuccess or kSockError
 */
AllReduceRobust::ReturnType AllReduceRobust::TryReConnectLinks(void) {
  utils::Error("TryReConnectLinks: not implemented");
  return kSuccess;
}
/*!
 * \brief if err_type indicates an error
 *         recover links according to the error type reported
 *        if there is no error, return true
 * \param err_type the type of error happening in the system
 * \return true if err_type is kSuccess, false otherwise
 */
bool AllReduceRobust::CheckAndRecover(ReturnType err_type) {
  if (err_type == kSuccess) return true;
  while(err_type != kSuccess) {
    switch(err_type) {
      case kGetExcept: err_type = TryResetLinks(); break;
      case kSockError: {
        TryResetLinks();
        err_type = TryReConnectLinks();
        break;
      }
      default: utils::Assert(false, "RecoverLinks: cannot reach here");
    }
  }
  return false;
}
/*!
 * \brief try to load check point
 *
 *        This is a collaborative function called by all nodes
 *        only the nodes with requester set to true really needs to load the check point
 *        other nodes acts as collaborative roles to complete this request
 *
 * \param requester whether current node is the requester
 * \return this function can return kSuccess/kSockError/kGetExcept, see ReturnType for details
 * \sa ReturnType
 */
AllReduceRobust::ReturnType AllReduceRobust::TryLoadCheckPoint(bool requester) {
  utils::Error("TryLoadCheckPoint: not implemented");
  return kSuccess;
}
/*!
 * \brief try to get the result of operation specified by seqno
 *
 *        This is a collaborative function called by all nodes
 *        only the nodes with requester set to true really needs to get the result
 *        other nodes acts as collaborative roles to complete this request
 *
 * \param buf the buffer to store the result, this parameter is only use when current node is requester
 * \param size the total size of the buffer, this parameter is only use when current node is requester
 * \param seqno sequence number of the operation, this is unique index of a operation in current iteration
 * \param requester whether current node is the requester
 * \return this function can return kSuccess/kSockError/kGetExcept, see ReturnType for details
 * \sa ReturnType
 */
AllReduceRobust::ReturnType AllReduceRobust::TryGetResult(void *sendrecvbuf, size_t size, int seqno, bool requester) {
  utils::Error("TryGetResult: not implemented");
  return kSuccess;
}
/*!
 * \brief try to run recover execution for a request action described by flag and seqno,
 *        the function will keep blocking to run possible recovery operations before the specified action,
 *        until the requested result is received by a recovering procedure,
 *        or the function discovers that the requested action is not yet executed, and return false
 *
 * \param buf the buffer to store the result
 * \param size the total size of the buffer
 * \param flag flag information about the action \sa ActionSummary
 * \param seqno sequence number of the action, if it is special action with flag set,
 *              seqno needs to be set to ActionSummary::kMaxSeq
 *
 * \return if this function can return true or false
 *    - true means buf already set to the
 *           result by recovering procedure, the action is complete, no further action is needed
 *    - false means this is the lastest action that has not yet been executed, need to execute the action
 */
bool AllReduceRobust::RecoverExec(void *buf, size_t size, int flag, int seqno) {
  if (flag != 0) {
    utils::Assert(seqno == ActionSummary::kMaxSeq, "must only set seqno for normal operations");
  }
  // request
  ActionSummary req(flag, seqno);
  while (true) {
    // action
    ActionSummary act = req;
    // get the reduced action
    if (!CheckAndRecover(TryAllReduce(&act, sizeof(act), 1, ActionSummary::Reducer))) continue;
    if (act.check_ack()) {
      if (act.check_point()) {
        // if we also have check_point, do check point first
        utils::Assert(!act.diff_seq(),
                      "check ack & check pt  cannot occur together with normal ops");
        // if we requested checkpoint, we are free to go
        if (req.check_point()) return true;
      } else if (act.load_check()) {
        // if there is only check_ack and load_check, do load_check
        if (!CheckAndRecover(TryLoadCheckPoint(req.load_check()))) continue;
        // if requested load check, then misson complete
        if (req.load_check()) return true;
      } else {
        // there is no check point and no load check, execute check ack
        if (req.check_ack()) return true;
      }
      // if execute to this point
      // this means the action requested has not been completed
      // try next round
    } else {
      if (act.check_point()) {
        if (act.diff_seq()) {
          utils::Assert(act.min_seqno() != ActionSummary::kMaxSeq, "min seq bug");
          bool requester = req.min_seqno() == act.min_seqno();
          if (!CheckAndRecover(TryGetResult(buf, size, act.min_seqno(), requester))) continue;
          if (requester) return true;
        } else  {
          // no difference in seq no, means we are free to check point
          if (req.check_point()) return true;
        }
      } else {
        // no check point
        if (act.load_check()) {
          // load check have higher priority, do load_check
          if (!CheckAndRecover(TryLoadCheckPoint(req.load_check()))) continue;
          // if requested load check, then misson complete
          if (req.load_check()) return true;
        } else {
          // no special flags, no checkpoint, check ack, load_check
          utils::Assert(act.min_seqno() != ActionSummary::kMaxSeq, "min seq bug");
          if (act.diff_seq()) {
            bool requester = req.min_seqno() == act.min_seqno();
            if (!CheckAndRecover(TryGetResult(buf, size, act.min_seqno(), requester))) continue;
            if (requester) return true;
          } else {
            // all the request is same, this is most recent command that is yet to be executed
            return false;
          }
        }
      }
      // something is still incomplete try next round
    }
  }
  utils::Assert(false, "RecoverExec: should not reach here");
  return true;
}
}  // namespace engine
<|endoftext|>


<commit_before>/*
This file belongs to the LIBQIF library.
A Quantitative Information Flow C++ Toolkit Library.
Copyright (C) 2013  Universidad Nacional de Río Cuarto(National University of Río Cuarto).
Author: Martinelli Fernán - fmartinelli89@gmail.com - Universidad Nacional de Río Cuarto (Argentina)
LIBQIF Version: 1.0
Date: 12th Nov 2013
========================================================================
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

=========================================================================
*/
#include "tests_aux.h"
using namespace qif::lp;

// define a type-parametrized test case (https://code.google.com/p/googletest/wiki/AdvancedGuide)
template <typename eT>
class LinearProgramTest : public BaseTest<eT> {};

TYPED_TEST_CASE_P(LinearProgramTest);

TYPED_TEST_P(LinearProgramTest, Optimal) {
	typedef TypeParam eT;
	typedef Method m_t;
	typedef Status s_t;
	const bool is_rat = std::is_same<eT, rat>::value;

	// the default acceptance range is too string for linear programs, we need a more permissive mrd
	eT md =	def_max_diff<eT>();
	eT mrd = def_max_rel_diff<float>();		// always use the mrd for floats

	#ifdef QIF_USE_ORTOOLS
	std::vector<Solver> solvers = { Solver::INTERNAL, Solver::GLPK, Solver::GLOP, Solver::CLP };
	#else
	std::vector<Solver> solvers = { Solver::INTERNAL, Solver::GLPK };
	#endif

	for(m_t method : { m_t::SIMPLEX_PRIMAL, m_t::SIMPLEX_DUAL, m_t::INTERIOR }) {
	for(Solver solver : solvers) {
	for(bool presolve : { false, true }) {
		if(method == m_t::INTERIOR    && (presolve || solver == Solver::GLOP || solver == Solver::CLP)) continue; // interior: no presolver, no GLOP support, unstable with CLP
		if(solver == Solver::INTERNAL && (presolve || method != m_t::SIMPLEX_PRIMAL)                  ) continue; // internal solver: only simplex_primal/no presolve
		if(is_rat                     && solver != Solver::INTERNAL                                   ) continue; // rat: only internal solver

		LinearProgram<eT> lp;
		lp.method = method;
		lp.solver = solver;
		lp.presolve = presolve;

		lp.from_matrix(
			format_num<eT>("1 2; 3 1"),
			format_num<eT>("1 2"),
			format_num<eT>("0.6 0.5")
		);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(s_t::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(46)/100, lp.objective(), md, mrd);
		expect_mat(format_num<eT>("0.6; 0.2"), lp.solution(), md, mrd);

		lp.from_matrix(
			format_num<eT>("1 1 0; 0 1 1"),
			format_num<eT>("1 1"),
			format_num<eT>("1 2 -1")
		);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(s_t::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(2), lp.objective(), md, mrd);
		expect_mat(format_num<eT>("0; 1; 0"), lp.solution(), md, mrd);

		lp.maximize = false;
		lp.from_matrix(
			format_num<eT>("3 -4; 1 2; 1 0"),
			format_num<eT>("12 4 1"),
			format_num<eT>("3 4"),
			{ '<', '>', '>' }
		);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(s_t::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(9), lp.objective(), md, mrd);
		expect_mat(format_num<eT>("1; 1.5"), lp.solution(), md, mrd);

		lp.maximize = false;
		lp.from_matrix(
			format_num<eT>("1 2 2; 2 1 2; 2 2 1"),
			format_num<eT>("20 20 20"),
			format_num<eT>("-10 -12 -12")
		);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(s_t::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(-136), lp.objective(), md, mrd);
		expect_mat(format_num<eT>("4; 4; 4"), lp.solution(), md, mrd);

		lp.clear();
		lp.maximize = false;
		auto v = lp.make_var(-5, infinity<eT>());
		lp.make_con(0, 0);		// glpk needs at least one constraint, add dummy one
		lp.set_obj_coeff(v, 1);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(s_t::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(-5), lp.objective(), md, mrd);
		expect_mat(format_num<eT>("-5"), lp.solution(), md, mrd);
	}}}
}

TYPED_TEST_P(LinearProgramTest, Infeasible) {
	typedef TypeParam eT;
	typedef Method m_t;
	typedef Status s_t;
	const bool is_rat = std::is_same<eT, rat>::value;

	#ifdef QIF_USE_ORTOOLS
	std::vector<Solver> solvers = { Solver::INTERNAL, Solver::GLPK, Solver::GLOP, Solver::CLP };
	#else
	std::vector<Solver> solvers = { Solver::INTERNAL, Solver::GLPK };
	#endif

	for(m_t method : { m_t::SIMPLEX_PRIMAL, m_t::SIMPLEX_DUAL, m_t::INTERIOR }) {
	for(Solver solver : solvers) {
	for(bool presolve : { false, true }) {
		if(method == m_t::INTERIOR    && (presolve || solver == Solver::GLOP || solver == Solver::CLP)) continue; // interior: no presolver, no GLOP support, unstable with CLP
		if(solver == Solver::INTERNAL && (presolve || method != m_t::SIMPLEX_PRIMAL)                  ) continue; // internal solver: only simplex_primal/no presolve
		if(is_rat                     && solver != Solver::INTERNAL                                   ) continue; // rat: only internal solver

		LinearProgram<eT> lp;
		lp.method = method;
		lp.solver = solver;
		lp.presolve = presolve;

		s_t status = method == m_t::INTERIOR
				? s_t::INFEASIBLE_OR_UNBOUNDED	// sometimes we just know that the problem is infeasible OR unbounded
				: s_t::INFEASIBLE;

		lp.from_matrix(
			format_num<eT>("1; 1"),
			format_num<eT>("3 2"),
			format_num<eT>("1"),
			{ '>', '<' }
		);

		EXPECT_FALSE(lp.solve());
		EXPECT_EQ(status, lp.status);

		lp.from_matrix(
			format_num<eT>("1; -1"),
			format_num<eT>("3 -2"),
			format_num<eT>("4"),
			{ '>', '>' }
		);

		EXPECT_FALSE(lp.solve());
		EXPECT_EQ(status, lp.status);
	}}}
}

TYPED_TEST_P(LinearProgramTest, Unbounded) {
	typedef TypeParam eT;
	typedef Method m_t;
	typedef Status s_t;
	const bool is_rat = std::is_same<eT, rat>::value;

	#ifdef QIF_USE_ORTOOLS
	std::vector<Solver> solvers = { Solver::INTERNAL, Solver::GLPK, Solver::GLOP, Solver::CLP };
	#else
	std::vector<Solver> solvers = { Solver::INTERNAL, Solver::GLPK };
	#endif

	for(m_t method : { m_t::SIMPLEX_PRIMAL, m_t::SIMPLEX_DUAL, m_t::INTERIOR }) {
	for(Solver solver : solvers) {
	for(bool presolve : { false, true }) {
		if(method == m_t::INTERIOR    && (presolve || solver == Solver::GLOP || solver == Solver::CLP)) continue; // interior: no presolver, no GLOP support, unstable with CLP
		if(solver == Solver::INTERNAL && (presolve || method != m_t::SIMPLEX_PRIMAL)                  ) continue; // internal solver: only simplex_primal/no presolve
		if(is_rat                     && solver != Solver::INTERNAL                                   ) continue; // rat: only internal solver

		// EXTRA conditions only for unbounded
		if(solver == Solver::GLOP || solver == Solver::CLP) continue; // OR-tools/DUAL seems unstable with unbounded problems (TODO: investigae)

		LinearProgram<eT> lp;
		lp.method = method;
		lp.solver = solver;
		lp.presolve = presolve;

		s_t status = solver != Solver::GLPK || (method == m_t::SIMPLEX_PRIMAL && !presolve)
				? s_t::UNBOUNDED
				: s_t::INFEASIBLE_OR_UNBOUNDED;	// sometimes we just know that the problem is infeasible OR unbounded

		lp.maximize = false;
		lp.from_matrix(
			format_num<eT>("1"),
			format_num<eT>("2"),
			format_num<eT>("-1"),
			{ '>' }
		);

		EXPECT_FALSE(lp.solve());
		EXPECT_EQ(status, lp.status);
	}}}
}

REGISTER_TYPED_TEST_CASE_P(LinearProgramTest, Optimal, Infeasible, Unbounded);

INSTANTIATE_TYPED_TEST_CASE_P(LinearProgram, LinearProgramTest, AllTypes);

<commit_msg>LinearProgram: tests: code cleanup<commit_after>#include "tests_aux.h"
using namespace qif::lp;

// define a type-parametrized test case (https://code.google.com/p/googletest/wiki/AdvancedGuide)
template <typename eT>
class LinearProgramTest : public BaseTest<eT> {
	public:
		void SetUp() override {
			// construct method/solver/presolve combinations to test
			#ifdef QIF_USE_ORTOOLS
			auto solvers = { Solver::INTERNAL, Solver::GLPK, Solver::GLOP, Solver::CLP };
			#else
			auto solvers = { Solver::INTERNAL, Solver::GLPK };
			#endif

			for(Method method : { Method::SIMPLEX_PRIMAL, Method::SIMPLEX_DUAL, Method::INTERIOR }) {
			for(Solver solver : solvers) {
			for(bool presolve : { false, true }) {
				// some combinations are not valid
				if(method == Method::INTERIOR && (presolve || solver == Solver::GLOP || solver == Solver::CLP)) continue; // interior: no presolver, no GLOP support, unstable with CLP
				if(solver == Solver::INTERNAL && (presolve || method != Method::SIMPLEX_PRIMAL)               ) continue; // internal solver: only simplex_primal/no presolve
				if(is_rat                     && solver != Solver::INTERNAL                                   ) continue; // rat: only internal solver

				combs.push_back(std::make_tuple(method, solver, presolve));
			}}}
		}
		const bool is_rat = std::is_same<eT, rat>::value;
		std::vector<std::tuple<Method,Solver,bool>> combs;
};

TYPED_TEST_CASE_P(LinearProgramTest);

TYPED_TEST_P(LinearProgramTest, Optimal) {
	typedef TypeParam eT;
	LinearProgramTest<eT>& t = *this;

	// the default acceptance range is too string for linear programs, we need a more permissive mrd
	eT md =	def_max_diff<eT>();
	eT mrd = def_max_rel_diff<float>();		// always use the mrd for floats

	for(auto comb : t.combs) {

		LinearProgram<eT> lp;
		std::tie(lp.method, lp.solver, lp.presolve) = comb;

		lp.from_matrix(
			format_num<eT>("1 2; 3 1"),
			format_num<eT>("1 2"),
			format_num<eT>("0.6 0.5")
		);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(Status::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(46)/100, lp.objective(), md, mrd);
		expect_mat(format_num<eT>("0.6; 0.2"), lp.solution(), md, mrd);

		lp.from_matrix(
			format_num<eT>("1 1 0; 0 1 1"),
			format_num<eT>("1 1"),
			format_num<eT>("1 2 -1")
		);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(Status::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(2), lp.objective(), md, mrd);
		expect_mat(format_num<eT>("0; 1; 0"), lp.solution(), md, mrd);

		lp.maximize = false;
		lp.from_matrix(
			format_num<eT>("3 -4; 1 2; 1 0"),
			format_num<eT>("12 4 1"),
			format_num<eT>("3 4"),
			{ '<', '>', '>' }
		);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(Status::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(9), lp.objective(), md, mrd);
		expect_mat(format_num<eT>("1; 1.5"), lp.solution(), md, mrd);

		lp.maximize = false;
		lp.from_matrix(
			format_num<eT>("1 2 2; 2 1 2; 2 2 1"),
			format_num<eT>("20 20 20"),
			format_num<eT>("-10 -12 -12")
		);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(Status::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(-136), lp.objective(), md, mrd);
		expect_mat(format_num<eT>("4; 4; 4"), lp.solution(), md, mrd);

		lp.clear();
		lp.maximize = false;
		auto v = lp.make_var(-5, infinity<eT>());
		lp.make_con(0, 0);		// glpk needs at least one constraint, add dummy one
		lp.set_obj_coeff(v, 1);

		EXPECT_TRUE(lp.solve());
		EXPECT_EQ(Status::OPTIMAL, lp.status);
		EXPECT_PRED_FORMAT4(equal4<eT>, eT(-5), lp.objective(), md, mrd);
		expect_mat(format_num<eT>("-5"), lp.solution(), md, mrd);
	}
}

TYPED_TEST_P(LinearProgramTest, Infeasible) {
	typedef TypeParam eT;
	LinearProgramTest<eT>& t = *this;

	for(auto comb : t.combs) {

		LinearProgram<eT> lp;
		std::tie(lp.method, lp.solver, lp.presolve) = comb;

		Status status = lp.method == Method::INTERIOR
				? Status::INFEASIBLE_OR_UNBOUNDED	// sometimes we just know that the problem is infeasible OR unbounded
				: Status::INFEASIBLE;

		lp.from_matrix(
			format_num<eT>("1; 1"),
			format_num<eT>("3 2"),
			format_num<eT>("1"),
			{ '>', '<' }
		);

		EXPECT_FALSE(lp.solve());
		EXPECT_EQ(status, lp.status);

		lp.from_matrix(
			format_num<eT>("1; -1"),
			format_num<eT>("3 -2"),
			format_num<eT>("4"),
			{ '>', '>' }
		);

		EXPECT_FALSE(lp.solve());
		EXPECT_EQ(status, lp.status);
	}
}

TYPED_TEST_P(LinearProgramTest, Unbounded) {
	typedef TypeParam eT;
	LinearProgramTest<eT>& t = *this;

	for(auto comb : t.combs) {

		LinearProgram<eT> lp;
		std::tie(lp.method, lp.solver, lp.presolve) = comb;

		// EXTRA conditions only for unbounded
		if(lp.solver == Solver::GLOP || lp.solver == Solver::CLP) continue; // OR-tools/DUAL seems unstable with unbounded problems (TODO: investigae)

		Status status = lp.solver != Solver::GLPK || (lp.method == Method::SIMPLEX_PRIMAL && !lp.presolve)
				? Status::UNBOUNDED
				: Status::INFEASIBLE_OR_UNBOUNDED;	// sometimes we just know that the problem is infeasible OR unbounded

		lp.maximize = false;
		lp.from_matrix(
			format_num<eT>("1"),
			format_num<eT>("2"),
			format_num<eT>("-1"),
			{ '>' }
		);

		EXPECT_FALSE(lp.solve());
		EXPECT_EQ(status, lp.status);
	}
}

REGISTER_TYPED_TEST_CASE_P(LinearProgramTest, Optimal, Infeasible, Unbounded);

INSTANTIATE_TYPED_TEST_CASE_P(LinearProgram, LinearProgramTest, AllTypes);

<|endoftext|>