<commit_before>/*
Copyright c1997-2007 <NAME>. All rights reserved.
This file is part of the Code Vault version 2.7
http://www.bombaydigital.com/
*/

#include "vmessageinputthread.h"

#include "vexception.h"
#include "vlogger.h"
#include "vmessage.h"
#include "vmessagepool.h"
#include "vclientsession.h"
#include "vbento.h"

// VMessageInputThread --------------------------------------------------------

VMessageInputThread::VMessageInputThread(const VString& name, VSocket* socket, VListenerThread* ownerThread, VServer* server, VMessagePool* messagePool)
: VSocketThread(name, socket, ownerThread),
mMessagePool(messagePool),
mSocketStream(socket, "VMessageInputThread"),
mInputStream(mSocketStream),
mConnected(false),
mSession(NULL),
mServer(server)
	{
	}

VMessageInputThread::~VMessageInputThread()
	{
	mMessagePool = NULL;
	mSession = NULL;
	mServer = NULL;
	}

void VMessageInputThread::run()
	{
	/*
	We process messages until we're "done". Done is flagged by the subclass
	marking the thread done when it sees fit. The subclass must catch any
	exceptions that are not catastrophic, because we are the last resort,
	and if we catch an exception we complete the thread, which will cause
	the connection to be shut down (the subclass may need to shut down
	additional resources by overridding run() and post-processing it).
	*/
	
	try
		{
		while (this->isRunning())
			this->_processNextRequest(); // Blocking read on socket; then message is dispatched.
		}
	catch (const VEOFException& /*ex*/)
		{
		// Usually just means the client has closed the connection.
		VLOGGER_MESSAGE_LEVEL(VLogger::kDebug, VString("[%s] VMessageInputThread connection closed.", mName.chars()));
		}
	catch (const VException& ex)
		{
		VLOGGER_MESSAGE_ERROR(VString("[%s] VMessageInputThread exiting due to top level exception #%d '%s'.", mName.chars(), ex.getError(), ex.what()));
		}
	catch (std::exception& ex)
		{
		VLOGGER_MESSAGE_ERROR(VString("[%s] VMessageInputThread exiting due to top level exception '%s'.", mName.chars(), ex.what()));
		}
	catch (...)
		{
		VLOGGER_MESSAGE_ERROR(VString("[%s] VMessageInputThread exiting due to top level unknown exception.", mName.chars()));
		}

	// This thread is done. Print our accumulated stats.

    mMessagePool->printStats();

	if (mSession != NULL)
		mSession->shutdown(this);
	}

void VMessageInputThread::attachSession(VClientSession* session)
	{
	mSession = session;<commit_msg>Check running state before logging supposed error that may just be socket closure during disconnect.<commit_after>/*
Copyright c1997-2007 <NAME>. All rights reserved.
This file is part of the Code Vault version 2.7
http://www.bombaydigital.com/
*/

#include "vmessageinputthread.h"

#include "vexception.h"
#include "vlogger.h"
#include "vmessage.h"
#include "vmessagepool.h"
#include "vclientsession.h"
#include "vbento.h"

// VMessageInputThread --------------------------------------------------------

VMessageInputThread::VMessageInputThread(const VString& name, VSocket* socket, VListenerThread* ownerThread, VServer* server, VMessagePool* messagePool)
: VSocketThread(name, socket, ownerThread),
mMessagePool(messagePool),
mSocketStream(socket, "VMessageInputThread"),
mInputStream(mSocketStream),
mConnected(false),
mSession(NULL),
mServer(server)
	{
	}

VMessageInputThread::~VMessageInputThread()
	{
	mMessagePool = NULL;
	mSession = NULL;
	mServer = NULL;
	}

void VMessageInputThread::run()
	{
	/*
	We process messages until we're "done". Done is flagged by the subclass
	marking the thread done when it sees fit. The subclass must catch any
	exceptions that are not catastrophic, because we are the last resort,
	and if we catch an exception we complete the thread, which will cause
	the connection to be shut down (the subclass may need to shut down
	additional resources by overridding run() and post-processing it).
    Note that in the "error" exceptions below, we don't bother logging if
    we know that the exception is due to expected input thread shutdown,
    recognized by the fact that we are no longer in running state.
	*/
	
	try
		{
		while (this->isRunning())
			this->_processNextRequest(); // Blocking read on socket; then message is dispatched.
		}
	catch (const VEOFException& /*ex*/)
		{
		// Usually just means the client has closed the connection.
		VLOGGER_MESSAGE_LEVEL(VLogger::kDebug, VString("[%s] VMessageInputThread connection closed.", mName.chars()));
		}
	catch (const VException& ex)
		{
        if (this->isRunning())
            VLOGGER_MESSAGE_ERROR(VString("[%s] VMessageInputThread exiting due to top level exception #%d '%s'.", mName.chars(), ex.getError(), ex.what()));
		}
	catch (std::exception& ex)
		{
        if (this->isRunning())
            VLOGGER_MESSAGE_ERROR(VString("[%s] VMessageInputThread exiting due to top level exception '%s'.", mName.chars(), ex.what()));
		}
	catch (...)
		{
        if (this->isRunning())
            VLOGGER_MESSAGE_ERROR(VString("[%s] VMessageInputThread exiting due to top level unknown exception.", mName.chars()));
		}

	// This thread is done. Print our accumulated stats.

    mMessagePool->printStats();

	if (mSession != NULL)
		mSession->shutdown(this);
	}

void VMessageInputThread::attachSession(VClientSession* session)
	{
	mSession = session;
{'fraction_non_alphanumeric': 0.133674630261661, 'fraction_numerical': 0.0037921880925293893, 'mean_word_length': 4.171568627450981, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3937274', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1568, 'n_words': 503}