source: pacpussensors/trunk/PacpusSocket/PacpusSocket.cpp@ 24

Last change on this file since 24 was 21, checked in by phudelai, 11 years ago

New component for the wifibot and the dualshock 3

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