[85] | 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 |
|
---|
| 20 | using namespace pacpus;
|
---|
| 21 |
|
---|
| 22 |
|
---|
| 23 | DECLARE_STATIC_LOGGER("pacpus.base.PacpusUDPSocket");
|
---|
| 24 |
|
---|
| 25 |
|
---|
| 26 | //////////////////////////////////////////////////////////////////////////
|
---|
| 27 | // Construct the factory
|
---|
| 28 | //////////////////////////////////////////////////////////////////////////
|
---|
| 29 | static ComponentFactory <PacpusUDPSocket> sFactory("PacpusUDPSocket");
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | //////////////////////////////////////////////////////////////////////////
|
---|
| 33 | // Constructeur
|
---|
| 34 | //////////////////////////////////////////////////////////////////////////
|
---|
| 35 | PacpusUDPSocket::PacpusUDPSocket(QString name)
|
---|
| 36 | : ComponentBase(name)
|
---|
| 37 | {
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | //////////////////////////////////////////////////////////////////////////
|
---|
| 42 | // Destructor
|
---|
| 43 | //////////////////////////////////////////////////////////////////////////
|
---|
| 44 | PacpusUDPSocket::~PacpusUDPSocket()
|
---|
| 45 | {
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 |
|
---|
| 49 | //////////////////////////////////////////////////////////////////////////
|
---|
| 50 | // Called by the ComponentManager to pass the XML parameters to the
|
---|
| 51 | // component
|
---|
| 52 | //////////////////////////////////////////////////////////////////////////
|
---|
| 53 | ComponentBase::COMPONENT_CONFIGURATION PacpusUDPSocket::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 PacpusUDPSocket::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 |
|
---|
| 97 | connect(udpSocket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()));
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | void PacpusUDPSocket::sendDatagrams(QString frame)
|
---|
| 102 | {
|
---|
| 103 | int sent=0;
|
---|
| 104 |
|
---|
| 105 | if (socketType_ == "client")
|
---|
| 106 | {
|
---|
| 107 | if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1)
|
---|
| 108 | qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
|
---|
| 109 | }
|
---|
| 110 | else if (socketType_ == "server")
|
---|
| 111 | {
|
---|
| 112 | for (int i = 0; i < listClients.size(); i++)
|
---|
| 113 | {
|
---|
| 114 | if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
|
---|
| 115 | qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
| 121 | void PacpusUDPSocket::sendDatagrams(QByteArray frame)
|
---|
| 122 | {
|
---|
| 123 | int sent=0;
|
---|
| 124 |
|
---|
| 125 | if (socketType_ == "client")
|
---|
| 126 | {
|
---|
| 127 | if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1)
|
---|
| 128 | qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl;
|
---|
| 129 | }
|
---|
| 130 | else if (socketType_ == "server")
|
---|
| 131 | {
|
---|
| 132 | for (int i = 0; i < listClients.size(); i++)
|
---|
| 133 | {
|
---|
| 134 | if ((sent = udpSocket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1)
|
---|
| 135 | qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl;
|
---|
| 136 | }
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 |
|
---|
| 141 | void PacpusUDPSocket::readPendingDatagrams()
|
---|
| 142 | {
|
---|
| 143 | while (udpSocket_->hasPendingDatagrams())
|
---|
| 144 | {
|
---|
| 145 | QByteArray datagram;
|
---|
| 146 | datagram.resize(udpSocket_->pendingDatagramSize());
|
---|
| 147 | QHostAddress sender;
|
---|
| 148 | quint16 senderPort;
|
---|
| 149 |
|
---|
| 150 | if(udpSocket_->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort) != -1)
|
---|
| 151 | {
|
---|
| 152 | if (socketType_ == "server")
|
---|
| 153 | {
|
---|
| 154 | bool flag = false;
|
---|
| 155 |
|
---|
| 156 | for (int i = 0; i < listClients.size(); i++)
|
---|
| 157 | {
|
---|
| 158 | if (listClients[i]->getAddress() == sender && listClients[i]->getPort() == senderPort)
|
---|
| 159 | flag = true;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | if (flag == false)
|
---|
| 163 | listClients << new Client(sender, senderPort);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | // Output
|
---|
| 167 | }
|
---|
| 168 | else
|
---|
| 169 | {
|
---|
| 170 | printf("Error when reading network frame\n");
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 |
|
---|
| 176 | //////////////////////////////////////////////////////////////////////////
|
---|
| 177 | // Called by the ComponentManager to stop the component
|
---|
| 178 | //////////////////////////////////////////////////////////////////////////
|
---|
| 179 | void PacpusUDPSocket::stopActivity()
|
---|
| 180 | {
|
---|
| 181 | udpSocket_->close();
|
---|
| 182 | delete udpSocket_;
|
---|
| 183 | }
|
---|