#ifndef PORTBRIDGE_H
#define PORTBRIDGE_H

#include <QObject>
#include <QWidget>

#include "lruserialport.h"

#include <QVector>
#include <QUdpSocket>
#include "xlrulogfile.h"

class PortBridge : public QObject
{
    Q_OBJECT

public:
    PortBridge(QObject* parent=0);

    void close();

    void setMasterFilter(LruSerialFilter*);

    void setSocket(QUdpSocket*, unsigned int port=0);

    void addUdpBridge(LruSerialPort* s0, bool slaveSlow=false);

    void addBridge(LruSerialPort* s0, LruSerialPort* s1, bool slaveSlow=false);

    void forwardCount(int inbytes, int outbytes);

    void getForwardCounter(int& in, int& out);

    bool forwardDisabled;

    unsigned int udpCharIn();
    unsigned int udpCharOut();

public slots:
    void udpEnable(bool enable=true);

private slots:

    void udpReadyRead();
    void serialUdpReadyRead();

private:
    class Implementation;
    Implementation& p_;

    QUdpSocket* s;
    bool udpEnabled;
};


class PortBridgeForwarder: public QObject
{
    Q_OBJECT

private:
    PortBridgeForwarder(QObject* parent=0);

    PortBridge* portBridge;

    //LruSerialPort* fromPort;
    LruSerialPort* masterPort;

    QVector<LruSerialPort*> slavePort;
    QVector<bool> slaveSlowFlags;

    void setMaster(LruSerialPort* master);
    void addSlave(LruSerialPort* slave, bool slaveSlow);

    //QWidget* controlWidget();

private slots:
    void serialForwardToMaster();
    void serialForwardToSlaves();

    void slowTimeout();

private:
    class Implementation;
    Implementation& p_;

    friend class PortBridge;
};

#endif // PORTBRIDGE_H
