2012-04-27 60 views
0

我想创建类似于:电路交换的数据结构?

我有一个模块可以为消息流做类似'电路交换'的事情。也就是说,它只有一个入口和多个出口。一旦消息到达入口,就根据某种逻辑选择一个输出端口(逻辑在问题的上下文中并不重要)。检查是否有任何正在进行的消息传输(对于第一条消息,不会有任何消息)。如果没有传输,则将消息发送到该输出端口,否则,该消息将保留在该特定输出端口的队列中。我需要为此通信决定数据结构。请指教

我的想法是有一个outports和相应的队列的地图。

queue<message> m_incoming_queue; 
typedef map<outport*,m_incoming_queue> transaction_map 

如果这是一个很好的解决方案,我想知道如何在运行时创建一个队列?因为我不知道会有多少出口,我会根据需求创建出口。

+0

也许你想看看http://www.zeromq.org/一个非常漂亮的轻量级消息框架。 – snies 2012-04-27 23:46:15

回答

0

也许喜欢的东西:

// At beginning 
typedef queue<message> MessageQueue 
typedef map<outport*, MessageQueue> transaction_map 
transaction_map tm() // Create the transaction map 


// On receipt of each message 
// (Some logic that determines outport* op and message m) 
if(tm.count(*op) == 0) 
{ 
    // There are no queues yet, create one and insert it 
    tm.insert(transaction_map::value_type(*op, MessageQueue())) 
} 
// There is already a queue created, so add to it 
tm[*op].push(m)