

#ifndef __DCB_H__
#define __DCB_H__

#include <thread>

#include "m2etis/pubsub/DeliverCallbackInterface.h"

class DCB : public m2etis::pubsub::DeliverCallbackInterface {
public:
	DCB() : _counter(0), _amount(), _timeOut() {
	}
	void deliverCallback(const m2etis::message::M2etisMessage::Ptr m) {
		_counter++;
	}

	void waitUntil(unsigned int messageAmount, uint64_t timeout) {
		_amount = messageAmount;
		_timeOut = timeout;
		std::thread([this]() {
			auto startTime = std::chrono::high_resolution_clock::now();
			do {
				std::this_thread::sleep_for(std::chrono::milliseconds(100));
			} while (_counter < _amount && _timeOut > uint64_t(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - startTime).count()));
		}).join();
	}

	unsigned int _counter;
	unsigned int _amount;
	uint64_t _timeOut;
};

#endif /* __DCB_H__ */
