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