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

Last change on this file since 110 was 99, checked in by nguyenhu, 9 years ago

compilation under linux with 0.2.X framework

File size: 6.6 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
[85]128 if (socketType_ == "client")
129 {
[99]130 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1)
131 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
[85]132 }
133 else if (socketType_ == "server")
134 {
135 for (int i = 0; i < listClients.size(); i++)
136 {
[99]137 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
138 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
[85]139 }
140 }
[99]141}
[85]142
143
[86]144//////////////////////////////////////////////////////////////////////////
145// Send datagram QByteArray
146//////////////////////////////////////////////////////////////////////////
[99]147void PacpusUDPSocket::sendDatagrams(QByteArray frame)
148{
[85]149 int sent=0;
[99]150
[85]151 if (socketType_ == "client")
152 {
[99]153 if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1)
154 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
[85]155 }
156 else if (socketType_ == "server")
157 {
158 for (int i = 0; i < listClients.size(); i++)
159 {
[99]160 if ((sent = udpSocket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
161 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
[85]162 }
163 }
[99]164}
[85]165
166
[86]167//////////////////////////////////////////////////////////////////////////
[99]168// Called when the socket receive a new datagram
[86]169//////////////////////////////////////////////////////////////////////////
[85]170void PacpusUDPSocket::readPendingDatagrams()
171{
[99]172 while (udpSocket_->hasPendingDatagrams())
[85]173 {
[99]174 QByteArray datagram;
175 datagram.resize(udpSocket_->pendingDatagramSize());
176 QHostAddress sender;
[85]177 quint16 senderPort;
[99]178
[85]179 if(udpSocket_->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort) != -1)
[99]180 {
[85]181 if (socketType_ == "server")
182 {
183 bool flag = false;
[99]184
185 for (int i = 0; i < listClients.size(); i++)
186 {
[85]187 if (listClients[i]->getAddress() == sender && listClients[i]->getPort() == senderPort)
188 flag = true;
189 }
[99]190
[85]191 if (flag == false)
192 listClients << new Client(sender, senderPort);
193 }
[99]194
195 if (udpSocketOutput_ && udpSocketOutput_->hasConnection())
196 udpSocketOutput_->send(QString(datagram.data()));
[85]197 }
198 else
199 {
200 printf("Error when reading network frame\n");
201 }
202 }
203}
204
205
206//////////////////////////////////////////////////////////////////////////
207// Called by the ComponentManager to stop the component
208//////////////////////////////////////////////////////////////////////////
209void PacpusUDPSocket::stopActivity()
210{
211 udpSocket_->close();
212 delete udpSocket_;
213}
Note: See TracBrowser for help on using the repository browser.