root/trunk/0036_SharedMemory/SharedMemory/SharedMemoryTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 4.54 KB

(June 27, 2010 09:51 UTC) Almost 2 years ago

Fixed the trouble on Windows.

 
Show/hide line numbers
//
//  SharedMemoryTest.cpp
//
//  Created by Setsu on 6/27/10.
//  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/Process.h>
#include <Poco/Pipe.h>
#include <Poco/PipeStream.h>
#include <Poco/StreamCopier.h>
#include <Poco/Format.h>
#include <Poco/Runnable.h>
#include <Poco/Thread.h>

#include <string>
#include <vector>
#include <iostream>

#include "ScopedLogMessage.h"

//----------------------------------------
//	const
//----------------------------------------
#if defined(POCO_OS_FAMILY_WINDOWS)
const std::string kCommandTable[] =	{"SharedMemoryTestServer.exe",
									 "SharedMemoryTestClient.exe"};
#else
const std::string kCommandTable[] =	{"./SharedMemoryTestServer",
									 "./SharedMemoryTestClient"};
#endif

const std::size_t kNumCommands = sizeof(kCommandTable)/sizeof(kCommandTable[0]);

//----------------------------------------
//	ProcessLauncher
//----------------------------------------
class ProcessLauncher: public Poco::Runnable
{
public:
	ProcessLauncher(ScopedLogMessage& msg, const std::string& command) :
		m_msg(msg)
	,	m_command(command)
	,	m_pPh(NULL)
	,	m_Pid(0)
	{
	}

	~ProcessLauncher()
	{
		int rc = m_pPh->wait();
		m_msg.Message(Poco::format(" \"%s\" terminated with return code = %d", m_command, rc));
		delete m_pPh;
	}
	
	void run()
	{
		m_msg.Message(Poco::format(" launch \"%s\"", m_command));
		std::vector<std::string> args;
		Poco::Pipe outPipe;
		m_pPh = new Poco::ProcessHandle(Poco::Process::launch(m_command, args, 0, &outPipe, 0));
		m_Pid = m_pPh->id();
		Poco::PipeInputStream istr(outPipe);
		Poco::StreamCopier::copyStream(istr, std::cout);
	}

	void stop()
	{
		m_msg.Message(Poco::format(" \"%s\" requestTermination", m_command));
		Poco::Process::requestTermination(m_Pid);
	}

private:
	ScopedLogMessage&		m_msg;
	const std::string&		m_command;
	Poco::ProcessHandle*	m_pPh;
	Poco::Process::PID		m_Pid;
};

//----------------------------------------
//	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("SharedMemoryTest ", "start", "end");

	Poco::Thread thread[kNumCommands];
	ProcessLauncher* pLauncher[kNumCommands];

	for(std::size_t i=0; i<kNumCommands; ++i)
	{
		pLauncher[i] = new ProcessLauncher(msg, kCommandTable[i]);
		thread[i].start(*pLauncher[i]);
		Poco::Thread::sleep(200);
	}

	Poco::Thread::sleep(2000);

	for(std::size_t i=0; i<kNumCommands; ++i)
	{
		std::size_t index = kNumCommands-1-i;

		pLauncher[index]->stop();
		thread[index].join();
		delete pLauncher[index];
	}

	return 0;
}