source: pacpussensors/trunk/PacpusSocket/PacpusUDPSocket.cpp@ 130

Last change on this file since 130 was 126, checked in by DHERBOMEZ Gérald, 9 years ago

StringGenerator component added to test PacpusUDPSocket
XML file to test provided

File size: 6.7 KB
Line 
1/*********************************************************************
2// created: 2012/03/01 - 14:06
3// filename: PacpusUDPSocket.cpp
4//
5// author: Pierre Hudelaine
6// Copyright Heudiasyc UMR UTC/CNRS 7253
7//
8// version: $Id: $
9//
10// purpose: Create network socket if needed in Pacpus
11//
12*********************************************************************/
13
14#include "PacpusUDPSocket.h"
15
16#include <qbuffer.h>
17#include <qbytearray.h>
18
19
20using namespace pacpus;
21
22
23DECLARE_STATIC_LOGGER("pacpus.base.PacpusUDPSocket");
24
25
26//////////////////////////////////////////////////////////////////////////
27// Construct the factory
28//////////////////////////////////////////////////////////////////////////
29static ComponentFactory <PacpusUDPSocket> sFactory("PacpusUDPSocket");
30
31
32//////////////////////////////////////////////////////////////////////////
33// Constructeur
34//////////////////////////////////////////////////////////////////////////
35PacpusUDPSocket::PacpusUDPSocket(QString name)
36 : ComponentBase(name)
37{
38}
39
40
41//////////////////////////////////////////////////////////////////////////
42// Destructor
43//////////////////////////////////////////////////////////////////////////
44PacpusUDPSocket::~PacpusUDPSocket()
45{
46}
47
48
49////////////////////////////////////////////////////////////////////////////////
50// AddOutputs
51////////////////////////////////////////////////////////////////////////////////
52void PacpusUDPSocket::addOutputs()
53{
54 addOutput<QString, PacpusUDPSocket>("string");
55}
56
57
58////////////////////////////////////////////////////////////////////////////////
59// AddInputs
60////////////////////////////////////////////////////////////////////////////////
61void PacpusUDPSocket::addInputs()
62{
63 addInput<QString, PacpusUDPSocket>("string", &PacpusUDPSocket::sendQString);
64}
65
66
67//////////////////////////////////////////////////////////////////////////
68// Called by the ComponentManager to pass the XML parameters to the
69// component
70//////////////////////////////////////////////////////////////////////////
71ComponentBase::COMPONENT_CONFIGURATION PacpusUDPSocket::configureComponent(XmlComponentConfig config)
72{
73
74 socketType_ = config.getProperty("typeSocket");
75
76 if (socketType_ == "client" || socketType_ == "Client")
77 {
78 address2send_.setAddress(config.getProperty("address"));
79 port2send_ = config.getProperty("port").toUInt();
80 socketType_ = "client";
81 }
82 else if (socketType_ == "server" || socketType_ == "Server")
83 {
84 port2bind_ = config.getProperty("port").toUInt();
85 socketType_ = "server";
86 }
87 else
88 {
89 qDebug("typeSocket incorrect, become client");
90 address2send_.setAddress(config.getProperty("address"));
91 port2send_ = config.getProperty("port").toUInt();
92 socketType_ = "client";
93 }
94
95 return ComponentBase::CONFIGURED_OK;
96}
97
98
99//////////////////////////////////////////////////////////////////////////
100// Called by the ComponentManager to start the component
101//////////////////////////////////////////////////////////////////////////
102void PacpusUDPSocket::startActivity()
103{
104 if (!(udpSocket_ = new QUdpSocket()))
105 qFatal("Failed to init UDP socket!");
106
107 if (socketType_ == "server")
108 {
109 if (udpSocket_->bind(QHostAddress::Any, port2bind_, QUdpSocket::DontShareAddress))
110 qDebug() << "Socket bound for server on port " << port2bind_;
111 else
112 qWarning() << "Failed to bind socket for server on port" << port2bind_;
113 }
114
115 connect(udpSocket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
116
117 udpSocketOutput_ = getTypedOutput<QString, PacpusUDPSocket>("string");
118}
119
120
121//////////////////////////////////////////////////////////////////////////
122// Send datagram QString
123//////////////////////////////////////////////////////////////////////////
124void PacpusUDPSocket::sendQString(const QString& frame)
125{
126 int sent=0;
127
128 LOG_INFO("component " << name() << " sent " << frame);
129
130 if (socketType_ == "client")
131 {
132 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1)
133 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
134 }
135 else if (socketType_ == "server")
136 {
137 for (int i = 0; i < listClients.size(); i++)
138 {
139 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
140 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
141 }
142 }
143}
144
145
146//////////////////////////////////////////////////////////////////////////
147// Send datagram QByteArray
148//////////////////////////////////////////////////////////////////////////
149void PacpusUDPSocket::sendDatagrams(QByteArray frame)
150{
151 int sent=0;
152
153 if (socketType_ == "client")
154 {
155 if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1)
156 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
157 }
158 else if (socketType_ == "server")
159 {
160 for (int i = 0; i < listClients.size(); i++)
161 {
162 if ((sent = udpSocket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
163 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
164 }
165 }
166}
167
168
169//////////////////////////////////////////////////////////////////////////
170// Called when the socket receive a new datagram
171//////////////////////////////////////////////////////////////////////////
172void PacpusUDPSocket::readPendingDatagrams()
173{
174 while (udpSocket_->hasPendingDatagrams())
175 {
176 QByteArray datagram;
177 datagram.resize(udpSocket_->pendingDatagramSize());
178 QHostAddress sender;
179 quint16 senderPort;
180
181 if(udpSocket_->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort) != -1)
182 {
183
184 LOG_INFO("component " << name() << " received " << QString(datagram.data()));
185
186 if (socketType_ == "server")
187 {
188 bool flag = false;
189
190 for (int i = 0; i < listClients.size(); i++)
191 {
192 if (listClients[i]->getAddress() == sender && listClients[i]->getPort() == senderPort)
193 flag = true;
194 }
195
196 if (flag == false)
197 listClients << new Client(sender, senderPort);
198 }
199
200
201 if (udpSocketOutput_ && udpSocketOutput_->hasConnection())
202 udpSocketOutput_->send(QString(datagram.data()));
203 }
204 else
205 {
206 printf("Error when reading network frame\n");
207 }
208 }
209}
210
211
212//////////////////////////////////////////////////////////////////////////
213// Called by the ComponentManager to stop the component
214//////////////////////////////////////////////////////////////////////////
215void PacpusUDPSocket::stopActivity()
216{
217 udpSocket_->close();
218 delete udpSocket_;
219}
Note: See TracBrowser for help on using the repository browser.