#include "socketio.hpp"
#include <iostream>
#include <exception>
#include <random>
template<typename T>
std::ostream& operator<<(std::ostream& out, std::vector<T> o){
	out << "(";
	for(unsigned int i = 0;i < o.size();i++){
		if(o[i] == '\r')continue;
		out << o[i];
		if(i < (o.size() - 1))
			out << ", ";
	}
	return out << ")";
}
template<>
std::ostream& operator<< <char>(std::ostream& out, std::vector<char> o){
	for(unsigned int i = 0;i < o.size();i++){
		out << o[i];
	}
	return out;
}
int main(){
	std::uniform_int_distribution<unsigned char> dis(0,20);
	std::mt19937_64 gen;
	server_socket ssock(80);
	while(true){
		cppsocket sock = ssock.accept_connection();
		try{
			while(true){
				std::vector<char> vec = sock.receive();
				std::cout << "Received: " << vec << std::endl;
				std::string resp(100, 'a');
				for(unsigned int i = 0;i < resp.size();i++){
					resp[i] = dis(gen) + 'a';
				}
				sock.write("Hallo " + resp);
			}
			sock.close();
			std::cout << "Closed\n";
		}
		catch(const std::exception& e){
			std::cout << e.what() << std::endl;
		}
		catch(...){
			std::cout << "Unidentified exception" << std::endl;
		}
	}
}