Пример #1
0
        void OnServiceInstanceDiscovered(object sender, ServiceInstanceDiscoveryEventArgs e)
        {
            try
            {
                var msg = e.Message;

                // Is it our service?
                var qsn = ServiceName + ".local";
                if (!e.ServiceInstanceName.EndsWith(qsn))
                {
                    return;
                }

                var addresses = GetAddresses(msg)
                                .Where(a => a.PeerId != LocalPeer.Id)
                                .ToArray();
                if (addresses.Length > 0)
                {
                    PeerDiscovered?.Invoke(this, new Peer {
                        Id = addresses[0].PeerId, Addresses = addresses
                    });
                }
            }
            catch (Exception ex)
            {
                log.Error("OnServiceInstanceDiscovered error", ex);
                // eat it
            }
        }
Пример #2
0
        /// <inheritdoc />
        public Task StartAsync()
        {
            _log.Debug("Starting");
            if (Addresses == null)
            {
                _log.Warn("No bootstrap addresses");
                return(Task.CompletedTask);
            }

            var peers = Addresses
                        .Where(a => a.HasPeerId)
                        .GroupBy(
                a => a.PeerId,
                a => a,
                (key, g) => new Peer {
                Id = key, Addresses = g.ToList()
            });

            foreach (var peer in peers)
            {
                try
                {
                    PeerDiscovered?.Invoke(this, peer);
                }
                catch (Exception e)
                {
                    _log.Error(e);
                }
            }

            return(Task.CompletedTask);
        }
Пример #3
0
        /// <summary>
        ///   Register that a peer has been discovered.
        /// </summary>
        /// <param name="peer">
        ///   The newly discovered peer.
        /// </param>
        /// <returns>
        ///   The registered peer.
        /// </returns>
        /// <remarks>
        ///   If the peer already exists, then the existing peer is updated with supplied
        ///   information and is then returned.  Otherwise, the <paramref name="peer"/>
        ///   is added to known peers and is returned.
        ///   <para>
        ///   If the peer already exists, then a union of the existing and new addresses
        ///   is used.  For all other information the <paramref name="peer"/>'s information
        ///   is used if not <b>null</b>.
        ///   </para>
        ///   <para>
        ///   If peer does not already exist, then the <see cref="PeerDiscovered"/> event
        ///   is raised.
        ///   </para>
        /// </remarks>
        public Peer RegisterPeer(Peer peer)
        {
            if (peer.Id == null)
            {
                throw new ArgumentNullException("peer.ID");
            }
            if (peer.Id == LocalPeer.Id)
            {
                throw new ArgumentException("Cannot register self.");
            }

            var isNew = false;
            var p     = otherPeers.AddOrUpdate(peer.Id.ToBase58(),
                                               (id) =>
            {
                isNew = true;
                return(peer);
            },
                                               (id, existing) =>
            {
                if (!Object.ReferenceEquals(existing, peer))
                {
                    existing.AgentVersion    = peer.AgentVersion ?? existing.AgentVersion;
                    existing.ProtocolVersion = peer.ProtocolVersion ?? existing.ProtocolVersion;
                    existing.PublicKey       = peer.PublicKey ?? existing.PublicKey;
                    existing.Latency         = peer.Latency ?? existing.Latency;
                    existing.Addresses       = existing
                                               .Addresses
                                               .Union(peer.Addresses)
                                               .ToList();
                }
                return(existing);
            });

            if (isNew)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug($"New peer registerd {p}");
                    foreach (var a in p.Addresses)
                    {
                        log.Debug($"  at {a}");
                    }
                }
                PeerDiscovered?.Invoke(this, p);
            }

            return(p);
        }
Пример #4
0
        /// <summary>
        ///   Register that a peer's address has been discovered.
        /// </summary>
        /// <param name="address">
        ///   An address to the peer. It must end with the peer ID.
        /// </param>
        /// <param name="cancel">
        ///   Is used to stop the task.  When cancelled, the <see cref="TaskCanceledException"/> is raised.
        /// </param>
        /// <returns>
        ///   A task that represents the asynchronous operation. The task's result
        ///   is the <see cref="Peer"/> that is registered.
        /// </returns>
        /// <exception cref="Exception">
        ///   The <see cref="BlackList"/> or <see cref="WhiteList"/> policies forbid it.
        ///   Or the "p2p/ipfs" protocol name is missing.
        /// </exception>
        /// <remarks>
        ///   If the <paramref name="address"/> is not already known, then it is
        ///   added to the <see cref="KnownPeerAddresses"/>.
        /// </remarks>
        public async Task <Peer> RegisterPeerAsync(MultiAddress address, CancellationToken cancel = default(CancellationToken))
        {
            var peerId = address.PeerId;

            if (peerId == LocalPeer.Id)
            {
                throw new Exception("Cannot register to self.");
            }

            if (!await IsAllowedAsync(address, cancel))
            {
                throw new Exception($"Communication with '{address}' is not allowed.");
            }

            var isNew = false;
            var p     = otherPeers.AddOrUpdate(peerId.ToBase58(),
                                               (id) => {
                log.Debug("new peer " + peerId);
                isNew = true;
                return(new Peer
                {
                    Id = id,
                    Addresses = new List <MultiAddress> {
                        address
                    }
                });
            },
                                               (id, peer) =>
            {
                peer.Addresses = peer.Addresses.ToList();
                var addrs      = (List <MultiAddress>)peer.Addresses;
                if (!addrs.Contains(address))
                {
                    addrs.Add(address);
                }
                return(peer);
            });

            if (isNew)
            {
                PeerDiscovered?.Invoke(this, p);
            }

            return(p);
        }
Пример #5
0
        void OnServiceInstanceDiscovered(object sender, ServiceInstanceDiscoveryEventArgs e)
        {
            var msg = e.Message;

            // Is it our service?
            var qsn = ServiceName + ".local";

            if (!e.ServiceInstanceName.EndsWith(qsn))
            {
                return;
            }

            foreach (var address in GetAddresses(msg))
            {
                if (LocalPeer.Id == address.PeerId)
                {
                    continue;
                }
                PeerDiscovered?.Invoke(this, new PeerDiscoveredEventArgs {
                    Address = address
                });
            }
        }
Пример #6
0
 void OnPeerDiscovered(PeerDiscoveredEventArgs e)
 {
     PeerDiscovered?.Invoke(this, e);
 }
Пример #7
0
        private unsafe void DiscoveredPeerHandler(byte *peerUtf8Ptr, int peerLength)
        {
            ReadOnlySpan <byte> peerUtf8 = new ReadOnlySpan <byte>(peerUtf8Ptr, peerLength);

            PeerDiscovered?.Invoke(peerUtf8);
        }