2015-07-06 19:54:22 -05:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 11:48:40 -05:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-06 19:54:22 -05:00
|
|
|
//
|
2015-07-23 11:35:11 -05:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-06 19:54:22 -05:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 11:48:40 -05:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-06 19:54:22 -05:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 11:48:40 -05:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-06 19:54:22 -05:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 11:48:40 -05:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-06 19:54:22 -05:00
|
|
|
|
2014-10-23 10:57:54 -05:00
|
|
|
package p2p
|
|
|
|
|
2015-02-13 16:54:34 -06:00
|
|
|
import "fmt"
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
// Protocol represents a P2P subprotocol implementation.
|
|
|
|
type Protocol struct {
|
|
|
|
// Name should contain the official protocol name,
|
|
|
|
// often a three-letter word.
|
|
|
|
Name string
|
2014-11-04 06:21:44 -06:00
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
// Version should contain the version number of the protocol.
|
|
|
|
Version uint
|
2014-11-04 06:21:44 -06:00
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
// Length should contain the number of message codes used
|
|
|
|
// by the protocol.
|
|
|
|
Length uint64
|
2014-11-04 06:21:44 -06:00
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
// Run is called in a new groutine when the protocol has been
|
|
|
|
// 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
|
2014-11-04 06:21:44 -06:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
func (p Protocol) cap() Cap {
|
|
|
|
return Cap{p.Name, p.Version}
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
// Cap is the structure of a peer capability.
|
|
|
|
type Cap struct {
|
|
|
|
Name string
|
|
|
|
Version uint
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:48:49 -06:00
|
|
|
func (cap Cap) RlpData() interface{} {
|
|
|
|
return []interface{}{cap.Name, cap.Version}
|
2014-10-23 10:57:54 -05:00
|
|
|
}
|
|
|
|
|
2015-02-13 16:54:34 -06:00
|
|
|
func (cap Cap) String() string {
|
|
|
|
return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
|
|
|
|
}
|
|
|
|
|
2015-06-26 07:48:50 -05:00
|
|
|
type capsByNameAndVersion []Cap
|
2014-11-21 14:48:49 -06:00
|
|
|
|
2015-06-26 07:48:50 -05:00
|
|
|
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)
|
|
|
|
}
|