#pragma once

#include <vector>
#include <thread>
#include <boost/asio.hpp>
#include <boost/asio/system_timer.hpp>
#include "Terminal.h"

namespace smackbio{
	namespace logserver{

		namespace asio = boost::asio;
		using boost::asio::ip::tcp;
		using boost::system::error_code;

		class LogServer : private TerminalList
		{
		public:
			LogServer(OnMessageCallback cb, void* cb_context, int port, bool enable_v6);
			~LogServer();

		private:
			bool stopping_ = false;
			asio::io_service io_serv_;
			std::vector<std::thread> workers_;
			tcp::acceptor listener_v4_;
			tcp::acceptor listener_v6_;
			asio::system_timer keepalive_timer_;

		private:
			void begin_accept(tcp::acceptor& acceptor);
			void add_client(const std::shared_ptr<tcp::socket>&);

			void begin_keepalive();
			void disconnect_inactive(const std::shared_ptr<Terminal>&);

			template<typename F> void for_each_terminals(F);
		};
	} 
}
