<commit_before>/*
 * Copyright (c) 2005, <NAME> and IFS Institute for Software at HSR Rapperswil, Switzerland
 * All rights reserved.
 *
 * This library/application is free software; you can redistribute and/or modify it under the terms of
 * the license that is included with this library/application in the file license.txt.
 */

//--- interface include --------------------------------------------------------
#include "RequestListener.h"

//--- standard modules used ----------------------------------------------------
#include "SysLog.h"
#include "Registry.h"
#include "Dbg.h"

//--- c-library modules used ---------------------------------------------------

//---- AcceptorFactoriesModule -----------------------------------------------------------
RegisterModule(AcceptorFactoriesModule);

AcceptorFactoriesModule::AcceptorFactoriesModule(const char *name) : WDModule(name)
{
	StartTrace1(AcceptorFactoriesModule.Ctor, "Name: <" << NotNull(name) << ">");
}

AcceptorFactoriesModule::~AcceptorFactoriesModule()
{
	StartTrace(AcceptorFactoriesModule.Dtor);
}

bool AcceptorFactoriesModule::Init(const ROAnything config)
{
	StartTrace(AcceptorFactoriesModule.Init);
	if (config.IsDefined("AcceptorFactories")) {
		HierarchyInstaller ai("AcceptorFactory");
		return RegisterableObject::Install(config["AcceptorFactories"], "AcceptorFactory", &ai);
	}
	return false;
}

bool AcceptorFactoriesModule::Finis()
{
	StartTrace(AcceptorFactoriesModule.Finis);
	return StdFinis("AcceptorFactory", "AcceptorFactories");
}

bool AcceptorFactoriesModule::ResetInit(const ROAnything config)
{
	// do nothing
	return true;
}

bool AcceptorFactoriesModule::ResetFinis(const ROAnything )
{
	// do nothing
	return true;
}

//---- AcceptorFactory -----------------------------------------------------------
AcceptorFactory::AcceptorFactory(const char *AcceptorFactoryName)
	: HierarchConfNamed(AcceptorFactoryName)
{
	StartTrace1(AcceptorFactory.Ctor, NotNull(AcceptorFactoryName));
}

AcceptorFactory::~AcceptorFactory()
{
	StartTrace(AcceptorFactory.Dtor);
}

Acceptor *AcceptorFactory::MakeAcceptor(AcceptorCallBack *ac)
{
	StartTrace(AcceptorFactory.MakeAcceptor);
	Trace("fName: " << fName);
	TraceAny(fConfig, "fConfig: ");

	const char *address = Lookup("Address", (const char *)0);
	long port = Lookup("Port", 80L);
	long backlog = Lookup("Backlog", 50L);

	Trace("Configuring: address<" << NotNull(address) << "> port: " << port << " backlog: " << backlog);
	return new Acceptor(address, port, backlog, ac);
}

bool AcceptorFactory::DoLoadConfig(const char *category)
{
	StartTrace1(AcceptorFactory.DoLoadConfig, "category: <" << NotNull(category) << ">");

	if ( HierarchConfNamed::DoLoadConfig(category) && fConfig.IsDefined(fName) ) {
		// AcceptorFactories use only a subset of the whole configuration file
		fConfig = fConfig[fName];
		Assert(!fConfig.IsNull());
		TraceAny(fConfig, "fConfig: ");
		return (!fConfig.IsNull());
	}

	Trace("fConfig is null, returning false");
	fConfig = Anything();
	return false;
}

bool AcceptorFactory::DoGetConfigName(const char *category, const char *objName, String &configFileName)
{
	StartTrace1(AcceptorFactory.DoGetConfigName, "category: <" << NotNull(category) << "> object: <" << NotNull(objName) << ">");
	configFileName = "Config";
	Trace("returning <" << configFileName << "> true");
	return true;
}

//---- registry interface
RegCacheImpl(AcceptorFactory);	// FindAcceptorFactory()

RegisterAcceptorFactory(AcceptorFactory);

//-- ListenerThread -----------------------------------------------------------------
ListenerThread::ListenerThread(AcceptorCallBack *ac)
	: Thread("ListenerThread"), fCallBack(ac), fAcceptor(0)
{
	StartTrace(ListenerThread.ListenerThread);
}

