TcpServerPool.h
Go to the documentation of this file.
1 /*
2  * TcpServerPool.h
3  *
4  * Created on: Nov 29, 2015
5  * Author: mathieu
6  */
7 
8 #ifndef TCPSERVERPOOL_H_
9 #define TCPSERVERPOOL_H_
10 
11 #include <find_object/FindObject.h>
12 #include <find_object/TcpServer.h>
14 #include <QtCore/QThread>
15 #include <QtCore/QSemaphore>
16 
17 class FindObjectWorker : public QObject
18 {
19  Q_OBJECT;
20 
21 public:
23  find_object::FindObject * sharedFindObject,
24  QSemaphore * sharedSemaphore,
25  int maxSemaphoreResources,
26  QObject * parent = 0) :
27  QObject(parent),
28  sharedFindObject_(sharedFindObject),
29  sharedSemaphore_(sharedSemaphore),
30  maxSemaphoreResources_(maxSemaphoreResources)
31  {
32  UASSERT(sharedFindObject != 0);
33  UASSERT(sharedSemaphore != 0);
34  UASSERT(maxSemaphoreResources > 0);
35  }
36 
37 public Q_SLOTS:
38  void detect(const cv::Mat & image)
39  {
40  sharedSemaphore_->acquire(1);
41  UINFO("Thread %p detecting...", (void *)this->thread());
43  sharedFindObject_->detect(image, info);
44  Q_EMIT objectsFound(info);
45  sharedSemaphore_->release(1);
46  }
47 
48  void addObjectAndUpdate(const cv::Mat & image, int id, const QString & filePath)
49  {
50  //block everyone!
52  UINFO("Thread %p adding object %d (%s)...", (void *)this->thread(), id, filePath.toStdString().c_str());
53  sharedFindObject_->addObjectAndUpdate(image, id, filePath);
55  }
56  void removeObjectAndUpdate(int id)
57  {
58  //block everyone!
60  UINFO("Thread %p removing object %d...", (void *)this->thread(), id);
63  }
64 
65 Q_SIGNALS:
67 
68 private:
70  QSemaphore * sharedSemaphore_;
72 };
73 
74 class TcpServerPool : public QObject
75 {
76  Q_OBJECT;
77 public:
78  TcpServerPool(find_object::FindObject * sharedFindObject, int threads, int port) :
79  sharedSemaphore_(threads)
80  {
81  UASSERT(sharedFindObject != 0);
82  UASSERT(threads>=1);
83 
84  qRegisterMetaType<cv::Mat>("cv::Mat");
85 
86  threadPool_.resize(threads);
87  for(int i=0; i<threads; ++i)
88  {
89  find_object::TcpServer * tcpServer = new find_object::TcpServer(port!=0?port++:0);
90  UINFO("TcpServer set on port: %d (IP=%s)",
91  tcpServer->getPort(),
92  tcpServer->getHostAddress().toString().toStdString().c_str());
93 
94  threadPool_[i] = new QThread(this);
95  FindObjectWorker * worker = new FindObjectWorker(sharedFindObject, &sharedSemaphore_, threads);
96 
97  tcpServer->moveToThread(threadPool_[i]);
98  worker->moveToThread(threadPool_[i]);
99  connect(threadPool_[i], SIGNAL(finished()), tcpServer, SLOT(deleteLater()));
100  connect(threadPool_[i], SIGNAL(finished()), worker, SLOT(deleteLater()));
101 
102  // connect stuff:
103  QObject::connect(worker, SIGNAL(objectsFound(find_object::DetectionInfo)), tcpServer, SLOT(publishDetectionInfo(find_object::DetectionInfo)));
104  QObject::connect(tcpServer, SIGNAL(detectObject(const cv::Mat &)), worker, SLOT(detect(const cv::Mat &)));
105  QObject::connect(tcpServer, SIGNAL(addObject(const cv::Mat &, int, const QString &)), worker, SLOT(addObjectAndUpdate(const cv::Mat &, int, const QString &)));
106  QObject::connect(tcpServer, SIGNAL(removeObject(int)), worker, SLOT(removeObjectAndUpdate(int)));
107  threadPool_[i]->start();
108  }
109  }
110 
111  virtual ~TcpServerPool()
112  {
113  for(int i=0; i<threadPool_.size(); ++i)
114  {
115  threadPool_[i]->quit();
116  threadPool_[i]->wait();
117  }
118  }
119 
120 private:
121  QVector<QThread*> threadPool_;
122  QSemaphore sharedSemaphore_;
123 };
124 
125 
126 
127 #endif /* TCPSERVERPOOL_H_ */
QSemaphore sharedSemaphore_
QHostAddress getHostAddress() const
Definition: TcpServer.cpp:49
QSemaphore * sharedSemaphore_
Definition: TcpServerPool.h:70
bool detect(const cv::Mat &image, find_object::DetectionInfo &info) const
virtual ~TcpServerPool()
void addObjectAndUpdate(const cv::Mat &image, int id=0, const QString &filePath=QString())
Definition: FindObject.cpp:383
#define UASSERT(condition)
void detect(const cv::Mat &image)
Definition: TcpServerPool.h:38
void addObjectAndUpdate(const cv::Mat &image, int id, const QString &filePath)
Definition: TcpServerPool.h:48
TcpServerPool(find_object::FindObject *sharedFindObject, int threads, int port)
Definition: TcpServerPool.h:78
quint16 getPort() const
Definition: TcpServer.cpp:73
QVector< QThread * > threadPool_
ULogger class and convenient macros.
void objectsFound(const find_object::DetectionInfo &)
void removeObjectAndUpdate(int id)
Definition: TcpServerPool.h:56
FindObjectWorker(find_object::FindObject *sharedFindObject, QSemaphore *sharedSemaphore, int maxSemaphoreResources, QObject *parent=0)
Definition: TcpServerPool.h:22
find_object::FindObject * sharedFindObject_
Definition: TcpServerPool.h:69
void removeObjectAndUpdate(int id)
Definition: FindObject.cpp:395
#define UINFO(...)


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Dec 12 2022 03:20:09