@@ -1,231 +1,233 | |||||
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 |
|
26 | |||
27 | SpwTcpPacketServer::SpwTcpPacketServer(QWidget *parent) : |
|
27 | SpwTcpPacketServer::SpwTcpPacketServer(QWidget *parent) : | |
28 | QWidget(parent), |
|
28 | QWidget(parent), | |
29 | ui(new Ui::SpwTcpPacketServer) |
|
29 | ui(new Ui::SpwTcpPacketServer) | |
30 | { |
|
30 | { | |
31 | ui->setupUi(this); |
|
31 | ui->setupUi(this); | |
32 | this->p_bridge = NULL; |
|
32 | this->p_bridge = NULL; | |
33 | this->p_server = new QTcpServer(); |
|
33 | this->p_server = new QTcpServer(); | |
34 | this->incomingPacketParser = new IncomingPacketParser(); |
|
34 | this->incomingPacketParser = new IncomingPacketParser(); | |
35 |
|
35 | |||
36 | connect(this->ui->startServeQpb,SIGNAL(clicked(bool)),SLOT(toggleServer())); |
|
36 | connect(this->ui->startServeQpb,SIGNAL(clicked(bool)),SLOT(toggleServer())); | |
37 | updateHostIP(); |
|
37 | updateHostIP(); | |
38 | this->ui->PortLineEdit->setText("2200"); |
|
38 | this->ui->PortLineEdit->setText("2200"); | |
39 | connect(this->p_server,SIGNAL(newConnection()),this,SLOT(newConnection())); |
|
39 | connect(this->p_server,SIGNAL(newConnection()),this,SLOT(newConnection())); | |
40 | resetStatististics(); |
|
40 | resetStatististics(); | |
41 |
|
41 | |||
42 | connect( this->incomingPacketParser, SIGNAL(sendPacketUsingSpaceWire(QByteArray)), |
|
42 | connect( this->incomingPacketParser, SIGNAL(sendPacketUsingSpaceWire(QByteArray)), | |
43 | this, SLOT(sendSPWPacketUsingSpaceWire(QByteArray))); |
|
43 | this, SLOT(sendSPWPacketUsingSpaceWire(QByteArray))); | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 | SpwTcpPacketServer::~SpwTcpPacketServer() |
|
46 | SpwTcpPacketServer::~SpwTcpPacketServer() | |
47 | { |
|
47 | { | |
48 | delete ui; |
|
48 | delete ui; | |
49 | } |
|
49 | } | |
50 |
|
50 | |||
51 | void SpwTcpPacketServer::setBridge(abstractSpwBridge *bridge) |
|
51 | void SpwTcpPacketServer::setBridge(abstractSpwBridge *bridge) | |
52 | { |
|
52 | { | |
53 | if(this->p_bridge!=NULL) |
|
53 | if(this->p_bridge!=NULL) | |
54 | { |
|
54 | { | |
55 | disconnect(this,SLOT(pushPacket(char*,int))); |
|
55 | disconnect(this,SLOT(pushPacket(char*,int))); | |
56 | } |
|
56 | } | |
57 | this->p_bridge = bridge; |
|
57 | this->p_bridge = bridge; | |
58 | connect(bridge,SIGNAL(pushPacketOverTCP(char*,int)),SLOT(pushPacket(char*,int))); |
|
58 | connect(bridge,SIGNAL(pushPacketOverTCP(char*,int)),SLOT(pushPacket(char*,int))); | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 | void SpwTcpPacketServer::pushPacket(char *packet, int size) |
|
61 | void SpwTcpPacketServer::pushPacket(char *packet, int size) | |
62 | { |
|
62 | { | |
63 | QByteArray data; |
|
63 | QByteArray data; | |
64 | char sizech[3]; |
|
64 | char sizech[3]; | |
65 | data.append((char)0); |
|
65 | data.append((char)0); | |
66 | sizech[0]=size & 0x0FF; |
|
66 | sizech[0]=size & 0x0FF; | |
67 | sizech[1]=(size>>8) & 0x0FF; |
|
67 | sizech[1]=(size>>8) & 0x0FF; | |
68 | sizech[2]=(size>>16) & 0x0FF; |
|
68 | sizech[2]=(size>>16) & 0x0FF; | |
69 | data.append(sizech,3); |
|
69 | data.append(sizech,3); | |
70 | data.append(packet,size); |
|
70 | data.append(packet,size); | |
71 | for(int i=0;i<connectedClients.count();i++) |
|
71 | for(int i=0;i<connectedClients.count();i++) | |
72 | { |
|
72 | { | |
73 | QTcpSocket* soc=connectedClients.at(i); |
|
73 | QTcpSocket* soc=connectedClients.at(i); | |
74 | if(soc->state()!=QAbstractSocket::ConnectedState) |
|
74 | if(soc->state()!=QAbstractSocket::ConnectedState) | |
75 | { |
|
75 | { | |
76 | connectedClients.removeAt(i); |
|
76 | connectedClients.removeAt(i); | |
77 | delete soc; |
|
77 | delete soc; | |
78 | } |
|
78 | } | |
79 | else |
|
79 | else | |
80 | { |
|
80 | { | |
81 | connectedClients.at(i)->write(data); |
|
81 | connectedClients.at(i)->write(data); | |
82 | onePacketTransmitted(); |
|
82 | onePacketTransmitted(); | |
83 | } |
|
83 | } | |
84 | } |
|
84 | } | |
85 | free(packet); |
|
85 | free(packet); | |
86 | } |
|
86 | } | |
87 |
|
87 | |||
88 | void SpwTcpPacketServer::toggleServer() |
|
88 | void SpwTcpPacketServer::toggleServer() | |
89 | { |
|
89 | { | |
90 | if(this->p_server->isListening()) |
|
90 | if(this->p_server->isListening()) | |
91 | { |
|
91 | { | |
92 | disconnectServer(); |
|
92 | disconnectServer(); | |
93 | } |
|
93 | } | |
94 | else |
|
94 | else | |
95 | { |
|
95 | { | |
96 | connectServer(); |
|
96 | connectServer(); | |
97 | } |
|
97 | } | |
98 | } |
|
98 | } | |
99 |
|
99 | |||
100 | void SpwTcpPacketServer::connectServer() |
|
100 | void SpwTcpPacketServer::connectServer() | |
101 | { |
|
101 | { | |
102 | this->p_server->listen(QHostAddress::Any,this->ui->PortLineEdit->text().toInt()); |
|
102 | this->p_server->listen(QHostAddress::Any,this->ui->PortLineEdit->text().toInt()); | |
103 | this->ui->startServeQpb->setText("Stop Server"); |
|
103 | this->ui->startServeQpb->setText("Stop Server"); | |
104 | resetStatististics(); |
|
104 | resetStatististics(); | |
105 | } |
|
105 | } | |
106 |
|
106 | |||
107 | void SpwTcpPacketServer::disconnectServer() |
|
107 | void SpwTcpPacketServer::disconnectServer() | |
108 | { |
|
108 | { | |
109 | this->ui->startServeQpb->setText("Start Server"); |
|
109 | this->ui->startServeQpb->setText("Start Server"); | |
110 | this->p_server->close(); |
|
110 | this->p_server->close(); | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 | void SpwTcpPacketServer::setServerPort(qint32 port) |
|
113 | void SpwTcpPacketServer::setServerPort(qint32 port) | |
114 | { |
|
114 | { | |
115 | this->ui->PortLineEdit->setText(QString("%1").arg(port)); |
|
115 | this->ui->PortLineEdit->setText(QString("%1").arg(port)); | |
116 | } |
|
116 | } | |
117 |
|
117 | |||
118 | void SpwTcpPacketServer::setServerSetIP(QString ip) |
|
118 | void SpwTcpPacketServer::setServerSetIP(QString ip) | |
119 | { |
|
119 | { | |
120 | this->ui->IPLineEdit->setText(ip); |
|
120 | this->ui->IPLineEdit->setText(ip); | |
121 | } |
|
121 | } | |
122 |
|
122 | |||
123 | void SpwTcpPacketServer::newConnection() |
|
123 | void SpwTcpPacketServer::newConnection() | |
124 | { |
|
124 | { | |
125 | QTcpSocket* soc=this->p_server->nextPendingConnection(); |
|
125 | QTcpSocket* soc=this->p_server->nextPendingConnection(); | |
126 | this->connectedClients.append(soc); |
|
126 | this->connectedClients.append(soc); | |
127 | this->ui->listWidget->addItem(this->connectedClients.last()->peerAddress().toString()); |
|
127 | this->ui->listWidget->addItem(this->connectedClients.last()->peerAddress().toString()); | |
128 | connect(soc,SIGNAL(readyRead()),this,SLOT(parseIncomingPacket())); |
|
128 | connect(soc,SIGNAL(readyRead()),this,SLOT(parseIncomingPacket())); | |
129 | } |
|
129 | } | |
130 |
|
130 | |||
131 | void SpwTcpPacketServer::parseIncomingPacket() |
|
131 | void SpwTcpPacketServer::parseIncomingPacket() | |
132 | { |
|
132 | { | |
133 | for(int i=0;i<connectedClients.count();i++) |
|
133 | for(int i=0;i<connectedClients.count();i++) | |
134 | { |
|
134 | { | |
135 | QTcpSocket* soc=connectedClients.at(i); |
|
135 | QTcpSocket* soc=connectedClients.at(i); | |
136 | if(soc->state()!=QAbstractSocket::ConnectedState) |
|
136 | if(soc->state()!=QAbstractSocket::ConnectedState) | |
137 | { |
|
137 | { | |
138 | connectedClients.removeAt(i); |
|
138 | connectedClients.removeAt(i); | |
139 | delete soc; |
|
139 | delete soc; | |
140 | } |
|
140 | } | |
141 | else |
|
141 | else | |
142 | { |
|
142 | { | |
143 | if(soc->bytesAvailable()!=0) |
|
143 | if(soc->bytesAvailable()!=0) | |
144 | { |
|
144 | { | |
145 | do |
|
145 | do | |
146 | { |
|
146 | { | |
147 | QByteArray data = soc->readAll(); |
|
147 | QByteArray data = soc->readAll(); | |
148 | incomingPacketParser->processIncomingQByteArray( data ); |
|
148 | incomingPacketParser->processIncomingQByteArray( data ); | |
149 | }while(soc->bytesAvailable()!=0); |
|
149 | }while(soc->bytesAvailable()!=0); | |
150 | } |
|
150 | } | |
151 | } |
|
151 | } | |
152 | } |
|
152 | } | |
153 | } |
|
153 | } | |
154 |
|
154 | |||
155 | void SpwTcpPacketServer::sendSPWPacketUsingSpaceWire(QByteArray data) |
|
155 | void SpwTcpPacketServer::sendSPWPacketUsingSpaceWire(QByteArray data) | |
156 | { |
|
156 | { | |
157 | onePacketReceived(); |
|
157 | onePacketReceived(); | |
158 | if(data[0]==(char)0) // Protocole = 0 => Host to SpaceWire packet transmission |
|
158 | if(data[0]==(char)0) // Protocole = 0 => Host to SpaceWire packet transmission | |
159 | { |
|
159 | { | |
160 | int size = (data[1]*256*256) + (data[2]*256) + data[3]; |
|
160 | int size = ( (unsigned char) data[1] ) * 256 * 256 | |
|
161 | + ( (unsigned char) data[2] ) * 256 | |||
|
162 | + (unsigned char) data[3]; | |||
161 | char* SPWpacket = (char*)malloc(size); |
|
163 | char* SPWpacket = (char*)malloc(size); | |
162 | memcpy(SPWpacket,data.data()+4,size); // 4 bytes will be added later to the packet |
|
164 | memcpy(SPWpacket,data.data()+4,size); // 4 bytes will be added later to the packet | |
163 | emit sendSPWPacket(SPWpacket,size); |
|
165 | emit sendSPWPacket(SPWpacket,size); | |
164 | } |
|
166 | } | |
165 | } |
|
167 | } | |
166 |
|
168 | |||
167 | void SpwTcpPacketServer::readReady() |
|
169 | void SpwTcpPacketServer::readReady() | |
168 | { |
|
170 | { | |
169 | for(int i=0;i<connectedClients.count();i++) |
|
171 | for(int i=0;i<connectedClients.count();i++) | |
170 | { |
|
172 | { | |
171 | QTcpSocket* soc=connectedClients.at(i); |
|
173 | QTcpSocket* soc=connectedClients.at(i); | |
172 | if(soc->state()!=QAbstractSocket::ConnectedState) |
|
174 | if(soc->state()!=QAbstractSocket::ConnectedState) | |
173 | { |
|
175 | { | |
174 | connectedClients.removeAt(i); |
|
176 | connectedClients.removeAt(i); | |
175 | delete soc; |
|
177 | delete soc; | |
176 | } |
|
178 | } | |
177 | else |
|
179 | else | |
178 | { |
|
180 | { | |
179 | if(soc->bytesAvailable()!=0) |
|
181 | if(soc->bytesAvailable()!=0) | |
180 | { |
|
182 | { | |
181 | do |
|
183 | do | |
182 | { |
|
184 | { | |
183 | QByteArray data = soc->readAll(); |
|
185 | QByteArray data = soc->readAll(); | |
184 | onePacketReceived(); |
|
186 | onePacketReceived(); | |
185 | if(data[0]==(char)0) // Protocole = 0 => Host to SpaceWire packet transmission |
|
187 | if(data[0]==(char)0) // Protocole = 0 => Host to SpaceWire packet transmission | |
186 | { |
|
188 | { | |
187 | int size = (data[1]*256*256) + (data[2]*256) + data[3]; |
|
189 | int size = (data[1]*256*256) + (data[2]*256) + data[3]; | |
188 | char* SPWpacket = (char*)malloc(size); |
|
190 | char* SPWpacket = (char*)malloc(size); | |
189 | memcpy(SPWpacket,data.data()+4,size); // 4 bytes will be added later to the packet |
|
191 | memcpy(SPWpacket,data.data()+4,size); // 4 bytes will be added later to the packet | |
190 | emit sendSPWPacket(SPWpacket,size); |
|
192 | emit sendSPWPacket(SPWpacket,size); | |
191 | } |
|
193 | } | |
192 | }while(soc->bytesAvailable()!=0); |
|
194 | }while(soc->bytesAvailable()!=0); | |
193 | } |
|
195 | } | |
194 | } |
|
196 | } | |
195 | } |
|
197 | } | |
196 | } |
|
198 | } | |
197 |
|
199 | |||
198 | void SpwTcpPacketServer::resetStatististics() |
|
200 | void SpwTcpPacketServer::resetStatististics() | |
199 | { |
|
201 | { | |
200 | receivedPackets = 0; |
|
202 | receivedPackets = 0; | |
201 | transmittedPackets = 0; |
|
203 | transmittedPackets = 0; | |
202 |
|
204 | |||
203 | this->ui->receivedPacketsNumber->display( QString::number(receivedPackets) ); |
|
205 | this->ui->receivedPacketsNumber->display( QString::number(receivedPackets) ); | |
204 | this->ui->transmittedPacketsNumber->display( QString::number(transmittedPackets) ); |
|
206 | this->ui->transmittedPacketsNumber->display( QString::number(transmittedPackets) ); | |
205 | } |
|
207 | } | |
206 |
|
208 | |||
207 | void SpwTcpPacketServer::updateHostIP() |
|
209 | void SpwTcpPacketServer::updateHostIP() | |
208 | { |
|
210 | { | |
209 | QList<QHostAddress> list = QNetworkInterface::allAddresses(); |
|
211 | QList<QHostAddress> list = QNetworkInterface::allAddresses(); | |
210 |
|
212 | |||
211 | for(int nIter=0; nIter<list.count(); nIter++) |
|
213 | for(int nIter=0; nIter<list.count(); nIter++) | |
212 | { |
|
214 | { | |
213 | if(!list[nIter].isLoopback()) |
|
215 | if(!list[nIter].isLoopback()) | |
214 | if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol ) |
|
216 | if (list[nIter].protocol() == QAbstractSocket::IPv4Protocol ) | |
215 | this->ui->IPLineEdit->setText(list[nIter].toString()); |
|
217 | this->ui->IPLineEdit->setText(list[nIter].toString()); | |
216 | } |
|
218 | } | |
217 | } |
|
219 | } | |
218 |
|
220 | |||
219 | void SpwTcpPacketServer::onePacketReceived() |
|
221 | void SpwTcpPacketServer::onePacketReceived() | |
220 | { |
|
222 | { | |
221 | receivedPackets = receivedPackets + 1; |
|
223 | receivedPackets = receivedPackets + 1; | |
222 |
|
224 | |||
223 | this->ui->receivedPacketsNumber->display( QString::number(receivedPackets) ); |
|
225 | this->ui->receivedPacketsNumber->display( QString::number(receivedPackets) ); | |
224 | } |
|
226 | } | |
225 |
|
227 | |||
226 | void SpwTcpPacketServer::onePacketTransmitted() |
|
228 | void SpwTcpPacketServer::onePacketTransmitted() | |
227 | { |
|
229 | { | |
228 | transmittedPackets = transmittedPackets + 1; |
|
230 | transmittedPackets = transmittedPackets + 1; | |
229 |
|
231 | |||
230 | this->ui->transmittedPacketsNumber->display( QString::number(transmittedPackets) ); |
|
232 | this->ui->transmittedPacketsNumber->display( QString::number(transmittedPackets) ); | |
231 | } |
|
233 | } |
General Comments 0
You need to be logged in to leave comments.
Login now