예제 #1
0
        private List <InternodeMessage> FindPairedMessages(SortedDictionary <MessageKey, Message> messagesMap,
                                                           List <Message> incompleteMessages, int internodeMessagesLimit)
        {
            var internodeMessages = new Dictionary <NodeIdsPair, List <InternodeMessage> >();

            while (messagesMap.Count > 0)
            {
                Message m1 = messagesMap.First().Value;
                Message m2;
                if (messagesMap.TryGetValue(m1.Key.MakeComplementKey(), out m2))
                {
                    var outgoingMessage = m1.Direction == Messaging.MessageDirection.Outgoing ? m1 : m2;
                    var incomingMessage = m1.Direction == Messaging.MessageDirection.Outgoing ? m2 : m1;

                    Debug.Assert(outgoingMessage.Direction != incomingMessage.Direction);
                    Debug.Assert(incomingMessage.InternodeMessage == null);
                    Debug.Assert(outgoingMessage.InternodeMessage == null);

                    if (outgoingMessage.Node == incomingMessage.Node)
                    {
                        log.AppendLine(string.Format("message '{0}' is sent and received by the same node {1}",
                                                     outgoingMessage.Key, outgoingMessage.Node.NodeId));
                    }
                    else
                    {
                        var discoveredMessage = new InternodeMessage(outgoingMessage.Key.ToString(), outgoingMessage, incomingMessage);
                        List <InternodeMessage> tmp;
                        var nodeIdsPair = new NodeIdsPair()
                        {
                            N1 = outgoingMessage.Node.NodeId, N2 = incomingMessage.Node.NodeId
                        };
                        if (!internodeMessages.TryGetValue(nodeIdsPair, out tmp))
                        {
                            internodeMessages.Add(nodeIdsPair, (tmp = new List <InternodeMessage>()));
                        }
                        tmp.Add(discoveredMessage);
                    }

                    messagesMap.Remove(m2.Key);
                }
                else
                {
                    incompleteMessages.Add(m1);
                }
                messagesMap.Remove(m1.Key);
            }

            EnforceMessagesLimit(internodeMessages, internodeMessagesLimit, log);
            var retList = internodeMessages.SelectMany(i => i.Value).ToList();

            InitializeLinks(retList);
            return(retList);
        }
예제 #2
0
        private bool CheckInternodeMessagesNrLimit(Message outgoing, Message incoming, Dictionary <NodeIdsPair, int> counters, int internodeMessagesLimitPerNode)
        {
            var nodeIdsPair = new NodeIdsPair()
            {
                N1 = outgoing.Node.NodeId, N2 = incoming.Node.NodeId
            };
            int currentNrOfMessages;

            counters.TryGetValue(nodeIdsPair, out currentNrOfMessages);
            if (currentNrOfMessages >= internodeMessagesLimitPerNode)
            {
                log.AppendLine(string.Format("Nodes pair ({0}, {1}) exceeded internode messages limit ({2}). Internode message '{3}' will be ignored.",
                                             nodeIdsPair.N1, nodeIdsPair.N2, internodeMessagesLimitPerNode, outgoing.Key));
                return(false);
            }
            else
            {
                ++currentNrOfMessages;
                counters[nodeIdsPair] = currentNrOfMessages;
                return(true);
            }
        }