数据交换的设计模式
Design pattern for data exchange
我有一组虚拟设备(D(。设备可以交叉连接(假设是物理连接(:
D1 -- D2
D2 -- D3
每个链接可能具有不同的延迟。
每个设备都包含一组可以生成一些数据的虚拟子设备。可以将子设备组织为逻辑连接。子设备将传输真实的字节数据。
我用C++,Qt。我尝试使用信号槽机制QSignalMapper,但我没有找到好的解决方案。
请帮助我建立清晰的抽象。我可以使用任何设计模式吗?
您应该定义一个模拟类之间物理链接的Link
类。最有趣的问题是,如何使用信号和插槽连接设备并模拟延迟?
我的建议是:实现一个send(QByteArray data)
槽,将数据排队到内部队列(模拟线路(,并将超时设置为具有可选抖动的给定默认延迟。然后,超时会触发一个信号,其中包含从队列中弹出的数据。
如果要模拟设备之间的路由器,则应考虑到队列中的数据越多,延迟就越大,因为需要重新传输。为了大致模拟这一点,您可以根据当前队列长度设置超时值。
以此为起点:
class Link : public QObject
{
Q_OBJECT
public:
Link(Device *from, Device *to) :
QObject(to), m_from(from), m_to(to)
{
//make my life dependant on both "from" and "to" objects
connect(from, SIGNAL(destroyed()), SLOT(deleteLater()));
//connect to the signals and slots of the devices
connect(from, SIGNAL(send(QByteArray,Device*)),
this, SLOT( send(QByteArray,Device*)));
connect(this, SIGNAL(receive(QByteArray,Device*,int)),
to, SLOT( receive(QByteArray,Device*,int)));
}
public slots:
void send(QByteArray data, Device *receiver) {
Message msg(data, 0, qobject_cast<Device*>(sender()), receiver);
send(msg);
}
void send(Message msg) {
msg.hops++; // here we increase the hops counter
m_queue.enqueue(msg);
QTimer::signalShot(m_delay, this, SLOT(timeout()));
}
signals:
void receive(QByteArray data, Device *sender, int hops);
void forward(Message);
private slots:
void timeout() {
receive(m_queue.dequeue());
}
void receive(Message msg) {
if(msg.receiver == m_to)
// msg reached destination!
emit receive(msg.data, msg.sender, msg.hops);
else
// forward to next link
emit forward(msg);
}
private:
static const int m_delay = 100; // delay set to 100 ms
QQueue<Message> m_queue;
Device *m_from, *m_to;
};
Message
类型定义如下:
struct Message {
QByteArray data;
int hops;
Device *sender;
Device *receiver;
Message(data, hops, sender) : data(data), hops(hops),
sender(sender), receiver(receiver) {}
};
然后只需创建这样的设备和链接:
// Create devices:
Device *d1 = new Device(this);
Device *d2 = new Device(this);
// Create link:
Link *d1d2 = new Link(d1, d2);
或带有转发规则的链接:
// Create devices:
Device *d1 = new Device(this);
Device *d2 = new Device(this);
Device *d3 = new Device(this);
// Create links:
Link *l1 = new Link(d1, d2);
Link *l2 = new Link(d2, d3);
// Create forwarding rule:
connect(l1, SIGNAL(forward(Message)), l2, SLOT(send(Message)));
然后,d1发送的每个数据(当它发出信号send(QByteArray)
时(将以100毫秒的延迟传输到d2的时隙receive(QByteArray)
。如果数据不是针对 d2 的,则forward(Message)
发出信号,该信号必须由另一个链路捕获(请参阅转发规则(。然后,它被视为新的传入消息并传递到 d3。
请注意,真正的网络不是这样运作的。您需要实施路由策略来完全模拟此类设置;这是相当困难的。
另请注意,我没有测试此代码。 ;)
此方法不会模拟将数据拆分为小段(每个段 1.5 KB(。要模拟真实的以太网设置,您还需要这样做。如果需要,请在评论中询问,我可能会延长课程。
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 资源管理设计模式
- 用于在回调中调用解析器的设计模式
- 设计帮助 - 为不同类型的消息处理通用接口的设计模式
- 在这种情况下我应该使用哪种设计模式
- C++中物体改变识别的设计模式?
- 确保所有构造函数调用相同的函数 c++ 设计模式
- 需要实例化不同类/对象并在启动时确定的硬件插槽的设计模式
- 设计模式,以避免不必要地添加抽象函数以适应新功能
- 工厂设计模式优化
- 使用C++模板的数据映射器设计模式
- 为什么以及如何使用原型设计模式
- 具有多个继承共享一个资源的对象 - 寻找良好的设计模式
- 在C++中创建观察器设计模式的好方法
- 现代C++在多大程度上消除了对设计模式的需求?
- 对于存储另一个类所需信息的类,例如其构造,是否有设计模式?
- 下面抽象工厂设计模式的实现是正确的吗
- sql记录集函数的状态设计模式
- 是否有可以处理方法调用依赖关系的设计模式?
- 数据交换的设计模式