2012-04-27 57 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)