Click here to Skip to main content
15,881,380 members
Articles / Desktop Programming / MFC

.NET Dynamic Software Load Balancing

Rate me:
Please Sign up or sign in to vote.
4.96/5 (111 votes)
9 Dec 200271 min read 510.5K   4.5K   242  
A Draft Implementation of an Idea for .NET Dynamic Software Load Balancing
//////////////////////////////////////////////////////////////////////////////
// .NET Dynamic Software Load Balancing
// A Draft Implementation of an Idea for .NET Dynamic Software Load Balancing
// (c) 2002, Stoyan Damov (stoyan_damov@hotmail.com)
// The software comes �AS IS�, with all faults and no warranties.
//////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LoadReportingServer.h"
#include "ReportingWorker.h"

namespace SoftwareLoadBalancing
{

LoadReportingServer::LoadReportingServer () :
	Tracer (S"LoadReportingServer")
{
	Trace (S"Initializing...");

	Trace (S"Configuring...");
	
	configurator = Configurator::Instance;
	
	String __gc* ip = configurator->GetText (CS_LRS, CK_LRS_IP_ADDRESS, 0);
	DEBUG_ASSERT (0 != ip);
	if (0 == ip)
		throw (new InvalidOperationException (
			S"IP address is not configured"));
	ipAddress = IpHelper::GetIpAddress (ip);
	DEBUG_ASSERT (0 != ipAddress);
	if (0 == ipAddress)
		throw (new InvalidOperationException (
			S"IP address is invalid"));

	port = configurator->GetInt (CS_LRS, CK_LRS_PORT, 0);
	DEBUG_ASSERT (0 != port);
	if (0 == port)
		throw (new InvalidOperationException (
			S"Port is not configured"));

	reportInterval = configurator->GetInt (
		CS_LRS, 
		CK_LRS_INTERVAL, 
		DefaultReportInterval);
			
	DEBUG_ASSERT (0 != configurator->Counters);
	if (0 == configurator->Counters)
		throw (new InvalidOperationException (
			S"Performance counters are not configured"));
	performanceCounters = configurator->Counters;

	Trace (S"Configuration done.");

	status = ServerStatus::Stopped;
	workerThread = 0;
	eventWorkerThreadDone = 0;
	workerThreadSucceeded = false;
	
	Trace (S"Initialization done.");
}

LoadReportingServer::~LoadReportingServer ()
{
	Trace (S"Terminating...");
	
	DEBUG_ASSERT (	status == ServerStatus::Stopped || 
					status == ServerStatus::Stopping);
	if (! (status == ServerStatus::Stopped || status == ServerStatus::Stopping))
		Stop ();
			
	Trace (S"Termination done.");
}

void LoadReportingServer::Start ()
{
	Trace (S"Starting...");

	DEBUG_ASSERT (! (status == ServerStatus::Starting ||
					 (status == ServerStatus::Started)));
	if (status == ServerStatus::Starting || status == ServerStatus::Started)
		return;
	
	// subscribe the server to receive notification when the configuration
	// changes (the underlying configuration file is modified)
	//
	configurator->add_Changed (
		new Configurator::ChangedEventHandler (
			this, &LoadReportingServer::OnConfigurationChanged));

	status = ServerStatus::Starting;

	// remove the const'ness of "this" to get away from C2664
	LoadReportingServer __gc* thisServer = 
		const_cast<LoadReportingServer __gc*> (this);

	// set up the signalling event
	//
	eventWorkerThreadDone = new AutoResetEvent (false);

	Trace (S"Starting worker thread...");
	// launch the worker thread
	//
	ReportingWorker __gc* worker = new ReportingWorker (
		thisServer, 
		new WorkerDoneEventHandler (
			this, 
			&LoadReportingServer::OnWorkerThreadDone),
		ipAddress,
		port,
		reportInterval,
		performanceCounters);
	workerThread = new Thread (new ThreadStart (worker, &ReportingWorker::Run));
	workerThread->Name = S"Load Reporting Server's ReportingWorker Thread";
	workerThread->Start ();
	Trace (S"Worker thread started.");

	// wait for the thread to signal us, by calling out the callback
	//
	eventWorkerThreadDone->WaitOne (Timeout::Infinite, false);

	// check the outcome of the thread result
	//
	if (workerThreadSucceeded)
	{
		status = ServerStatus::Started;
		Trace (S"Started.");
	}
	else
	{
		Stop ();
	}
}

// PUBLIC
void LoadReportingServer::Stop ()
{
	Trace (S"Stopping...");

	DEBUG_ASSERT (! (status == ServerStatus::Stopping ||
					 (status == ServerStatus::Stopped)));
	if (status == ServerStatus::Stopping || status == ServerStatus::Stopped)
		return;

	// unsubscribe from the "configuration changed" event
	//
	configurator->remove_Changed (
		new Configurator::ChangedEventHandler (
			this, &LoadReportingServer::OnConfigurationChanged));

	status = ServerStatus::Stopping;
	if (workerThread->IsAlive && !workerThread->Join (ThreadJoinTimeout))
	{
		Trace (S"Aborting worker thread...");
		workerThread->Abort ();
		workerThread->Join ();
		Trace (S"Worker thread aborted.");
	}
	status = ServerStatus::Stopped;
	
	Trace (S"Stopped.");
}

ServerStatus LoadReportingServer::get_Status ()
{
	return (status);
}

void LoadReportingServer::OnConfigurationChanged (
	Object __gc*	sender,
	EventArgs __gc*	e)
{
	(void) sender;	// unreferenced
	(void) e;		// ditto
	
	// unsubscribe temporarily
	//
	configurator->remove_Changed (
		new Configurator::ChangedEventHandler (
			this, &LoadReportingServer::OnConfigurationChanged));

	// restart to reflect the configuration changes
	//
	Stop ();
	Start ();
	
	// re-subscribe for the event
	//
	configurator->add_Changed (
		new Configurator::ChangedEventHandler (
			this, &LoadReportingServer::OnConfigurationChanged));
}

void LoadReportingServer::OnWorkerThreadDone (bool succeeded)
{
	workerThreadSucceeded = succeeded;
	// signal the main thread's Start method, that the
	// workerThread is up and running or it has exited
	// prematurely
	//
	eventWorkerThreadDone->Set ();
}

} // namespace

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Bulgaria Bulgaria
I'm crazy about programming, bleeding-edge technologies and my wife, Irina. Thinking seriously to start living in Centurian time.

The image shows me, happy :P

My blog

Comments and Discussions