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

Last change on this file since 87 was 87, checked in by phudelai, 9 years ago

gestion des Inputs Pacpsu

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>("udpSocketOutput");
55}
56
57
58////////////////////////////////////////////////////////////////////////////////
59// AddInputs
60////////////////////////////////////////////////////////////////////////////////
61void PacpusUDPSocket::addInputs()
62{
63 addInput<QString, PacpusUDPSocket>("udpSocketInput", &PacpusUDPSocket::sendDatagrams);
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 updSocketOutput_ = getTypedOutput<QString, PacpusUDPSocket>("udpSocketOutput");
118}
119
120
121//////////////////////////////////////////////////////////////////////////
122// Send datagram QString
123//////////////////////////////////////////////////////////////////////////
124void PacpusUDPSocket::sendDatagrams(QString frame)
125{
126 int sent=0;
127
128 if (socketType_ == "client")
129 {
130 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1)
131 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
132 }
133 else if (socketType_ == "server")
134 {
135 for (int i = 0; i < listClients.size(); i++)
136 {
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;
139 }
140 }
141}
142
143
144//////////////////////////////////////////////////////////////////////////
145// Send datagram QByteArray
146//////////////////////////////////////////////////////////////////////////
147void PacpusUDPSocket::sendDatagrams(QByteArray frame)
148{
149 int sent=0;
150
151 if (socketType_ == "client")
152 {
153 if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1)
154 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
155 }
156 else if (socketType_ == "server")
157 {
158 for (int i = 0; i < listClients.size(); i++)
159 {
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;
162 }
163 }
164}
165
166
167//////////////////////////////////////////////////////////////////////////
168// Called when the socket receive a new datagram
169//////////////////////////////////////////////////////////////////////////
170void PacpusUDPSocket::readPendingDatagrams()
171{
172 while (udpSocket_->hasPendingDatagrams())
173 {
174 QByteArray datagram;
175 datagram.resize(udpSocket_->pendingDatagramSize());
176 QHostAddress sender;
177 quint16 senderPort;
178
179 if(udpSocket_->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort) != -1)
180 {
181 if (socketType_ == "server")
182 {
183 bool flag = false;
184
185 for (int i = 0; i < listClients.size(); i++)
186 {
187 if (listClients[i]->getAddress() == sender && listClients[i]->getPort() == senderPort)
188 flag = true;
189 }
190
191 if (flag == false)
192 listClients << new Client(sender, senderPort);
193 }
194
195 if (updSocketOutput_ && updSocketOutput_->hasConnection())
196 updSocketOutput_->send(QString(datagram.data()));
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.