<commit_before>/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

/* This must be included before anything else */
#if HAVE_CONFIG_H
#  include <config.h>
#endif


#include <yajr/internal/comms.hpp>

#include <yajr/internal/walkAndDumpHandlesCb.hpp>

#include <opflex/logging/internal/logging.hpp>

#include <uv.h>

namespace {

    void prepareAgainCB(uv_timer_t *) { LOG(DEBUG); }

}

namespace yajr {
    namespace comms {
        namespace internal {

#ifdef COMMS_DEBUG_OBJECT_COUNT
::boost::atomic<size_t> Peer::LoopData::counter(0);
#endif

std::ostream& operator << (
        std::ostream& os,
        Peer::LoopData const * lD)
                                        __attribute__((no_instrument_function));


void internal::Peer::LoopData::onPrepareLoop() {

    if (destroying_ && !refCount_) {

        CountHandle countHandle = { this, 0 };

        uv_walk(prepare_.loop, walkAndCountHandlesCb, &countHandle);

        if (countHandle.counter) {
            LOG(INFO) << "Still waiting on " << countHandle.counter << " handles";

            return;
        }

        LOG(INFO) << this << " Stopping and closing loop watcher";
        uv_prepare_stop(&prepare_);
        uv_close((uv_handle_t*)&prepare_, &fini);

        assert(prepare_.data == this);

        return;
    }

    uint64_t now = uv_now(prepare_.loop);

    peers[TO_RESOLVE]
        .clear_and_dispose(RetryPeer());

    peers[TO_LISTEN]
        .clear_and_dispose(RetryPeer());

    if (now - lastRun_ < 750) {
        goto prepared;
    }

    if (peers[RETRY_TO_CONNECT].begin() !=
        peers[RETRY_TO_CONNECT].end()) {

        LOG(INFO) << "retrying first RETRY_TO_CONNECT peer";

        /* retry just the first active peer in the queue */
        peers[RETRY_TO_CONNECT]
            .erase_and_dispose(peers[RETRY_TO_CONNECT].begin(), RetryPeer());

        LOG(DEBUG)
            << " Stopping prepareAgain_ @"
            << reinterpret_cast<void *>(&prepareAgain_)<commit_msg>check invariants on all peers as part of loop prepare<commit_after>/*
 * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

/* This must be included before anything else */
#if HAVE_CONFIG_H
#  include <config.h>
#endif


#include <yajr/internal/comms.hpp>

#include <yajr/internal/walkAndDumpHandlesCb.hpp>

#include <opflex/logging/internal/logging.hpp>

#include <uv.h>

namespace {

    void prepareAgainCB(uv_timer_t *) { LOG(DEBUG); }

}

namespace yajr {
    namespace comms {
        namespace internal {

#ifdef COMMS_DEBUG_OBJECT_COUNT
::boost::atomic<size_t> Peer::LoopData::counter(0);
#endif

std::ostream& operator << (
        std::ostream& os,
        Peer::LoopData const * lD)
                                        __attribute__((no_instrument_function));


void internal::Peer::LoopData::onPrepareLoop() {

    if (destroying_ && !refCount_) {

        CountHandle countHandle = { this, 0 };

        uv_walk(prepare_.loop, walkAndCountHandlesCb, &countHandle);

        if (countHandle.counter) {
            LOG(INFO) << "Still waiting on " << countHandle.counter << " handles";

            return;
        }

        LOG(INFO) << this << " Stopping and closing loop watcher";
        uv_prepare_stop(&prepare_);
        uv_close((uv_handle_t*)&prepare_, &fini);

        assert(prepare_.data == this);

        return;
    }

    uint64_t now = uv_now(prepare_.loop);

    peers[TO_RESOLVE]
        .clear_and_dispose(RetryPeer());

    peers[TO_LISTEN]
        .clear_and_dispose(RetryPeer());

#ifndef NDEBUG
    for (size_t state = 0; state < TOTAL_STATES; ++state) {
        for (Peer::List::const_iterator peer = peers[state].cbegin();
                peer != peers[state].cend();
                ++peer) {
            peer->__checkInvariants();
        }
    }
#endif

    if (now - lastRun_ < 750) {
        goto prepared;
    }

    if (peers[RETRY_TO_CONNECT].begin() !=
        peers[RETRY_TO_CONNECT].end()) {

        LOG(INFO) << "retrying first RETRY_TO_CONNECT peer";

        /* retry just the first active peer in the queue */
        peers[RETRY_TO_CONNECT]
            .erase_and_dispose(peers[RETRY_TO_CONNECT].begin(), RetryPeer());

        LOG(DEBUG)
            << " Stopping prepareAgain_ @"
            << reinterpret_cast<void *>(&prepareAgain_)
{'fraction_non_alphanumeric': 0.1213643021552626, 'fraction_numerical': 0.006486712701401967, 'mean_word_length': 2.740219092331768, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5189262', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1472, 'n_words': 334}