Ignore:
Timestamp:
Mar 27, 2014, 12:53:43 PM (11 years ago)
Author:
Marek Kurdej
Message:

Reworked threading in InputInterfaceBase, each slot has its own processing thread.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/examples/ProducerConsumerExample/ProducerExample.cpp

    r288 r291  
    11#include "ProducerExample.h"
    22
    3 #include <fstream>
    43#include <Pacpus/kernel/ComponentFactory.h>
    54#include <Pacpus/kernel/ComponentManager.h>
    65#include <Pacpus/kernel/InputOutputInterface.h>
    76#include <Pacpus/kernel/Log.h>
     7
     8#include <boost/bind.hpp>
     9#include <boost/thread.hpp>
    810
    911using namespace pacpus;
     
    1820ProducerExample::ProducerExample(QString name)
    1921    : ComponentBase(name)
     22    , mInterval(boost::posix_time::milliseconds(150))
     23    , mTimer(mIo, mInterval)
    2024{
    21     LOG_TRACE("constructor(" << name << ")");
     25    PACPUS_LOG_FUNCTION();
    2226
    2327    namespace po = boost::program_options;
     
    3034ProducerExample::~ProducerExample()
    3135{
    32     LOG_TRACE("destructor");
     36    PACPUS_LOG_FUNCTION();
    3337}
    3438
    35 ComponentBase::COMPONENT_CONFIGURATION
    36 ProducerExample::configureComponent(XmlComponentConfig /*config*/)
     39ComponentBase::COMPONENT_CONFIGURATION ProducerExample::configureComponent(XmlComponentConfig /*config*/)
    3740{
    3841    PACPUS_LOG_FUNCTION();
     
    5457void ProducerExample::startActivity()
    5558{
    56     LOG_TRACE(Q_FUNC_INFO);
     59    PACPUS_LOG_FUNCTION();
    5760
    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
    5972    setActive(true);
    6073    setState(MONITOR_OK);
    6174    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();
    6280}
    6381
    6482void ProducerExample::stopActivity()
    6583{
    66     LOG_TRACE(Q_FUNC_INFO);
     84    PACPUS_LOG_FUNCTION();
    6785
    6886    setActive(false);
    6987    setState(STOPPED);
    7088    LOG_INFO("stopped component '" << getName() << "'");
     89   
     90    mFile.close();
    7191}
    7292
    73 void ProducerExample::run()
     93void ProducerExample::produce(int& counter)
    7494{
    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        );
    77105
    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)));
    106116}
Note: See TracChangeset for help on using the changeset viewer.