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

Last change on this file since 132 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
RevLine 
[85]1/*********************************************************************
2// created: 2012/03/01 - 14:06
3// filename: PacpusUDPSocket.cpp
4//
5// author: Pierre Hudelaine
6// Copyright Heudiasyc UMR UTC/CNRS 7253
[99]7//
[85]8// version: $Id: $
9//
10// purpose: Create network socket if needed in Pacpus
11//
12*********************************************************************/
13
[99]14#include "PacpusUDPSocket.h"
[85]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//////////////////////////////////////////////////////////////////////////
[99]29static ComponentFactory <PacpusUDPSocket> sFactory("PacpusUDPSocket");
[85]30
31
32//////////////////////////////////////////////////////////////////////////
33// Constructeur
34//////////////////////////////////////////////////////////////////////////
35PacpusUDPSocket::PacpusUDPSocket(QString name)
[99]36 : ComponentBase(name)
[85]37{
[99]38}
[85]39
40
41//////////////////////////////////////////////////////////////////////////
42// Destructor
43//////////////////////////////////////////////////////////////////////////
[99]44PacpusUDPSocket::~PacpusUDPSocket()
[85]45{
46}
47
48
[86]49////////////////////////////////////////////////////////////////////////////////
50// AddOutputs
51////////////////////////////////////////////////////////////////////////////////
52void PacpusUDPSocket::addOutputs()
53{
[99]54 addOutput<QString, PacpusUDPSocket>("string");
[86]55}
56
57
[87]58////////////////////////////////////////////////////////////////////////////////
59// AddInputs
60////////////////////////////////////////////////////////////////////////////////
61void PacpusUDPSocket::addInputs()
62{
[99]63 addInput<QString, PacpusUDPSocket>("string", &PacpusUDPSocket::sendQString);
[87]64}
65
66
[85]67//////////////////////////////////////////////////////////////////////////
[99]68// Called by the ComponentManager to pass the XML parameters to the
69// component
[85]70//////////////////////////////////////////////////////////////////////////
[99]71ComponentBase::COMPONENT_CONFIGURATION PacpusUDPSocket::configureComponent(XmlComponentConfig config)
72{
[85]73
74 socketType_ = config.getProperty("typeSocket");
[99]75
[85]76 if (socketType_ == "client" || socketType_ == "Client")
77 {
78 address2send_.setAddress(config.getProperty("address"));
79 port2send_ = config.getProperty("port").toUInt();
[99]80 socketType_ = "client";
[85]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
[99]95 return ComponentBase::CONFIGURED_OK;
96}
[85]97
[99]98
[85]99//////////////////////////////////////////////////////////////////////////
100// Called by the ComponentManager to start the component
101//////////////////////////////////////////////////////////////////////////
[99]102void PacpusUDPSocket::startActivity()
103{
[85]104 if (!(udpSocket_ = new QUdpSocket()))
105 qFatal("Failed to init UDP socket!");
[99]106
[85]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 }
[99]114
[85]115 connect(udpSocket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
[99]116
117 udpSocketOutput_ = getTypedOutput<QString, PacpusUDPSocket>("string");
[85]118}
119
120
[86]121//////////////////////////////////////////////////////////////////////////
122// Send datagram QString
123//////////////////////////////////////////////////////////////////////////
[99]124void PacpusUDPSocket::sendQString(const QString& frame)
125{
[85]126 int sent=0;
[99]127
[126]128 LOG_INFO("component " << name() << " sent " << frame);
129
[85]130 if (socketType_ == "client")
131 {
[99]132 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1)
133 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
[85]134 }
135 else if (socketType_ == "server")
136 {
137 for (int i = 0; i < listClients.size(); i++)
138 {
[99]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;
[85]141 }
142 }
[99]143}
[85]144
145
[86]146//////////////////////////////////////////////////////////////////////////
147// Send datagram QByteArray
148//////////////////////////////////////////////////////////////////////////
[99]149void PacpusUDPSocket::sendDatagrams(QByteArray frame)
150{
[85]151 int sent=0;
[99]152
[85]153 if (socketType_ == "client")
154 {
[99]155 if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1)
156 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
[85]157 }
158 else if (socketType_ == "server")
159 {
160 for (int i = 0; i < listClients.size(); i++)
161 {
[99]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;
[85]164 }
165 }
[99]166}
[85]167
168
[86]169//////////////////////////////////////////////////////////////////////////
[99]170// Called when the socket receive a new datagram
[86]171//////////////////////////////////////////////////////////////////////////
[85]172void PacpusUDPSocket::readPendingDatagrams()
173{
[99]174 while (udpSocket_->hasPendingDatagrams())
[85]175 {
[99]176 QByteArray datagram;
177 datagram.resize(udpSocket_->pendingDatagramSize());
178 QHostAddress sender;
[85]179 quint16 senderPort;
[99]180
[85]181 if(udpSocket_->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort) != -1)
[99]182 {
[126]183
184 LOG_INFO("component " << name() << " received " << QString(datagram.data()));
185
[85]186 if (socketType_ == "server")
187 {
188 bool flag = false;
[99]189
190 for (int i = 0; i < listClients.size(); i++)
191 {
[85]192 if (listClients[i]->getAddress() == sender && listClients[i]->getPort() == senderPort)
193 flag = true;
194 }
[99]195
[85]196 if (flag == false)
197 listClients << new Client(sender, senderPort);
198 }
[99]199
[126]200
[99]201 if (udpSocketOutput_ && udpSocketOutput_->hasConnection())
202 udpSocketOutput_->send(QString(datagram.data()));
[85]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.