CameraTcpServer.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #include "find_object/Settings.h"
29 
30 #include "CameraTcpServer.h"
31 #include <QtNetwork/QTcpSocket>
32 #include <QtNetwork/QNetworkInterface>
33 #include <QtCore/QDataStream>
34 
35 namespace find_object {
36 
37 CameraTcpServer::CameraTcpServer(quint16 port, QObject *parent) :
38  QTcpServer(parent),
39  blockSize_(0)
40 {
41  if (!this->listen(QHostAddress::Any, port))
42  {
43  UERROR("Unable to start the Camera TCP server: %s", this->errorString().toStdString().c_str());
44  return;
45  }
46 }
47 
49 {
50  cv::Mat img;
51  if(images_.size())
52  {
53  // if queue changed after tcp connection ended with images still in the buffer
54  int queue = Settings::getCamera_9queueSize();
55  while(queue > 0 && images_.size() > queue)
56  {
57  images_.pop_front();
58  }
59 
60  img = images_.front();
61  images_.pop_front();
62  }
63  if(this->findChildren<QTcpSocket*>().size() == 1)
64  {
65  this->findChildren<QTcpSocket*>().first()->waitForReadyRead(100);
66  }
67  return img;
68 }
69 
71 {
72  return this->findChildren<QTcpSocket*>().size() > 0;
73 }
74 
75 QHostAddress CameraTcpServer::getHostAddress() const
76 {
77  QHostAddress hostAddress;
78 
79  QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
80  // use the first non-localhost IPv4 address
81  for (int i = 0; i < ipAddressesList.size(); ++i)
82  {
83  if (ipAddressesList.at(i) != QHostAddress::LocalHost && ipAddressesList.at(i).toIPv4Address())
84  {
85  hostAddress = ipAddressesList.at(i).toString();
86  break;
87  }
88  }
89 
90  // if we did not find one, use IPv4 localhost
91  if (hostAddress.isNull())
92  {
93  hostAddress = QHostAddress(QHostAddress::LocalHost);
94  }
95 
96  return hostAddress;
97 }
98 
99 quint16 CameraTcpServer::getPort() const
100 {
101  return this->serverPort();
102 }
103 
104 void CameraTcpServer::incomingConnection(int socketDescriptor)
105 {
106  QList<QTcpSocket*> clients = this->findChildren<QTcpSocket*>();
107  if(clients.size() >= 1)
108  {
109  UWARN("A client is already connected. Only one connection allowed at the same time.");
110  QTcpSocket socket;
111  socket.setSocketDescriptor(socketDescriptor);
112  socket.close(); // close without sending an acknowledge
113  }
114  else
115  {
116  QTcpSocket * socket = new QTcpSocket(this);
117  connect(socket, SIGNAL(readyRead()), this, SLOT(readReceivedData()));
118  connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
119  connect(socket, SIGNAL(disconnected()), this, SLOT(connectionLost()));
120  socket->setSocketDescriptor(socketDescriptor);
121  socket->write(QByteArray("1")); // send acknowledge
122  }
123 }
124 
126 {
127  QTcpSocket * client = (QTcpSocket*)sender();
128  QDataStream in(client);
129  in.setVersion(QDataStream::Qt_4_0);
130 
131  if (blockSize_ == 0)
132  {
133  if (client->bytesAvailable() < (int)sizeof(quint64))
134  {
135  return;
136  }
137 
138  in >> blockSize_;
139  }
140 
141  if (client->bytesAvailable() < (int)blockSize_)
142  {
143  return;
144  }
145 
146  std::vector<unsigned char> buf(blockSize_);
147  in.readRawData((char*)buf.data(), blockSize_);
148  images_.push_back(cv::imdecode(buf, cv::IMREAD_UNCHANGED));
149  int queue = Settings::getCamera_9queueSize();
150  while(queue > 0 && images_.size() > queue)
151  {
152  images_.pop_front();
153  }
154  blockSize_ = 0;
155 }
156 
157 void CameraTcpServer::displayError(QAbstractSocket::SocketError socketError)
158 {
159  switch (socketError)
160  {
161  case QAbstractSocket::RemoteHostClosedError:
162  break;
163  case QAbstractSocket::HostNotFoundError:
164  UWARN("CameraTcp: Tcp error: The host was not found. Please "
165  "check the host name and port settings.\n");
166  break;
167  case QAbstractSocket::ConnectionRefusedError:
168  UWARN("CameraTcp: The connection was refused by the peer. "
169  "Make sure your images server is running, "
170  "and check that the host name and port "
171  "settings are correct.");
172  break;
173  default:
174  //UERROR("The following error occurred: %s.", this->errorString().toStdString().c_str());
175  break;
176  }
177 }
178 
180 {
181  //printf("[WARNING] CameraTcp: Connection lost!\n");
182  ((QTcpSocket*)sender())->close();
183  sender()->deleteLater();
184  blockSize_ = 0; // reset
185 }
186 
187 } // namespace find_object
QVector< cv::Mat > images_
virtual void incomingConnection(int socketDescriptor)
QHostAddress getHostAddress() const
void displayError(QAbstractSocket::SocketError socketError)
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
CameraTcpServer(quint16 port=0, QObject *parent=0)
#define UERROR(...)
ULogger class and convenient macros.
#define UWARN(...)


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Jun 10 2019 13:21:31