VPTissue Reference Manual
Protocol.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 "Protocol.h"
21 
22 #include "Connection.h"
23 #include "ServerInfo.h"
24 
25 #include <QTcpSocket>
26 #include <boost/property_tree/ptree.hpp>
27 #include <boost/property_tree/xml_parser.hpp>
28 
29 using boost::property_tree::ptree;
30 
31 namespace SimPT_Parex {
32 
33 Protocol::Protocol(QTcpSocket* socket, QObject *parent)
34  : QObject(parent), m_connection(new Connection(socket))
35 {
36  m_connection->setParent(this);
37  connect(m_connection, SIGNAL(ReceivedMessage(const QByteArray&)), this, SLOT(Receive(const QByteArray&)));
38  connect(m_connection, SIGNAL(ConnectionClosed()), this, SIGNAL(Ended()));
39  connect(m_connection, SIGNAL(Error(const std::string&)), this, SIGNAL(Error(const std::string&)));
40  connect(m_connection, SIGNAL(Error(const std::string&)), this, SIGNAL(Ended()));
41 }
42 
44  : QObject(parent)
45 {
46  QTcpSocket *socket = new QTcpSocket();
47  socket->connectToHost(server->GetAddress(), server->GetPort());
48  if (socket->waitForConnected(2000)) {
49  m_connection = new Connection(socket, this);
50 
51  connect(m_connection, SIGNAL(ReceivedMessage(const QByteArray&)), this, SLOT(Receive(const QByteArray&)));
52  connect(m_connection, SIGNAL(ConnectionClosed()), this, SIGNAL(Ended()));
53  connect(m_connection, SIGNAL(Error(const std::string&)), this, SIGNAL(Error(const std::string&)));
54  connect(m_connection, SIGNAL(Error(const std::string&)), this, SIGNAL(Ended()));
55  }
56  else {
57  m_connection = nullptr;
58  }
59 }
60 
62 {
63 }
64 
65 std::string Protocol::GetClientIP() const {
66  return m_connection->GetPeerAddress();
67 }
68 
69 int Protocol::GetClientPort() const {
70  return m_connection->GetPeerPort();
71 }
72 
74 {
75  return (m_connection != nullptr && m_connection->IsConnected());
76 }
77 
78 void Protocol::SendPtree(const ptree &pt)
79 {
80  std::ostringstream stream;
81  write_xml(stream, pt);
82  QByteArray message(stream.str().data(), stream.str().size());
83  m_connection->SendMessage(message);
84 }
85 
86 void Protocol::Receive(const QByteArray &message)
87 {
88  std::istringstream s(message.data());
89  ptree pt;
90  read_xml(s, pt); // TODO More error handling, for example of the possible exceptions of this call?
91  ReceivePtree(pt);
92 }
93 
94 } // namespace
Protocol(QTcpSocket *socket, QObject *parent=nullptr)
Constructor.
Definition: Protocol.cpp:33
Interface for ServerInfo.
Interface for Protocol.
void Error(const std::string &error)
Signal emitted when an error occured.
Interface for Connection.
void SendPtree(const boost::property_tree::ptree &pt)
Sends a ptree over the connection.
Definition: Protocol.cpp:78
int GetPort()
Return the port of the server.
Definition: ServerInfo.cpp:54
void SendMessage(const QByteArray &message)
Sends a message over the connection.
Definition: Connection.cpp:63
void Ended()
Signal emitted when the protocol has ended and the connection is closed.
bool IsConnected()
Check if still connected.
Definition: Connection.cpp:49
Class managing a TCP stream of messages.
Definition: Connection.h:31
virtual ~Protocol()
Destructor.
Definition: Protocol.cpp:61
QString GetAddress()
Return the address of the server.
Definition: ServerInfo.cpp:49
Class for storing server info used on client-side.
Definition: ServerInfo.h:29
virtual void ReceivePtree(const boost::property_tree::ptree &pt)=0
Method called when a ptree was received over the connection.
Namespace for SimPT parameter explorer package.
Definition: Client.cpp:52
bool IsConnected()
Check if still connected.
Definition: Protocol.cpp:73
see the online Qt documentation