##// END OF EJS Templates
Some cleaning.
jeandet -
r59:e7cdc214d72a default
parent child
Show More
@@ -1,233 +1,253
1 /*------------------------------------------------------------------------------
1 /*------------------------------------------------------------------------------
2 -- This file is a part of the SocExplorer Software
2 -- This file is a part of the SocExplorer Software
3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
3 -- Copyright (C) 2014, Plasma Physics Laboratory - CNRS
4 --
4 --
5 -- This program is free software; you can redistribute it and/or modify
5 -- This program is free software; you can redistribute it and/or modify
6 -- it under the terms of the GNU General Public License as published by
6 -- it under the terms of the GNU General Public License as published by
7 -- the Free Software Foundation; either version 3 of the License, or
7 -- the Free Software Foundation; either version 3 of the License, or
8 -- (at your option) any later version.
8 -- (at your option) any later version.
9 --
9 --
10 -- This program is distributed in the hope that it will be useful,
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
13 -- GNU General Public License for more details.
14 --
14 --
15 -- You should have received a copy of the GNU General Public License
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 -- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 -------------------------------------------------------------------------------*/
18 -------------------------------------------------------------------------------*/
19 /*-- Author : Alexis Jeandet
19 /*-- Author : Alexis Jeandet
20 -- Mail : alexis.jeandet@member.fsf.org
20 -- Mail : alexis.jeandet@member.fsf.org
21 ----------------------------------------------------------------------------*/
21 ----------------------------------------------------------------------------*/
22 #include "spwtcppacketserver.h"
22 #include "spwtcppacketserver.h"
23 #include "ui_spwtcppacketserver.h"
23 #include "ui_spwtcppacketserver.h"
24 #include <QNetworkInterface>
24 #include <QNetworkInterface>
25 #include <QByteArray>
25 #include <QByteArray>
26 #include <socexplorerengine.h>
26
27
27 SpwTcpPacketServer::SpwTcpPacketServer(QWidget *parent) :
28 SpwTcpPacketServer::SpwTcpPacketServer(QWidget *parent) :
28 QWidget(parent),
29 QWidget(parent),
29 ui(new Ui::SpwTcpPacketServer)
30 ui(new Ui::SpwTcpPacketServer)
30 {
31 {
31 ui->setupUi(this);
32 ui->setupUi(this);
32 this->p_bridge = NULL;
33 this->p_bridge = NULL;
33 this->p_server = new QTcpServer();
34 this->p_server = new QTcpServer();
34 this->incomingPacketParser = new IncomingPacketParser();
35 this->incomingPacketParser = new IncomingPacketParser();
35
36
36 connect(this->ui->startServeQpb,SIGNAL(clicked(bool)),SLOT(toggleServer()));
37 connect(this->ui->startServeQpb,SIGNAL(clicked(bool)),SLOT(toggleServer()));
37 updateHostIP();
38 updateHostIP();
38 this->ui->PortLineEdit->setText("2200");
39 this->ui->PortLineEdit->setText("2200");
39 connect(this->p_server,SIGNAL(newConnection()),this,SLOT(newConnection()));
40 connect(this->p_server,SIGNAL(newConnection()),this,SLOT(newConnection()));
40 resetStatististics();
41 resetStatististics();
41
42
42 connect( this->incomingPacketParser, SIGNAL(sendPacketUsingSpaceWire(QByteArray)),
43 connect( this->incomingPacketParser, SIGNAL(sendPacketUsingSpaceWire(QByteArray)),
43 this, SLOT(sendSPWPacketUsingSpaceWire(QByteArray)));
44 this, SLOT(sendSPWPacketUsingSpaceWire(QByteArray)));
44 }
45 }
45
46
46 SpwTcpPacketServer::~SpwTcpPacketServer()
47 SpwTcpPacketServer::~SpwTcpPacketServer()
47 {
48 {
48 delete ui;
49 delete ui;
49 }
50 }
50
51
51 void SpwTcpPacketServer::setBridge(abstractSpwBridge *bridge)
52 void SpwTcpPacketServer::setBridge(abstractSpwBridge *bridge)
52 {
53 {
53 if(this->p_bridge!=NULL)
54 if(this->p_bridge!=NULL)
54 {
55 {
55 disconnect(this,SLOT(pushPacket(char*,int)));
56 disconnect(this,SLOT(pushPacket(char*,int)));
56 }
57 }
57 this->p_bridge = bridge;
58 this->p_bridge = bridge;
58 connect(bridge,SIGNAL(pushPacketOverTCP(char*,int)),SLOT(pushPacket(char*,int)));
59 connect(bridge,SIGNAL(pushPacketOverTCP(char*,int)),SLOT(pushPacket(char*,int)));
59 }
60 }
60
61
61 void SpwTcpPacketServer::pushPacket(char *packet, int size)
62 void SpwTcpPacketServer::pushPacket(char *packet, int size)
62 {
63 {
63 QByteArray data;
64 QByteArray data;
64 char sizech[3];
65 char sizech[3];
65 data.append((char)0);
66 data.append((char)0);
66 sizech[0]=size & 0x0FF;
67 sizech[0]=size & 0x0FF;
67 sizech[1]=(size>>8) & 0x0FF;
68 sizech[1]=(size>>8) & 0x0FF;
68 sizech[2]=(size>>16) & 0x0FF;
69 sizech[2]=(size>>16) & 0x0FF;
69 data.append(sizech,3);
70 data.append(sizech,3);
70 data.append(packet,size);
71 data.append(packet,size);
71 for(int i=0;i<connectedClients.count();i++)
72 for(int i=0;i<connectedClients.count();i++)
72 {
73 {
73 QTcpSocket* soc=connectedClients.at(i);
74 QTcpSocket* soc=connectedClients.at(i);
74 if(soc->state()!=QAbstractSocket::ConnectedState)
75 if(soc->state()!=QAbstractSocket::ConnectedState)
75 {
76 {
76 connectedClients.removeAt(i);
77 connectedClients.removeAt(i);
77 delete soc;
78 delete soc;
78 }
79 }
79 else
80 else
80 {
81 {
81 connectedClients.at(i)->write(data);
82 connectedClients.at(i)->write(data);
82 onePacketTransmitted();
83 onePacketTransmitted();
83 }
84 }
84 }
85 }
85 free(packet);
86 free(packet);
86 }
87 }
87
88
88 void SpwTcpPacketServer::toggleServer()
89 void SpwTcpPacketServer::toggleServer()
89 {
90 {
90 if(this->p_server->isListening())
91 if(this->p_server->isListening())
91 {
92 {
92 disconnectServer();
93 disconnectServer();
93 }
94 }
94 else
95 else
95 {
96 {
96 connectServer();
97 connectServer();
97 }
98 }
98 }
99 }
99
100
100 void SpwTcpPacketServer::connectServer()
101 void SpwTcpPacketServer::connectServer()
101 {
102 {
102 this->p_server->listen(QHostAddress::Any,this->ui->PortLineEdit->text().toInt());
103 this->p_server->listen(QHostAddress::Any,this->ui->PortLineEdit->text().toInt());
103 this->ui->startServeQpb->setText("Stop Server");
104 this->ui->startServeQpb->setText("Stop Server");
104 resetStatististics();
105 resetStatististics();
105 }
106 }
106
107
107 void SpwTcpPacketServer::disconnectServer()
108 void SpwTcpPacketServer::disconnectServer()
108 {
109 {
109 this->ui->startServeQpb->setText("Start Server");
110 this->ui->startServeQpb->setText("Start Server");
110 this->p_server->close();
111 this->p_server->close();
111 }
112 }
112
113
113 void SpwTcpPacketServer::setServerPort(qint32 port)
114 void SpwTcpPacketServer::setServerPort(qint32 port)
114 {
115 {
115 this->ui->PortLineEdit->setText(QString("%1").arg(port));
116 this->ui->PortLineEdit->setText(QString("%1").arg(port));
116 }
117 }
117
118
118 void SpwTcpPacketServer::setServerSetIP(QString ip)
119 void SpwTcpPacketServer::setServerSetIP(QString ip)
119 {
120 {
120 this->ui->IPLineEdit->setText(ip);
121 this->ui->IPLineEdit->setText(ip);
121 }
122 }
122
123
123 void SpwTcpPacketServer::newConnection()
124 void SpwTcpPacketServer::newConnection()
124 {
125 {
125 QTcpSocket* soc=this->p_server->nextPendingConnection();
126 QTcpSocket* soc=this->p_server->nextPendingConnection();
126 this->connectedClients.append(soc);
127 this->connectedClients.append(soc);
127 this->ui->listWidget->addItem(this->connectedClients.last()->peerAddress().toString());
128 this->ui->listWidget->addItem(this->connectedClients.last()->peerAddress().toString());
128 connect(soc,SIGNAL(readyRead()),this,SLOT(parseIncomingPacket()));
129 connect(soc,SIGNAL(readyRead()),this,SLOT(parseIncomingPacket()));
129 }
130 }
130
131
131 void SpwTcpPacketServer::parseIncomingPacket()
132 void SpwTcpPacketServer::parseIncomingPacket()
132 {
133 {
133 for(int i=0;i<connectedClients.count();i++)
134 for(int i=0;i<connectedClients.count();i++)
134 {
135 {
135 QTcpSocket* soc=connectedClients.at(i);
136 QTcpSocket* soc=connectedClients.at(i);
136 if(soc->state()!=QAbstractSocket::ConnectedState)
137 if(soc->state()!=QAbstractSocket::ConnectedState)
137 {
138 {
138 connectedClients.removeAt(i);
139 connectedClients.removeAt(i);
139 delete soc;
140 delete soc;
140 }
141 }
141 else
142 else
142 {
143 {
143 if(soc->bytesAvailable()!=0)
144 if(soc->bytesAvailable()!=0)
144 {
145 {
145 do
146 do
146 {
147 {
147 QByteArray data = soc->readAll();
148 QByteArray data = soc->readAll();
148 incomingPacketParser->processIncomingQByteArray( data );
149 incomingPacketParser->processIncomingQByteArray( data );
149 }while(soc->bytesAvailable()!=0);
150 }while(soc->bytesAvailable()!=0);
150 }
151 }
151 }
152 }
152 }
153 }
153 }
154 }
154
155
155 void SpwTcpPacketServer::sendSPWPacketUsingSpaceWire(QByteArray data)
156 void SpwTcpPacketServer::sendSPWPacketUsingSpaceWire(QByteArray data)
156 {
157 {
157 onePacketReceived();
158 onePacketReceived();
158 if(data[0]==(char)0) // Protocole = 0 => Host to SpaceWire packet transmission
159 if(data[0]==(char)0) // Protocole = 0 => Host to SpaceWire packet transmission
159 {
160 {
160 int size = ( (unsigned char) data[1] ) * 256 * 256
161 int size = ( (unsigned char) data[1] ) * 256 * 256
161 + ( (unsigned char) data[2] ) * 256
162 + ( (unsigned char) data[2] ) * 256
162 + (unsigned char) data[3];
163 + (unsigned char) data[3];
163 char* SPWpacket = (char*)malloc(size);
164 char* SPWpacket = (char*)malloc(size);
164 memcpy(SPWpacket,data.data()+4,size); // 4 bytes will be added later to the packet
165 if(SPWpacket!=NULL)
165 emit sendSPWPacket(SPWpacket,size);
166 {
167 memcpy(SPWpacket,data.data()+4,size); // 4 bytes will be added later to the packet
168 emit sendSPWPacket(SPWpacket,size);
169 }
170 else
171 {
172 //TODO print some message
173 }
166 }
174 }
167 }
175 }
168
176
169 void SpwTcpPacketServer::readReady()
177 void SpwTcpPacketServer::readReady()
170 {
178 {
171 for(int i=0;i<connectedClients.count();i++)
179 for(int i=0;i<connectedClients.count();i++)
172 {
180 {
173 QTcpSocket* soc=connectedClients.at(i);
181 QTcpSocket* soc=connectedClients.at(i);
174 if(soc->state()!=QAbstractSocket::ConnectedState)
182 if(soc->state()!=QAbstractSocket::ConnectedState)
175 {
183 {
176 connectedClients.removeAt(i);
184 connectedClients.removeAt(i);
177 delete soc;
185 delete soc;
178 }
186 }
179 else
187 else
180 {
188 {
181 if(soc->bytesAvailable()!=0)
189 if(soc->bytesAvailable()!=0)
182 {
190 {
183 do
191 do
184 {
192 {
185 QByteArray data = soc->readAll();
193 QByteArray data = soc->readAll();
186 onePacketReceived();
194 onePacketReceived();
187 if(data[0]==(char)0) // Protocole = 0 => Host to SpaceWire packet transmission
195 if(data[0]==(char)0) // Protocole = 0 => Host to SpaceWire packet transmission
188 {
196 {
189 int size = (data[1]*256*256) + (data[2]*256) + data[3];
197 // TODO -> change heap allocation to stack risk of memory leak
198 // if no slot conected to sendSPWPacket signal or segfault if more
199 // than one receiver.
200 int size = ( (unsigned char) data[1] ) * 256 * 256
201 + ( (unsigned char) data[2] ) * 256
202 + (unsigned char) data[3];
190 char* SPWpacket = (char*)malloc(size);
203 char* SPWpacket = (char*)malloc(size);
191 memcpy(SPWpacket,data.data()+4,size); // 4 bytes will be added later to the packet
204 if(SPWpacket!=NULL)
192 emit sendSPWPacket(SPWpacket,size);
205 {
206 memcpy(SPWpacket,data.data()+4,size); // 4 bytes will be added later to the packet
207 emit sendSPWPacket(SPWpacket,size);
208 }
209 else
210 {
211 //TODO print some message
212 }
193 }
213 }
194 }while(soc->bytesAvailable()!=0);
214 }while(soc->bytesAvailable()!=0);
195 }
215 }
196 }
216 }
197 }
217 }
198 }
218 }
199
219
200 void SpwTcpPacketServer::resetStatististics()
220 void SpwTcpPacketServer::resetStatististics()
201 {
221 {
202 receivedPackets = 0;
222 receivedPackets = 0;
203 transmittedPackets = 0;
223 transmittedPackets = 0;
204
224
205 this->ui->receivedPacketsNumber->display( QString::number(receivedPackets) );
225 this->ui->receivedPacketsNumber->display( QString::number(receivedPackets) );
206 this->ui->transmittedPacketsNumber->display( QString::number(transmittedPackets) );
226 this->ui->transmittedPacketsNumber->display( QString::number(transmittedPackets) );
207 }
227 }
208
228
209 void SpwTcpPacketServer::updateHostIP()
229 void SpwTcpPacketServer::updateHostIP()
210 {
230 {
211 QList<QHostAddress> list = QNetworkInterface::allAddresses();
231 QList<QHostAddress> list = QNetworkInterface::allAddresses();
212
232
213 for(int nIter=0; nIter<list.count(); nIter++)
233 for(int nIter=0; nIter<list.count(); nIter++)
214 {
234 {
215 if(!list[nIter].isLoopback())
235 if(!list[nIter].isLoopback())
216 if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
236 if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol )
217 this->ui->IPLineEdit->setText(list[nIter].toString());
237 this->ui->IPLineEdit->setText(list[nIter].toString());
218 }
238 }
219 }
239 }
220
240
221 void SpwTcpPacketServer::onePacketReceived()
241 void SpwTcpPacketServer::onePacketReceived()
222 {
242 {
223 receivedPackets = receivedPackets + 1;
243 receivedPackets = receivedPackets + 1;
224
244
225 this->ui->receivedPacketsNumber->display( QString::number(receivedPackets) );
245 this->ui->receivedPacketsNumber->display( QString::number(receivedPackets) );
226 }
246 }
227
247
228 void SpwTcpPacketServer::onePacketTransmitted()
248 void SpwTcpPacketServer::onePacketTransmitted()
229 {
249 {
230 transmittedPackets = transmittedPackets + 1;
250 transmittedPackets = transmittedPackets + 1;
231
251
232 this->ui->transmittedPacketsNumber->display( QString::number(transmittedPackets) );
252 this->ui->transmittedPacketsNumber->display( QString::number(transmittedPackets) );
233 }
253 }
General Comments 0
You need to be logged in to leave comments. Login now