<commit_before>/* ************************************************************************* */
/* This file is part of Shard.                                               */
/*                                                                           */
/* Shard is free software: you can redistribute it and/or modify             */
/* it under the terms of the GNU Affero General Public License as            */
/* published by the Free Software Foundation.                                */
/*                                                                           */
/* This program is distributed in the hope that it will be useful,           */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the              */
/* GNU Affero General Public License for more details.                       */
/*                                                                           */
/* You should have received a copy of the GNU Affero General Public License  */
/* along with this program. If not, see <http://www.gnu.org/licenses/>.      */
/* ************************************************************************* */

#include "shard/tokenizer/Tokenizer.hpp"

/* ************************************************************************* */

// C++
#include <algorithm>
#include <cmath>
#include <iostream>

// Shard
#include "shard/StaticArray.hpp"

/* ************************************************************************* */

namespace shard {
inline namespace v1 {
namespace tokenizer {

/* ************************************************************************* */

namespace
{
    static const StaticArray<std::pair<String, KeywordType>, KeywordTypeCount> g_keywordMap
    {{
    #define KEYWORD(name, str) { str, KeywordType::name },
    #include "shard/tokenizer/Token.def"
    }};

    /**
     * @brief returns escaped char value.
     */
    static char getEscaped(const char value)
    {
        switch (value)
        {
            case '"': return '"';
            case '\'': return '\'';
            case '?': return '\?';
            case '\\': return '\\';
            case '0': return '\0';
            case 'a': return '\a';
            case 'b': return '\b';
            case 'f': return '\f';
            case 'n': return '\n';
            case 'r': return '\r';
            case 't': return '\t';
            case 'v': return '\v';
            default: throw InvalidEscapeSequenceException();
        }
    }

    static Token::IntType getNumericValue(char value)
    {
        switch (value)
        {
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F': return value - 'A' + 10;
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f': return value - 'a' + 10;
            default: return value - '0';
        }
    }
}

/* ************************************************************************* */

void Tokenizer::tokenizeNumber()
{
    bool floatFlag = false;
    int digitSize = 0;

    const auto readNumber = [&](int base = 10) -> Token::IntType
    {
        digitSize = 0;
        Token::IntType res = 0;
        if (!isDigit(base))
        {
            throw ExpectedNumberException();
        }
        do
        {
            res = res * base + getNumericValue(m_src.extract());
            ++digitSize;
        }
        while (isDigit(base));
        return res;
    };

    if (is('0'))
    {
        switch(m_src.getNext())
        {
            case 'b':
            case 'B': m_src.toss(); m_current = Token(readNumber(2)); return;
            case 'x':
            case 'X': m_src.toss(); m_current = Token(readNumber(16)); return;
            case 'o':
            case 'O': m_src.toss(); m_current = Token(readNumber(8)); return;
            default: m_src.unget(); break;
        }
    }

    Token::IntType base = readNumber();

    Token::FloatType decimal = 0;
    if (match('.'))
    {
        floatFlag = true;
        decimal = readNumber();
        decimal = decimal / std::pow(10.0, digitSize);
    }

    Token::FloatType exp = 1;
    if (match('e', 'E'))
    {
        floatFlag = true;
        bool negativeExp = false;
        switch (m_src.get())
        {
            case '-': negativeExp = true;
            case '+': m_src.toss();
            default: break;
        }
        exp = std::pow(10.0, readNumber());
        exp = negativeExp ? (1 / exp) : exp;
    }

    Token::FloatType value = (static_cast<Token::FloatType>(base) + decimal) * exp;

    m_current = floatFlag ? Token(value) : Token(base);
}

void Tokenizer::tokenizeString()
{
    String value;

    while (!match(string_literal_border))
    {
        if (empty())
        {
            throw StringWithoutEndException();
        }
        if (match('\\'))
        {
            if (empty())
            {
                throw StringWithoutEndException();
            }
            value += getEscaped(m_src.extract());
        }
        else
        {
            value += m_src.extract();
        }
    }

    m_current = Token(TokenType::String, value);
}

void Tokenizer::tokenizeChar()
{
    Token::CharType value = m_src.extract();
    if (value >= 0x80)
    {
        char additionalBytes;
        if (value >= 0xF0)
        {
            value &= 0x07;
            additionalBytes = 3;
        }
        else if (value >= 0xE0)
        {
            value &= 0x0F;
            additionalBytes = 2;
        }
        else if (value >= 0xC0)
        {
            value &= 0x1F;
            additionalBytes = 1;
        }
        for (char i = 0; i < additionalBytes; ++i)
        {
            value = (value << 6)|(m_src.extract() & 0x3F);
        }
    }
    else
    {
        switch (value)
        {
            case '\\':
            {
                if (empty())
                {
                    throw CharWithoutEndException();
                }
                value = getEscaped(m_src.extract());
                break;
            }
            case char_literal_border:
            {
                throw EmptyCharLiteralException();
            }
            default: break;
        }
    }
    if (!match(char_literal_border))
    {
        throw CharWithoutEndException();
    }

    m_current = Token(value);
}

void Tokenizer::tokenizeIdentifier() noexcept
{
    String value;

    do
    {
        value += m_src.extract();
    }
    while (isIdentifier());
    auto search = std::find_if(
        g_keywordMap.begin(), g_keywordMap.end(),
        [&](const std::pair<String, KeywordType>& test){ return test.first == value; }
    );
    if (search != g_keywordMap.end())
    {
        m_current = Token(search->second);
    }
    else
    {
        m_current = Token(value);
    }
}

void Tokenizer::tokenizeOperator()
{
    switch (char local = m_src.extract())
    {
        case '.': m_current = Token(TokenType::Period); break;
        case ',': m_current = Token(TokenType::Comma); break;
        case ':': m_current = Token(TokenType::Colon); break;
        case ';': m_current = Token(TokenType::Semicolon); break;
        case '{': m_current = Token(TokenType::BraceO); break;
        case '}': m_current = Token(TokenType::BraceC); break;
        case '[': m_current = Token(TokenType::SquareO); break;
        case ']': m_current = Token(TokenType::SquareC); break;
        case '(': m_current = Token(TokenType::ParenO); break;
        case ')': m_current = Token(TokenType::ParenC); break;
        case '?': m_current = Token(TokenType::QMark); break;
        case '~': m_current = Token(TokenType::Tilde); break;
        case '#': m_current = Token(TokenType::Hash); break;
        case '\\': m_current = Token(TokenType::Backslash); break;
        case '=':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::EqualEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Equal); break;
            } break;
        }
        case '!':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::EMarkEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::EMark); break;
            } break;
        }
        case '+':
        {
            switch (m_src.get())
            {
                case '+': m_current = Token(TokenType::PlusPlus); m_src.toss(); break;
                case '=': m_current = Token(TokenType::PlusEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Plus); break;
            } break;
        }
        case '-':
        {
            switch (m_src.get())
            {
                case '-': m_current = Token(TokenType::MinusMinus); m_src.toss(); break;
                case '=': m_current = Token(TokenType::MinusEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Minus); break;
            } break;
        }
        case '*':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::StarEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Star); break;
            } break;
        }
        case '/':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::SlashEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Slash); break;
            } break;
        }
        case '^':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::CaretEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Caret); break;
            } break;
        }
        case '%':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::PercentEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Equal); break;
            } break;
        }
        case '&':
        {
            switch (m_src.get())
            {
                case '&':
                {
                    switch (m_src.getNext())
                    {
                        case '=': m_current = Token(TokenType::AmpAmpEqual); m_src.toss(); break;
                        default: m_current = Token(TokenType::AmpAmp); break;
                    } break;
                }
                case '=': m_current = Token(TokenType::AmpEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Amp); break;
            } break;
        }
        case '|':
        {
            switch (m_src.get())
            {
                case '|':
                {
                    switch (m_src.getNext())
                    {
                        case '=': m_current = Token(TokenType::PipePipeEqual); m_src.toss(); break;
                        default: m_current = Token(TokenType::PipePipe); break;
                    } break;
                }
                case '=': m_current = Token(TokenType::PipeEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Pipe); break;
            } break;
        }
        case '<':
        {
            switch (m_src.get())
            {
                case '<':
                {
                    switch (m_src.getNext())
                    {
                        case '=': m_current = Token(TokenType::LessLessEqual); m_src.toss(); break;
                        default: m_current = Token(TokenType::LessLess); break;
                    } break;
                }
                case '=': m_current = Token(TokenType::LessEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Less); break;
            } break;
        }
        case '>':
        {
            switch (m_src.get())
            {
                case '>':
                {
                    switch (m_src.getNext())
                    {
                        case '=': m_current = Token(TokenType::GreaterGreaterEqual); m_src.toss(); break;
                        default: m_current = Token(TokenType::GreaterGreater); break;
                    } break;
                }
                case '=': m_current = Token(TokenType::GreaterEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Greater); break;
            } break;
        }
        default: throw UnknownOperatorException(local);
    }
}

void Tokenizer::next()
{
    skipWhitespace();
    skipComments();
    skipWhitespace();

    if (empty())
    {
        m_current = Token(TokenType::End);
    }
    else if (match(string_literal_border))
    {
        tokenizeString();
    }
    else if (match(char_literal_border))
    {
        tokenizeChar();
    }
    else if (isDigit())
    {
        tokenizeNumber();
    }
    else if (isIdentifier())
    {
        tokenizeIdentifier();
    }
    else
    {
        tokenizeOperator();
    }
}

/* ************************************************************************* */

}
}
}

/* ************************************************************************* */

<commit_msg>some preparations for hexadecimal float feature<commit_after>/* ************************************************************************* */
/* This file is part of Shard.                                               */
/*                                                                           */
/* Shard is free software: you can redistribute it and/or modify             */
/* it under the terms of the GNU Affero General Public License as            */
/* published by the Free Software Foundation.                                */
/*                                                                           */
/* This program is distributed in the hope that it will be useful,           */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the              */
/* GNU Affero General Public License for more details.                       */
/*                                                                           */
/* You should have received a copy of the GNU Affero General Public License  */
/* along with this program. If not, see <http://www.gnu.org/licenses/>.      */
/* ************************************************************************* */

#include "shard/tokenizer/Tokenizer.hpp"

/* ************************************************************************* */

// C++
#include <algorithm>
#include <cmath>
#include <iostream>

// Shard
#include "shard/StaticArray.hpp"

/* ************************************************************************* */

