Changeset 206 in pacpusframework


Ignore:
Timestamp:
10/30/13 12:26:34 (11 years ago)
Author:
Marek Kurdej
Message:

Major: cleaned connection interfaces.

Location:
trunk
Files:
9 edited

Legend:

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

    r179 r206  
    9191    setState(MONITOR_OK);
    9292}
     93
     94void testPacpusTypedEventConverts()
     95{
     96    PacpusTypedEvent<float> floatEvent(TYPED_EVENT, 1.0f);
     97    PacpusTypedEvent<double> doubleEvent = floatEvent;
     98   
     99    PacpusTypedEvent<int> intEvent(TYPED_EVENT, 2);
     100    //PacpusTypedEvent<QImage> imageEvent = intEvent;
     101}
  • trunk/include/Pacpus/kernel/ComponentBase.h

    r202 r206  
    8484    /// Ctor of ComponentBase.
    8585    /// @param name Name of your component.
    86     ComponentBase(const QString& name);
     86    ComponentBase(const QString & name);
    8787
    8888    /// Dtor of ComponentBase.
     
    100100    /// @return Name of the component.
    101101    QString getName() const;
     102   
     103    /// @returns @b true if the component has been started and is active (working)
     104    bool isActive() const;
    102105
    103106protected:
     
    170173    }
    171174
    172     bool isActive() const;
    173175    void setActive(bool isActive);
    174176    bool isRecording() const;
  • trunk/include/Pacpus/kernel/InputOutputBase.h

    r200 r206  
    3232    virtual std::size_t getDataSize() const = 0;
    3333    virtual const std::type_info & getDataType() const = 0;
     34   
     35    //virtual PacpusEvent * getEventTemplate() = 0;
    3436   
    3537    void addConnection(ConnectionBase connection);
     
    99101    QStringList getInputConnectedList();
    100102
    101     void send(/*const*/ QByteArray & data);
     103    //void send(/*const*/ QByteArray & data);
    102104};
    103105
  • trunk/include/Pacpus/kernel/InputOutputInterface.h

    r202 r206  
    2323        : InputInterfaceBase(name, component, component)
    2424        , method(processMethod)
    25     {}
     25    {
     26    }
    2627
    2728    ~InputInterface()
    28     {}
     29    {
     30    }
    2931
    3032    std::size_t getDataSize() const
     
    3941
    4042    // FIXME: what's the purpose of this function?
    41     PacpusEvent * getEventTemplate()
    42     {
    43         return new PacpusTypedEvent<T>(TYPED_EVENT);
    44     }
     43    //PacpusEvent * getEventTemplate()
     44    //{
     45    //    return new PacpusTypedEvent<T>(TYPED_EVENT);
     46    //}
    4547
    4648    // FIXME: what's the purpose of this function?
    4749    void customEvent(QEvent * event)
    4850    {
    49         // TODO check component state started
    50         //if (_component) get state
     51        // check that component has been started
     52        if ((NULL == getComponent()) || (!getComponent()->isActive())) {
     53            LOG_DEBUG("component is not active");
     54            return;
     55        }
     56       
     57        LOG_DEBUG("Receiver: " << getSignature());
     58
     59        //PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *>(event);
     60        PacpusEvent * pacpusEvent = dynamic_cast<PacpusEvent *>(event);
     61        if (!pacpusEvent) {
     62            LOG_WARN("dynamic_cast failed: not a PacpusEvent");
     63            return;
     64        }
     65        PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *>(pacpusEvent);
     66        if (!typedEvent) {
     67            LOG_WARN("dynamic_cast failed: incompatible event types");
     68            return;
     69        }
     70
    5171        switch (event->type()) {
    5272        case TYPED_EVENT:
    53             {
    54             PacpusTypedEvent<T> * typedEvent = dynamic_cast<PacpusTypedEvent<T> *> (event);
    55 
    56             LOG_DEBUG("Receiver: " << getSignature());
    57 
    58             if (typedEvent->timerange() < 500 && readingMode() == TimeBounded) {
     73            if (TimeBounded == readingMode() && typedEvent->timerange() < 500) {
    5974                LOG_WARN("Incorrect TimeRange (0), switch to NeverSkip");
    6075                readingMode() = NeverSkip;
     
    6378            switch (readingMode()) {
    6479            case TimeBounded:
    65 
    6680                if (road_time() - typedEvent->time() > typedEvent->timerange()) {
    67                     LOG_DEBUG("Data skipped, receiver: " << this->getSignature());
     81                    LOG_TRACE("Data skipped, receiver: " << this->getSignature());
    6882                    break;
    6983                }
     
    7387
    7488            case GetLast:
    75 
    7689                (dynamic_cast<C*>(component())->*method)(typedEvent->data());
    7790                // delete all remaining events
     
    8598            default:
    8699                LOG_WARN("Unknown reading mode " << readingMode());
     100                break;
    87101            }
    88102            break;
    89         }
    90103
    91104        // Add here new event type if needed
     
    95108            break;
    96109        }
    97     event->accept();
     110        event->accept();
    98111    }
    99112
    100113    // TODO for Pulling mode (not yet implemented !!!)
    101     T & getData() {
     114    T & getData()
     115    {
    102116        T data;
    103117        // TODO ask output data;
    104 
    105118        return data;
    106119    }
     
    125138    {}
    126139
    127     // Used by Components to send data througth typed output
     140    /// Send data through a typed output
    128141    void send(const T & data, road_time_t t = road_time(), road_timerange_t tr = 0);
    129142   
     
    150163        //  It is not safe to access the event after it has been posted.   
    151164        QEvent * newEvent = new PacpusTypedEvent<T>(TYPED_EVENT, data, t, tr);
    152         QCoreApplication::postEvent(it->getInterface(), newEvent, it->getPriority());
     165        QCoreApplication::postEvent(
     166            it->getInterface(),
     167            newEvent,
     168            it->getPriority()
     169        );
    153170        LOG_DEBUG("Sender: " << it->getInterface()->getSignature());
    154171        LOG_DEBUG("Data &: " << &data);
  • trunk/include/Pacpus/kernel/PacpusEvent.h

    r196 r206  
    3838
    3939    // TODO: should we make it virtual pure ???
    40     virtual QDataStream& streamOut(QDataStream& out)
     40    virtual QDataStream & streamOut(QDataStream & out)
    4141    {
    4242        return out;
     
    7777{
    7878public:
    79     TypedData(const T& data)
    80     : m_data(data)
     79    TypedData(const T & data)
     80        : m_data(data)
    8181    {
    8282    }
     
    9696    }
    9797
    98     size_t size() const
     98    std::size_t size() const
    9999    {
    100100        return sizeof(T);
     
    112112public:
    113113    // FIXME: why we need `data = T()` ???
    114     PacpusTypedEvent(PacpusEventType type, const T & data = T(), road_time_t t = road_time(), road_timerange_t tr = 0 )
     114    PacpusTypedEvent(PacpusEventType type, const T & data/* = T()*/, road_time_t t = road_time(), road_timerange_t tr = 0)
    115115        : PacpusEvent(type, t, tr)
    116116        , TypedData<T>(data)
    117     {}
     117    {
     118    }
     119   
     120    /// Conversion constructor from another PacpusTypedEvent,
     121    /// when T is convertible to S
     122    template <typename S>
     123    PacpusTypedEvent(const PacpusTypedEvent<S> & other)
     124        : PacpusEvent(static_cast<PacpusEventType>(other.type()), other.time(), other.timerange())
     125        , TypedData<T>(other.data())
     126    {
     127    }
    118128
    119129    ~PacpusTypedEvent()
    120     {}
    121 
    122     QDataStream& streamOut(QDataStream& out)
     130    {
     131    }
     132
     133    QDataStream & streamOut(QDataStream & out)
    123134    {
    124135        // FIXME Stream Data errors
     
    126137    }
    127138
    128     QDataStream& streamIn(QDataStream& in)
     139    QDataStream & streamIn(QDataStream & in)
    129140    {
    130141        return in >> (quint64&) time() >> timerange() /*>> m_data*/;
     
    135146{
    136147public:
    137     GenericData(const char* data, size_t size)
     148    GenericData(const char * data, size_t size)
    138149        : m_data(NULL)
    139150        , m_dataSize(size)
     
    164175    }
    165176
    166     size_t size() const
     177    std::size_t size() const
    167178    {
    168179        return m_dataSize;
     
    170181
    171182private:
    172     char* m_data;
    173     size_t m_dataSize;
     183    char * m_data;
     184    std::size_t m_dataSize;
    174185};
    175186
     
    179190{
    180191public:
    181     PacpusGenericEvent(PacpusEventType type, const char* data, size_t size)
     192    PacpusGenericEvent(PacpusEventType type, const char * data, size_t size)
    182193        : PacpusEvent(type)
    183194        , GenericData(data, size)
     
    192203} // namespace pacpus
    193204
    194 PACPUSLIB_API inline QDataStream& operator<< (QDataStream& out, pacpus::PacpusEvent& ev)
     205PACPUSLIB_API inline QDataStream & operator<<(QDataStream & out, pacpus::PacpusEvent & ev)
    195206{
    196207    /*return ev.streamOut(out);*/
     
    198209}
    199210
    200 PACPUSLIB_API inline QDataStream& operator>> (QDataStream& in, pacpus::PacpusEvent& ev)
     211PACPUSLIB_API inline QDataStream & operator>>(QDataStream & in, pacpus::PacpusEvent & ev)
    201212{
    202213    /*return ev.streamIn(in);*/
  • trunk/src/PacpusLib/ComponentBase.cpp

    r204 r206  
    214214OutputInterfaceBase * ComponentBase::getOutput(QString outputName) const
    215215{
    216 /*    QList<QString> keys = output.keys();
    217     for (int i=0; i<keys.size();++i)
    218         LOG_INFO("Key : " << keys[i])*/;
    219 
    220216    if (outputs().contains(outputName)) {
    221217        return outputs()[outputName];
    222218    }
    223     LOG_WARN("Component " << getName() << " does not containt output " << outputName);
     219    LOG_WARN("Component " << getName() << " does not contain output " << outputName);
    224220    return NULL;
    225221}
  • trunk/src/PacpusLib/ComponentManager.cpp

    r204 r206  
    2222using namespace pacpus;
    2323
     24//template <typename _Elem, typename _Traits, typename _ListElem>
     25//std::basic_ostream<_Elem, _Traits> & operator<<(std::basic_ostream<_Elem, _Traits> & os, const QList<_ListElem> & list)
     26//{
     27//    typedef QList<_ListElem> ListType;
     28//    for (ListType::const_iterator it = list.cbegin(), itend = list.cend(); it != itend; ++it) {
     29//        os << *it << "\n";
     30//    }
     31//    return os;
     32//}
     33
    2434template <typename _Elem, typename _Traits, typename _ListElem>
    2535std::basic_ostream<_Elem, _Traits> & operator<<(std::basic_ostream<_Elem, _Traits> & os, const QList<_ListElem> & list)
     
    3646////////////////////////////////////////////////////////////////////////////////
    3747
     48/// Connects OutputInterfaceBase @b out to InputInterfaceBase @b in using given priority and reading mode.
     49/// @returns @b true if connection has been added successfully, @b false otherwise.
    3850bool connectInterface(OutputInterfaceBase * out, InputInterfaceBase * in, int priority, InputInterfaceBase::ReadingMode mode = InputInterfaceBase::GetLast);
    3951
     
    4254bool connectInterface(OutputInterfaceBase * out, InputInterfaceBase * in, int priority, InputInterfaceBase::ReadingMode mode)
    4355{
    44     if ((out->getDataType() == in->getDataType())
    45             || (out->getDataType() == typeid(QByteArray))
    46             || (in->getDataType() == typeid(QByteArray))) {
    47         // Add connection
    48         out->addConnection(ConnectionBase(in, priority));  // TODO make connect function
    49         in->addConnection(ConnectionBase(out, priority));
    50         in->setReadingMode(mode);
    51         //LOG_INFO("connection : Output " << out->getSignature() << " => Input " << in->getSignature());
    52         return true;
    53     } else {
    54         //LOG_WARN("connecting " << out->getSignature() << ":" << out->getDataType() << " to " << in->getSignature() << ":" << in->getDataType() << " failled : DataType incompatible " <<  QString(typeid(QByteArray).name()));
    55         return false;
    56     }
     56    // check connections
     57    if (!out || !in) {
     58        LOG_WARN("null connection");
     59        return false;
     60    }
     61
     62    // check if connection type are compatible
     63    // FIXME: it should permit an OutputType to be a type conversible to InputType
     64    if (out->getDataType() != in->getDataType()) {
     65        LOG_WARN("connection types do not match: "
     66            << out->getSignature() << "." << out->getDataType().name()
     67            << " -> "
     68            << in->getSignature() << "." << in->getDataType().name()
     69        );
     70        return false;
     71    }
     72    //if ((out->getDataType() == in->getDataType())
     73    //        || (out->getDataType() == typeid(QByteArray))
     74    //        || (in->getDataType() == typeid(QByteArray))) {
     75   
     76    // add connection
     77    out->addConnection(ConnectionBase(in, priority));  // TODO make connect function
     78    in->addConnection(ConnectionBase(out, priority));
     79    in->setReadingMode(mode);
     80    //LOG_INFO("connection : Output " << out->getSignature() << " => Input " << in->getSignature());
     81    return true;
    5782}
    5883
     
    145170    LOG_TRACE("registerComponent(name="<< name << ")");
    146171
    147     if (componentMap_.contains(name))
    148     {
     172    if (componentMap_.contains(name)) {
    149173        LOG_WARN("cannot register component '" << name << "'. A component with the same name exists already");
    150174        return false;
     
    235259}
    236260
    237 bool ComponentManager::createConnection(const QString& outputSignature, const QString& inputSignature, const QString& type, int priority = 0)
     261bool ComponentManager::createConnection(const QString & outputSignature, const QString & inputSignature, const QString & type, int priority = 0)
    238262{
    239263    // FIXME: use 2 signatures (output component + output connection) instead of 1 separated by a (".") dot
     
    247271        return false;
    248272    }
    249     // NOTE Create communicationInterface if needed ??
     273    // FIXME: Create communicationInterface if needed ??
    250274    return connectInterface(
    251275        getComponent(output[0])->getOutput(output[1]),
  • trunk/src/PacpusLib/ConnectionManager.cpp

    r89 r206  
    1 
    21#include  <Pacpus/kernel/ConnectionManager.h>
    32#include  <Pacpus/kernel/Log.h>
     
    1514{
    1615    LOG_TRACE("destructor");
    17 
    18 
    1916}
    2017
     
    4138    LOG_TRACE(Q_FUNC_INFO);
    4239
    43     ComponentManager *mgr = ComponentManager::getInstance();
    44 
     40    ComponentManager * mgr = ComponentManager::getInstance();
    4541
    4642    // TODO read XML file and build ConnectionList
    4743    QList<ConnectionDescriptor> ConnectionList;
    48     ConnectionList.append(ConnectionDescriptor("testComponent1","image","testComponent2","image","type"));
     44    ConnectionList.append(ConnectionDescriptor("testComponent1", "image", "testComponent2", "image", "type"));
    4945
    5046    ComponentBase * sender, * receiver;
     
    5248    OutputInterfaceBase * out;
    5349
    54     for(QList<ConnectionDescriptor>::iterator i = ConnectionList.begin(); i != ConnectionList.end(); ++i) {
    55 
     50    for (QList<ConnectionDescriptor>::iterator i = ConnectionList.begin(); i != ConnectionList.end(); ++i) {
    5651        // TODO Handle errors
    5752        sender = mgr->getComponent(i->_outputComponent);
    5853        out = sender->getOutput(i->_outputName);
    59 
    6054
    6155        receiver = mgr->getComponent(i->_inputComponent);
     
    6660        LOG_INFO(" Output " << "out" << " connected to " << "in") // TODO replace out / in
    6761    }
    68 
    6962
    7063//    CledComponent * cledComponent = (CledComponent*) m_component[cName];
     
    8275//    in1->setConnection(buffer,true);
    8376//    out1->setConnection(buffer,true);
    84 
    85 
    8677}
    87 
  • trunk/src/PacpusLib/InputOutputBase.cpp

    r200 r206  
    55#include <Pacpus/kernel/InputOutputInterface.h>
    66#include <Pacpus/kernel/Log.h>
    7 
    8 #include <QApplication>
    97
    108using namespace pacpus;
     
    124122}
    125123
    126 // TODO for serialization prupose (not yet implemented !!!)
    127 void OutputInterfaceBase::send(/*const*/ QByteArray & data)
    128 {
    129     // TODO check at least one Typed connection
    130 
    131     for (QList<ConnectionBase>::iterator it = connections().begin(); it!=connections().end(); ++it) {
    132             QDataStream in(&data,QIODevice::ReadOnly);
    133             PacpusEvent * event = dynamic_cast<InputInterfaceBase*>(connections().at(0).getInterface())->getEventTemplate();
    134             event->streamIn(in);
    135             QApplication::postEvent(it->getInterface(),event,it->getPriority());
    136     }
    137 }
     124// TODO for serialization purpose (not yet implemented !!!)
     125//void OutputInterfaceBase::send(/*const*/ QByteArray & data)
     126//{
     127//    // TODO check at least one Typed connection
     128//
     129//    for (QList<ConnectionBase>::iterator it = connections().begin(), itend = connections().end(); it != itend; ++it) {
     130//            QDataStream in(&data, QIODevice::ReadOnly);
     131//            InputInterfaceBase * interfaceBase
     132//                = dynamic_cast<InputInterfaceBase *>(connections().at(0).getInterface());
     133//            //AbstractInterface * interfaceBase = connections().at(0).getInterface();
     134//            if (!interfaceBase) {
     135//                continue;
     136//            }
     137//            PacpusEvent * event = interfaceBase->getEventTemplate();
     138//            if (!event) {
     139//                continue;
     140//            }
     141//            event->streamIn(in);
     142//            QCoreApplication::postEvent(
     143//                it->getInterface(),
     144//                event,
     145//                it->getPriority()
     146//            );
     147//    }
     148//}
Note: See TracChangeset for help on using the changeset viewer.