VPTissue Reference Manual
ClientHandler.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 "ClientHandler.h"
21 
22 #include "ExplorationManager.h"
26 
27 #include <QString>
28 #include <QTcpSocket>
29 #include <iostream>
30 
31 class QTcpSocket;
32 
33 namespace SimPT_Parex {
34 
35 class ExplorationManager;
36 using namespace std;
37 
38 ClientHandler::ClientHandler(QTcpSocket* socket,
39  const std::shared_ptr<ExplorationManager>& explorationManager, QObject* parent)
40  : QObject(parent), m_protocol(nullptr), m_exploration_manager(explorationManager)
41 {
42  assert(socket && "socket cannot be a nullptr");
43  assert(explorationManager.get() && "explorationManager cannot be a nullptr");
44 
45  m_protocol = new ServerClientProtocol(socket, parent);
46 
47  connect(m_protocol, SIGNAL(Ended()), this, SLOT(deleteLater()));
48  connect(m_protocol, SIGNAL(Error(const std::string&)),
49  this, SLOT(DisplayError(const std::string&)));
50 
51  // Connects for registering received Explorations
52  connect(m_protocol, SIGNAL(ExplorationReceived(const Exploration*)),
53  this, SLOT(RegisterExploration(const Exploration*)));
54  connect(m_exploration_manager.get(), SIGNAL(Updated()), this, SLOT(Refresh()));
55 
56  connect(m_protocol, SIGNAL(DeleteExploration(const std::string&)),
57  this, SLOT(DeleteExploration(const std::string&)));
58  connect(m_protocol, SIGNAL(ExplorationNamesRequested()),
59  this, SLOT(SendExplorationNames()));
60  connect(m_protocol, SIGNAL(Subscribe(const std::string&)),
61  this, SLOT(Subscribe(const std::string&)));
62  connect(m_protocol, SIGNAL(Unsubscribe(const std::string&)),
63  this, SLOT(Unsubscribe(const std::string&)));
64  connect(m_protocol, SIGNAL(StopTask(const std::string&, int)),
65  m_exploration_manager.get(), SLOT(StopTask(const std::string&, int)));
66  connect(m_protocol, SIGNAL(RestartTask(const std::string&, int)),
67  m_exploration_manager.get(), SLOT(RestartTask(const std::string&, int)));
68 }
69 
70 void ClientHandler::DisplayError(const std::string& error) const
71 {
72  cerr << "ClientHandler Error: " << error << endl;
73 }
74 
75 void ClientHandler::DeleteExploration(const std::string& name)
76 {
77  Unsubscribe(name);
78  m_exploration_manager->DeleteExploration(name);
79 }
80 
81 void ClientHandler::Refresh()
82 {
83  if (m_subscribed_exploration != "") {
84  const ExplorationProgress *progress
85  = m_exploration_manager->GetExplorationProgress(m_subscribed_exploration);
86  if (progress) {
87  m_protocol->SendStatus(*progress);
88  }
89  } else {
90  m_protocol->SendStatusDeleted("");
91  }
92 }
93 
94 void ClientHandler::RegisterExploration(const Exploration* exploration)
95 {
96  cout << "ClientHandler::RegisterExploration" << endl;
97  m_exploration_manager->RegisterExploration(exploration);
98  Subscribe(exploration->GetName());
99 }
100 
101 void ClientHandler::SendExplorationNames()
102 {
103  m_protocol->SendExplorationNames(m_exploration_manager->GetExplorationNames());
104 }
105 
106 void ClientHandler::Subscribe(const std::string& name)
107 {
108  m_subscribed_exploration = name;
109  Refresh();
110 }
111 
112 void ClientHandler::Unsubscribe(const std::string& name)
113 {
114  if (m_subscribed_exploration == name){
115  m_subscribed_exploration = "";
116  }
117  Refresh();
118 }
119 
120 } // namespace
void SendStatusDeleted(const std::string &name)
Send a deleted status for the exploration with the specified name.
Interface for Exploration.
STL namespace.
ClientHandler(QTcpSocket *socket, const std::shared_ptr< ExplorationManager > &explorationManager, QObject *parent=0)
Constructor.
void SendExplorationNames(const std::vector< std::string > &names)
Send list of Exploration names.
Interface for ServerClientProtocol.
Interface for ClientHandler.
Interface for ExplorationManager.
ServerClient Protocol to deal with Client-Server communication (Server-side)
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
see the online Qt documentation
Class describing a generic exploration.
Definition: Exploration.h:33
Interface for ExplorationProgress.
void SendStatus(const ExplorationProgress &status)
Send status for given exploration.