namespace shard {
inline namespace v1 {
namespace tokenizer {

/* ************************************************************************* */

namespace
{
    static const StaticArray<std::pair<String, KeywordType>, KeywordTypeCount> g_keywordMap
    {{
    #define KEYWORD(name, str) { str, KeywordType::name },
    #include "shard/tokenizer/Token.def"
    }};

    /**
     * @brief returns escaped char value.
     */
    static char getEscaped(const char value)
    {
        switch (value)
        {
            case '"': return '"';
            case '\'': return '\'';
            case '?': return '\?';
            case '\\': return '\\';
            case '0': return '\0';
            case 'a': return '\a';
            case 'b': return '\b';
            case 'f': return '\f';
            case 'n': return '\n';
            case 'r': return '\r';
            case 't': return '\t';
            case 'v': return '\v';
            default: throw InvalidEscapeSequenceException();
        }
    }

    /**
     * @brief returns numeric value of character in hexadecimal base.
     */
    static Token::IntType getNumericValue(char value)
    {
        switch (value)
        {
            case 'A':
            case 'B':
            case 'C':
            case 'D':
            case 'E':
            case 'F': return value - 'A' + 10;
            case 'a':
            case 'b':
            case 'c':
            case 'd':
            case 'e':
            case 'f': return value - 'a' + 10;
            default: return value - '0';
        }
    }
}

/* ************************************************************************* */

void Tokenizer::tokenizeNumber()
{
    bool floatFlag = false;
    int digitSize = 0;
    int base = 10;

    const auto readNumber = [&]() -> Token::IntType
    {
        digitSize = 0;
        if (!isDigit(base))
        {
            throw ExpectedNumberException();
        }

        Token::IntType res = 0;
        do
        {
            res = res * base + getNumericValue(m_src.extract());
            ++digitSize;
        }
        while (isDigit(base));
        return res;
    };

    Token::IntType integer = readNumber();

    if (integer == 0)
    {
        switch(m_src.extract())
        {
            case 'x':
            case 'X': base = 16; m_current = Token(readNumber()); return;
            case 'o':
            case 'O': base = 8; m_current = Token(readNumber()); return;
            case 'b':
            case 'B': base = 2; m_current = Token(readNumber()); return;
            default: break;
        }
    }

    Token::FloatType decimal = 0;
    if (match('.'))
    {
        floatFlag = true;
        decimal = readNumber();
        decimal = decimal / std::pow(10.0, digitSize);
    }

    Token::FloatType exp = 1;
    if (match('e', 'E'))
    {
        floatFlag = true;
        bool negativeExp = false;
        switch (m_src.get())
        {
            case '-': negativeExp = true;
            case '+': m_src.toss();
            default: break;
        }
        exp = std::pow(10.0, readNumber());
        exp = negativeExp ? (1 / exp) : exp;
    }

    Token::FloatType value = (static_cast<Token::FloatType>(integer) + decimal) * exp;

    m_current = floatFlag ? Token(value) : Token(integer);
}

void Tokenizer::tokenizeString()
{
    String value;

    while (!match(string_literal_border))
    {
        if (empty())
        {
            throw StringWithoutEndException();
        }
        if (match('\\'))
        {
            if (empty())
            {
                throw StringWithoutEndException();
            }
            value += getEscaped(m_src.extract());
        }
        else
        {
            value += m_src.extract();
        }
    }

    m_current = Token(TokenType::String, value);
}

void Tokenizer::tokenizeChar()
{
    Token::CharType value = m_src.extract();
    if (value >= 0x80)
    {
        char additionalBytes;
        if (value >= 0xF0)
        {
            value &= 0x07;
            additionalBytes = 3;
        }
        else if (value >= 0xE0)
        {
            value &= 0x0F;
            additionalBytes = 2;
        }
        else if (value >= 0xC0)
        {
            value &= 0x1F;
            additionalBytes = 1;
        }
        for (char i = 0; i < additionalBytes; ++i)
        {
            value = (value << 6)|(m_src.extract() & 0x3F);
        }
    }
    else
    {
        switch (value)
        {
            case '\\':
            {
                if (empty())
                {
                    throw CharWithoutEndException();
                }
                value = getEscaped(m_src.extract());
                break;
            }
            case char_literal_border:
            {
                throw EmptyCharLiteralException();
            }
            default: break;
        }
    }
    if (!match(char_literal_border))
    {
        throw CharWithoutEndException();
    }

    m_current = Token(value);
}

void Tokenizer::tokenizeIdentifier() noexcept
{
    String value;

    do
    {
        value += m_src.extract();
    }
    while (isIdentifier());
    auto search = std::find_if(
        g_keywordMap.begin(), g_keywordMap.end(),
        [&](const std::pair<String, KeywordType>& test){ return test.first == value; }
    );
    if (search != g_keywordMap.end())
    {
        m_current = Token(search->second);
    }
    else
    {
        m_current = Token(value);
    }
}

void Tokenizer::tokenizeOperator()
{
    switch (char local = m_src.extract())
    {
        case '.': m_current = Token(TokenType::Period); break;
        case ',': m_current = Token(TokenType::Comma); break;
        case ':': m_current = Token(TokenType::Colon); break;
        case ';': m_current = Token(TokenType::Semicolon); break;
        case '{': m_current = Token(TokenType::BraceO); break;
        case '}': m_current = Token(TokenType::BraceC); break;
        case '[': m_current = Token(TokenType::SquareO); break;
        case ']': m_current = Token(TokenType::SquareC); break;
        case '(': m_current = Token(TokenType::ParenO); break;
        case ')': m_current = Token(TokenType::ParenC); break;
        case '?': m_current = Token(TokenType::QMark); break;
        case '~': m_current = Token(TokenType::Tilde); break;
        case '#': m_current = Token(TokenType::Hash); break;
        case '\\': m_current = Token(TokenType::Backslash); break;
        case '=':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::EqualEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Equal); break;
            } break;
        }
        case '!':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::EMarkEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::EMark); break;
            } break;
        }
        case '+':
        {
            switch (m_src.get())
            {
                case '+': m_current = Token(TokenType::PlusPlus); m_src.toss(); break;
                case '=': m_current = Token(TokenType::PlusEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Plus); break;
            } break;
        }
        case '-':
        {
            switch (m_src.get())
            {
                case '-': m_current = Token(TokenType::MinusMinus); m_src.toss(); break;
                case '=': m_current = Token(TokenType::MinusEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Minus); break;
            } break;
        }
        case '*':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::StarEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Star); break;
            } break;
        }
        case '/':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::SlashEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Slash); break;
            } break;
        }
        case '^':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::CaretEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Caret); break;
            } break;
        }
        case '%':
        {
            switch (m_src.get())
            {
                case '=': m_current = Token(TokenType::PercentEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Equal); break;
            } break;
        }
        case '&':
        {
            switch (m_src.get())
            {
                case '&':
                {
                    switch (m_src.getNext())
                    {
                        case '=': m_current = Token(TokenType::AmpAmpEqual); m_src.toss(); break;
                        default: m_current = Token(TokenType::AmpAmp); break;
                    } break;
                }
                case '=': m_current = Token(TokenType::AmpEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Amp); break;
            } break;
        }
        case '|':
        {
            switch (m_src.get())
            {
                case '|':
                {
                    switch (m_src.getNext())
                    {
                        case '=': m_current = Token(TokenType::PipePipeEqual); m_src.toss(); break;
                        default: m_current = Token(TokenType::PipePipe); break;
                    } break;
                }
                case '=': m_current = Token(TokenType::PipeEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Pipe); break;
            } break;
        }
        case '<':
        {
            switch (m_src.get())
            {
                case '<':
                {
                    switch (m_src.getNext())
                    {
                        case '=': m_current = Token(TokenType::LessLessEqual); m_src.toss(); break;
                        default: m_current = Token(TokenType::LessLess); break;
                    } break;
                }
                case '=': m_current = Token(TokenType::LessEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Less); break;
            } break;
        }
        case '>':
        {
            switch (m_src.get())
            {
                case '>':
                {
                    switch (m_src.getNext())
                    {
                        case '=': m_current = Token(TokenType::GreaterGreaterEqual); m_src.toss(); break;
                        default: m_current = Token(TokenType::GreaterGreater); break;
                    } break;
                }
                case '=': m_current = Token(TokenType::GreaterEqual); m_src.toss(); break;
                default: m_current = Token(TokenType::Greater); break;
            } break;
        }
        default: throw UnknownOperatorException(local);
    }
}

void Tokenizer::next()
{
    skipWhitespace();
    skipComments();
    skipWhitespace();

    if (empty())
    {
        m_current = Token(TokenType::End);
    }
    else if (match(string_literal_border))
    {
        tokenizeString();
    }
    else if (match(char_literal_border))
    {
        tokenizeChar();
    }
    else if (isDigit())
    {
        tokenizeNumber();
    }
    else if (isIdentifier())
    {
        tokenizeIdentifier();
    }
    else
    {
        tokenizeOperator();
    }
}

/* ************************************************************************* */

}
}
}

/* ************************************************************************* */

<|endoftext|>


<commit_before>//! \file
/*
**  Copyright (C) - Triton
**
**  This program is under the terms of the LGPLv3 License.
*/

#include <iostream>
#include "snapshot.hpp"

namespace tracer {
  namespace pintool {

      Snapshot::Snapshot() {
        this->locked              = true;
        this->snapshotTaintEngine = nullptr;
        this->snapshotSymEngine   = nullptr;
        this->mustBeRestore       = false;
      }

      Snapshot::~Snapshot() {
      }

      /* Add the modification byte. */
      void Snapshot::addModification(triton::__uint mem, char byte) {
        if (this->locked == false && this->memory.find(mem) == this->memory.end())
          this->memory[mem] = byte;
      }

      /* Enable the snapshot engine. */
      void Snapshot::takeSnapshot(CONTEXT *ctx) {
        /* 1 - Unlock the engine */
        this->locked = false;

        /* 2 - Save current symbolic engine state */
        this->snapshotSymEngine = new triton::engines::symbolic::SymbolicEngine(*triton::api.getSymbolicEngine());

        /* 3 - Save current taint engine state */
        this->snapshotTaintEngine = new triton::engines::taint::TaintEngine(*triton::api.getTaintEngine());

        /* 4 - Save current taint engine state */
        this->nodesList = triton::smt2lib::allocatedNodes;

        /* 5 - Save the Triton CPU state */
        #if defined(__x86_64__) || defined(_M_X64)
        this->cpu = new triton::arch::x86::x8664Cpu(*reinterpret_cast<triton::arch::x86::x8664Cpu*>(triton::api.getCpu()));
        #endif
        #if defined(__i386) || defined(_M_IX86)
        this->cpu = new triton::arch::x86::x86Cpu(*reinterpret_cast<triton::arch::x86::x86Cpu*>(triton::api.getCpu()));
        #endif

        /* 6 - Save Pin registers context */
        PIN_SaveContext(ctx, &this->pinCtx);
      }

