GCC Code Coverage Report


./
File: src/XpertMass/MassDataServer.cpp
Date: 2024-08-24 11:26:06
Lines:
0/37
0.0%
Functions:
0/6
0.0%
Branches:
0/60
0.0%

Line Branch Exec Source
1 /* BEGIN software license
2 *
3 * MsXpertSuite - mass spectrometry software suite
4 * -----------------------------------------------
5 * Copyright (C) 2009--2020 Filippo Rusconi
6 *
7 * http://www.msxpertsuite.org
8 *
9 * This file is part of the MsXpertSuite project.
10 *
11 * The MsXpertSuite project is the successor of the massXpert project. This
12 * project now includes various independent modules:
13 *
14 * - massXpert, model polymer chemistries and simulate mass spectrometric data;
15 * - mineXpert, a powerful TIC chromatogram/mass spectrum viewer/miner;
16 *
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
29 *
30 * *****************************************************************************
31 * This specific code is a port to C++ of the Envemind Python code by Radzinski
32 * and colleagues of IsoSpec fame (Lacki, Startek and company :-)
33 *
34 * See https://github.com/PiotrRadzinski/envemind.
35 * *****************************************************************************
36 *
37 * END software license
38 */
39
40
41 #include <QDebug>
42 #include <QDateTime>
43
44 #include "MassDataServer.hpp"
45 #include "MassDataServerThread.hpp"
46
47
48 namespace MsXpS
49 {
50 namespace libXpertMass
51 {
52
53
54 /*!
55 \class MsXpS::libXpertMass::MassDataServer
56 \inmodule libXpertMass
57 \ingroup XpertMassUtilities
58 \inheaderfile MassDataServer.hpp
59
60 \brief The MassDataServer class provides a network server.
61 */
62
63 /*!
64 \variable MsXpS::libXpertMass::MassDataServer::m_data
65
66 \brief The data to be served.
67 */
68
69 /*!
70 \brief Constructs a MassDataServer instance.
71
72 \list
73 \li \a parent: QObject parent.
74 \endlist
75 */
76 MassDataServer::MassDataServer(QObject *parent) : QTcpServer(parent)
77 {
78 }
79
80 /*!
81 \brief Destructs this MassDataServer instance.
82 */
83 MassDataServer::~MassDataServer()
84 {
85 }
86
87 /*!
88 \brief Sets to this MassDataServer instance the data to be served in
89 \a byte_array.
90 */
91 void
92 MassDataServer::serveData(const QByteArray &byte_array)
93 {
94 m_data = byte_array;
95
96 // qDebug() << "Stored the byte array to be served upon connection. Size:"
97 //<< m_data.size();
98 }
99
100 /*!
101 \brief Handles an incoming connection with socket descriptor \a
102 socket_descriptor.
103
104 If the member m_data are not empty, allocates a MassDataServerThread to serve
105 these data.
106 */
107 void
108 MassDataServer::incomingConnection(qintptr socket_descriptor)
109 {
110 if(!m_data.size())
111 {
112 qDebug()
113 << "In this incoming connection we have no data to serve. Returning at:"
114 << QDateTime::currentDateTime().toString();
115
116 // It is not because we have not data to use as a response to the caller
117 // that we do not perform the connection closing stuff! Otherwise we
118 // consume a file descriptor (the socket) each time a connection is tried
119 // here from the client.... Bug that has broken my head for weeks...
120 QTcpSocket tcpSocket;
121
122 if(!tcpSocket.setSocketDescriptor(socket_descriptor))
123 return;
124
125 tcpSocket.disconnectFromHost();
126 tcpSocket.waitForDisconnected();
127
128 return;
129 }
130
131 qDebug() << "In this incoming connection, the data to serve have size:"
132 << m_data.size() << "at:" << QDateTime::currentDateTime().toString();
133
134 MassDataServerThread *mass_data_server_thread_p =
135 new MassDataServerThread(socket_descriptor, m_data, this);
136
137 connect(mass_data_server_thread_p,
138 &MassDataServerThread::finished,
139 mass_data_server_thread_p,
140 &MassDataServerThread::deleteLater);
141
142 connect(mass_data_server_thread_p,
143 &MassDataServerThread::errorSignal,
144 this,
145 &MassDataServer::error);
146
147 connect(
148 mass_data_server_thread_p,
149 &MassDataServerThread::writtenDataSignal,
150 [this](std::size_t written_bytes) {
151 qDebug() << "The data were written by the thread-based server socket "
152 "with written_bytes:"
153 << written_bytes << ". Clearing the data now.";
154 m_data = "";
155 });
156
157 mass_data_server_thread_p->start();
158 }
159
160 /*!
161 \brief Reports the \a socket_error to the console using qDebug().
162 */
163 void
164 MassDataServer::error(QTcpSocket::SocketError socket_error)
165 {
166 qDebug() << "An error occurred in the thread:" << socket_error;
167 }
168
169
170 } // namespace libXpertMass
171
172 } // namespace MsXpS
173