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

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

Socket:

  • Despecialisation de PacpusUDPSocket (anciennement spécialisée Airplug)
  • Ajout d'une classe UDPSocket

CanDriver:

  • Préparation au code du driver CAN Softing
  • Ajout des libs pour la compilation
File size: 6.4 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// Called by the ComponentManager to pass the XML parameters to the
60// component
61//////////////////////////////////////////////////////////////////////////
62ComponentBase::COMPONENT_CONFIGURATION PacpusUDPSocket::configureComponent(XmlComponentConfig config)
63{
64
65 socketType_ = config.getProperty("typeSocket");
66
67 if (socketType_ == "client" || socketType_ == "Client")
68 {
69 address2send_.setAddress(config.getProperty("address"));
70 port2send_ = config.getProperty("port").toUInt();
71 socketType_ = "client";
72 }
73 else if (socketType_ == "server" || socketType_ == "Server")
74 {
75 port2bind_ = config.getProperty("port").toUInt();
76 socketType_ = "server";
77 }
78 else
79 {
80 qDebug("typeSocket incorrect, become client");
81 address2send_.setAddress(config.getProperty("address"));
82 port2send_ = config.getProperty("port").toUInt();
83 socketType_ = "client";
84 }
85
86 return ComponentBase::CONFIGURED_OK;
87}
88
89
90//////////////////////////////////////////////////////////////////////////
91// Called by the ComponentManager to start the component
92//////////////////////////////////////////////////////////////////////////
93void PacpusUDPSocket::startActivity()
94{
95 if (!(udpSocket_ = new QUdpSocket()))
96 qFatal("Failed to init UDP socket!");
97
98 if (socketType_ == "server")
99 {
100 if (udpSocket_->bind(QHostAddress::Any, port2bind_, QUdpSocket::DontShareAddress))
101 qDebug() << "Socket bound for server on port " << port2bind_;
102 else
103 qWarning() << "Failed to bind socket for server on port" << port2bind_;
104 }
105
106 connect(udpSocket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
107
108 updSocketOutput_ = getTypedOutput<QString, PacpusUDPSocket>("udpSocketOutput");
109}
110
111
112//////////////////////////////////////////////////////////////////////////
113// Send datagram QString
114//////////////////////////////////////////////////////////////////////////
115void PacpusUDPSocket::sendDatagrams(QString frame)
116{
117 int sent=0;
118
119 if (socketType_ == "client")
120 {
121 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1)
122 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
123 }
124 else if (socketType_ == "server")
125 {
126 for (int i = 0; i < listClients.size(); i++)
127 {
128 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
129 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
130 }
131 }
132}
133
134
135//////////////////////////////////////////////////////////////////////////
136// Send datagram QByteArray
137//////////////////////////////////////////////////////////////////////////
138void PacpusUDPSocket::sendDatagrams(QByteArray frame)
139{
140 int sent=0;
141
142 if (socketType_ == "client")
143 {
144 if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1)
145 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
146 }
147 else if (socketType_ == "server")
148 {
149 for (int i = 0; i < listClients.size(); i++)
150 {
151 if ((sent = udpSocket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
152 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
153 }
154 }
155}
156
157
158//////////////////////////////////////////////////////////////////////////
159// Called when the socket receive a new datagram
160//////////////////////////////////////////////////////////////////////////
161void PacpusUDPSocket::readPendingDatagrams()
162{
163 while (udpSocket_->hasPendingDatagrams())
164 {
165 QByteArray datagram;
166 datagram.resize(udpSocket_->pendingDatagramSize());
167 QHostAddress sender;
168 quint16 senderPort;
169
170 if(udpSocket_->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort) != -1)
171 {
172 if (socketType_ == "server")
173 {
174 bool flag = false;
175
176 for (int i = 0; i < listClients.size(); i++)
177 {
178 if (listClients[i]->getAddress() == sender && listClients[i]->getPort() == senderPort)
179 flag = true;
180 }
181
182 if (flag == false)
183 listClients << new Client(sender, senderPort);
184 }
185
186 if (updSocketOutput_ && updSocketOutput_->hasConnection())
187 updSocketOutput_->send(QString(datagram.data()));
188 }
189 else
190 {
191 printf("Error when reading network frame\n");
192 }
193 }
194}
195
196
197//////////////////////////////////////////////////////////////////////////
198// Called by the ComponentManager to stop the component
199//////////////////////////////////////////////////////////////////////////
200void PacpusUDPSocket::stopActivity()
201{
202 udpSocket_->close();
203 delete udpSocket_;
204}
Note: See TracBrowser for help on using the repository browser.