      /* Restore the snapshot. */
      void Snapshot::restoreSnapshot(CONTEXT *ctx) {

        if (this->mustBeRestore == false)
          return;

        /* 1 - Restore all memory modification. */
        for(auto i = this->memory.begin(); i != this->memory.end(); ++i){
          *(reinterpret_cast<char*>(i->first)) = i->second;
        }
        this->memory.clear();

        /* 2 - Delete unused expressions */
        auto currentExpressions     = triton::api.getSymbolicExpressions();
        auto snapshotExpressions    = this->snapshotSymEngine->getSymbolicExpressions();
        triton::__uint currentSize  = currentExpressions.size();
        triton::__uint snapshotSize = snapshotExpressions.size();
        for (auto i = currentExpressions.begin(); i != currentExpressions.end(); ++i) {
          if (snapshotExpressions.find(i->first) == snapshotExpressions.end())
            delete currentExpressions[i->first];
        }

        /* 3 - Delete unused variables */
        auto currentSymbolicVars   = triton::api.getSymbolicVariables();
        auto snapshotSymbolicVars  = this->snapshotSymEngine->getSymbolicVariables();
        currentSize                = currentSymbolicVars.size();
        snapshotSize               = snapshotSymbolicVars.size();
        for (auto i = currentSymbolicVars.begin(); i != currentSymbolicVars.end(); ++i) {
          if (snapshotSymbolicVars.find(i->first) == snapshotSymbolicVars.end())
            delete currentSymbolicVars[i->first];
        }

        /* 4 - Delete unused AST nodes */
        for (auto i = triton::smt2lib::allocatedNodes.begin(); i != triton::smt2lib::allocatedNodes.end(); ++i) {
          if (this->nodesList.find(*i) == this->nodesList.end())
            delete *i;
        }

        /* 5 - Restore current symbolic engine state */
        *triton::api.getSymbolicEngine() = *this->snapshotSymEngine;

        /* 6 - Restore current taint engine state */
        *triton::api.getTaintEngine() = *this->snapshotTaintEngine;

        /* 7 - Restore current AST node state */
        triton::smt2lib::allocatedNodes = this->nodesList;

        /* 8 - Restore the Triton CPU state */
        #if defined(__x86_64__) || defined(_M_X64)
        *reinterpret_cast<triton::arch::x86::x8664Cpu*>(triton::api.getCpu()) = *this->cpu;
        #endif
        #if defined(__i386) || defined(_M_IX86)
        *reinterpret_cast<triton::arch::x86::x86Cpu*>(triton::api.getCpu()) = *this->cpu;
        #endif

        /* 9 - Restore Pin registers context */
        PIN_SaveContext(&this->pinCtx, ctx);

        this->mustBeRestore = false;
        PIN_UnlockClient();
        PIN_ExecuteAt(ctx);
      }

      /* Disable the snapshot engine. */
      void Snapshot::disableSnapshot(void) {
        this->locked = true;
      }

      /* Reset the snapshot engine.
       * Clear all backups for a new snapshot. */
      void Snapshot::resetEngine(void) {
        this->memory.clear();

        delete this->snapshotSymEngine;
        this->snapshotSymEngine = nullptr;

        delete this->snapshotTaintEngine;
        this->snapshotTaintEngine = nullptr;
      }

      /* Check if the snapshot engine is locked. */
      bool Snapshot::isLocked(void) {
        return this->locked;
      }

      CONTEXT *Snapshot::getCtx(void) {
        return &this->pinCtx;
      }

      /* Check if we must restore the snapshot */
      bool Snapshot::mustBeRestored(void) {
        return this->mustBeRestore;
      }

      /* Check if we must restore the snapshot */
      void Snapshot::setRestore(bool flag) {
        this->mustBeRestore = flag;
      }

  };
};
<commit_msg>Add doc<commit_after>//! \file
/*
**  Copyright (C) - Triton
**
**  This program is under the terms of the LGPLv3 License.
*/

#include <iostream>
#include "snapshot.hpp"

/*! \page Snapshot_page Snapshot
    \brief [**pintool internal**] All information about the snapshot engine.

\tableofcontents
\section snapshot_description Description

The pintool shipped in this project (see \ref Tracer_page) allows the user to replay a trace. During the execution, it is possible to
take a snapshot of the registers and memory states. Then, at each program point, it is possible to restore the previous snapshot.

Example: Imagine a trace with a `LOAD` value, this value is controllable by the user. Then, some operations are applied to this value
and at the end the value is verified with a constant. At the compare instruction the formula of the operation applied to the value is
built, by assigning a symbolic variable to the input value, it is possible to solve the formula (if it is satisfiable). So, it will
be useful to directly inject the model returned by the solver in memory instead of re-run the program.

As taking a snapshot of the full memory is not really possible, the pintool saves all bytes before each modification of the memory (`STORE` access)
in a map \f$ \langle addr, byte \rangle \f$.

When the snapshot must be restored, all modifications are re-injected in memory like this: \f$ \forall (addr, byte \in map) : *addr \gets byte \f$

*/

namespace tracer {
  namespace pintool {

      Snapshot::Snapshot() {
        this->locked              = true;
        this->snapshotTaintEngine = nullptr;
        this->snapshotSymEngine   = nullptr;
        this->mustBeRestore       = false;
      }

      Snapshot::~Snapshot() {
      }

      /* Add the modification byte. */
      void Snapshot::addModification(triton::__uint mem, char byte) {
        if (this->locked == false && this->memory.find(mem) == this->memory.end())
          this->memory[mem] = byte;
      }

      /* Enable the snapshot engine. */
      void Snapshot::takeSnapshot(CONTEXT *ctx) {
        /* 1 - Unlock the engine */
        this->locked = false;

        /* 2 - Save current symbolic engine state */
        this->snapshotSymEngine = new triton::engines::symbolic::SymbolicEngine(*triton::api.getSymbolicEngine());

        /* 3 - Save current taint engine state */
        this->snapshotTaintEngine = new triton::engines::taint::TaintEngine(*triton::api.getTaintEngine());

        /* 4 - Save current taint engine state */
        this->nodesList = triton::smt2lib::allocatedNodes;

        /* 5 - Save the Triton CPU state */
        #if defined(__x86_64__) || defined(_M_X64)
        this->cpu = new triton::arch::x86::x8664Cpu(*reinterpret_cast<triton::arch::x86::x8664Cpu*>(triton::api.getCpu()));
        #endif
        #if defined(__i386) || defined(_M_IX86)
        this->cpu = new triton::arch::x86::x86Cpu(*reinterpret_cast<triton::arch::x86::x86Cpu*>(triton::api.getCpu()));
        #endif

        /* 6 - Save Pin registers context */
        PIN_SaveContext(ctx, &this->pinCtx);
      }

      /* Restore the snapshot. */
      void Snapshot::restoreSnapshot(CONTEXT *ctx) {

        if (this->mustBeRestore == false)
          return;

        /* 1 - Restore all memory modification. */
        for(auto i = this->memory.begin(); i != this->memory.end(); ++i){
          *(reinterpret_cast<char*>(i->first)) = i->second;
        }
        this->memory.clear();

        /* 2 - Delete unused expressions */
        auto currentExpressions     = triton::api.getSymbolicExpressions();
        auto snapshotExpressions    = this->snapshotSymEngine->getSymbolicExpressions();
        triton::__uint currentSize  = currentExpressions.size();
        triton::__uint snapshotSize = snapshotExpressions.size();
        for (auto i = currentExpressions.begin(); i != currentExpressions.end(); ++i) {
          if (snapshotExpressions.find(i->first) == snapshotExpressions.end())
            delete currentExpressions[i->first];
        }

        /* 3 - Delete unused variables */
        auto currentSymbolicVars   = triton::api.getSymbolicVariables();
        auto snapshotSymbolicVars  = this->snapshotSymEngine->getSymbolicVariables();
        currentSize                = currentSymbolicVars.size();
        snapshotSize               = snapshotSymbolicVars.size();
        for (auto i = currentSymbolicVars.begin(); i != currentSymbolicVars.end(); ++i) {
          if (snapshotSymbolicVars.find(i->first) == snapshotSymbolicVars.end())
            delete currentSymbolicVars[i->first];
        }

        /* 4 - Delete unused AST nodes */
        for (auto i = triton::smt2lib::allocatedNodes.begin(); i != triton::smt2lib::allocatedNodes.end(); ++i) {
          if (this->nodesList.find(*i) == this->nodesList.end())
            delete *i;
        }

        /* 5 - Restore current symbolic engine state */
        *triton::api.getSymbolicEngine() = *this->snapshotSymEngine;

        /* 6 - Restore current taint engine state */
        *triton::api.getTaintEngine() = *this->snapshotTaintEngine;

        /* 7 - Restore current AST node state */
        triton::smt2lib::allocatedNodes = this->nodesList;

        /* 8 - Restore the Triton CPU state */
        #if defined(__x86_64__) || defined(_M_X64)
        *reinterpret_cast<triton::arch::x86::x8664Cpu*>(triton::api.getCpu()) = *this->cpu;
        #endif
        #if defined(__i386) || defined(_M_IX86)
        *reinterpret_cast<triton::arch::x86::x86Cpu*>(triton::api.getCpu()) = *this->cpu;
        #endif

        /* 9 - Restore Pin registers context */
        PIN_SaveContext(&this->pinCtx, ctx);

        this->mustBeRestore = false;
        PIN_UnlockClient();
        PIN_ExecuteAt(ctx);
      }

      /* Disable the snapshot engine. */
      void Snapshot::disableSnapshot(void) {
        this->locked = true;
      }

      /* Reset the snapshot engine.
       * Clear all backups for a new snapshot. */
      void Snapshot::resetEngine(void) {
        this->memory.clear();

        delete this->snapshotSymEngine;
        this->snapshotSymEngine = nullptr;

        delete this->snapshotTaintEngine;
        this->snapshotTaintEngine = nullptr;
      }

      /* Check if the snapshot engine is locked. */
      bool Snapshot::isLocked(void) {
        return this->locked;
      }

      CONTEXT *Snapshot::getCtx(void) {
        return &this->pinCtx;
      }

      /* Check if we must restore the snapshot */
      bool Snapshot::mustBeRestored(void) {
        return this->mustBeRestore;
      }

      /* Check if we must restore the snapshot */
      void Snapshot::setRestore(bool flag) {
        this->mustBeRestore = flag;
      }

  };
};
<|endoftext|>


<commit_before>#include "qtfirebasemessaging.h"

#include <QPointer>
#include <QGuiApplication>
#include <QQmlParserStatus>

#include <QMutexLocker>

#include <stdint.h>
#include "firebase/app.h"
#include "firebase/internal/common.h"
#include "firebase/messaging.h"
#include "firebase/util.h"

