Changeset 291 in pacpusframework for trunk/examples/ProducerConsumerExample/ProducerExample.cpp
- Timestamp:
- Mar 27, 2014, 12:53:43 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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 }
Note:
See TracChangeset
for help on using the changeset viewer.