Changeset 291 in pacpusframework for trunk


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.

Location:
trunk
Files:
5 edited

Legend:

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

    r288 r291  
    1717    LOG_TRACE("constructor(" << name << ")");
    1818
    19     LOG_INFO("Thread " << thread.currentThread());
    20     LOG_INFO("Current Thread " << QThread::currentThread());
    21 
    22     namespace po = boost::program_options;
    2319    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")
    2521    ;
    2622}
     
    5551{
    5652    LOG_TRACE(Q_FUNC_INFO);
    57     moveToThread(&thread);
    5853    m_counter = 0;
    5954   
     
    6358    }
    6459
    65     thread.start();
    6660    setState(MONITOR_OK);
    6761    LOG_INFO("started component '" << getName() << "'");
     
    7266    LOG_TRACE(Q_FUNC_INFO);
    7367
    74     QMetaObject::invokeMethod(&thread, "quit");
    7568    m_file.close();
    7669    setState(STOPPED);
     
    9790   
    9891    PacpusTypedEvent<int> intEvent(TYPED_EVENT, 2);
    99     //PacpusTypedEvent<QImage> imageEvent = intEvent;
     92    //PacpusTypedEvent<QImage> imageEvent = intEvent; // should not compile
    10093}
  • trunk/examples/ProducerConsumerExample/ConsumerExample.h

    r288 r291  
    77#include <Pacpus/kernel/ComponentBase.h>
    88#include <QImage>
    9 #include <QThread>
    109#include <string>
    1110
     
    3332
    3433    QImage matrix;
    35     QThread thread;
    3634
    3735    int m_counter;
  • 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}
  • trunk/examples/ProducerConsumerExample/ProducerExample.h

    r176 r291  
    1616#include <Pacpus/kernel/road_time.h>
    1717//#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>
    1922#include <QImage>
    2023
    21 namespace pacpus {                                                                             
     24namespace pacpus
     25{
    2226
    2327class PRODUCERCONSUMEREXAMPLE_API ProducerExample
    24         : public QThread
    25         , public ComponentBase
     28    : public QObject
     29    , public ComponentBase
    2630{
    2731    Q_OBJECT
     
    4145    virtual COMPONENT_CONFIGURATION configureComponent(XmlComponentConfig config);
    4246
    43 protected:
    44     void run();
    45 
    46 public Q_SLOTS:
     47private:
     48    void produce(int& counter);
    4749
    4850private:
    4951    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;
    5159};
    5260
  • trunk/src/PacpusLib/InputOutputBase.cpp

    r206 r291  
    2121
    2222AbstractInterface::~AbstractInterface()
    23 {}
     23{
     24}
    2425
    2526QString AbstractInterface::getSignature() const
     
    7172
    7273InputInterfaceBase::InputInterfaceBase(QString name, ComponentBase * component, QObject * parent)
    73     : AbstractInterface(name, component, parent)
    74 {}
     74    : AbstractInterface(name, component, /*parent*/ NULL)
     75{
     76}
    7577
    7678InputInterfaceBase::~InputInterfaceBase()
    77 {}
     79{
     80}
    7881
    7982// TODO for serialization prupose (not yet implemented !!!)
Note: See TracChangeset for help on using the changeset viewer.