VPTissue Reference Manual
ClientProtocol.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 "ClientProtocol.h"
21 
22 #include "Exploration.h"
23 #include "ExplorationProgress.h"
24 #include "FileExploration.h"
25 #include "ParameterExploration.h"
26 #include "util/misc/StringUtils.h"
27 
28 #include <boost/property_tree/ptree.hpp>
29 
30 using namespace std;
31 using boost::property_tree::ptree;
32 
33 namespace SimPT_Parex {
34 
35 ClientProtocol::ClientProtocol(ServerInfo* server, QObject *parent)
36  : Protocol(server, parent)
37 {
38 }
39 
41 {
42  if (!IsConnected())
43  return false;
44 
45  ptree writer;
46 
47  if (dynamic_cast<const ParameterExploration*>(exploration) != nullptr) {
48  writer.put_child("parameter_exploration", exploration->ToPtree());
49  } else if (dynamic_cast<const FileExploration*>(exploration) != nullptr) {
50  writer.put_child("file_exploration", exploration->ToPtree());
51  } else {
52  assert("Exploration type unknown.");
53  }
54 
55  SendPtree(writer);
56  return true;
57 }
58 
59 bool ClientProtocol::DeleteExploration(const std::string &name)
60 {
61  if (!IsConnected())
62  return false;
63 
64  ptree writer;
65  writer.put("Control.Delete", name);
66 
67  SendPtree(writer);
68  return true;
69 }
70 
72 {
73  if (!IsConnected())
74  return false;
75 
76  ptree writer;
77  writer.put("Control.RequestExplorations", "Explorations");
78 
79  SendPtree(writer);
80  return true;
81 }
82 
83 bool ClientProtocol::SubscribeUpdates(const std::string &explorationName)
84 {
85  if (!IsConnected())
86  return false;
87 
88  ptree writer;
89  writer.put("Control.Subscribe", explorationName);
90 
91  SendPtree(writer);
92  return true;
93 }
94 
95 bool ClientProtocol::UnsubscribeUpdates(const std::string &explorationName)
96 {
97  if (!IsConnected())
98  return false;
99 
100  ptree writer;
101  writer.put("Control.Unsubscribe", explorationName);
102 
103  SendPtree(writer);
104  return true;
105 }
106 
107 bool ClientProtocol::StopTask(const std::string &name, int task)
108 {
109  if (!IsConnected())
110  return false;
111 
112  ptree writer;
113  writer.put("Control.Stop.name", name);
114  writer.put("Control.Stop.id", to_string(task));
115 
116  SendPtree(writer);
117  return true;
118 }
119 
120 bool ClientProtocol::RestartTask(const std::string &name, int task)
121 {
122  if (!IsConnected())
123  return false;
124 
125  ptree writer;
126  writer.put("Control.Start.name", name);
127  writer.put("Control.Start.id", to_string(task));
128 
129  SendPtree(writer);
130  return true;
131 }
132 
133 void ClientProtocol::ReceivePtree(const ptree &reader)
134 {
135  if (reader.find("Control") != reader.not_found()) {
136  ptree control_pt = reader.get_child("Control");
137 
138  if (control_pt.find("ExplorationNames") != control_pt.not_found()) {
139  std::vector<std::string> names;
140  int size = control_pt.get<int>("ExplorationNames.size");
141  for (int i = 0; i < size; ++i) {
142  names.push_back(control_pt.get<std::string>(
143  "ExplorationNames.exploration" + to_string(i) + ".name"));
144  }
145 
146  emit ExplorationNames(names);
147  } else if (control_pt.find("Status") != control_pt.not_found()) {
148  ptree status_pt = control_pt.get_child("Status");
149  if (status_pt.find("deleted") != status_pt.not_found()) {
150  emit ExplorationDeleted();
151  } else {
152  emit ExplorationStatus(
153  new ExplorationProgress(status_pt.get_child("exploration_progress")));
154  }
155  }
156  }
157 }
158 
159 } // namespace
Base class for the XML/ptree protocols between client, server and node.
Definition: Protocol.h:35
Interface for Exploration.
STL namespace.
bool RequestExplorationNames()
Request a list of current Explorations.
void ExplorationStatus(const ExplorationProgress *exploration) const
Signal emitted when new status update is received.
virtual void ReceivePtree(const boost::property_tree::ptree &reader)
Receive a ptree message Implementation of Protocol::ReceivePtree.
Class describing the progress state of an exploration.
void SendPtree(const boost::property_tree::ptree &pt)
Sends a ptree over the connection.
Definition: Protocol.cpp:78
bool RestartTask(const std::string &name, int task)
Request to restart a task.
bool SubscribeUpdates(const std::string &explorationName)
Subscribe for regular updates.
virtual boost::property_tree::ptree ToPtree() const
Convert the exploration to a ptree.
Definition: Exploration.cpp:51
bool SendExploration(const Exploration *exploration)
Send exploration.
Interface for ClientProtocol.
bool DeleteExploration(const std::string &name)
Request a stop and delete of the given exploration.
String manipulation utilities.
Class for storing server info used on client-side.
Definition: ServerInfo.h:29
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
void ExplorationNames(const std::vector< std::string > &names) const
Signal emitted when exploration names are received.
bool UnsubscribeUpdates(const std::string &explorationName)
Unsubscribe for regular updates.
void ExplorationDeleted() const
Signal emitted when subscribed exploration has been deleted.
bool IsConnected()
Check if still connected.
Definition: Protocol.cpp:73
see the online Qt documentation
Class describing a generic exploration.
Definition: Exploration.h:33
bool StopTask(const std::string &name, int task)
Request to stop a task.
Interface for ParameterExploration.
Interface for ExplorationProgress.
Interface for FileExploration.