<commit_before>#include "MultithreadedServer.hpp"
#include "CMSException.hpp"
#include "NetworkException.hpp"
#include "ServerCommon.hpp"

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <tr1/memory>

using namespace boost::asio;

// NOTE: Code inspired from Blocking TCP Server example
// in Boost 1.52.0 documentation.

namespace cms
{

	void clientSession(SocketPtr socket, int threadNo,
		OrderManagerPtr orderManager,
		std::tr1::shared_ptr<CommandParser> commandParser)
	{
		try
		{
			while (true)
			{
				// Receive command from client
				char data[BUFFER_SIZE] = { 0 };
				boost::system::error_code error;
				socket->read_some(boost::asio::buffer(data), error);
				// If connection error with client, terminate the session
				// the server by breaking out of the loop
				if (error == error::eof) // connection cleanly closed
				{
					std::cout << "Client has closed connection" << std::endl;
					break;
				}
				else if (error)
				{
					std::stringstream errorMessage;
					errorMessage << "Connection error with client (error code " << error << ")";
					throw net::NetworkException(errorMessage.str());
				}
				// Parse command and execute it
				// If exception occurs in command parsing or execution,
				// then send exception message back to client
				std::string result;
				try
				{
					CommandPtr command = commandParser->parse(data);
				 	result = command->execute(orderManager);
				}
				catch (const CMSException& ex)
				{
					result = ex.what();
				}

				socket->send(boost::asio::buffer(&result[0], result.size()));
			}
		}
		catch (const std::exception& ex)
		{
			std::cout << "EXCEPTION THROWN IN THREAD " << threadNo << ": " << ex.what() << std::endl;

		}
		std::cout << "TERMINATING THREAD " << threadNo << std::endl;
	}

	MultithreadedServer::MultithreadedServer(int port) : port(port), threadCounter(0)
	{
	}

	void MultithreadedServer::run(OrderManagerPtr orderManager,
		std::tr1::shared_ptr<CommandParser> commandParser)
	{
		io_service ioService;
		ip::tcp::acceptor acceptor(ioService,
			ip::tcp::endpoint(ip::tcp::v4(), port));
		while (true)
		{
			// Wait until client requests a connection
			SocketPtr socketToClient(new ip::tcp::socket(ioService));
			acceptor.accept(*socketToClient);
			// Create a thread to handle the new client
			boost::thread clientSessionThread(boost::bind(
				clientSession, socketToClient, threadCounter,
				orderManager, commandParser));
			clientSessionThread.detach();

			threadCounter++;
		}

		// Client is connected! Serve the connected client
		// until it disconnects. Then, terminate the server
		
		std::cout << "TERMINATING SERVER" << std::endl;
	}

}<commit_msg>Added extra output to indicate when client has connected to server<commit_after>#include "MultithreadedServer.hpp"
#include "CMSException.hpp"
#include "NetworkException.hpp"
#include "ServerCommon.hpp"

#include <iostream>
#include <boost/asio.hpp>
#include <boost/thread/thread.hpp>
#include <tr1/memory>

using namespace boost::asio;

// NOTE: Code inspired from Blocking TCP Server example
// in Boost 1.52.0 documentation.

namespace cms
{

	void clientSession(SocketPtr socket, int threadNo,
		OrderManagerPtr orderManager,
		std::tr1::shared_ptr<CommandParser> commandParser)
	{
		try
		{
			while (true)
			{
				// Receive command from client
				char data[BUFFER_SIZE] = { 0 };
				boost::system::error_code error;
				socket->read_some(boost::asio::buffer(data), error);
				// If connection error with client, terminate the session
				// the server by breaking out of the loop
				if (error == error::eof) // connection cleanly closed
				{
					std::cout << "Client has closed connection" << std::endl;
					break;
				}
				else if (error)
				{
					std::stringstream errorMessage;
					errorMessage << "Connection error with client (error code " << error << ")";
					throw net::NetworkException(errorMessage.str());
				}
				// Parse command and execute it
				// If exception occurs in command parsing or execution,
				// then send exception message back to client
				std::string result;
				try
				{
					CommandPtr command = commandParser->parse(data);
				 	result = command->execute(orderManager);
				}
				catch (const CMSException& ex)
				{
					result = ex.what();
				}

				socket->send(boost::asio::buffer(&result[0], result.size()));
			}
		}
		catch (const std::exception& ex)
		{
			std::cout << "EXCEPTION THROWN IN THREAD " << threadNo << ": " << ex.what() << std::endl;

		}
		std::cout << "TERMINATING THREAD " << threadNo << std::endl;
	}

	MultithreadedServer::MultithreadedServer(int port) : port(port), threadCounter(0)
	{
	}

	void MultithreadedServer::run(OrderManagerPtr orderManager,
		std::tr1::shared_ptr<CommandParser> commandParser)
	{
		io_service ioService;
		ip::tcp::acceptor acceptor(ioService,
			ip::tcp::endpoint(ip::tcp::v4(), port));
		while (true)
		{
			// Wait until client requests a connection
			SocketPtr socketToClient(new ip::tcp::socket(ioService));
			acceptor.accept(*socketToClient);
			// Create a thread to handle the new client
			std::cout << "NEW CLIENT CONNECTED" << std::endl;
			std::cout << "STARTING THREAD " << threadCounter << std::endl;
			boost::thread clientSessionThread(boost::bind(
				clientSession, socketToClient, threadCounter,
				orderManager, commandParser));
			clientSessionThread.detach();

			threadCounter++;
		}

		// Client is connected! Serve the connected client
		// until it disconnects. Then, terminate the server
		
		std::cout << "TERMINATING SERVER" << std::endl;
	}

}
{'fraction_non_alphanumeric': 0.12964285714285714, 'fraction_numerical': 0.003928571428571429, 'mean_word_length': 3.7994858611825193, 'pattern_counts': {'":': 2, '<': 85, '<?xml version=': 0, '>': 23, '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': '7565328', 'n_tokens_mistral': 2028, 'n_tokens_neox': 1666, 'n_words': 450}