VPTissue Reference Manual
WorkerRepresentative.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011-2016 Universiteit Antwerpen
3  *
4  * Licensed under the EUPL, Version 1.1 or as soon they will be approved by
5  * the European Commission - subsequent versions of the EUPL (the "Licence");
6  * You may not use this work except in compliance with the Licence.
7  * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl5
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the Licence is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the Licence for the specific language governing
13  * permissions and limitations under the Licence.
14  */
20 #include "WorkerRepresentative.h"
21 
23 #include "parex_protocol/SimTask.h"
24 
25 #include <QTimer>
26 #include <iostream>
27 
28 namespace SimPT_Parex {
29 
30 class SimResult;
31 using namespace std;
32 
33 WorkerRepresentative::WorkerRepresentative(const QHostAddress& addr, quint16 port)
34  : m_addr(new QHostAddress(addr)), m_port(port), m_socket(nullptr),
35  m_protocol(nullptr), m_currentTaskId(-1), m_currentTaskName("")
36 {
37 }
38 
40  quint16 port, int taskId, std::string taskName)
41  : m_addr(new QHostAddress(addr)), m_port(port), m_socket(nullptr),
42  m_protocol(nullptr), m_currentTaskId(taskId), m_currentTaskName(taskName)
43 {
44 }
45 
47 {
48 }
49 
50 void WorkerRepresentative::Connected()
51 {
52  if (m_currentTaskId != -1) {
53  m_protocol = new ServerNodeProtocol(m_socket, this);
54  connect(m_protocol, SIGNAL(Error(const std::string&)),
55  this, SLOT(DisplayError(const std::string&)));
56  connect(m_protocol, SIGNAL(ResultReceived(const SimResult&)),
57  this, SLOT(Finished(const SimResult&)));
58  } else {
59  emit ReadyToWork();
60  }
61 }
62 
63 void WorkerRepresentative::Delete(const std::string name)
64 {
65  Init();
66 
67  m_protocol->Delete(name);
68 }
69 
70 void WorkerRepresentative::DisplayError(const std::string& error) const
71 {
72  cerr << "WorkerRepresentative Error: " << error << endl;
73 }
74 
75 void WorkerRepresentative::DisplayError(QAbstractSocket::SocketError error) const
76 {
77  cerr << "WorkerRepresentative Socket Error: " << error << endl;
78 }
79 
81 {
82  Init();
83  m_currentTaskId = task.GetId();
84  m_currentTaskName = task.GetExploration();
85  m_protocol->SendTask(task);
86 }
87 
88 void WorkerRepresentative::Finished(const SimResult& res)
89 {
90  emit FinishedWork(res);
91 
92  //wait untill all events are processed
93  QTimer::singleShot(0, this, SIGNAL(ReadyToWork()));
94 }
95 
97 {
98  return m_currentTaskName;
99 }
100 
101 const QHostAddress* WorkerRepresentative::GetSenderAddress() const
102 {
103  return m_addr;
104 }
105 
107 {
108  return m_port;
109 }
110 
112 {
113  return m_currentTaskId;
114 }
115 
116 void WorkerRepresentative::Init()
117 {
118  if (!m_protocol) {
119  m_protocol = new ServerNodeProtocol(m_socket, this);
120  connect(m_protocol, SIGNAL(Error(const std::string&)),
121  this, SLOT(DisplayError(const std::string&)));
122  connect(m_protocol, SIGNAL(ResultReceived(const SimResult&)),
123  this, SLOT(Finished(const SimResult&)));
124  }
125 }
126 
128 {
129  m_socket = new QTcpSocket();
130  m_socket->connectToHost(*m_addr, m_port);
131 
132  connect(m_socket, SIGNAL(connected()), this, SLOT(Connected()));
133  connect(m_socket, SIGNAL(disconnected()), this, SIGNAL(Disconnected()));
134 
135  connect(m_socket, SIGNAL(error(QAbstractSocket::SocketError)),
136  this, SLOT(DisplayError(QAbstractSocket::SocketError)));
137 
138 }
139 
141 {
142  Init();
143 
144  m_protocol->StopTask();
145 }
146 
147 } // namespace
void Setup()
Tries to connect to the worker and sets up the signals.
STL namespace.
void StopTask()
Orders the node to stop its current task.
A container class for the final result of a simulation.
Definition: SimResult.h:29
void SendTask(const SimTask &task)
Sends a task to the server.
void DoWork(const SimTask &)
Pass the job to the worker.
Protocol at the server side to communicate with the node.
WorkerRepresentative(const QHostAddress &addr, quint16 port)
Constructor for a worker without work.
int GetSenderPort() const
Gets the port of the worker.
void ReadyToWork()
Emitted when the worker is ready to do a new job.
Interface for ServerNodeProtocol.
void FinishedWork(const SimResult &)
Emitted when the worker has finished his job and has the result ready.
void StopTask()
Stop the current task.
int GetTaskId() const
Gets the id of the currently executing task.
const QHostAddress * GetSenderAddress() const
Gets the address of the worker.
void Delete(const std::string name)
Delete an exploration.
Interface for SimTask.
std::string GetExplName() const
Gets the name current tasks' exploration.
void Delete(const std::string &name)
Orders the node to delete all files it has for a certain exploration.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
std::string GetExploration() const
Get the name of the exploration.
Definition: SimTask.cpp:71
Contains all information needed for a transmitable simulation task.
Definition: SimTask.h:31
int GetId() const
Get the id of the task in the exploration.
Definition: SimTask.cpp:76
void Disconnected()
Emitted when the worker has disconnected.
Interface for WorkerRepresentative.