1
// Copyright (c) 2016-2019 Uber Technologies, Inc.
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
7
// http://www.apache.org/licenses/LICENSE-2.0
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
18
// PeerInfo defines peer metadata scoped to a torrent.
20
PeerID PeerID `json:"peer_id"`
22
Port int `json:"port"`
23
Origin bool `json:"origin"`
24
Complete bool `json:"complete"`
27
// NewPeerInfo creates a new PeerInfo.
33
complete bool) *PeerInfo {
44
// PeerInfoFromContext derives PeerInfo from a PeerContext.
45
func PeerInfoFromContext(pctx PeerContext, complete bool) *PeerInfo {
46
return NewPeerInfo(pctx.PeerID, pctx.IP, pctx.Port, pctx.Origin, complete)
49
// PeerInfos groups PeerInfo structs for sorting.
50
type PeerInfos []*PeerInfo
53
func (s PeerInfos) Len() int { return len(s) }
56
func (s PeerInfos) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
58
// PeersByPeerID sorts PeerInfos by peer id.
59
type PeersByPeerID struct{ PeerInfos }
62
func (s PeersByPeerID) Less(i, j int) bool {
63
return s.PeerInfos[i].PeerID.LessThan(s.PeerInfos[j].PeerID)
66
// SortedByPeerID returns a copy of peers which has been sorted by peer id.
67
func SortedByPeerID(peers []*PeerInfo) []*PeerInfo {
68
c := make([]*PeerInfo, len(peers))
70
sort.Sort(PeersByPeerID{PeerInfos(c)})