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(port!=0);
83  UASSERT(threads>=1);
84 
85  qRegisterMetaType<cv::Mat>("cv::Mat");
86 
87  threadPool_.resize(threads);
88  for(int i=0; i<threads; ++i)
89  {
90  find_object::TcpServer * tcpServer = new find_object::TcpServer(port++);
91  UINFO("TcpServer set on port: %d (IP=%s)",
92  tcpServer->getPort(),
93  tcpServer->getHostAddress().toString().toStdString().c_str());
94 
95  threadPool_[i] = new QThread(this);
96  FindObjectWorker * worker = new FindObjectWorker(sharedFindObject, &sharedSemaphore_, threads);
97 
98  tcpServer->moveToThread(threadPool_[i]);
99  worker->moveToThread(threadPool_[i]);
100  connect(threadPool_[i], SIGNAL(finished()), tcpServer, SLOT(deleteLater()));
101  connect(threadPool_[i], SIGNAL(finished()), worker, SLOT(deleteLater()));
102 
103  // connect stuff:
104  QObject::connect(worker, SIGNAL(objectsFound(find_object::DetectionInfo)), tcpServer, SLOT(publishDetectionInfo(find_object::DetectionInfo)));
105  QObject::connect(tcpServer, SIGNAL(detectObject(const cv::Mat &)), worker, SLOT(detect(const cv::Mat &)));
106  QObject::connect(tcpServer, SIGNAL(addObject(const cv::Mat &, int, const QString &)), worker, SLOT(addObjectAndUpdate(const cv::Mat &, int, const QString &)));
107  QObject::connect(tcpServer, SIGNAL(removeObject(int)), worker, SLOT(removeObjectAndUpdate(int)));
108  threadPool_[i]->start();
109  }
110  }
111 
112  virtual ~TcpServerPool()
113  {
114  for(int i=0; i<threadPool_.size(); ++i)
115  {
116  threadPool_[i]->quit();
117  threadPool_[i]->wait();
118  }
119  }
120 
121 private:
122  QVector<QThread*> threadPool_;
123  QSemaphore sharedSemaphore_;
124 };
125 
126 
127 
128 #endif /* TCPSERVERPOOL_H_ */
quint16 getPort() const
Definition: TcpServer.cpp:73
QSemaphore sharedSemaphore_
QSemaphore * sharedSemaphore_
Definition: TcpServerPool.h:70
virtual ~TcpServerPool()
void addObjectAndUpdate(const cv::Mat &image, int id=0, const QString &filePath=QString())
Definition: FindObject.cpp:327
QHostAddress getHostAddress() const
Definition: TcpServer.cpp:49
#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
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:339
bool detect(const cv::Mat &image, find_object::DetectionInfo &info) const
#define UINFO(...)


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