29 #include <QPushButton>
40 const int WorkerNode::g_broadcast_port = 45678;
43 :
QTcpServer(parent), m_simulator(simulator), m_simulator_thread(new QThread(this)),
44 m_advertiser(g_broadcast_port, verbose), m_verbose(verbose), m_protocol(nullptr),
45 m_current_task(nullptr), m_last_address(QHostAddress::Null), m_queue(), m_results()
47 assert(simulator &&
"simulator cannot be a nullptr");
49 qRegisterMetaType<SimTask>(
"SimTask");
50 qRegisterMetaType<SimResult>(
"SimResult");
51 qRegisterMetaType<std::string>(
"std::string");
54 listen(QHostAnyAddress());
56 connect(
this, SIGNAL(newConnection()),
this, SLOT(ConnectionReceived()));
60 m_simulator->moveToThread(m_simulator_thread);
61 connect(m_simulator, SIGNAL(TaskSolved(
const SimResult&)),
62 this, SLOT(FinishedWork(
const SimResult&)));
64 m_simulator, SLOT(SolveTask(
const SimTask&)));
65 m_simulator_thread->start();
71 m_simulator->deleteLater();
72 m_simulator_thread->quit();
73 m_simulator_thread->wait();
76 void WorkerNode::ConnectionReceived()
79 cout <<
"Connection received" << endl;
82 QTcpSocket* socket = nextPendingConnection();
88 m_protocol =
new NodeProtocol(socket,
this);
90 connect(m_protocol, SIGNAL(Ended()), m_protocol, SLOT(deleteLater()));
91 connect(m_protocol, SIGNAL(Error(
const std::string&)),
92 this, SLOT(DisplayError(
const std::string&)));
95 connect(m_protocol, SIGNAL(TaskReceived(
const SimTask*)),
96 this, SLOT(StartedWork(
const SimTask*)));
97 connect(m_protocol, SIGNAL(StopTask()),
this, SLOT(StopTask()));
98 connect(m_protocol, SIGNAL(Delete(
const std::string&)),
99 this, SLOT(Delete(
const std::string&)));
103 connect(m_protocol, SIGNAL(Delete(
const std::string&)),
104 m_simulator, SLOT(Delete(
const std::string&)));
106 connect(m_protocol, SIGNAL(SuccessfullySent()),
this, SLOT(ResultSent()));
108 connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
109 this, SLOT(HandleError(QAbstractSocket::SocketError)));
111 if (socket->peerAddress() == m_last_address) {
113 if (!m_results.empty())
116 while (!m_queue.empty()) {
117 delete m_queue.front();
121 while (!m_results.empty()) {
125 m_last_address = socket->peerAddress();
128 cerr <<
"Already connected, ignoring new connection." << endl;
134 void WorkerNode::Delete(
const std::string& name)
142 void WorkerNode::DisplayError(
const std::string& error)
const
145 cerr <<
"WorkerNode Error: " << error << endl;
149 void WorkerNode::FinishedWork(
const SimResult& result)
151 delete m_current_task;
152 m_current_task =
nullptr;
154 m_results.push(result);
159 cout <<
"Disconnected when finished task" << endl;
162 if (!m_queue.empty()) {
164 cout <<
"More work in the queue, starting that now" << endl;
165 m_current_task = m_queue.front();
169 cout <<
"Finished job, waiting for more work" << endl;
173 void WorkerNode::HandleError(QAbstractSocket::SocketError )
176 m_protocol =
nullptr;
180 void WorkerNode::ResultSent()
183 if (!m_results.empty() && m_protocol && m_protocol->
IsConnected()) {
188 void WorkerNode::StartAdvertiser()
190 if (!m_results.empty()) {
191 m_advertiser.
Start(serverPort(),
192 m_results.front().GetExplorationName(), m_results.front().GetTaskId());
193 }
else if (m_current_task) {
194 m_advertiser.
Start(serverPort(),
197 m_advertiser.
Start(serverPort());
201 void WorkerNode::StartedWork(
const SimTask* task)
203 if (!m_current_task){
204 m_current_task = task;
208 cerr <<
"WARNING: Received work while still working, queuing the work" << endl;
214 void WorkerNode::StopTask()
see the online Qt documentation
A container class for the final result of a simulation.
void StopTask()
Stop the current task.
void NewTask(const SimTask &)
Emitted whenever a new task is ready to be simulated.
Interface for the Simulator.
void SendSimResult(const SimResult &result)
Sends the result of a simulation back to the server.
virtual ~WorkerNode()
Destroy the node, closing all open connections.
void Start(int serverPort)
Start finding a server to do work for.
Interface for WorkerNode.
void Stop()
Stop finding a server.
Simulator handling requested simulation tasks.
Interface for NodeProtocol.
Namespace for SimPT parameter explorer package.
std::string GetExploration() const
Get the name of the exploration.
Contains all information needed for a transmitable simulation task.
bool IsConnected()
Check if still connected.
int GetId() const
Get the id of the task in the exploration.
see the online Qt documentation
Hack for QT 4 -> Qt 5 transition.