Example #1
0
        public void Add(IPeer peer)
        {
            lock(this)
            {
                if (!_container.Contains(peer) && !peer.Equals(Peer.Self))
                {
                    var lease = RemotingServices.GetLifetimeService((MarshalByRefObject) peer) as ILease;
                    if (lease != null) lease.Register(new SearchEngineSponsor());

                    _container.Add(peer);
                    try
                    {
                        foreach (var peer1 in peer.PeerContainer.GetAvailablePeers())
                        {
                            Add(peer1);
                        }
                    }
                    catch(RemotingException)
                    {
                        _container.Remove(peer);
                    }
                    catch (WebException)
                    {
                        _container.Remove(peer);
                    }
                }
            }
        }
Example #2
0
        bool ValidateRequestWithPiece(IPeer peer, BlockInfo request, Piece piece, out bool pieceComplete, out IList <IPeer> peersInvolved)
        {
            pieceComplete = false;
            peersInvolved = null;

            // Pick out the block that this piece message belongs to
            int blockIndex = Block.IndexOf(piece.Blocks, request.StartOffset, request.RequestLength);

            if (blockIndex == -1 || !peer.Equals(piece.Blocks[blockIndex].RequestedOff))
            {
                logger.InfoFormatted("Piece validation failed: {0}-{1}. {2} No block ", request.PieceIndex, request.StartOffset, peer);
                return(false);
            }
            if (piece.Blocks[blockIndex].Received)
            {
                logger.InfoFormatted("Piece validation failed: {0}-{1}. {2} Already received.", request.PieceIndex, request.StartOffset, peer);
                return(false);
            }
            if (!piece.Blocks[blockIndex].Requested)
            {
                logger.InfoFormatted("Piece validation failed: {0}-{1}. {2} Not requested.", request.PieceIndex, request.StartOffset, peer);
                return(false);
            }
            peer.AmRequestingPiecesCount--;
            piece.Blocks[blockIndex].Received = true;

            if (piece.AllBlocksReceived)
            {
                pieceComplete = true;
                peersInvolved = piece.Blocks.Select(t => t.RequestedOff).Distinct().ToArray();
            }
            return(true);
        }
Example #3
0
        public void Add(IPeer peer)
        {
            lock (this)
            {
                if (!_container.Contains(peer) && !peer.Equals(Peer.Self))
                {
                    var lease = RemotingServices.GetLifetimeService((MarshalByRefObject)peer) as ILease;
                    if (lease != null)
                    {
                        lease.Register(new SearchEngineSponsor());
                    }

                    _container.Add(peer);
                    try
                    {
                        foreach (var peer1 in peer.PeerContainer.GetAvailablePeers())
                        {
                            Add(peer1);
                        }
                    }
                    catch (RemotingException)
                    {
                        _container.Remove(peer);
                    }
                    catch (WebException)
                    {
                        _container.Remove(peer);
                    }
                }
            }
        }