namespace messaging = ::firebase::messaging;

QtFirebaseMessaging *QtFirebaseMessaging::self = nullptr;

QtFirebaseMessaging::QtFirebaseMessaging(QObject* parent)
    : QObject(parent)
    , _ready(false)
    , _hasMissingDependency(false)
    , _initializing(false)
    , __QTFIREBASE_ID(QString().asprintf("%8p", static_cast<void*> (this)))
    , g_listener(new MessageListener(this))
    , _data()
    , _token()
{
    if(!self) {
        self = this;
        qDebug() << self << "::QtFirebaseMessaging" << "singleton";
    }

    if(qFirebase->ready()) {
        //Call init outside of constructor, otherwise signal readyChanged not emited
        QTimer::singleShot(100, this, &QtFirebaseMessaging::init);
    } else {
        connect(qFirebase,&QtFirebase::readyChanged, this, &QtFirebaseMessaging::init);
        qFirebase->requestInit();
    }

    connect(qFirebase,&QtFirebase::futureEvent, this, &QtFirebaseMessaging::onFutureEvent);
}

QtFirebaseMessaging::~QtFirebaseMessaging()
{
    if(_ready) {
        messaging::Terminate();
    }
}

void QtFirebaseMessaging::classBegin()
{
}

void QtFirebaseMessaging::componentComplete()
{
    // Connect on componentComplete so the signals are emited in the correct order
    connect(g_listener, &MessageListener::onMessageReceived, this, &QtFirebaseMessaging::getMessage);
    connect(g_listener, &MessageListener::onTokenReceived, this, &QtFirebaseMessaging::getToken);
}

bool QtFirebaseMessaging::checkInstance(const char *function)
{
    const bool b = (QtFirebaseMessaging::self != nullptr);
    if (!b)
        qWarning("QtFirebaseMessaging::%s: Please instantiate the QtFirebaseMessaging object first", function);
    return b;
}

void QtFirebaseMessaging::init()
{
    if(!qFirebase->ready()) {
        qDebug() << self << "::init" << "base not ready";
        return;
    }

    if(!_ready && !_initializing) {
        _initializing = true;

        auto initResult = messaging::Initialize(*qFirebase->firebaseApp(), g_listener);
        if(firebase::kInitResultFailedMissingDependency == initResult)
            setHasMissingDependency(true);

        _initializing = false;

        if(firebase::kInitResultSuccess != initResult)
            return;

        setReady(true);
#if QTFIREBASE_FIREBASE_VERSION >= QTFIREBASE_FIREBASE_VERSION_CHECK(7, 0, 0)
#if !defined(Q_OS_DARWIN) // https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in
        QPointer<QtFirebaseMessaging> self { this };

        // firebase::messaging::Listener::OnTokenReceived() may not be called on second app launch
        auto future = messaging::GetToken();
        future.OnCompletion([self, future](const firebase::FutureBase &){
            if(!self)
                return;
            if(future.result())
                self->setToken(QString::fromStdString(*future.result()));
        });
#endif
#endif
    }
}

void QtFirebaseMessaging::onFutureEvent(const QString &eventId, const firebase::FutureBase &future)
{
    if(!eventId.startsWith(__QTFIREBASE_ID))
        return;

    qDebug() << self << "::onFutureEvent" << eventId;

    if(future.status() != firebase::kFutureStatusComplete)
    {
        qDebug() << this << "::onFutureEvent initializing failed." << "ERROR: Action failed with error code and message: " << future.error() << future.error_message();
        _initializing = false;
        return;
    }
    qDebug() << this << "::onFutureEvent initialized ok";
    _initializing = false;
}

void QtFirebaseMessaging::getMessage()
{
    setData(g_listener->data());
}

void QtFirebaseMessaging::getToken()
{
    setToken(g_listener->token());
}

bool QtFirebaseMessaging::ready()
{
    return _ready;
}

void QtFirebaseMessaging::setReady(bool ready)
{
    if (_ready != ready) {
        _ready = ready;
        emit readyChanged();
    }
}

bool QtFirebaseMessaging::hasMissingDependency()
{
    return _hasMissingDependency;
}

void QtFirebaseMessaging::setHasMissingDependency(bool hasMissingDependency)
{
    if (_hasMissingDependency != hasMissingDependency) {
        _hasMissingDependency = hasMissingDependency;
        emit hasMissingDependencyChanged();
    }
}

QVariantMap QtFirebaseMessaging::data()
{
    return _data;
}

void QtFirebaseMessaging::setData(const QVariantMap &data)
{
    if (_data != data) {
        _data = data;
        emit dataChanged();
        emit messageReceived();
    }
}

QString QtFirebaseMessaging::token()
{
    QMutexLocker lock { &_tokenMutex };
    return _token;
}

void QtFirebaseMessaging::setToken(const QString &token)
{
    QMutexLocker lock { &_tokenMutex };
    if (_token != token) {
        _token = token;
        lock.unlock();

        emit tokenChanged();
    }
}

void QtFirebaseMessaging::subscribe(const QString &topic)
{
    if(!_ready) {
        qDebug() << this << "::subscribe native part not ready";
        return;
    }

    // TODO queue these futures so repeated calls don't get lost
    auto result = firebase::messaging::Subscribe(topic.toUtf8());

    result.OnCompletion([this, topic](const firebase::FutureBase &completed_future){
        if(completed_future.error() == firebase::messaging::kErrorNone) {
            emit subscribed(topic);
        } else {
            emit error(completed_future.error(), QString(QString::fromUtf8(completed_future.error_message())));
        }
    });
}

void QtFirebaseMessaging::unsubscribe(const QString &topic)
{
    if(!_ready) {
        qDebug() << this << "::unsubscribe native part not ready";
        return;
    }

    // TODO queue these futures so repeated calls don't get lost
    auto result = firebase::messaging::Unsubscribe(topic.toUtf8());

    result.OnCompletion([this, topic](const firebase::FutureBase &completed_future){
        if(completed_future.error() == firebase::messaging::kErrorNone) {
            emit unsubscribed(topic);
        } else {
            emit error(completed_future.error(), QString(QString::fromUtf8(completed_future.error_message())));
        }
    });
}

MessageListener::MessageListener(QObject* parent)
    : QObject(parent)
{
}

void MessageListener::OnMessage(const messaging::Message &message)
{
    // When messages are received by the server, they are placed into an
    // internal queue, waiting to be consumed. When ProcessMessages is called,
    // this OnMessage function is called once for each queued message.

    QVariantMap data;

    if (message.notification) {
        if (!message.notification->title.empty()) {
            const QString key = QStringLiteral("nTitle");
            const QString value = QString::fromStdString(message.notification->title.c_str());
            data.insert(key, value);
        }
        if (!message.notification->body.empty()) {
            const QString key = QStringLiteral("nBody");
            const QString value = QString::fromStdString(message.notification->body.c_str());
            data.insert(key, value);
        }
        if (!message.notification->icon.empty()) {
            const QString key = QStringLiteral("nIcon");
            const QString value = QString::fromStdString(message.notification->icon.c_str());
            data.insert(key, value);
        }
        if (!message.notification->tag.empty()) {
            const QString key = QStringLiteral("nTag");
            const QString value = QString::fromStdString(message.notification->tag.c_str());
            data.insert(key, value);
        }
        if (!message.notification->color.empty()) {
            const QString key = QStringLiteral("nColor");
            const QString value = QString::fromStdString(message.notification->color.c_str());
            data.insert(key, value);
        }
        if (!message.notification->sound.empty()) {
            const QString key = QStringLiteral("nSound");
            const QString value = QString::fromStdString(message.notification->sound.c_str());
            data.insert(key, value);
        }
        if (!message.notification->click_action.empty()) {
            const QString key = QStringLiteral("nClickAction");
            const QString value = QString::fromStdString(message.notification->click_action.c_str());
            data.insert(key, value);
        }
    }

    if (message.notification_opened) {
        const QString key = QStringLiteral("launchnotification");
        data.insert(key, true);
    }

    for (const auto& field : message.data)
    {
        const QString key = QString::fromStdString(field.first);
        const QString value = QString::fromStdString(field.second);

        data.insert(key, value);
    }

    setData(data);
}

void MessageListener::OnTokenReceived(const char *token)
{
    setToken(QString::fromUtf8(token));
}

void MessageListener::connectNotify(const QMetaMethod &signal)
{
    if (signal == QMetaMethod::fromSignal(&MessageListener::onMessageReceived)) {
        _messageReceivedConnected = true;

        if(_notifyMessageReceived) {
            emit onMessageReceived();
            _notifyMessageReceived = false;
        }
    }

    if (signal == QMetaMethod::fromSignal(&MessageListener::onTokenReceived)) {
        _tokenReceivedConnected = true;

        if(_notifyTokenReceived) {
            emit onTokenReceived();
            _notifyTokenReceived = false;
        }
    }
}

QVariantMap MessageListener::data()
{
    return _data;
}

void MessageListener::setData(const QVariantMap &data)
{
    if (_data != data) {
        _notifyMessageReceived = true;
        _data = data;

        if(_messageReceivedConnected) {
            emit onMessageReceived();
            _notifyMessageReceived = false;
        }
    }
}

QString MessageListener::token()
{
    QMutexLocker lock { &_tokenMutex };
    return _token;
}

void MessageListener::setToken(const QString &token)
{
    QMutexLocker lock { &_tokenMutex };
    if (_token != token) {
        _token = token;
        lock.unlock();

        _notifyTokenReceived = true;

        if(_tokenReceivedConnected) {
            emit onTokenReceived();
            _notifyTokenReceived = false;
        }
    }
}
<commit_msg>Messaging: Fix token against Firebase C++ SDK 8.6.0 on Android<commit_after>#include "qtfirebasemessaging.h"

#include <QPointer>
#include <QGuiApplication>
#include <QQmlParserStatus>

#include <QMutexLocker>

#include <stdint.h>
#include "firebase/app.h"
#include "firebase/internal/common.h"
#include "firebase/messaging.h"
#include "firebase/util.h"

namespace messaging = ::firebase::messaging;

QtFirebaseMessaging *QtFirebaseMessaging::self = nullptr;

QtFirebaseMessaging::QtFirebaseMessaging(QObject* parent)
    : QObject(parent)
    , _ready(false)
    , _hasMissingDependency(false)
    , _initializing(false)
    , __QTFIREBASE_ID(QString().asprintf("%8p", static_cast<void*> (this)))
    , g_listener(new MessageListener(this))
    , _data()
    , _token()
{
    if(!self) {
        self = this;
        qDebug() << self << "::QtFirebaseMessaging" << "singleton";
    }

    if(qFirebase->ready()) {
        //Call init outside of constructor, otherwise signal readyChanged not emited
        QTimer::singleShot(100, this, &QtFirebaseMessaging::init);
    } else {
        connect(qFirebase,&QtFirebase::readyChanged, this, &QtFirebaseMessaging::init);
        qFirebase->requestInit();
    }

    connect(qFirebase,&QtFirebase::futureEvent, this, &QtFirebaseMessaging::onFutureEvent);
}

