protocol.go 2.75 KB
Newer Older
1
// Copyright 2014 The go-ethereum Authors
2
// This file is part of the go-ethereum library.
3
//
4
// The go-ethereum library is free software: you can redistribute it and/or modify
5 6 7 8
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
9
// The go-ethereum library is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 13 14
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
15
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16

zelig's avatar
zelig committed
17 18
package p2p

19 20 21
import (
	"fmt"

22
	"github.com/ethereum/go-ethereum/p2p/enode"
23
	"github.com/ethereum/go-ethereum/p2p/enr"
24
)
25

26 27 28 29 30
// Protocol represents a P2P subprotocol implementation.
type Protocol struct {
	// Name should contain the official protocol name,
	// often a three-letter word.
	Name string
Felix Lange's avatar
Felix Lange committed
31

32 33
	// Version should contain the version number of the protocol.
	Version uint
Felix Lange's avatar
Felix Lange committed
34

35 36 37
	// Length should contain the number of message codes used
	// by the protocol.
	Length uint64
Felix Lange's avatar
Felix Lange committed
38

Wuxiang's avatar
Wuxiang committed
39
	// Run is called in a new goroutine when the protocol has been
40 41 42 43 44 45 46
	// negotiated with a peer. It should read and write messages from
	// rw. The Payload for each message must be fully consumed.
	//
	// The peer connection is closed when Start returns. It should return
	// any protocol-level error (such as an I/O error) that is
	// encountered.
	Run func(peer *Peer, rw MsgReadWriter) error
47 48 49 50 51 52 53 54

	// NodeInfo is an optional helper method to retrieve protocol specific metadata
	// about the host node.
	NodeInfo func() interface{}

	// PeerInfo is an optional helper method to retrieve protocol specific metadata
	// about a certain peer in the network. If an info retrieval function is set,
	// but returns nil, it is assumed that the protocol handshake is still running.
55
	PeerInfo func(id enode.ID) interface{}
56 57 58

	// Attributes contains protocol specific information for the node record.
	Attributes []enr.Entry
Felix Lange's avatar
Felix Lange committed
59 60
}

61 62
func (p Protocol) cap() Cap {
	return Cap{p.Name, p.Version}
zelig's avatar
zelig committed
63 64
}

65 66 67 68
// Cap is the structure of a peer capability.
type Cap struct {
	Name    string
	Version uint
zelig's avatar
zelig committed
69 70
}

71 72 73 74
func (cap Cap) String() string {
	return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
}

75
type capsByNameAndVersion []Cap
76

77 78 79 80 81
func (cs capsByNameAndVersion) Len() int      { return len(cs) }
func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
func (cs capsByNameAndVersion) Less(i, j int) bool {
	return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
}
82 83

func (capsByNameAndVersion) ENRKey() string { return "cap" }