TcpResponse.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 
28 #include "TcpResponse.h"
29 
30 #include <opencv2/opencv.hpp>
31 #include <QtGui/QTransform>
32 #include <QtCore/QPointF>
33 #include <QtCore/QTime>
34 
35 TcpResponse::TcpResponse(QObject *parent) :
36  QTcpSocket(parent),
37  blockSize_(0),
38  dataReceived_(false)
39 {
40  connect(this, SIGNAL(readyRead()), this, SLOT(readReceivedData()));
41  connect(this, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
42  connect(this, SIGNAL(disconnected()), this, SLOT(connectionLost()));
43 }
44 
46 {
47  QDataStream in(this);
48  in.setVersion(QDataStream::Qt_4_0);
49 
50  if (blockSize_ == 0)
51  {
52  if (this->bytesAvailable() < (int)sizeof(quint16))
53  {
54  return;
55  }
56 
57  in >> blockSize_;
58  }
59 
60  if (this->bytesAvailable() < blockSize_)
61  {
62  return;
63  }
64 
65  blockSize_ = 0;
66 
67  in >> info_;
68 
69  dataReceived_ = true;
70  Q_EMIT detectionReceived();
71 }
72 
73 void TcpResponse::displayError(QAbstractSocket::SocketError socketError)
74 {
75  switch (socketError)
76  {
77  case QAbstractSocket::RemoteHostClosedError:
78  break;
79  case QAbstractSocket::HostNotFoundError:
80  printf("Tcp error: The host was not found. Please "
81  "check the host name and port settings.\n");
82  break;
83  case QAbstractSocket::ConnectionRefusedError:
84  printf("The connection was refused by the peer. "
85  "Make sure Find-Object is running, "
86  "and check that the host name and port "
87  "settings are correct.\n");
88  break;
89  default:
90  printf("The following error occurred: %s.\n", this->errorString().toStdString().c_str());
91  break;
92  }
93 }
94 
96 {
97  printf("Connection lost!\n");
98 }
99 
TcpResponse(QObject *parent=0)
Definition: TcpResponse.cpp:35
quint16 blockSize_
Definition: TcpResponse.h:55
find_object::DetectionInfo info_
Definition: TcpResponse.h:56
void connectionLost()
Definition: TcpResponse.cpp:95
void displayError(QAbstractSocket::SocketError socketError)
Definition: TcpResponse.cpp:73
void readReceivedData()
Definition: TcpResponse.cpp:45
static bool in(Reader::Char c, Reader::Char c1, Reader::Char c2, Reader::Char c3, Reader::Char c4)
Definition: jsoncpp.cpp:244
void detectionReceived()
bool dataReceived_
Definition: TcpResponse.h:57


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26