QtFirebaseMessaging::~QtFirebaseMessaging()
{
    if(_ready) {
        messaging::Terminate();
    }
}

void QtFirebaseMessaging::classBegin()
{
}

void QtFirebaseMessaging::componentComplete()
{
    // Connect on componentComplete so the signals are emited in the correct order
    connect(g_listener, &MessageListener::onMessageReceived, this, &QtFirebaseMessaging::getMessage);
    connect(g_listener, &MessageListener::onTokenReceived, this, &QtFirebaseMessaging::getToken);
}

bool QtFirebaseMessaging::checkInstance(const char *function)
{
    const bool b = (QtFirebaseMessaging::self != nullptr);
    if (!b)
        qWarning("QtFirebaseMessaging::%s: Please instantiate the QtFirebaseMessaging object first", function);
    return b;
}

void QtFirebaseMessaging::init()
{
    if(!qFirebase->ready()) {
        qDebug() << self << "::init" << "base not ready";
        return;
    }

    if(!_ready && !_initializing) {
        _initializing = true;

        auto initResult = messaging::Initialize(*qFirebase->firebaseApp(), g_listener);
        if(firebase::kInitResultFailedMissingDependency == initResult)
            setHasMissingDependency(true);

        _initializing = false;

        if(firebase::kInitResultSuccess != initResult)
            return;

        setReady(true);
#if (QTFIREBASE_FIREBASE_VERSION >= QTFIREBASE_FIREBASE_VERSION_CHECK(7, 0, 0)) && (QTFIREBASE_FIREBASE_VERSION < QTFIREBASE_FIREBASE_VERSION_CHECK(8, 6, 0))
#if defined(Q_OS_ANDROID) // https://github.com/firebase/firebase-cpp-sdk/pull/667
        QPointer<QtFirebaseMessaging> self { this };

        // firebase::messaging::Listener::OnTokenReceived() may not be called on second app launch
        auto future = messaging::GetToken();
        future.OnCompletion([self, future](const firebase::FutureBase &){
            if(!self)
                return;
            if(future.result())
                self->setToken(QString::fromStdString(*future.result()));
        });
#endif
#endif
    }
}

void QtFirebaseMessaging::onFutureEvent(const QString &eventId, const firebase::FutureBase &future)
{
    if(!eventId.startsWith(__QTFIREBASE_ID))
        return;

    qDebug() << self << "::onFutureEvent" << eventId;

    if(future.status() != firebase::kFutureStatusComplete)
    {
        qDebug() << this << "::onFutureEvent initializing failed." << "ERROR: Action failed with error code and message: " << future.error() << future.error_message();
        _initializing = false;
        return;
    }
    qDebug() << this << "::onFutureEvent initialized ok";
    _initializing = false;
}

void QtFirebaseMessaging::getMessage()
{
    setData(g_listener->data());
}

void QtFirebaseMessaging::getToken()
{
    setToken(g_listener->token());
}

bool QtFirebaseMessaging::ready()
{
    return _ready;
}

void QtFirebaseMessaging::setReady(bool ready)
{
    if (_ready != ready) {
        _ready = ready;
        emit readyChanged();
    }
}

bool QtFirebaseMessaging::hasMissingDependency()
{
    return _hasMissingDependency;
}

void QtFirebaseMessaging::setHasMissingDependency(bool hasMissingDependency)
{
    if (_hasMissingDependency != hasMissingDependency) {
        _hasMissingDependency = hasMissingDependency;
        emit hasMissingDependencyChanged();
    }
}

QVariantMap QtFirebaseMessaging::data()
{
    return _data;
}

void QtFirebaseMessaging::setData(const QVariantMap &data)
{
    if (_data != data) {
        _data = data;
        emit dataChanged();
        emit messageReceived();
    }
}

QString QtFirebaseMessaging::token()
{
    QMutexLocker lock { &_tokenMutex };
    return _token;
}

void QtFirebaseMessaging::setToken(const QString &token)
{
    QMutexLocker lock { &_tokenMutex };
    if (_token != token) {
        _token = token;
        lock.unlock();

        emit tokenChanged();
    }
}

void QtFirebaseMessaging::subscribe(const QString &topic)
{
    if(!_ready) {
        qDebug() << this << "::subscribe native part not ready";
        return;
    }

    // TODO queue these futures so repeated calls don't get lost
    auto result = firebase::messaging::Subscribe(topic.toUtf8());

    result.OnCompletion([this, topic](const firebase::FutureBase &completed_future){
        if(completed_future.error() == firebase::messaging::kErrorNone) {
            emit subscribed(topic);
        } else {
            emit error(completed_future.error(), QString(QString::fromUtf8(completed_future.error_message())));
        }
    });
}

void QtFirebaseMessaging::unsubscribe(const QString &topic)
{
    if(!_ready) {
        qDebug() << this << "::unsubscribe native part not ready";
        return;
    }

    // TODO queue these futures so repeated calls don't get lost
    auto result = firebase::messaging::Unsubscribe(topic.toUtf8());

    result.OnCompletion([this, topic](const firebase::FutureBase &completed_future){
        if(completed_future.error() == firebase::messaging::kErrorNone) {
            emit unsubscribed(topic);
        } else {
            emit error(completed_future.error(), QString(QString::fromUtf8(completed_future.error_message())));
        }
    });
}

MessageListener::MessageListener(QObject* parent)
    : QObject(parent)
{
}

void MessageListener::OnMessage(const messaging::Message &message)
{
    // When messages are received by the server, they are placed into an
    // internal queue, waiting to be consumed. When ProcessMessages is called,
    // this OnMessage function is called once for each queued message.

    QVariantMap data;

    if (message.notification) {
        if (!message.notification->title.empty()) {
            const QString key = QStringLiteral("nTitle");
            const QString value = QString::fromStdString(message.notification->title.c_str());
            data.insert(key, value);
        }
        if (!message.notification->body.empty()) {
            const QString key = QStringLiteral("nBody");
            const QString value = QString::fromStdString(message.notification->body.c_str());
            data.insert(key, value);
        }
        if (!message.notification->icon.empty()) {
            const QString key = QStringLiteral("nIcon");
            const QString value = QString::fromStdString(message.notification->icon.c_str());
            data.insert(key, value);
        }
        if (!message.notification->tag.empty()) {
            const QString key = QStringLiteral("nTag");
            const QString value = QString::fromStdString(message.notification->tag.c_str());
            data.insert(key, value);
        }
        if (!message.notification->color.empty()) {
            const QString key = QStringLiteral("nColor");
            const QString value = QString::fromStdString(message.notification->color.c_str());
            data.insert(key, value);
        }
        if (!message.notification->sound.empty()) {
            const QString key = QStringLiteral("nSound");
            const QString value = QString::fromStdString(message.notification->sound.c_str());
            data.insert(key, value);
        }
        if (!message.notification->click_action.empty()) {
            const QString key = QStringLiteral("nClickAction");
            const QString value = QString::fromStdString(message.notification->click_action.c_str());
            data.insert(key, value);
        }
    }

    if (message.notification_opened) {
        const QString key = QStringLiteral("launchnotification");
        data.insert(key, true);
    }

    for (const auto& field : message.data)
    {
        const QString key = QString::fromStdString(field.first);
        const QString value = QString::fromStdString(field.second);

        data.insert(key, value);
    }

    setData(data);
}

void MessageListener::OnTokenReceived(const char *token)
{
    setToken(QString::fromUtf8(token));
}

void MessageListener::connectNotify(const QMetaMethod &signal)
{
    if (signal == QMetaMethod::fromSignal(&MessageListener::onMessageReceived)) {
        _messageReceivedConnected = true;

        if(_notifyMessageReceived) {
            emit onMessageReceived();
            _notifyMessageReceived = false;
        }
    }

    if (signal == QMetaMethod::fromSignal(&MessageListener::onTokenReceived)) {
        _tokenReceivedConnected = true;

        if(_notifyTokenReceived) {
            emit onTokenReceived();
            _notifyTokenReceived = false;
        }
    }
}

QVariantMap MessageListener::data()
{
    return _data;
}

void MessageListener::setData(const QVariantMap &data)
{
    if (_data != data) {
        _notifyMessageReceived = true;
        _data = data;

        if(_messageReceivedConnected) {
            emit onMessageReceived();
            _notifyMessageReceived = false;
        }
    }
}

QString MessageListener::token()
{
    QMutexLocker lock { &_tokenMutex };
    return _token;
}

void MessageListener::setToken(const QString &token)
{
    QMutexLocker lock { &_tokenMutex };
    if (_token != token) {
        _token = token;
        lock.unlock();

        _notifyTokenReceived = true;

        if(_tokenReceivedConnected) {
            emit onTokenReceived();
            _notifyTokenReceived = false;
        }
    }
}
<|endoftext|>


<commit_before>/*=============================================================================

    This file is part of FLINT.

    FLINT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    FLINT 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 General Public License for more details.

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

=============================================================================*/
/******************************************************************************

    Copyright (C) 2013 Tom Bachmann

******************************************************************************/

#include <sstream>

#include "flintxx/vector.h"

#include "flintxx/test/helpers.h"
#include "flintxx/test/myint.h"
#include "fmpz_vecxx.h"

using namespace flint;

template<class Vec, class Tuple>
struct tuple_has
    : mp::or_<
          mp::equal_types<Vec, typename Tuple::head_t>,
          tuple_has<Vec, typename Tuple::tail_t> > { };
template<class Vec>
struct tuple_has<Vec, empty_tuple> : mp::false_ { };

template<class Vec, class Expr>
bool has_vector_temporaries(const Expr& e)
{
    return tuple_has<Vec*, typename Expr::ev_traits_t::rule_t::temporaries_t>::val;
}

template<class Vec>
void test(const Vec& original, const char* str = "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)")
{
    Vec v(original);
    Vec w(original);
    Vec u(original);
    Vec x(original);
    for(long i = 0;i < v.size();++i)
    {
        v[i] = i;
        w[i] = WORD(0);
        u[i] = 2*i;
        x[i] = 8*i;
    }
    tassert(v.to_string() == str);
    tassert(v == v);
    tassert(v != w);

    tassert(u == v + v);
    tassert(v == v + w);
    tassert(x == ((v + v) + (v + v)) + ((v + v) + (v + v)));
    tassert((v + w)[0] == v[0] + w[0]);

    if(!mp::equal_types<Vec, fmpz_vecxx>::val)
        tassert(!has_vector_temporaries<Vec>(
                    ((v + v) + (v + v)) + ((v + v) + (v + v))));
}

