GCC Code Coverage Report


./
File: src/XpertMass/MassDataServerThread.cpp
Date: 2024-08-24 11:26:06
Lines:
0/30
0.0%
Functions:
0/3
0.0%
Branches:
0/44
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 "MassDataServerThread.hpp"
45
46
47 namespace MsXpS
48 {
49 namespace libXpertMass
50 {
51
52 /*!
53 \class MsXpS::libXpertMass::MassDataServerThread
54 \inmodule libXpertMass
55 \ingroup XpertMassUtilities
56 \inheaderfile MassDataServerThread.hpp
57
58 \brief The MassDataServerThread class provides a server in a QThread instance.
59 */
60
61
62 /*!
63 \variable MsXpS::libXpertMass::MassDataServerThread::m_data
64
65 \brief The data to be served.
66 */
67
68 /*!
69 \variable MsXpS::libXpertMass::MassDataServerThread::m_socketDescriptor
70
71 \brief The socket descriptor of the connection.
72 */
73
74 /*!
75 \variable MsXpS::libXpertMass::MassDataServerThread::mpa_tcpSocket
76
77 \brief The socket to be used for the connection.
78 */
79
80
81 /*!
82 \brief Constructs a MassDataServerThread instance.
83
84 \list
85 \li \a socket_descriptor: the socket descriptor of the connection to the
86 client.
87 \li \a data: the data to be served.
88 \li \a parent: the parent QObject.
89 \endlist
90 */
91 MassDataServerThread::MassDataServerThread(qintptr socket_descriptor,
92 const QByteArray &data,
93 QObject *parent)
94 : QThread(parent), m_socketDescriptor(socket_descriptor), m_data(data)
95 {
96 // qDebug() << "Constructing the server thread with data of size:"
97 //<< m_data.size() << this;
98 }
99
100 /*!
101 \brief Destructs this MassDataServerThread instance.
102
103 Deletes mpa_tcpSocket if non-nullptr.
104 */
105 MassDataServerThread::~MassDataServerThread()
106 {
107 if(mpa_tcpSocket != nullptr)
108 {
109 qDebug("Now deleting the socket.");
110 delete mpa_tcpSocket;
111 }
112 }
113
114 /*!
115 \brief Runs the task in this thread.
116
117 Allocates a new QTcpSocket and sets the \c m_socketDescriptor. If the member
118 m_data are not empty, writes the data to the socket and disconnects.
119 */
120 void
121 MassDataServerThread::run()
122 {
123 // qDebug() << "The server thread has been run at"
124 //<< QDateTime::currentDateTime();
125
126 mpa_tcpSocket = new QTcpSocket();
127
128 if(!mpa_tcpSocket->setSocketDescriptor(m_socketDescriptor))
129 {
130 qDebug() << "There was an error initializing the socket descriptor.";
131
132 emit errorSignal(mpa_tcpSocket->error());
133 return;
134 }
135
136 if(m_data.size())
137 {
138 QByteArray byte_array;
139 QDataStream out_stream(&byte_array, QIODevice::WriteOnly);
140 out_stream.setVersion(QDataStream::Qt_5_0);
141 out_stream << m_data;
142
143 // qDebug() << "On the verge of writing byte_array of size:"
144 //<< byte_array.size();
145
146 int written_bytes = mpa_tcpSocket->write(byte_array);
147
148 // qDebug() << "Now written " << written_bytes << " bytes to the socket
149 // at"
150 //<< QDateTime::currentDateTime();
151
152 if(written_bytes >= byte_array.size())
153 {
154 // qDebug() << "The data could be written fine. Clearing them now.";
155
156 emit writtenDataSignal(written_bytes);
157
158 m_data.clear();
159 }
160 // else
161 // qDebug() << "The data could not be written fully, not sending "
162 //"successful process signal.";
163 }
164 // else
165 // qDebug() << "There are no data to be written to the socket at"
166 //<< QDateTime::currentDateTime();
167
168 qDebug() << "Now trying to disconnect from host.";
169 mpa_tcpSocket->disconnectFromHost();
170 if (mpa_tcpSocket->state() == QAbstractSocket::UnconnectedState
171 || mpa_tcpSocket->waitForDisconnected(1000)) {
172 qDebug("Succesfully disconnected from the host!");
173 }
174 }
175
176 } // namespace libXpertMass
177
178 } // namespace MsXpS
179