<commit_before>/*
 * Copyright 2017, <NAME>
 *
 * This is PUBLIC DOMAIN software so use at your own risk as it comes
 * with no warranties. This code is yours to share, use and modify without
 * any restrictions or obligations.
 *
 * For more information see conwrap/LICENSE or refer refer to http://unlicense.org
 *
 * Author: gimesketvirtadieni at gmail dot com (<NAME>)
 */

#pragma once

#include <functional>
#include <memory>
#include <optional>
#include <sstream>  // std::stringstream
#include <string>

#include "slim/Chunk.hpp"
#include "slim/EncoderBase.hpp"
#include "slim/log/log.hpp"
#include "slim/util/AsyncWriter.hpp"
#include "slim/util/ExpandableBuffer.hpp"


namespace slim
{
	namespace proto
	{
		template<typename ConnectionType>
		class StreamingSession
		{
			public:
				StreamingSession(std::reference_wrapper<ConnectionType> co, std::unique_ptr<EncoderBase> e, std::string id)
				: connection{co}
				, encoderPtr{std::move(e)}
				, clientID{id}
				{
					LOG(DEBUG) << LABELS{"proto"} << "HTTP session object was created (id=" << this << ")";

					// creating response string
					std::stringstream ss;
					ss << "HTTP/1.1 200 OK\r\n"
					   << "Server: SlimStreamer ("
					   // TODO: provide version to the constructor
					   << VERSION
					   << ")\r\n"
					   << "Connection: close\r\n"
					   << "Content-Type: " << encoderPtr->getMIME() << "\r\n"
					   << "\r\n";

					// sending response string
					connection.get().write(ss.str());
				}

				virtual ~StreamingSession()
				{
					LOG(DEBUG) << LABELS{"proto"} << "HTTP session object was deleted (id=" << this << ")";
				}

				StreamingSession(const StreamingSession&) = delete;             // non-copyable
				StreamingSession& operator=(const StreamingSession&) = delete;  // non-assignable
				StreamingSession(StreamingSession&& rhs) = delete;              // non-movable
				StreamingSession& operator=(StreamingSession&& rhs) = delete;   // non-movable-assignable

				inline auto getClientID()
				{
					return clientID;
				}

				inline void onChunk(Chunk& chunk)
				{
					if (encoderPtr->getSamplingRate() == chunk.getSamplingRate())
					{
						encoderPtr->encode(chunk.getData(), chunk.getSize());
					}
					else
					{
						LOG(WARNING) << LABELS{"proto"} << "Closing HTTP connection due to different sampling rate used by a client";
						connection.get().stop();
					}
				}

				inline void onRequest(unsigned char* buffer, std::size_t size)
				{
					// TODO: make more strick validation
					std::string get{"GET"};
					std::string s{(char*)buffer, get.size()};<commit_msg>Minor change<commit_after>/*
 * Copyright 2017, <NAME>
 *
 * This is PUBLIC DOMAIN software so use at your own risk as it comes
 * with no warranties. This code is yours to share, use and modify without
 * any restrictions or obligations.
 *
 * For more information see conwrap/LICENSE or refer refer to http://unlicense.org
 *
 * Author: gimesketvirtadieni at gmail dot com (<NAME>)
 */

#pragma once

#include <functional>
#include <memory>
#include <optional>
#include <sstream>  // std::stringstream
#include <string>

#include "slim/Chunk.hpp"
#include "slim/EncoderBase.hpp"
#include "slim/log/log.hpp"
#include "slim/util/AsyncWriter.hpp"
#include "slim/util/ExpandableBuffer.hpp"


namespace slim
{
	namespace proto
	{
		template<typename ConnectionType>
		class StreamingSession
		{
			public:
				StreamingSession(std::reference_wrapper<ConnectionType> co, std::unique_ptr<EncoderBase> e, std::string id)
				: connection{co}
				, encoderPtr{std::move(e)}
				, clientID{id}
				{
					LOG(DEBUG) << LABELS{"proto"} << "HTTP session object was created (id=" << this << ")";

					// creating response string
					std::stringstream ss;
					ss << "HTTP/1.1 200 OK\r\n"
					   << "Server: SlimStreamer ("
					   // TODO: provide version to the constructor
					   << VERSION
					   << ")\r\n"
					   << "Connection: close\r\n"
					   << "Content-Type: " << encoderPtr->getMIME() << "\r\n"
					   << "\r\n";

					// sending response string
					connection.get().write(ss.str());
				}

				virtual ~StreamingSession()
				{
					LOG(DEBUG) << LABELS{"proto"} << "HTTP session object was deleted (id=" << this << ")";
				}

				StreamingSession(const StreamingSession&) = delete;             // non-copyable
				StreamingSession& operator=(const StreamingSession&) = delete;  // non-assignable
				StreamingSession(StreamingSession&& rhs) = delete;              // non-movable
				StreamingSession& operator=(StreamingSession&& rhs) = delete;   // non-movable-assignable

				inline auto getClientID()
				{
					return clientID;
				}

				inline void onChunk(Chunk& chunk)
				{
					if (encoderPtr->getSamplingRate() == chunk.getSamplingRate())
					{
						encoderPtr->encode(chunk.getData(), chunk.getSize());
					}
					else
					{
						LOG(WARNING) << LABELS{"proto"} << "Closing HTTP connection due to different sampling rate used by a client (session rate=" << encoderPtr->getSamplingRate() << "; chunk rate=" << chunk.getSamplingRate() << ")";
						connection.get().stop();
					}
				}

				inline void onRequest(unsigned char* buffer, std::size_t size)
				{
					// TODO: make more strick validation
					std::string get{"GET"};
					std::string s{(char*)buffer, get.size()};
{'fraction_non_alphanumeric': 0.14156626506024098, 'fraction_numerical': 0.00338855421686747, 'mean_word_length': 3.061926605504587, 'pattern_counts': {'":': 0, '<': 107, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5268632', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1604, 'n_words': 420}