Changeset 99 in pacpussensors for trunk/PacpusSocket/PacpusUDPSocket.cpp
- Timestamp:
- Oct 15, 2015, 2:47:01 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/PacpusSocket/PacpusUDPSocket.cpp
r87 r99 5 5 // author: Pierre Hudelaine 6 6 // Copyright Heudiasyc UMR UTC/CNRS 7253 7 // 7 // 8 8 // version: $Id: $ 9 9 // … … 12 12 *********************************************************************/ 13 13 14 #include "PacpusUDPSocket.h" 14 #include "PacpusUDPSocket.h" 15 15 16 16 #include <qbuffer.h> … … 27 27 // Construct the factory 28 28 ////////////////////////////////////////////////////////////////////////// 29 static ComponentFactory <PacpusUDPSocket> sFactory("PacpusUDPSocket"); 29 static ComponentFactory <PacpusUDPSocket> sFactory("PacpusUDPSocket"); 30 30 31 31 … … 34 34 ////////////////////////////////////////////////////////////////////////// 35 35 PacpusUDPSocket::PacpusUDPSocket(QString name) 36 : ComponentBase(name) 37 { 38 } 36 : ComponentBase(name) 37 { 38 } 39 39 40 40 … … 42 42 // Destructor 43 43 ////////////////////////////////////////////////////////////////////////// 44 PacpusUDPSocket::~PacpusUDPSocket() 44 PacpusUDPSocket::~PacpusUDPSocket() 45 45 { 46 46 } … … 52 52 void PacpusUDPSocket::addOutputs() 53 53 { 54 addOutput<QString, PacpusUDPSocket>(" udpSocketOutput");54 addOutput<QString, PacpusUDPSocket>("string"); 55 55 } 56 56 … … 61 61 void PacpusUDPSocket::addInputs() 62 62 { 63 addInput<QString, PacpusUDPSocket>("udpSocketInput", &PacpusUDPSocket::sendDatagrams);64 } 65 66 67 ////////////////////////////////////////////////////////////////////////// 68 // Called by the ComponentManager to pass the XML parameters to the 69 // component 70 ////////////////////////////////////////////////////////////////////////// 71 ComponentBase::COMPONENT_CONFIGURATION PacpusUDPSocket::configureComponent(XmlComponentConfig config) 72 { 63 addInput<QString, PacpusUDPSocket>("string", &PacpusUDPSocket::sendQString); 64 } 65 66 67 ////////////////////////////////////////////////////////////////////////// 68 // Called by the ComponentManager to pass the XML parameters to the 69 // component 70 ////////////////////////////////////////////////////////////////////////// 71 ComponentBase::COMPONENT_CONFIGURATION PacpusUDPSocket::configureComponent(XmlComponentConfig config) 72 { 73 73 74 74 socketType_ = config.getProperty("typeSocket"); 75 75 76 76 if (socketType_ == "client" || socketType_ == "Client") 77 77 { 78 78 address2send_.setAddress(config.getProperty("address")); 79 79 port2send_ = config.getProperty("port").toUInt(); 80 socketType_ = "client"; 80 socketType_ = "client"; 81 81 } 82 82 else if (socketType_ == "server" || socketType_ == "Server") … … 92 92 socketType_ = "client"; 93 93 } 94 95 return ComponentBase::CONFIGURED_OK; 96 } 94 95 return ComponentBase::CONFIGURED_OK; 96 } 97 97 98 98 … … 100 100 // Called by the ComponentManager to start the component 101 101 ////////////////////////////////////////////////////////////////////////// 102 void PacpusUDPSocket::startActivity() 103 { 102 void PacpusUDPSocket::startActivity() 103 { 104 104 if (!(udpSocket_ = new QUdpSocket())) 105 105 qFatal("Failed to init UDP socket!"); 106 106 107 107 if (socketType_ == "server") 108 108 { … … 112 112 qWarning() << "Failed to bind socket for server on port" << port2bind_; 113 113 } 114 114 115 115 connect(udpSocket_, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams())); 116 117 u pdSocketOutput_ = getTypedOutput<QString, PacpusUDPSocket>("udpSocketOutput");116 117 udpSocketOutput_ = getTypedOutput<QString, PacpusUDPSocket>("string"); 118 118 } 119 119 … … 122 122 // Send datagram QString 123 123 ////////////////////////////////////////////////////////////////////////// 124 void PacpusUDPSocket::send Datagrams(QString frame)125 { 124 void PacpusUDPSocket::sendQString(const QString& frame) 125 { 126 126 int sent=0; 127 127 128 128 if (socketType_ == "client") 129 129 { 130 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1) 131 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl; 130 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), address2send_, port2send_)) == -1) 131 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl; 132 132 } 133 133 else if (socketType_ == "server") … … 135 135 for (int i = 0; i < listClients.size(); i++) 136 136 { 137 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1) 138 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl; 137 if ((sent = udpSocket_->writeDatagram(frame.toLocal8Bit(), listClients[i]->getAddress(), listClients[i]->getPort())) == -1) 138 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl; 139 139 } 140 140 } 141 } 141 } 142 142 143 143 … … 145 145 // Send datagram QByteArray 146 146 ////////////////////////////////////////////////////////////////////////// 147 void PacpusUDPSocket::sendDatagrams(QByteArray frame) 148 { 147 void PacpusUDPSocket::sendDatagrams(QByteArray frame) 148 { 149 149 int sent=0; 150 150 151 151 if (socketType_ == "client") 152 152 { 153 if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1) 154 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl; 153 if ((sent = udpSocket_->writeDatagram(frame, address2send_, port2send_)) == -1) 154 qDebug() << "Failed to send the frame: " << address2send_ << port2send_ << frame << endl; 155 155 } 156 156 else if (socketType_ == "server") … … 158 158 for (int i = 0; i < listClients.size(); i++) 159 159 { 160 if ((sent = udpSocket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1) 161 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl; 160 if ((sent = udpSocket_->writeDatagram(frame, listClients[i]->getAddress(), listClients[i]->getPort())) == -1) 161 qDebug() << "Failed to send the frame: " << listClients[i]->getAddress() << listClients[i]->getPort() << frame << endl; 162 162 } 163 163 } 164 } 165 166 167 ////////////////////////////////////////////////////////////////////////// 168 // Called when the socket receive a new datagram 164 } 165 166 167 ////////////////////////////////////////////////////////////////////////// 168 // Called when the socket receive a new datagram 169 169 ////////////////////////////////////////////////////////////////////////// 170 170 void PacpusUDPSocket::readPendingDatagrams() 171 171 { 172 while (udpSocket_->hasPendingDatagrams()) 173 { 174 QByteArray datagram; 175 datagram.resize(udpSocket_->pendingDatagramSize()); 176 QHostAddress sender; 172 while (udpSocket_->hasPendingDatagrams()) 173 { 174 QByteArray datagram; 175 datagram.resize(udpSocket_->pendingDatagramSize()); 176 QHostAddress sender; 177 177 quint16 senderPort; 178 178 179 179 if(udpSocket_->readDatagram(datagram.data(), datagram.size(), &sender, &senderPort) != -1) 180 { 180 { 181 181 if (socketType_ == "server") 182 182 { 183 183 bool flag = false; 184 185 for (int i = 0; i < listClients.size(); i++) 186 { 184 185 for (int i = 0; i < listClients.size(); i++) 186 { 187 187 if (listClients[i]->getAddress() == sender && listClients[i]->getPort() == senderPort) 188 188 flag = true; 189 189 } 190 190 191 191 if (flag == false) 192 192 listClients << new Client(sender, senderPort); 193 193 } 194 195 if (u pdSocketOutput_ && updSocketOutput_->hasConnection())196 u pdSocketOutput_->send(QString(datagram.data()));194 195 if (udpSocketOutput_ && udpSocketOutput_->hasConnection()) 196 udpSocketOutput_->send(QString(datagram.data())); 197 197 } 198 198 else
Note:
See TracChangeset
for help on using the changeset viewer.