// b/c generic vector has no assignment implementation ...
void
test_fmpz_vecxx()
{
    fmpz_vecxx a(10), b(10);
    a[0] = 1;
    tassert(a != b);
    a = b;
    tassert(a == b);
}

int
main()
{
    std::cout << "vector....";

    typedef make_vector<myint>::type intvec;
    typedef make_vector_n<myint, 10>::type intvec10;

    test(intvec(10));
    test(intvec10());
    test(fmpz_vecxx(10), "10  0 1 2 3 4 5 6 7 8 9");
    test_fmpz_vecxx();

    std::cout << "PASS" << std::endl;

    return 0;
}
<commit_msg>Another long.<commit_after>/*=============================================================================

    This file is part of FLINT.

    FLINT is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    FLINT 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 General Public License for more details.

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

=============================================================================*/
/******************************************************************************

    Copyright (C) 2013 Tom Bachmann

******************************************************************************/

#include <sstream>

#include "flintxx/vector.h"

#include "flintxx/test/helpers.h"
#include "flintxx/test/myint.h"
#include "fmpz_vecxx.h"

using namespace flint;

template<class Vec, class Tuple>
struct tuple_has
    : mp::or_<
          mp::equal_types<Vec, typename Tuple::head_t>,
          tuple_has<Vec, typename Tuple::tail_t> > { };
template<class Vec>
struct tuple_has<Vec, empty_tuple> : mp::false_ { };

template<class Vec, class Expr>
bool has_vector_temporaries(const Expr& e)
{
    return tuple_has<Vec*, typename Expr::ev_traits_t::rule_t::temporaries_t>::val;
}

template<class Vec>
void test(const Vec& original, const char* str = "(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)")
{
    Vec v(original);
    Vec w(original);
    Vec u(original);
    Vec x(original);
    for(slong i = 0;i < v.size();++i)
    {
        v[i] = i;
        w[i] = WORD(0);
        u[i] = 2*i;
        x[i] = 8*i;
    }
    tassert(v.to_string() == str);
    tassert(v == v);
    tassert(v != w);

    tassert(u == v + v);
    tassert(v == v + w);
    tassert(x == ((v + v) + (v + v)) + ((v + v) + (v + v)));
    tassert((v + w)[0] == v[0] + w[0]);

    if(!mp::equal_types<Vec, fmpz_vecxx>::val)
        tassert(!has_vector_temporaries<Vec>(
                    ((v + v) + (v + v)) + ((v + v) + (v + v))));
}

// b/c generic vector has no assignment implementation ...
void
test_fmpz_vecxx()
{
    fmpz_vecxx a(10), b(10);
    a[0] = 1;
    tassert(a != b);
    a = b;
    tassert(a == b);
}

int
main()
{
    std::cout << "vector....";

    typedef make_vector<myint>::type intvec;
    typedef make_vector_n<myint, 10>::type intvec10;

    test(intvec(10));
    test(intvec10());
    test(fmpz_vecxx(10), "10  0 1 2 3 4 5 6 7 8 9");
    test_fmpz_vecxx();

    std::cout << "PASS" << std::endl;

    return 0;
}
<|endoftext|>


<commit_before>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// 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.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Kaveh Vahedipour
////////////////////////////////////////////////////////////////////////////////

#include "Job.h"

static std::string const DBServer = "DBServer";

using namespace arangodb::consensus;

bool arangodb::consensus::compareServerLists(Slice plan, Slice current) {
  if (!plan.isArray() || !current.isArray()) {
    return false;
  }
  std::vector<std::string> planv, currv;
  for (auto const& srv : VPackArrayIterator(plan)) {
    if (srv.isString()) {
      planv.push_back(srv.copyString());
    }
  }
  for (auto const& srv : VPackArrayIterator(current)) {
    if (srv.isString()) {
      currv.push_back(srv.copyString());
    }
  }
  bool equalLeader = !planv.empty() && !currv.empty() &&
                     planv.front() == currv.front();
  std::sort(planv.begin(), planv.end());
  std::sort(currv.begin(), currv.end());
  return equalLeader && currv == planv;
}

Job::Job(JOB_STATUS status, Node const& snapshot, AgentInterface* agent,
         std::string const& jobId, std::string const& creator)
  : _status(status),
    _snapshot(snapshot),
    _agent(agent),
    _jobId(jobId),
    _creator(creator),
    _jb(nullptr) {
}

Job::~Job() {}

// this will be initialized in the AgencyFeature
std::string Job::agencyPrefix = "/arango";

bool Job::finish(
  std::string const& server, std::string const& shard,
  bool success, std::string const& reason, query_t const payload) {

  Builder pending, finished;

  // Get todo entry
  bool started = false;
  { VPackArrayBuilder guard(&pending);
    if (_snapshot.exists(pendingPrefix + _jobId).size() == 3) {
      _snapshot(pendingPrefix + _jobId).toBuilder(pending);
      started = true;
    } else if (_snapshot.exists(toDoPrefix + _jobId).size() == 3) {
      _snapshot(toDoPrefix + _jobId).toBuilder(pending);
    } else {
      LOG_TOPIC(DEBUG, Logger::AGENCY)
        << "Nothing in pending to finish up for job " << _jobId;
      return false;
    }
  }

  std::string jobType;
  try {
    jobType = pending.slice()[0].get("type").copyString();
  } catch (std::exception const&) {
    LOG_TOPIC(WARN, Logger::AGENCY)
      << "Failed to obtain type of job " << _jobId;
  }

  // Prepare pending entry, block toserver
  { VPackArrayBuilder guard(&finished);
    VPackObjectBuilder guard2(&finished);

    addPutJobIntoSomewhere(finished, success ? "Finished" : "Failed",
                           pending.slice()[0], reason);

    addRemoveJobFromSomewhere(finished, "ToDo", _jobId);
    addRemoveJobFromSomewhere(finished, "Pending", _jobId);

    // Additional payload, which is to be executed in the finish transaction
    if (payload != nullptr) {
      Slice slice = payload->slice();
      TRI_ASSERT(slice.isObject());
      if (slice.length() > 0) {
        for (auto const& oper : VPackObjectIterator(slice)) {
          finished.add(oper.key.copyString(), oper.value);
        }
      }
    }

    // --- Remove blocks if specified:
    if (started && !server.empty()) {
      addReleaseServer(finished, server);
    }
    if (started && !shard.empty()) {
      addReleaseShard(finished, shard);
    }

  }  // close object and array

  write_ret_t res = singleWriteTransaction(_agent, finished);
  if (res.accepted && res.indices.size() == 1 && res.indices[0]) {
    LOG_TOPIC(DEBUG, Logger::AGENCY)
      << "Successfully finished job " << jobType << "(" << _jobId << ")";
    _status = (success ? FINISHED : FAILED);
    return true;
  }

  return false;
}

std::string Job::randomIdleGoodAvailableServer(
  Node const& snap, std::vector<std::string> const& exclude) {

  std::vector<std::string> as = availableServers(snap);
  std::string ret;
  auto ex(exclude);

  // ungood;
  try {
    for (auto const& srv : snap(healthPrefix).children()) {
      if ((*srv.second)("Status").toJson() != "GOOD") {
        ex.push_back(srv.first);
      }
    }
  } catch (...) {}

  // blocked;
  try {
    for (auto const& srv : snap(blockedServersPrefix).children()) {
      ex.push_back(srv.first);
    }
  } catch (...) {}

  // Remove excluded servers
  std::sort(std::begin(ex), std::end(ex));
  as.erase(
    std::remove_if(
      std::begin(as),std::end(as),
      [&](std::string const& s){
        return std::binary_search(
          std::begin(ex), std::end(ex), s);}), std::end(as));

  // Choose random server from rest
  if (!as.empty()) {
    if (as.size() > 1) {
      std::random_shuffle(as.begin(), as.end());
    }
    ret = as.front();
  }

  return ret;

}

std::string Job::randomIdleGoodAvailableServer(Node const& snap,
                                               Slice const& exclude) {

  std::vector<std::string> ev;
  if (exclude.isArray()) {
    for (const auto& s : VPackArrayIterator(exclude)) {
      if (s.isString()) {
        ev.push_back(s.copyString());
      }
    }
  }
  return randomIdleGoodAvailableServer(snap,ev);

}

std::vector<std::string> Job::availableServers(Node const& snapshot) {

  std::vector<std::string> ret;

  // Get servers from plan
  Node::Children const& dbservers = snapshot(plannedServers).children();
  for (auto const& srv : dbservers) {
    ret.push_back(srv.first);
  }

  // Remove cleaned servers from ist
  try {
    for (auto const& srv :
           VPackArrayIterator(snapshot(cleanedPrefix).slice())) {
      ret.erase(
        std::remove(ret.begin(), ret.end(), srv.copyString()),
        ret.end());
    }
  } catch (...) {}

  // Remove failed servers from list
  try {
    for (auto const& srv :
           VPackObjectIterator(snapshot(failedServersPrefix).slice())) {
      ret.erase(
        std::remove(ret.begin(), ret.end(), srv.key.copyString()),
        ret.end());
    }
  } catch (...) {}

  return ret;

}

std::vector<Job::shard_t> Job::clones(
  Node const& snapshot, std::string const& database,
  std::string const& collection, std::string const& shard) {

  std::vector<shard_t> ret;
  ret.emplace_back(collection, shard);  // add (collection, shard) as first item

  try {
    std::string databasePath = planColPrefix + database,
      planPath = databasePath + "/" + collection + "/shards";

    auto myshards = snapshot(planPath).children();
    auto steps = std::distance(myshards.begin(), myshards.find(shard));

    for (const auto& colptr : snapshot(databasePath).children()) { // collections

      auto const col = *colptr.second;
      auto const otherCollection = colptr.first;

      try {
        std::string const& prototype =
          col("distributeShardsLike").slice().copyString();
        if (otherCollection != collection && prototype == collection) {
          auto othershards = col("shards").children();
          auto opos = othershards.begin();
          std::advance(opos, steps);
          auto const& otherShard = opos->first;
          ret.emplace_back(otherCollection, otherShard);
        }
      } catch(...) {}

    }
  } catch (...) {
    ret.clear();
    ret.emplace_back(collection, shard);
  }

  return ret;
}

