VPTissue Reference Manual
ServerClientProtocol.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 "ServerClientProtocol.h"
21 
22 #include "Exploration.h"
23 #include "ExplorationProgress.h"
24 #include "FileExploration.h"
25 #include "ParameterExploration.h"
26 #include "Protocol.h"
27 #include "util/misc/StringUtils.h"
28 
29 class QTcpSocket;
30 
31 using namespace std;
32 using boost::property_tree::ptree;
33 
34 namespace SimPT_Parex {
35 
36 ServerClientProtocol::ServerClientProtocol(QTcpSocket *socket, QObject *parent)
37  : Protocol(socket, parent)
38 {
39 }
40 
42 {
43 }
44 
45 void ServerClientProtocol::SendAck(const std::string &name)
46 {
47  ptree writer;
48  writer.put("Exploration.Name", name);
49  SendPtree(writer);
50 }
51 
52 void ServerClientProtocol::SendExplorationNames(const std::vector<std::string> &names)
53 {
54  ptree writer;
55  writer.put("Control.ExplorationNames.size", names.size());
56  int i = 0;
57  for (auto name : names) {
58  writer.put("Control.ExplorationNames.exploration" + to_string(i) + ".name", name);
59  i++;
60  }
61  SendPtree(writer);
62 }
63 
65 {
66  ptree writer;
67  writer.put_child("Control.Status.exploration_progress", status.ToPtree());
68  SendPtree(writer);
69 }
70 
71 void ServerClientProtocol::SendStatusDeleted(const std::string &name)
72 {
73  ptree writer;
74  writer.put("Control.Status.name", name);
75  writer.put("Control.Status.deleted", "");
76  SendPtree(writer);
77 }
78 
79 void ServerClientProtocol::ReceivePtree(const ptree &reader)
80 {
81  if (reader.find("parameter_exploration") != reader.not_found()) {
82  Exploration* exploration = new ParameterExploration(reader.get_child("parameter_exploration"));
83  exploration->SetName(exploration->GetName() + QDateTime::currentDateTime().toString("_yyyy_MM_dd_HH_mm_ss_zzz").toStdString());
84 
85  emit ExplorationReceived(exploration);
86  } else if (reader.find("file_exploration") != reader.not_found()) {
87  Exploration* exploration = new FileExploration(reader.get_child("file_exploration"));
88  exploration->SetName(exploration->GetName() + QDateTime::currentDateTime().toString("_yyyy_MM_dd_HH_mm_ss_zzz").toStdString());
89 
90  emit ExplorationReceived(exploration);
91  } else if (reader.find("Control") != reader.not_found()) {
92  ptree control_pt = reader.get_child("Control");
93 
94  if (control_pt.find("Subscribe") != control_pt.not_found()) {
95  emit Subscribe(control_pt.get<std::string>("Subscribe"));
96  } else if (control_pt.find("Unsubscribe") != control_pt.not_found()) {
97  emit Unsubscribe(control_pt.get<std::string>("Unsubscribe"));
98  } else if (control_pt.find("Stop") != control_pt.not_found()) {
99  emit StopTask(control_pt.get<std::string>("Stop.name"), control_pt.get<int>("Stop.id"));
100  } else if (control_pt.find("Start") != control_pt.not_found()) {
101  emit RestartTask(control_pt.get<std::string>("Start.name"), control_pt.get<int>("Start.id"));
102  } else if (control_pt.find("Delete") != control_pt.not_found()) {
103  emit DeleteExploration(control_pt.get<std::string>("Delete"));
104  } else if (control_pt.find("RequestExplorations") != control_pt.not_found()) {
106  }
107  }
108 }
109 
110 } // namespace
void SendStatusDeleted(const std::string &name)
Send a deleted status for the exploration with the specified name.
Base class for the XML/ptree protocols between client, server and node.
Definition: Protocol.h:35
Interface for Exploration.
STL namespace.
void SetName(const std::string &name)
Changes the name of the exploration.
Definition: Exploration.h:74
void Subscribe(const std::string &name)
Emitted when a request for subscription arrives.
Interface for Protocol.
virtual void ReceivePtree(const boost::property_tree::ptree &reader)
Receive a ptree message Implementation of Protocol::ReceivePtree.
Class describing a file-based exploration and its simulation tasks.
void SendAck(const std::string &name)
Acknowledgement for sent exploration.
void ExplorationNamesRequested()
Emitted when the names of the explorations are requested.
Class describing the progress state of an exploration.
boost::property_tree::ptree ToPtree() const
Convert the exploration and progress to a ptree.
void SendExplorationNames(const std::vector< std::string > &names)
Send list of Exploration names.
void SendPtree(const boost::property_tree::ptree &pt)
Sends a ptree over the connection.
Definition: Protocol.cpp:78
Interface for ServerClientProtocol.
const std::string & GetName() const
Returns the name of the exploration.
Definition: Exploration.h:79
void DeleteExploration(const std::string &name)
Emitted when a request to stop/delete the given exploration is received.
void StopTask(const std::string &name, int id)
Emitted when a request for stopping a task arrives.
String manipulation utilities.
void ExplorationReceived(const Exploration *exploration)
Emitted when an exploration is received.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
void Unsubscribe(const std::string &name)
Emitted when a request for unsubscription arrives.
Class describing a parameter exploration and its simulation tasks.
see the online Qt documentation
Class describing a generic exploration.
Definition: Exploration.h:33
void RestartTask(const std::string &name, int id)
Emitted when a request for stopping a task arrives.
Interface for ParameterExploration.
Interface for ExplorationProgress.
void SendStatus(const ExplorationProgress &status)
Send status for given exploration.
Interface for FileExploration.