Added acknowledge to CameraTcpServer (a client must wait a response before sending images)

git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@378 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
matlabbe 2014-08-06 03:00:17 +00:00
parent 072f6cc8f5
commit da4ee8637b
4 changed files with 55 additions and 45 deletions

View File

@ -17,14 +17,11 @@ CameraTcpServer::CameraTcpServer(quint16 port, QObject *parent) :
QTcpServer(parent), QTcpServer(parent),
blockSize_(0) blockSize_(0)
{ {
this->setMaxPendingConnections(1);
if (!this->listen(QHostAddress::Any, port)) if (!this->listen(QHostAddress::Any, port))
{ {
UERROR("Unable to start the Camera TCP server: %s", this->errorString().toStdString().c_str()); UERROR("Unable to start the Camera TCP server: %s", this->errorString().toStdString().c_str());
return; return;
} }
connect(this, SIGNAL(newConnection()), this, SLOT(addClient()));
} }
cv::Mat CameraTcpServer::getImage() cv::Mat CameraTcpServer::getImage()
@ -83,22 +80,23 @@ quint16 CameraTcpServer::getPort() const
return this->serverPort(); return this->serverPort();
} }
void CameraTcpServer::addClient() void CameraTcpServer::incomingConnection(int socketDescriptor)
{ {
QTcpSocket * client = this->nextPendingConnection();
QList<QTcpSocket*> clients = this->findChildren<QTcpSocket*>(); QList<QTcpSocket*> clients = this->findChildren<QTcpSocket*>();
if(clients.size() > 1) if(clients.size() >= 1)
{ {
UWARN("A client is already connected. Only one connection allowed at the same time."); UWARN("A client is already connected. Only one connection allowed at the same time.");
client->close(); QTcpSocket socket;
client->deleteLater(); socket.setSocketDescriptor(socketDescriptor);
} }
else else
{ {
connect(client, SIGNAL(readyRead()), this, SLOT(readReceivedData())); QTcpSocket * socket = new QTcpSocket(this);
connect(client, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); connect(socket, SIGNAL(readyRead()), this, SLOT(readReceivedData()));
connect(client, SIGNAL(disconnected()), this, SLOT(connectionLost())); connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError)));
connect(socket, SIGNAL(disconnected()), this, SLOT(connectionLost()));
socket->setSocketDescriptor(socketDescriptor);
socket->write(QByteArray("1"));
} }
} }

View File

@ -23,14 +23,14 @@ public:
QHostAddress getHostAddress() const; QHostAddress getHostAddress() const;
quint16 getPort() const; quint16 getPort() const;
protected:
virtual void incomingConnection ( int socketDescriptor );
private Q_SLOTS: private Q_SLOTS:
void readReceivedData(); void readReceivedData();
void displayError(QAbstractSocket::SocketError socketError); void displayError(QAbstractSocket::SocketError socketError);
void connectionLost(); void connectionLost();
private Q_SLOTS:
void addClient();
private: private:
quint64 blockSize_; quint64 blockSize_;
QVector<cv::Mat> images_; QVector<cv::Mat> images_;

View File

@ -99,7 +99,7 @@ int main(int argc, char * argv[])
} }
server.connectToHost(ipAddress, port); server.connectToHost(ipAddress, port);
if(!server.waitForConnected()) if(!server.waitForReadyRead())
{ {
printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), port); printf("ERROR: Unable to connect to %s:%d\n", ipAddress.toStdString().c_str(), port);
return -1; return -1;

View File

@ -148,6 +148,9 @@ int main(int argc, char * argv[])
QObject::connect(&response, SIGNAL(detectionReceived()), &app, SLOT(quit())); QObject::connect(&response, SIGNAL(detectionReceived()), &app, SLOT(quit()));
QObject::connect(&response, SIGNAL(disconnected()), &app, SLOT(quit())); QObject::connect(&response, SIGNAL(disconnected()), &app, SLOT(quit()));
QObject::connect(&request, SIGNAL(disconnected()), &app, SLOT(quit()));
QObject::connect(&response, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
QObject::connect(&request, SIGNAL(error(QAbstractSocket::SocketError)), &app, SLOT(quit()));
request.connectToHost(ipAddress, portOut); request.connectToHost(ipAddress, portOut);
response.connectToHost(ipAddress, portIn); response.connectToHost(ipAddress, portIn);
@ -164,7 +167,6 @@ int main(int argc, char * argv[])
return -1; return -1;
} }
// publish image // publish image
std::vector<unsigned char> buf; std::vector<unsigned char> buf;
cv::imencode(".png", image, buf); cv::imencode(".png", image, buf);
@ -176,48 +178,58 @@ int main(int argc, char * argv[])
out.writeRawData((char*)buf.data(), (int)buf.size()); out.writeRawData((char*)buf.data(), (int)buf.size());
out.device()->seek(0); out.device()->seek(0);
out << (quint64)(block.size() - sizeof(quint64)); out << (quint64)(block.size() - sizeof(quint64));
request.write(block);
printf("Image published, waiting for response...\n");
QTime time;
time.start();
// wait for response if(request.waitForReadyRead())
app.exec();
if(response.dataReceived())
{ {
printf("Response received! (%d ms)\n", time.elapsed()); qint64 bytes = request.write(block);
// print detected objects printf("Image published (%d bytes), waiting for response...\n", (int)bytes);
if(response.info().objDetected_.size()) QTime time;
time.start();
// wait for response
app.exec();
if(response.dataReceived())
{ {
QList<int> ids = response.info().objDetected_.uniqueKeys(); printf("Response received! (%d ms)\n", time.elapsed());
for(int i=0; i<ids.size(); ++i) // print detected objects
if(response.info().objDetected_.size())
{ {
int count = response.info().objDetected_.count(ids[i]); QList<int> ids = response.info().objDetected_.uniqueKeys();
if(count == 1) for(int i=0; i<ids.size(); ++i)
{ {
printf("Object %d detected.\n", ids[i]); int count = response.info().objDetected_.count(ids[i]);
} if(count == 1)
else {
{ printf("Object %d detected.\n", ids[i]);
printf("Object %d detected %d times.\n", ids[i], count); }
else
{
printf("Object %d detected %d times.\n", ids[i], count);
}
} }
} }
else
{
printf("No objects detected.\n");
}
// write json
if(!jsonPath.isEmpty() && JsonWriter::available())
{
JsonWriter::write(response.info(), jsonPath);
printf("JSON written to \"%s\"\n", jsonPath.toStdString().c_str());
}
} }
else else
{ {
printf("No objects detected.\n"); printf("Failed to receive a response...\n");
} return -1;
// write json
if(!jsonPath.isEmpty() && JsonWriter::available())
{
JsonWriter::write(response.info(), jsonPath);
printf("JSON written to \"%s\"\n", jsonPath.toStdString().c_str());
} }
} }
else else
{ {
printf("Failed to receive a response...\n"); printf("Server is busy...\n");
return -1;
} }
return 0; return 0;