root/trunk/0058_MulticastSocket/MulticastSocket/MulticastSocketTest.cpp

User picture

Author: Setsu

Revision: 348 («Previous)


File Size: 5.22 KB

(February 26, 2011 23:17 UTC) About 1 year ago

0058_MulticastSocket appended.

 
Show/hide line numbers
//
//  MulticastSocketTest.cpp
//
//  Created by Setsu on Feb 26, 2011.
//  Copyright 2011 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 <Poco/Net/Net.h>
#include <Poco/Net/IPAddress.h>
#include <Poco/String.h>

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

#include "ScopedLogMessage.h"

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

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

const char* kMulticastAddresses[] = {"239.255.1.2", "FF02::1"};
const std::string kMulticastPort("12345");
const std::string kWaitingPort("23456");

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

	~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()
	{
		std::string args;
		for(std::size_t i=0; i<m_args.size(); ++i)
		{
			args += " " + m_args[i];
		}
		m_msg.Message(Poco::format(" launch \"%s%s\"", m_command, args));
		Poco::Pipe outPipe;
		m_pPh = new Poco::ProcessHandle(Poco::Process::launch(m_command, m_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;
	const std::vector<std::string>	m_args;
};

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

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

	int addressIndex = 0;
	if(1 < argc)
	{
		if("ipv6" == Poco::toLower(std::string(argv[1])))	++addressIndex;
	}

	for(std::size_t i=0; i<kNumCommands; ++i)
	{
		std::vector<std::string> args;
		args.push_back(kMulticastAddresses[addressIndex]);
		args.push_back(kMulticastPort);
		args.push_back(kWaitingPort);
		pLauncher[i] = new ProcessLauncher(msg, kCommandTable[i], args);
		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;
}