2016-01-25 06:41:39 +01:00
|
|
|
// Copyright 2016 The Chihaya Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by the BSD 2-Clause license,
|
|
|
|
// which can be found in the LICENSE file.package middleware
|
|
|
|
|
|
|
|
package chihaya
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
2016-02-16 01:49:25 +01:00
|
|
|
|
|
|
|
"github.com/chihaya/chihaya/pkg/event"
|
2016-01-25 06:41:39 +01:00
|
|
|
)
|
|
|
|
|
2016-04-07 16:24:37 +02:00
|
|
|
// PeerID represents a peer ID.
|
2016-02-16 01:49:25 +01:00
|
|
|
type PeerID string
|
2016-04-07 16:24:37 +02:00
|
|
|
|
|
|
|
// InfoHash represents an infohash in hexadecimal notation.
|
2016-02-16 01:49:25 +01:00
|
|
|
type InfoHash string
|
|
|
|
|
2016-01-25 06:41:39 +01:00
|
|
|
// AnnounceRequest represents the parsed parameters from an announce request.
|
2016-02-16 01:49:25 +01:00
|
|
|
type AnnounceRequest struct {
|
|
|
|
Event event.Event
|
|
|
|
InfoHash InfoHash
|
|
|
|
PeerID PeerID
|
2016-02-17 23:39:21 +01:00
|
|
|
|
|
|
|
IPv4, IPv6 net.IP
|
|
|
|
Port uint16
|
2016-02-16 01:49:25 +01:00
|
|
|
|
|
|
|
Compact bool
|
2016-02-19 14:15:40 +01:00
|
|
|
NumWant int32
|
2016-02-16 01:49:25 +01:00
|
|
|
|
|
|
|
Left, Downloaded, Uploaded uint64
|
|
|
|
|
|
|
|
Params Params
|
|
|
|
}
|
2016-01-25 06:41:39 +01:00
|
|
|
|
|
|
|
// AnnounceResponse represents the parameters used to create an announce
|
|
|
|
// response.
|
|
|
|
type AnnounceResponse struct {
|
|
|
|
Compact bool
|
|
|
|
Complete int32
|
|
|
|
Incomplete int32
|
|
|
|
Interval time.Duration
|
|
|
|
MinInterval time.Duration
|
|
|
|
IPv4Peers []Peer
|
|
|
|
IPv6Peers []Peer
|
|
|
|
}
|
|
|
|
|
|
|
|
// ScrapeRequest represents the parsed parameters from a scrape request.
|
2016-02-16 01:49:25 +01:00
|
|
|
type ScrapeRequest struct {
|
|
|
|
InfoHashes []InfoHash
|
|
|
|
Params Params
|
|
|
|
}
|
2016-01-25 06:41:39 +01:00
|
|
|
|
|
|
|
// ScrapeResponse represents the parameters used to create a scrape response.
|
|
|
|
type ScrapeResponse struct {
|
2016-04-03 02:22:15 +02:00
|
|
|
Files map[InfoHash]Scrape
|
2016-01-25 06:41:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scrape represents the state of a swarm that is returned in a scrape response.
|
|
|
|
type Scrape struct {
|
|
|
|
Complete int32
|
|
|
|
Incomplete int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peer represents the connection details of a peer that is returned in an
|
|
|
|
// announce response.
|
|
|
|
type Peer struct {
|
2016-02-16 01:49:25 +01:00
|
|
|
ID PeerID
|
2016-01-25 06:41:39 +01:00
|
|
|
IP net.IP
|
|
|
|
Port uint16
|
|
|
|
}
|
2016-02-16 01:49:25 +01:00
|
|
|
|
|
|
|
// Params is used to fetch request parameters.
|
|
|
|
type Params interface {
|
|
|
|
String(key string) (string, error)
|
|
|
|
}
|
2016-03-31 23:47:06 +02:00
|
|
|
|
|
|
|
// Equal reports whether peer and x are the same.
|
|
|
|
func (peer *Peer) Equal(x *Peer) bool {
|
|
|
|
if peer.ID == x.ID && peer.Port == x.Port && peer.IP.Equal(x.IP) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|