Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tfarmserver.h"
Toshihiro Shimizu 890ddd
#include "tfarmexecutor.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tconvert.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <vector></vector>
Toshihiro Shimizu 890ddd
using namespace std;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TFarmServerStub::Imp : public TFarmExecutor
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	Imp(TFarmServer *server, int port)
Toshihiro Shimizu 890ddd
		: TFarmExecutor(port), m_server(server)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		assert(m_server);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// TFarmExecutor overrides
Shinya Kitaoka 3bfa54
	QString execute(const std::vector<qstring> &argv);</qstring>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TFarmServer *m_server;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
QString TFarmServerStub::Imp::execute(const std::vector<qstring> &argv)</qstring>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#ifdef DEBUG
Toshihiro Shimizu 890ddd
	std::cout << "executing " << argv[0].c_str() << endl;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (argv.size() > 0) {
Toshihiro Shimizu 890ddd
#ifdef DEBUG
Toshihiro Shimizu 890ddd
		for (int i = 0; i < (int)argv.size(); ++i)
Toshihiro Shimizu 890ddd
			std::cout << argv[i] << " ";
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (argv[0] == "addTask" && argv.size() == 3) {
Toshihiro Shimizu 890ddd
			int ret = m_server->addTask(argv[1], argv[2]);
Toshihiro Shimizu 890ddd
			return QString::number(ret);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "terminateTask" && argv.size() > 1) {
Toshihiro Shimizu 890ddd
			int ret = m_server->terminateTask(argv[1]);
Toshihiro Shimizu 890ddd
			return QString::number(ret);
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "getTasks") {
Shinya Kitaoka 3bfa54
			std::vector<qstring> tasks;</qstring>
Toshihiro Shimizu 890ddd
			int ret = m_server->getTasks(tasks);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			QString reply(QString::number(ret));
Toshihiro Shimizu 890ddd
			reply += ",";
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
			std::vector<qstring>::iterator it = tasks.begin();</qstring>
Toshihiro Shimizu 890ddd
			for (; it != tasks.end(); ++it) {
Toshihiro Shimizu 890ddd
				reply += *it;
Toshihiro Shimizu 890ddd
				reply += ",";
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (!reply.isEmpty())
Toshihiro Shimizu 890ddd
				reply.remove(reply.length() - 1, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			return reply;
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "queryHwInfo") {
Toshihiro Shimizu 890ddd
			TFarmServer::HwInfo hwInfo;
Toshihiro Shimizu 890ddd
			m_server->queryHwInfo(hwInfo);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			QString ret;
Toshihiro Shimizu 890ddd
			ret += QString::number((int)hwInfo.m_cpuCount);
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number((int)(hwInfo.m_totPhysMem / 1024));
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number((int)(hwInfo.m_availPhysMem / 1024));
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number((int)(hwInfo.m_totVirtMem / 1024));
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number((int)(hwInfo.m_availVirtMem / 1024));
Toshihiro Shimizu 890ddd
			ret += ",";
Toshihiro Shimizu 890ddd
			ret += QString::number(hwInfo.m_type);
Toshihiro Shimizu 890ddd
			return ret;
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "attachController" && argv.size() > 3) {
Toshihiro Shimizu 890ddd
			int port = argv[3].toInt();
Toshihiro Shimizu 890ddd
			m_server->attachController(argv[1], argv[2], port);
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		} else if (argv[0] == "detachController" && argv.size() > 3) {
Toshihiro Shimizu 890ddd
			int port = argv[3].toInt();
Toshihiro Shimizu 890ddd
			m_server->detachController(argv[1], argv[2], port);
Toshihiro Shimizu 890ddd
			return "";
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return "";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFarmServerStub::TFarmServerStub(TFarmServer *farmServer, int port)
Toshihiro Shimizu 890ddd
	: m_imp(new Imp(farmServer, port))
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TFarmServerStub::~TFarmServerStub()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TFarmServerStub::run()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_imp->run();
Toshihiro Shimizu 890ddd
	return m_imp->getExitCode();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TFarmServerStub::shutdown()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TTcpIpClient client;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int socketId;
Toshihiro Shimizu 890ddd
	int ret = client.connect(TSystem::getHostName(), "", m_imp->getPort(), socketId);
Toshihiro Shimizu 890ddd
	if (ret == OK) {
Toshihiro Shimizu 890ddd
		ret = client.send(socketId, "shutdown");
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TFarmServerStub::getPort() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_imp->getPort();
Toshihiro Shimizu 890ddd
}