root/trunk/0026_NotificationQueue/NotificationQueueTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 6.03 KB

(June 16, 2010 11:07 UTC) Almost 2 years ago

0026_NotificationQueue

 
Show/hide line numbers
//
//  NotificationQueueTest.cpp
//
//  Created by Setsu on 6/6/2010.
//  Copyright 2010 RoundSquare Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// 
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//

//----------------------------------------
//	include
//----------------------------------------
#include <Poco/Logger.h>
#include <Poco/PatternFormatter.h>
#include <Poco/FormattingChannel.h>
#include <Poco/ConsoleChannel.h>
#include <Poco/Format.h>
#include <Poco/Notification.h>
#include <Poco/NotificationQueue.h>
#include <Poco/PriorityNotificationQueue.h>
#include <Poco/ThreadPool.h>
#include <Poco/Thread.h>
#include <Poco/Runnable.h>
#include <Poco/Random.h>
#include <Poco/AutoPtr.h>

#include <string>
#include <typeinfo>

#include "ScopedLogMessage.h"

//----------------------------------------
//	const
//----------------------------------------
const int kNumQueue	= 12;
const long kSleepMaxTime	= 200;
const long kSleepWaitTime	= 500;

//----------------------------------------
//	WorkNotification
//----------------------------------------
class WorkNotification : public Poco::Notification
{
public:
	WorkNotification(int data):
		m_data(data)
	{
	}

	int data() const
	{
		return m_data;
	}

private:
	int	m_data;
};

//----------------------------------------
//	Worker
//----------------------------------------
template<class T>
class Worker : public Poco::Runnable
{
	typedef Poco::AutoPtr<WorkNotification> WorkNotificationPtr;

public:
	Worker(const std::string& name, T& queue, ScopedLogMessage& msg):
		m_name(name)
	,	m_queue(queue)
	,	m_msg(msg)
	{
		m_msg.Message(Poco::format("  %s created", m_name));
	}

	~Worker()
	{
		m_msg.Message(Poco::format("  %s deleted", m_name));
	}

	void run()
	{
		Poco::Random rnd;
		for(;;)
		{
			Poco::Notification::Ptr pNf(m_queue.waitDequeueNotification());
			if(pNf)
			{
				WorkNotificationPtr pWorkNf = pNf.cast<WorkNotification>();
				if(pWorkNf)
				{
					m_msg.Message(Poco::format("    %s got work notification %d", m_name, pWorkNf->data()));
					Poco::Thread::sleep(rnd.next(kSleepMaxTime));
				}
			}
			else break;
		}
	}

private:
	std::string				m_name;
	T&						m_queue;
	ScopedLogMessage&		m_msg;
};

//----------------------------------------
//	MyNotificationQueue
//----------------------------------------
template<class T>
class MyNotificationQueue : public T
{
public:
	MyNotificationQueue(ScopedLogMessage& msg) :
		m_msg(msg)
	,	m_numEnqueued(0)
	{
	}

	void MyEnqueueNotification(Poco::Notification::Ptr pNotification)
	{
		m_msg.Message(Poco::format("Class %s is not supported!", std::string(typeid(T).name())));
	}

private:
	ScopedLogMessage&	m_msg;
	int					m_numEnqueued;
};

template<>
void MyNotificationQueue<Poco::NotificationQueue>::MyEnqueueNotification(Poco::Notification::Ptr pNotification)
{
	enqueueNotification(pNotification);
	++m_numEnqueued;
}

template<>
void MyNotificationQueue<Poco::PriorityNotificationQueue>::MyEnqueueNotification(Poco::Notification::Ptr pNotification)
{
	enqueueNotification(pNotification, kNumQueue-m_numEnqueued);
	++m_numEnqueued;
}

//----------------------------------------
//	TestNotificationQueue
//----------------------------------------
template<class T>
void TestNotificationQueue(ScopedLogMessage& msg, const std::string& title)
{
	msg.Message(Poco::format("--- %s ---", title));

	MyNotificationQueue<T> queue(msg);

	Worker< MyNotificationQueue<T> > worker1("Worker 1", queue, msg);
	Worker< MyNotificationQueue<T> > worker2("Worker 2", queue, msg);
	Worker< MyNotificationQueue<T> > worker3("Worker 3", queue, msg);

	for(int i=0; i<kNumQueue; ++i)
	{
		queue.MyEnqueueNotification(new WorkNotification(i));
	}

	Poco::ThreadPool::defaultPool().start(worker1);
	Poco::ThreadPool::defaultPool().start(worker2);
	Poco::ThreadPool::defaultPool().start(worker3);

	while(!queue.empty())
	{
		Poco::Thread::sleep(kSleepMaxTime);
	}
	Poco::Thread::sleep(kSleepWaitTime);

	queue.wakeUpAll();
	Poco::ThreadPool::defaultPool().joinAll();
}

//----------------------------------------
//	PrepareConsoleLogger
//----------------------------------------
void PrepareConsoleLogger(const std::string& name, int level=Poco::Message::PRIO_INFORMATION)
{
	Poco::FormattingChannel* pFCConsole = new Poco::FormattingChannel(new Poco::PatternFormatter("%t"));
	pFCConsole->setChannel(new Poco::ConsoleChannel);
	pFCConsole->open();

	Poco::Logger::create(name, pFCConsole, level);
}

//----------------------------------------
//	main
//----------------------------------------
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);

	ScopedLogMessage msg("NotificationQueueTest ", "start", "end");

	TestNotificationQueue<Poco::NotificationQueue>(msg, "Poco::NotificationQueue");
	TestNotificationQueue<Poco::PriorityNotificationQueue>(msg, "Poco::PriorityNotificationQueue");

	return 0;
}