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