Ejemplo n.º 1
0
        /// <summary>
        /// Returns the registered handler for the provided message, if any.
        /// </summary>
        /// <param name="message">The message a handler should be found for.</param>
        /// <returns>The handler for the provided message or null, if none has been registered for that message.</returns>
        public DispatchHandler AssociatedHandler(Message.Message message)
        {
            if (message == null || !message.IsRequest())
            {
                return(null);
            }

            PeerAddress recipient = message.Recipient;

            // search for handler, 0 is ping
            // if we send with peerId = ZERO, then we take the first one we found
            if (recipient.PeerId.IsZero && message.Command == Rpc.Rpc.Commands.Ping.GetNr())
            {
                Number160 peerId = _peerBeanMaster.ServerPeerAddress.PeerId;
                return(SearchHandler(peerId, peerId, Rpc.Rpc.Commands.Ping.GetNr()));
            }
            else
            {
                // else we search for the handler that we are responsible for
                DispatchHandler handler = SearchHandler(recipient.PeerId, recipient.PeerId, message.Command);
                if (handler != null)
                {
                    return(handler);
                }
                else
                {
                    // If we could not find a handler that we are responsible for, we
                    // are most likely a relay. Since we have no ID of the relay, we
                    // just take the first one.
                    var handlers = SearchHandler(Convert.ToInt32(message.Command));
                    foreach (var entry in handlers)
                    {
                        if (entry.Key.DomainKey.Equals(recipient.PeerId))
                        {
                            return(entry.Value);
                        }
                    }
                    return(null);
                }
            }
        }