[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 |
|
---|
| 20 | using namespace pacpus;
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | DECLARE_STATIC_LOGGER("pacpus.base.PacpusSocket");
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | //////////////////////////////////////////////////////////////////////////
|
---|
| 27 | // Construct the factory
|
---|
| 28 | //////////////////////////////////////////////////////////////////////////
|
---|
| 29 | static ComponentFactory <PacpusSocket> sFactory("PacpusSocket");
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | //////////////////////////////////////////////////////////////////////////
|
---|
| 33 | // C
|
---|
| 34 | //////////////////////////////////////////////////////////////////////////
|
---|
| 35 | PacpusSocket::PacpusSocket(QString name)
|
---|
| 36 | : ComponentBase(name)
|
---|
| 37 | {
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | //////////////////////////////////////////////////////////////////////////
|
---|
| 42 | // Destructor
|
---|
| 43 | //////////////////////////////////////////////////////////////////////////
|
---|
| 44 | PacpusSocket::~PacpusSocket()
|
---|
| 45 | {
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | //////////////////////////////////////////////////////////////////////////
|
---|
| 50 | // Called by the ComponentManager to pass the XML parameters to the
|
---|
| 51 | // component
|
---|
| 52 | //////////////////////////////////////////////////////////////////////////
|
---|
| 53 | ComponentBase::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 | //////////////////////////////////////////////////////////////////////////
|
---|
| 84 | void 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))
|
---|
[31] | 92 | qDebug() << "Socket bound for server on port " << port2bind_;
|
---|
[21] | 93 | else
|
---|
[36] | 94 | qWarning() << "Failed to bind socket on port" << port2bind_;
|
---|
[21] | 95 | }
|
---|
| 96 | else if (socketType_ == "client")
|
---|
| 97 | {
|
---|
| 98 | if (udpSocket_->bind(QHostAddress::Any, port2send_ + 1, QUdpSocket::DontShareAddress))
|
---|
[31] | 99 | qDebug() << "Socket bound for client on port " << port2send_ + 1;
|
---|
[21] | 100 | else
|
---|
[31] | 101 | qWarning() << "Failed to bind socket" << port2send_ + 1;
|
---|
[21] | 102 | }
|
---|
| 103 |
|
---|
| 104 | connect(udpSocket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
| 108 | void 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)
|
---|
[31] | 115 | qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
|
---|
| 116 | //else
|
---|
| 117 | //qDebug() << "TO NETWORK:" << address2send_ << port2send_ << frame << "size" << sent;
|
---|
[21] | 118 | }
|
---|
| 119 | else if (socketType_ == "server")
|
---|
| 120 | {
|
---|
| 121 | for (int i = 0; i < listClients.size(); i++)
|
---|
| 122 | {
|
---|
[34] | 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;
|
---|
[21] | 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 |
|
---|
| 132 | void PacpusSocket::sendDatagrams(QByteArray frame)
|
---|
| 133 | {
|
---|
| 134 | int sent=0;
|
---|
| 135 |
|
---|
| 136 | if (socketType_ == "client")
|
---|
| 137 | {
|
---|
| 138 | if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1)
|
---|
[31] | 139 | qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
|
---|
| 140 | //else
|
---|
| 141 | //qDebug() << "TO NETWORK:" << address2send_ << port2send_ << frame << "size" << sent;
|
---|
[21] | 142 | }
|
---|
| 143 | else if (socketType_ == "server")
|
---|
| 144 | {
|
---|
| 145 | for (int i = 0; i < listClients.size(); i++)
|
---|
| 146 | {
|
---|
[31] | 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;
|
---|
[21] | 151 | }
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 |
|
---|
| 156 | void 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)
|
---|
[34] | 176 | flag = true;
|
---|
[21] | 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 | }
|
---|
[34] | 191 | else if (datagram.size() == 4)
|
---|
| 192 | {
|
---|
[21] | 193 | // Manette Dualshock 3 pour le wifibot
|
---|
| 194 | emit newDatagram(datagram);
|
---|
[34] | 195 | }
|
---|
| 196 | else
|
---|
| 197 | {
|
---|
[21] | 198 | QString string(datagram.data());
|
---|
| 199 | emit newQString(string);
|
---|
| 200 | }
|
---|
| 201 | }
|
---|
| 202 | else
|
---|
| 203 | {
|
---|
| 204 | printf("Error when reading network frame\n");
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 | //////////////////////////////////////////////////////////////////////////
|
---|
| 211 | // Called by the ComponentManager to stop the component
|
---|
| 212 | //////////////////////////////////////////////////////////////////////////
|
---|
| 213 | void PacpusSocket::stopActivity()
|
---|
| 214 | {
|
---|
| 215 | udpSocket_->close();
|
---|
| 216 | delete udpSocket_;
|
---|
| 217 | }
|
---|