<commit_before>/**
 *  @file
 *  @author     2012 <NAME> (<EMAIL>)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  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.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#include "umundo/connection/Node.h"
#include "umundo/discovery/NodeQuery.h"
#include "umundo/common/Factory.h"

namespace umundo {

NodeQuery::NodeQuery(string domain, ResultSet<NodeStub>* listener) :
	_notifyImmediately(true), _domain(domain), _transport("tcp"), _listener(listener) {
}

NodeQuery::~NodeQuery() {
}

void NodeQuery::found(shared_ptr<NodeStub> node) {
	UMUNDO_LOCK(_mutex);
	if (_notifyImmediately) {
		if (_nodes.find(node->getUUID()) != _nodes.end()) {
//      LOG_DEBUG("Changed node %s", SHORT_UUID(node->getUUID()).c_str());
			_listener->changed(node);
		} else {
			LOG_DEBUG("Added node %s", SHORT_UUID(node->getUUID()).c_str());
			_listener->added(node);
			_nodes[node->getUUID()] = node;
		}
	} else {
		_pendingFinds.insert(node);
	}
	UMUNDO_UNLOCK(_mutex);
}

void NodeQuery::removed(shared_ptr<NodeStub> node) {
	UMUNDO_LOCK(_mutex);
	if (_notifyImmediately) {
		LOG_DEBUG("Removed node %s", SHORT_UUID(node->getUUID()).c_str());
		_listener->removed(node);
		_nodes.erase(node->getUUID());
	} else {
		_pendingRemovals.insert(node);
	}
	UMUNDO_UNLOCK(_mutex);
}

void NodeQuery::notifyImmediately(bool notifyImmediately) {
	_notifyImmediately = notifyImmediately;
}

void NodeQuery::notifyResultSet() {
	UMUNDO_LOCK(_mutex);
	set<shared_ptr<NodeStub> >::const_iterator nodeIter;

	for (nodeIter = _pendingRemovals.begin(); nodeIter != _pendingRemovals.end(); nodeIter++) {
		LOG_DEBUG("Removed node %s", SHORT_UUID((*nodeIter)->getUUID()).c_str());
		_listener->removed(*nodeIter);
		_nodes.erase((*nodeIter)->getUUID());
	}

	for (nodeIter = _pendingFinds.begin(); nodeIter != _pendingFinds.end(); nodeIter++) {
		if (_nodes.find((*nodeIter)->getUUID()) != _nodes.end()) {
//      LOG_DEBUG("Changed node %s", SHORT_UUID((*nodeIter)->getUUID()).c_str());
			_listener->changed(*nodeIter);<commit_msg>Hunting bug in discovery on new test-setup<commit_after>/**
 *  @file
 *  @author     2012 <NAME> (<EMAIL>)
 *  @copyright  Simplified BSD
 *
 *  @cond
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the FreeBSD license as published by the FreeBSD
 *  project.
 *
 *  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.
 *
 *  You should have received a copy of the FreeBSD license along with this
 *  program. If not, see <http://www.opensource.org/licenses/bsd-license>.
 *  @endcond
 */

#include "umundo/connection/Node.h"
#include "umundo/discovery/NodeQuery.h"
#include "umundo/common/Factory.h"

namespace umundo {

NodeQuery::NodeQuery(string domain, ResultSet<NodeStub>* listener) :
	_notifyImmediately(true), _domain(domain), _transport("tcp"), _listener(listener) {
}

NodeQuery::~NodeQuery() {
}

void NodeQuery::found(shared_ptr<NodeStub> node) {
	UMUNDO_LOCK(_mutex);
	assert(node.get() != NULL);
	if (_notifyImmediately) {
		if (_nodes.find(node->getUUID()) != _nodes.end()) {
//      LOG_DEBUG("Changed node %s", SHORT_UUID(node->getUUID()).c_str());
			_listener->changed(node);
		} else {
			LOG_DEBUG("Added node %s", SHORT_UUID(node->getUUID()).c_str());
			_listener->added(node);
			_nodes[node->getUUID()] = node;
		}
	} else {
		_pendingFinds.insert(node);
	}
	UMUNDO_UNLOCK(_mutex);
}

void NodeQuery::removed(shared_ptr<NodeStub> node) {
	UMUNDO_LOCK(_mutex);
	assert(node.get() != NULL);
	if (_notifyImmediately) {
		LOG_DEBUG("Removed node %s", SHORT_UUID(node->getUUID()).c_str());
		_listener->removed(node);
		_nodes.erase(node->getUUID());
	} else {
		_pendingRemovals.insert(node);
	}
	UMUNDO_UNLOCK(_mutex);
}

void NodeQuery::notifyImmediately(bool notifyImmediately) {
	_notifyImmediately = notifyImmediately;
}

void NodeQuery::notifyResultSet() {
	UMUNDO_LOCK(_mutex);
	set<shared_ptr<NodeStub> >::const_iterator nodeIter;

	for (nodeIter = _pendingRemovals.begin(); nodeIter != _pendingRemovals.end(); nodeIter++) {
		assert(node.get() != NULL);
		LOG_DEBUG("Removed node %s", SHORT_UUID((*nodeIter)->getUUID()).c_str());
		_listener->removed(*nodeIter);
		_nodes.erase((*nodeIter)->getUUID());
	}

	for (nodeIter = _pendingFinds.begin(); nodeIter != _pendingFinds.end(); nodeIter++) {
		assert(node.get() != NULL);
		if (_nodes.find((*nodeIter)->getUUID()) != _nodes.end()) {
//      LOG_DEBUG("Changed node %s", SHORT_UUID((*nodeIter)->getUUID()).c_str());
			_listener->changed(*nodeIter);
{'fraction_non_alphanumeric': 0.15897032815877382, 'fraction_numerical': 0.0015720180782078995, 'mean_word_length': 5.103117505995204, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4214616', 'n_tokens_mistral': 1784, 'n_tokens_neox': 1753, 'n_words': 367}