ListenerThread::~ListenerThread()
{
	StartTrace(ListenerThread.Dtor);

//	Terminate();
	delete fAcceptor;<commit_msg>acceptor factories objects can also have their config be reloaded  - added specific Termination/Initialization classes to achieve this<commit_after>/*
 * Copyright (c) 2005, <NAME> and IFS Institute for Software at HSR Rapperswil, Switzerland
 * All rights reserved.
 *
 * This library/application is free software; you can redistribute and/or modify it under the terms of
 * the license that is included with this library/application in the file license.txt.
 */

//--- interface include --------------------------------------------------------
#include "RequestListener.h"

//--- standard modules used ----------------------------------------------------
#include "SysLog.h"
#include "Registry.h"
#include "Dbg.h"

//--- c-library modules used ---------------------------------------------------

//---- AcceptorFactoriesReInitInstaller ------------------------------------------------------
/*! alias installer installs the same object with different names in the registry */
class EXPORTDECL_WDBASE AcceptorFactoriesReInitInstaller : public InstallerPolicy
{
public:
	AcceptorFactoriesReInitInstaller(const char *category)
		: InstallerPolicy(category)
	{}
	virtual ~AcceptorFactoriesReInitInstaller() {};

protected:
	virtual bool DoInstall(const ROAnything installerSpec, Registry *r) {
		StartTrace1(AcceptorFactoriesReInitInstaller.DoInstall, "cat <" << GetCategory() << ">");
		// nothing to do, base class does the right thing!
		return true;
	}
};

//---- AcceptorFactoriesReInitTerminator ------------------------------------------------------
class EXPORTDECL_WDBASE AcceptorFactoriesReInitTerminator : public TerminationPolicy
{
public:
	AcceptorFactoriesReInitTerminator(const char *category)
		: TerminationPolicy(category)
	{}
	virtual ~AcceptorFactoriesReInitTerminator() {};

protected:
	virtual bool DoTerminate(Registry *r) {
		StartTrace1(AcceptorFactoriesReInitTerminator.DoTerminate, "cat <" << GetCategory() << ">");
		// nothing to do, base class does the right thing!
		return true;
	}
};

//---- AcceptorFactoriesModule -----------------------------------------------------------
RegisterModule(AcceptorFactoriesModule);

AcceptorFactoriesModule::AcceptorFactoriesModule(const char *name) : WDModule(name)
{
	StartTrace1(AcceptorFactoriesModule.Ctor, "Name: <" << NotNull(name) << ">");
}

AcceptorFactoriesModule::~AcceptorFactoriesModule()
{
	StartTrace(AcceptorFactoriesModule.Dtor);
}

bool AcceptorFactoriesModule::Init(const ROAnything config)
{
	StartTrace(AcceptorFactoriesModule.Init);
	if (config.IsDefined("AcceptorFactories")) {
		HierarchyInstaller ai("AcceptorFactory");
		return RegisterableObject::Install(config["AcceptorFactories"], "AcceptorFactory", &ai);
	}
	return false;
}

bool AcceptorFactoriesModule::Finis()
{
	StartTrace(AcceptorFactoriesModule.Finis);
	return StdFinis("AcceptorFactory", "AcceptorFactories");
}

bool AcceptorFactoriesModule::ResetInit(const ROAnything config)
{
	AcceptorFactoriesReInitInstaller hi("AcceptorFactory");
	return RegisterableObject::Install(config["AcceptorFactories"], "AcceptorFactory", &hi);
}

bool AcceptorFactoriesModule::ResetFinis(const ROAnything )
{
	AcceptorFactoriesReInitTerminator at("AcceptorFactory");
	return RegisterableObject::ResetTerminate("AcceptorFactory", &at);
}

//---- AcceptorFactory -----------------------------------------------------------
AcceptorFactory::AcceptorFactory(const char *AcceptorFactoryName)
	: HierarchConfNamed(AcceptorFactoryName)
{
	StartTrace1(AcceptorFactory.Ctor, NotNull(AcceptorFactoryName));
}

AcceptorFactory::~AcceptorFactory()
{
	StartTrace(AcceptorFactory.Dtor);
}

Acceptor *AcceptorFactory::MakeAcceptor(AcceptorCallBack *ac)
{
	StartTrace(AcceptorFactory.MakeAcceptor);
	Trace("fName: " << fName);
	TraceAny(fConfig, "fConfig: ");

	const char *address = Lookup("Address", (const char *)0);
	long port = Lookup("Port", 80L);
	long backlog = Lookup("Backlog", 50L);

	Trace("Configuring: address<" << NotNull(address) << "> port: " << port << " backlog: " << backlog);
	return new Acceptor(address, port, backlog, ac);
}

bool AcceptorFactory::DoLoadConfig(const char *category)
{
	StartTrace1(AcceptorFactory.DoLoadConfig, "category: <" << NotNull(category) << "> object: <" << fName << ">");

	if ( HierarchConfNamed::DoLoadConfig(category) && fConfig.IsDefined(fName) ) {
		// AcceptorFactories use only a subset of the whole configuration file
		fConfig = fConfig[fName];
		Assert(!fConfig.IsNull());
		TraceAny(fConfig, "fConfig: ");
		return (!fConfig.IsNull());
	}
	String cfgFilename;
	DoGetConfigName(fCategory, fName, cfgFilename);
	SysLog::Info(String("AcceptorFactory::DoLoadConfig: no specific config entry for <") << fName << "> found in " << cfgFilename << ".any");
	fConfig = Anything();
	// because these object are hierarchical, it can be that some of them do not have their own config
	// so we must not fail here
	return true;
}

bool AcceptorFactory::DoGetConfigName(const char *category, const char *objName, String &configFileName)
{
	StartTrace1(AcceptorFactory.DoGetConfigName, "category: <" << NotNull(category) << "> object: <" << NotNull(objName) << ">");
	configFileName = "Config";
	Trace("returning <" << configFileName << "> true");
	return true;
}

//---- registry interface
RegCacheImpl(AcceptorFactory);	// FindAcceptorFactory()

RegisterAcceptorFactory(AcceptorFactory);

//-- ListenerThread -----------------------------------------------------------------
ListenerThread::ListenerThread(AcceptorCallBack *ac)
	: Thread("ListenerThread"), fCallBack(ac), fAcceptor(0)
{
	StartTrace(ListenerThread.ListenerThread);
}

ListenerThread::~ListenerThread()
{
	StartTrace(ListenerThread.Dtor);

//	Terminate();
	delete fAcceptor;
{'fraction_non_alphanumeric': 0.20465018852115627, 'fraction_numerical': 0.003142019271051529, 'mean_word_length': 3.8009049773755654, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 21, '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': '3845485', 'n_tokens_mistral': 2756, 'n_tokens_neox': 2690, 'n_words': 586}