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