std::string Job::findNonblockedCommonHealthyInSyncFollower( // Which is in "GOOD" health
  Node const& snap, std::string const& db, std::string const& col,
  std::string const& shrd) {

  auto cs = clones(snap, db, col, shrd);       // clones
  auto nclones = cs.size();                    // #clones
  std::map<std::string,bool> good;

  for (const auto& i : snap(healthPrefix).children()) {
    good[i.first] = ((*i.second)("Status").toJson() == "GOOD");
  }

  std::map<std::string,size_t> currentServers;
  for (const auto& clone : cs) {
    auto currentShardPath =
      curColPrefix + db + "/" + clone.collection + "/"
      + clone.shard + "/servers";
    auto plannedShardPath =
      planColPrefix + db + "/" + clone.collection + "/shards/"
      + clone.shard;
    size_t i = 0;
    for (const auto& server : VPackArrayIterator(snap(currentShardPath).getArray())) {
      auto id = server.copyString();
      if (i++ == 0) {
        // Skip leader
        continue;
      }

      if (!good[id]) {
        // Skip unhealthy servers
        continue;
      }

      if (snap.has(blockedServersPrefix + id)) {
        // server is blocked
        continue;
      }

      // check if it is also part of the plan...because if not the soon to be leader
      // will drop the collection
      bool found = false;
      for (const auto& plannedServer : VPackArrayIterator(snap(plannedShardPath).getArray())) {
        if (plannedServer == server) {
          found = true;
          continue;
        }
      }

      if (!found) {
        continue;
      }

      // increment a counter for this server
      // and in case it is applicable for ALL our distributeLikeShards
      // finally return it :)
      if (++currentServers[id] == nclones) {
        return id;
      }
    }
  }

  return std::string();

}

std::string Job::uuidLookup (std::string const& shortID) {
  for (auto const& uuid : _snapshot(mapUniqueToShortID).children()) {
    if ((*uuid.second)("ShortName").getString() == shortID) {
      return uuid.first;
    }
  }
  return std::string();
}

std::string Job::id(std::string const& idOrShortName) {
  std::string id = uuidLookup(idOrShortName);
  if (!id.empty()) {
    return id;
  }
  return idOrShortName;
}

bool Job::abortable(Node const& snapshot, std::string const& jobId) {

  if (!snapshot.has(pendingPrefix + jobId)) {
    return false;
  }
  auto const& job = snapshot(pendingPrefix + jobId);
  if (!job.has("type")) {
    return false;
  }
  auto const& type = job("type").getString();

  if (type == "failedServer" || type == "failedLeader") {
    return false;
  } else if (type == "addFollower" || type == "moveShard" ||
             type == "cleanOutServer") {
    return true;
  }

  // We should never get here
  TRI_ASSERT(false);
  return false;
}

void Job::doForAllShards(Node const& snapshot,
	std::string& database,
	std::vector<shard_t>& shards,
  std::function<void(Slice plan, Slice current, std::string& planPath)> worker) {
	for (auto const& collShard : shards) {
		std::string shard = collShard.shard;
		std::string collection = collShard.collection;

    std::string planPath =
      planColPrefix + database + "/" + collection + "/shards/" + shard;
    std::string curPath = curColPrefix + database + "/" + collection
                          + "/" + shard + "/servers";

		Slice plan = snapshot(planPath).slice();
		Slice current = snapshot(curPath).slice();

    worker(plan, current, planPath);
  }
}

void Job::addIncreasePlanVersion(Builder& trx) {
  trx.add(VPackValue(planVersion));
  { VPackObjectBuilder guard(&trx);
    trx.add("op", VPackValue("increment"));
  }
}

void Job::addRemoveJobFromSomewhere(Builder& trx, std::string where,
  std::string jobId) {
  trx.add(VPackValue("/Target/" + where + "/" + jobId));
  { VPackObjectBuilder guard(&trx);
    trx.add("op", VPackValue("delete"));
  }
}

void Job::addPutJobIntoSomewhere(Builder& trx, std::string where, Slice job,
    std::string reason) {
  Slice jobIdSlice = job.get("jobId");
  TRI_ASSERT(jobIdSlice.isString());
  std::string jobId = jobIdSlice.copyString();
  trx.add(VPackValue("/Target/" + where + "/" + jobId));
  { VPackObjectBuilder guard(&trx);
    if (where == "Pending") {
      trx.add("timeStarted",
        VPackValue(timepointToString(std::chrono::system_clock::now())));
    } else {
      trx.add("timeFinished",
        VPackValue(timepointToString(std::chrono::system_clock::now())));
    }
    for (auto const& obj : VPackObjectIterator(job)) {
      trx.add(obj.key.copyString(), obj.value);
    }
    if (!reason.empty()) {
      trx.add("reason", VPackValue(reason));
    }
  }
}

void Job::addPreconditionCollectionStillThere(Builder& pre,
    std::string database, std::string collection) {
  std::string planPath
      = planColPrefix + database + "/" + collection;
  pre.add(VPackValue(planPath));
  { VPackObjectBuilder guard(&pre);
    pre.add("oldEmpty", VPackValue(false));
  }
}

void Job::addPreconditionServerNotBlocked(Builder& pre, std::string server) {
	pre.add(VPackValue(blockedServersPrefix + server));
	{ VPackObjectBuilder serverLockEmpty(&pre);
		pre.add("oldEmpty", VPackValue(true));
	}
}

void Job::addPreconditionServerGood(Builder& pre, std::string server) {
	pre.add(VPackValue(healthPrefix + server + "/Status"));
	{ VPackObjectBuilder serverGood(&pre);
		pre.add("old", VPackValue("GOOD"));
	}
}

void Job::addPreconditionShardNotBlocked(Builder& pre, std::string shard) {
	pre.add(VPackValue(blockedShardsPrefix + shard));
	{ VPackObjectBuilder shardLockEmpty(&pre);
		pre.add("oldEmpty", VPackValue(true));
	}
}

void Job::addPreconditionUnchanged(Builder& pre,
    std::string key, Slice value) {
  pre.add(VPackValue(key));
  { VPackObjectBuilder guard(&pre);
    pre.add("old", value);
  }
}

void Job::addBlockServer(Builder& trx, std::string server, std::string jobId) {
  trx.add(blockedServersPrefix + server, VPackValue(jobId));
}

void Job::addBlockShard(Builder& trx, std::string shard, std::string jobId) {
  trx.add(blockedShardsPrefix + shard, VPackValue(jobId));
}

void Job::addReleaseServer(Builder& trx, std::string server) {
  trx.add(VPackValue(blockedServersPrefix + server));
  { VPackObjectBuilder guard(&trx);
    trx.add("op", VPackValue("delete"));
  }
}

void Job::addReleaseShard(Builder& trx, std::string shard) {
  trx.add(VPackValue(blockedShardsPrefix + shard));
  { VPackObjectBuilder guard(&trx);
    trx.add("op", VPackValue("delete"));
  }
}

std::string Job::checkServerGood(Node const& snapshot,
                                 std::string const& server) {
  if (!snapshot.has(healthPrefix + server + "/Status")) {
    return "UNCLEAR";
  }
  if (snapshot(healthPrefix + server + "/Status").getString() != "GOOD") {
    return "UNHEALTHY";
  }
  return "GOOD";
}

<commit_msg>availableServers fixed<commit_after>////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// 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.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Kaveh Vahedipour
////////////////////////////////////////////////////////////////////////////////

#include "Job.h"

static std::string const DBServer = "DBServer";

using namespace arangodb::consensus;

bool arangodb::consensus::compareServerLists(Slice plan, Slice current) {
  if (!plan.isArray() || !current.isArray()) {
    return false;
  }
  std::vector<std::string> planv, currv;
  for (auto const& srv : VPackArrayIterator(plan)) {
    if (srv.isString()) {
      planv.push_back(srv.copyString());
    }
  }
  for (auto const& srv : VPackArrayIterator(current)) {
    if (srv.isString()) {
      currv.push_back(srv.copyString());
    }
  }
  bool equalLeader = !planv.empty() && !currv.empty() &&
                     planv.front() == currv.front();
  std::sort(planv.begin(), planv.end());
  std::sort(currv.begin(), currv.end());
  return equalLeader && currv == planv;
}

Job::Job(JOB_STATUS status, Node const& snapshot, AgentInterface* agent,
         std::string const& jobId, std::string const& creator)
  : _status(status),
    _snapshot(snapshot),
    _agent(agent),
    _jobId(jobId),
    _creator(creator),
    _jb(nullptr) {
}

Job::~Job() {}

// this will be initialized in the AgencyFeature
std::string Job::agencyPrefix = "/arango";

bool Job::finish(
  std::string const& server, std::string const& shard,
  bool success, std::string const& reason, query_t const payload) {

  Builder pending, finished;

  // Get todo entry
  bool started = false;
  { VPackArrayBuilder guard(&pending);
    if (_snapshot.exists(pendingPrefix + _jobId).size() == 3) {
      _snapshot(pendingPrefix + _jobId).toBuilder(pending);
      started = true;
    } else if (_snapshot.exists(toDoPrefix + _jobId).size() == 3) {
      _snapshot(toDoPrefix + _jobId).toBuilder(pending);
    } else {
      LOG_TOPIC(DEBUG, Logger::AGENCY)
        << "Nothing in pending to finish up for job " << _jobId;
      return false;
    }
  }

  std::string jobType;
  try {
    jobType = pending.slice()[0].get("type").copyString();
  } catch (std::exception const&) {
    LOG_TOPIC(WARN, Logger::AGENCY)
      << "Failed to obtain type of job " << _jobId;
  }

  // Prepare pending entry, block toserver
  { VPackArrayBuilder guard(&finished);
    VPackObjectBuilder guard2(&finished);

    addPutJobIntoSomewhere(finished, success ? "Finished" : "Failed",
                           pending.slice()[0], reason);

    addRemoveJobFromSomewhere(finished, "ToDo", _jobId);
    addRemoveJobFromSomewhere(finished, "Pending", _jobId);

    // Additional payload, which is to be executed in the finish transaction
    if (payload != nullptr) {
      Slice slice = payload->slice();
      TRI_ASSERT(slice.isObject());
      if (slice.length() > 0) {
        for (auto const& oper : VPackObjectIterator(slice)) {
          finished.add(oper.key.copyString(), oper.value);
        }
      }
    }

    // --- Remove blocks if specified:
    if (started && !server.empty()) {
      addReleaseServer(finished, server);
    }
    if (started && !shard.empty()) {
      addReleaseShard(finished, shard);
    }

  }  // close object and array

  write_ret_t res = singleWriteTransaction(_agent, finished);
  if (res.accepted && res.indices.size() == 1 && res.indices[0]) {
    LOG_TOPIC(DEBUG, Logger::AGENCY)
      << "Successfully finished job " << jobType << "(" << _jobId << ")";
    _status = (success ? FINISHED : FAILED);
    return true;
  }

  return false;
}

