Changeset 291 in pacpusframework for trunk/examples/ProducerConsumerExample
- Timestamp:
- Mar 27, 2014, 12:53:43 PM (11 years ago)
- Location:
- trunk/examples/ProducerConsumerExample
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/examples/ProducerConsumerExample/ConsumerExample.cpp
r288 r291 17 17 LOG_TRACE("constructor(" << name << ")"); 18 18 19 LOG_INFO("Thread " << thread.currentThread());20 LOG_INFO("Current Thread " << QThread::currentThread());21 22 namespace po = boost::program_options;23 19 addParameters() 24 ("output-path", po::value<std::string>(&mOutputFileName)->default_value("consumer.txt"), "set output file path")20 ("output-path", value<std::string>(&mOutputFileName)->default_value("consumer.txt"), "set output file path") 25 21 ; 26 22 } … … 55 51 { 56 52 LOG_TRACE(Q_FUNC_INFO); 57 moveToThread(&thread);58 53 m_counter = 0; 59 54 … … 63 58 } 64 59 65 thread.start();66 60 setState(MONITOR_OK); 67 61 LOG_INFO("started component '" << getName() << "'"); … … 72 66 LOG_TRACE(Q_FUNC_INFO); 73 67 74 QMetaObject::invokeMethod(&thread, "quit");75 68 m_file.close(); 76 69 setState(STOPPED); … … 97 90 98 91 PacpusTypedEvent<int> intEvent(TYPED_EVENT, 2); 99 //PacpusTypedEvent<QImage> imageEvent = intEvent; 92 //PacpusTypedEvent<QImage> imageEvent = intEvent; // should not compile 100 93 } -
trunk/examples/ProducerConsumerExample/ConsumerExample.h
r288 r291 7 7 #include <Pacpus/kernel/ComponentBase.h> 8 8 #include <QImage> 9 #include <QThread>10 9 #include <string> 11 10 … … 33 32 34 33 QImage matrix; 35 QThread thread;36 34 37 35 int m_counter; -
trunk/examples/ProducerConsumerExample/ProducerExample.cpp
r288 r291 1 1 #include "ProducerExample.h" 2 2 3 #include <fstream>4 3 #include <Pacpus/kernel/ComponentFactory.h> 5 4 #include <Pacpus/kernel/ComponentManager.h> 6 5 #include <Pacpus/kernel/InputOutputInterface.h> 7 6 #include <Pacpus/kernel/Log.h> 7 8 #include <boost/bind.hpp> 9 #include <boost/thread.hpp> 8 10 9 11 using namespace pacpus; … … 18 20 ProducerExample::ProducerExample(QString name) 19 21 : ComponentBase(name) 22 , mInterval(boost::posix_time::milliseconds(150)) 23 , mTimer(mIo, mInterval) 20 24 { 21 LOG_TRACE("constructor(" << name << ")");25 PACPUS_LOG_FUNCTION(); 22 26 23 27 namespace po = boost::program_options; … … 30 34 ProducerExample::~ProducerExample() 31 35 { 32 LOG_TRACE("destructor");36 PACPUS_LOG_FUNCTION(); 33 37 } 34 38 35 ComponentBase::COMPONENT_CONFIGURATION 36 ProducerExample::configureComponent(XmlComponentConfig /*config*/) 39 ComponentBase::COMPONENT_CONFIGURATION ProducerExample::configureComponent(XmlComponentConfig /*config*/) 37 40 { 38 41 PACPUS_LOG_FUNCTION(); … … 54 57 void ProducerExample::startActivity() 55 58 { 56 LOG_TRACE(Q_FUNC_INFO);59 PACPUS_LOG_FUNCTION(); 57 60 58 start(); 61 using boost::bind; 62 using boost::ref; 63 64 std::fstream mFile(outputFileName, std::ios_base::out | std::ios_base::app); 65 if (!mFile.is_open()) { 66 LOG_ERROR("file '" << outputFileName << "' cannot be opened"); 67 return; 68 } 69 70 mImageOutput = getTypedOutput<QImage, ProducerExample>("image"); 71 59 72 setActive(true); 60 73 setState(MONITOR_OK); 61 74 LOG_INFO("started component '" << getName() << "'"); 75 76 int counter = 0; 77 mTimer.async_wait(bind(&ProducerExample::produce, this, ref(counter))); 78 boost::thread t(bind(&boost::asio::io_service::run, ref(mIo))); 79 //mIo.run(); 62 80 } 63 81 64 82 void ProducerExample::stopActivity() 65 83 { 66 LOG_TRACE(Q_FUNC_INFO);84 PACPUS_LOG_FUNCTION(); 67 85 68 86 setActive(false); 69 87 setState(STOPPED); 70 88 LOG_INFO("stopped component '" << getName() << "'"); 89 90 mFile.close(); 71 91 } 72 92 73 void ProducerExample:: run()93 void ProducerExample::produce(int& counter) 74 94 { 75 unsigned int counter = 1; 76 int waitTimeMicros = 150 * 1000; 95 if (!isActive()) { 96 return; 97 } 98 99 QImage mat(10, 10, QImage::Format_RGB32); 100 //mat.fill( qRgb(189, 149, 39)); 101 //mat.setPixel(0,0,i); 102 LOG_INFO("Sending QImage: " 103 << "size = " << mat.size().width()<< " x " << mat.size().height() 104 ); 77 105 78 std::fstream file(outputFileName, std::ios_base::out | std::ios_base::app); 79 if (!file.is_open()) { 80 LOG_ERROR("file '" << outputFileName << "' cannot be opened"); 81 } 82 83 QImage mat(10000, 1000, QImage::Format_RGB32); 84 //mat.fill( qRgb(189, 149, 39)); 85 86 OutputInterface<QImage, ProducerExample> * imageOutput = 87 getTypedOutput<QImage, ProducerExample>("image"); 88 89 while (isActive()) { 90 //mat.setPixel(0,0,i); 91 LOG_INFO("Sent QImage: " 92 << "size = " << mat.size().width()<< " x " << mat.size().height() 93 ); 94 95 checkedSend(imageOutput, mat); 96 97 LOG_INFO("Sent data=" << counter << ", time=" << road_time()); 98 file << counter << " " << road_time() << "\n" << std::flush; 99 100 usleep(waitTimeMicros); 101 ++counter; 102 setState(MONITOR_OK); 103 } 104 105 file.close(); 106 checkedSend(mImageOutput, mat); 107 LOG_INFO("Sent data=" << counter << ", time=" << road_time()); 108 mFile << counter << " " << road_time() << "\n" << std::flush; 109 ++counter; 110 setState(MONITOR_OK); 111 112 using boost::bind; 113 using boost::ref; 114 mTimer.expires_at(mTimer.expires_at() + mInterval); 115 mTimer.async_wait(bind(&ProducerExample::produce, this, ref(counter))); 106 116 } -
trunk/examples/ProducerConsumerExample/ProducerExample.h
r176 r291 16 16 #include <Pacpus/kernel/road_time.h> 17 17 //#include <Pacpus/structure/genericStructures.h> 18 #include <QThread> 18 19 #include <boost/asio.hpp> 20 #include <boost/date_time/posix_time/posix_time_types.hpp> 21 #include <fstream> 19 22 #include <QImage> 20 23 21 namespace pacpus { 24 namespace pacpus 25 { 22 26 23 27 class PRODUCERCONSUMEREXAMPLE_API ProducerExample 24 : public QThread25 28 : public QObject 29 , public ComponentBase 26 30 { 27 31 Q_OBJECT … … 41 45 virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config); 42 46 43 protected: 44 void run(); 45 46 public Q_SLOTS: 47 private: 48 void produce(int& counter); 47 49 48 50 private: 49 51 std::string mOutputFileName; 50 //QThread thread; 52 53 std::fstream mFile; 54 OutputInterface<QImage, ProducerExample>* mImageOutput; 55 56 boost::asio::io_service mIo; 57 boost::posix_time::time_duration mInterval; 58 boost::asio::deadline_timer mTimer; 51 59 }; 52 60
Note:
See TracChangeset
for help on using the changeset viewer.