std::string Job::randomIdleGoodAvailableServer(
  Node const& snap, std::vector<std::string> const& exclude) {

  std::vector<std::string> as = availableServers(snap);
  std::string ret;
  auto ex(exclude);

  // ungood;
  try {
    for (auto const& srv : snap(healthPrefix).children()) {
      if ((*srv.second)("Status").toJson() != "GOOD") {
        ex.push_back(srv.first);
      }
    }
  } catch (...) {}

  // blocked;
  try {
    for (auto const& srv : snap(blockedServersPrefix).children()) {
      ex.push_back(srv.first);
    }
  } catch (...) {}

  // Remove excluded servers
  std::sort(std::begin(ex), std::end(ex));
  as.erase(
    std::remove_if(
      std::begin(as),std::end(as),
      [&](std::string const& s){
        return std::binary_search(
          std::begin(ex), std::end(ex), s);}), std::end(as));

  // Choose random server from rest
  if (!as.empty()) {
    if (as.size() > 1) {
      std::random_shuffle(as.begin(), as.end());
    }
    ret = as.front();
  }

  return ret;

}

std::string Job::randomIdleGoodAvailableServer(Node const& snap,
                                               Slice const& exclude) {

  std::vector<std::string> ev;
  if (exclude.isArray()) {
    for (const auto& s : VPackArrayIterator(exclude)) {
      if (s.isString()) {
        ev.push_back(s.copyString());
      }
    }
  }
  return randomIdleGoodAvailableServer(snap,ev);

}

std::vector<std::string> Job::availableServers(Node const& snapshot) {

  std::vector<std::string> ret;

  // Get servers from plan
  Node::Children const& dbservers = snapshot(plannedServers).children();
  for (auto const& srv : dbservers) {
    ret.push_back(srv.first);
  }

  // Remove cleaned servers from ist
  try {
    for (auto const& srv :
           VPackArrayIterator(snapshot(cleanedPrefix).slice())) {
      ret.erase(
        std::remove(ret.begin(), ret.end(), srv.copyString()),
        ret.end());
    }
  } catch (...) {}

  // Remove failed servers from list
  try {
    for (auto const& srv : snapshot(failedServersPrefix).children()) {
      ret.erase(
        std::remove(ret.begin(), ret.end(), srv.first), ret.end());
    }
  } catch (...) {}

  return ret;

}

std::vector<Job::shard_t> Job::clones(
  Node const& snapshot, std::string const& database,
  std::string const& collection, std::string const& shard) {

  std::vector<shard_t> ret;
  ret.emplace_back(collection, shard);  // add (collection, shard) as first item

  try {
    std::string databasePath = planColPrefix + database,
      planPath = databasePath + "/" + collection + "/shards";

    auto myshards = snapshot(planPath).children();
    auto steps = std::distance(myshards.begin(), myshards.find(shard));

    for (const auto& colptr : snapshot(databasePath).children()) { // collections

      auto const col = *colptr.second;
      auto const otherCollection = colptr.first;

      try {
        std::string const& prototype =
          col("distributeShardsLike").slice().copyString();
        if (otherCollection != collection && prototype == collection) {
          auto othershards = col("shards").children();
          auto opos = othershards.begin();
          std::advance(opos, steps);
          auto const& otherShard = opos->first;
          ret.emplace_back(otherCollection, otherShard);
        }
      } catch(...) {}

    }
  } catch (...) {
    ret.clear();
    ret.emplace_back(collection, shard);
  }

  return ret;
}

std::string Job::findNonblockedCommonHealthyInSyncFollower( // Which is in "GOOD" health
  Node const& snap, std::string const& db, std::string const& col,
  std::string const& shrd) {

  auto cs = clones(snap, db, col, shrd);       // clones
  auto nclones = cs.size();                    // #clones
  std::map<std::string,bool> good;

  for (const auto& i : snap(healthPrefix).children()) {
    good[i.first] = ((*i.second)("Status").toJson() == "GOOD");
  }

  std::map<std::string,size_t> currentServers;
  for (const auto& clone : cs) {
    auto currentShardPath =
      curColPrefix + db + "/" + clone.collection + "/"
      + clone.shard + "/servers";
    auto plannedShardPath =
      planColPrefix + db + "/" + clone.collection + "/shards/"
      + clone.shard;
    size_t i = 0;
    for (const auto& server : VPackArrayIterator(snap(currentShardPath).getArray())) {
      auto id = server.copyString();
      if (i++ == 0) {
        // Skip leader
        continue;
      }

      if (!good[id]) {
        // Skip unhealthy servers
        continue;
      }

      if (snap.has(blockedServersPrefix + id)) {
        // server is blocked
        continue;
      }

      // check if it is also part of the plan...because if not the soon to be leader
      // will drop the collection
      bool found = false;
      for (const auto& plannedServer : VPackArrayIterator(snap(plannedShardPath).getArray())) {
        if (plannedServer == server) {
          found = true;
          continue;
        }
      }

      if (!found) {
        continue;
      }

      // increment a counter for this server
      // and in case it is applicable for ALL our distributeLikeShards
      // finally return it :)
      if (++currentServers[id] == nclones) {
        return id;
      }
    }
  }

  return std::string();

}

std::string Job::uuidLookup (std::string const& shortID) {
  for (auto const& uuid : _snapshot(mapUniqueToShortID).children()) {
    if ((*uuid.second)("ShortName").getString() == shortID) {
      return uuid.first;
    }
  }
  return std::string();
}

std::string Job::id(std::string const& idOrShortName) {
  std::string id = uuidLookup(idOrShortName);
  if (!id.empty()) {
    return id;
  }
  return idOrShortName;
}

bool Job::abortable(Node const& snapshot, std::string const& jobId) {

  if (!snapshot.has(pendingPrefix + jobId)) {
    return false;
  }
  auto const& job = snapshot(pendingPrefix + jobId);
  if (!job.has("type")) {
    return false;
  }
  auto const& type = job("type").getString();

  if (type == "failedServer" || type == "failedLeader") {
    return false;
  } else if (type == "addFollower" || type == "moveShard" ||
             type == "cleanOutServer") {
    return true;
  }

  // We should never get here
  TRI_ASSERT(false);
  return false;
}

void Job::doForAllShards(Node const& snapshot,
	std::string& database,
	std::vector<shard_t>& shards,
  std::function<void(Slice plan, Slice current, std::string& planPath)> worker) {
	for (auto const& collShard : shards) {
		std::string shard = collShard.shard;
		std::string collection = collShard.collection;

    std::string planPath =
      planColPrefix + database + "/" + collection + "/shards/" + shard;
    std::string curPath = curColPrefix + database + "/" + collection
                          + "/" + shard + "/servers";

		Slice plan = snapshot(planPath).slice();
		Slice current = snapshot(curPath).slice();

    worker(plan, current, planPath);
  }
}

void Job::addIncreasePlanVersion(Builder& trx) {
  trx.add(VPackValue(planVersion));
  { VPackObjectBuilder guard(&trx);
    trx.add("op", VPackValue("increment"));
  }
}

void Job::addRemoveJobFromSomewhere(Builder& trx, std::string where,
  std::string jobId) {
  trx.add(VPackValue("/Target/" + where + "/" + jobId));
  { VPackObjectBuilder guard(&trx);
    trx.add("op", VPackValue("delete"));
  }
}

void Job::addPutJobIntoSomewhere(Builder& trx, std::string where, Slice job,
    std::string reason) {
  Slice jobIdSlice = job.get("jobId");
  TRI_ASSERT(jobIdSlice.isString());
  std::string jobId = jobIdSlice.copyString();
  trx.add(VPackValue("/Target/" + where + "/" + jobId));
  { VPackObjectBuilder guard(&trx);
    if (where == "Pending") {
      trx.add("timeStarted",
        VPackValue(timepointToString(std::chrono::system_clock::now())));
    } else {
      trx.add("timeFinished",
        VPackValue(timepointToString(std::chrono::system_clock::now())));
    }
    for (auto const& obj : VPackObjectIterator(job)) {
      trx.add(obj.key.copyString(), obj.value);
    }
    if (!reason.empty()) {
      trx.add("reason", VPackValue(reason));
    }
  }
}

void Job::addPreconditionCollectionStillThere(Builder& pre,
    std::string database, std::string collection) {
  std::string planPath
      = planColPrefix + database + "/" + collection;
  pre.add(VPackValue(planPath));
  { VPackObjectBuilder guard(&pre);
    pre.add("oldEmpty", VPackValue(false));
  }
}

void Job::addPreconditionServerNotBlocked(Builder& pre, std::string server) {
	pre.add(VPackValue(blockedServersPrefix + server));
	{ VPackObjectBuilder serverLockEmpty(&pre);
		pre.add("oldEmpty", VPackValue(true));
	}
}

void Job::addPreconditionServerGood(Builder& pre, std::string server) {
	pre.add(VPackValue(healthPrefix + server + "/Status"));
	{ VPackObjectBuilder serverGood(&pre);
		pre.add("old", VPackValue("GOOD"));
	}
}

void Job::addPreconditionShardNotBlocked(Builder& pre, std::string shard) {
	pre.add(VPackValue(blockedShardsPrefix + shard));
	{ VPackObjectBuilder shardLockEmpty(&pre);
		pre.add("oldEmpty", VPackValue(true));
	}
}

void Job::addPreconditionUnchanged(Builder& pre,
    std::string key, Slice value) {
  pre.add(VPackValue(key));
  { VPackObjectBuilder guard(&pre);
    pre.add("old", value);
  }
}

void Job::addBlockServer(Builder& trx, std::string server, std::string jobId) {
  trx.add(blockedServersPrefix + server, VPackValue(jobId));
}

void Job::addBlockShard(Builder& trx, std::string shard, std::string jobId) {
  trx.add(blockedShardsPrefix + shard, VPackValue(jobId));
}

void Job::addReleaseServer(Builder& trx, std::string server) {
  trx.add(VPackValue(blockedServersPrefix + server));
  { VPackObjectBuilder guard(&trx);
    trx.add("op", VPackValue("delete"));
  }
}

void Job::addReleaseShard(Builder& trx, std::string shard) {
  trx.add(VPackValue(blockedShardsPrefix + shard));
  { VPackObjectBuilder guard(&trx);
    trx.add("op", VPackValue("delete"));
  }
}

std::string Job::checkServerGood(Node const& snapshot,
                                 std::string const& server) {
  if (!snapshot.has(healthPrefix + server + "/Status")) {
    return "UNCLEAR";
  }
  if (snapshot(healthPrefix + server + "/Status").getString() != "GOOD") {
    return "UNHEALTHY";
  }
  return "GOOD";
}

<|endoftext|>