github.com/clem109/go-ethereum@v1.8.3-0.20180316121352-fe6cf00f480a/p2p/protocol.go (about)

     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  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     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  // GNU Lesser General Public License for more details.
    13  //
    14  // 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  
    17  package p2p
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/ethereum/go-ethereum/p2p/discover"
    23  )
    24  
    25  // Protocol represents a P2P subprotocol implementation.
    26  type Protocol struct {
    27  	// Name should contain the official protocol name,
    28  	// often a three-letter word.
    29  	Name string
    30  
    31  	// Version should contain the version number of the protocol.
    32  	Version uint
    33  
    34  	// Length should contain the number of message codes used
    35  	// by the protocol.
    36  	Length uint64
    37  
    38  	// Run is called in a new groutine when the protocol has been
    39  	// negotiated with a peer. It should read and write messages from
    40  	// rw. The Payload for each message must be fully consumed.
    41  	//
    42  	// The peer connection is closed when Start returns. It should return
    43  	// any protocol-level error (such as an I/O error) that is
    44  	// encountered.
    45  	Run func(peer *Peer, rw MsgReadWriter) error
    46  
    47  	// NodeInfo is an optional helper method to retrieve protocol specific metadata
    48  	// about the host node.
    49  	NodeInfo func() interface{}
    50  
    51  	// PeerInfo is an optional helper method to retrieve protocol specific metadata
    52  	// about a certain peer in the network. If an info retrieval function is set,
    53  	// but returns nil, it is assumed that the protocol handshake is still running.
    54  	PeerInfo func(id discover.NodeID) interface{}
    55  }
    56  
    57  func (p Protocol) cap() Cap {
    58  	return Cap{p.Name, p.Version}
    59  }
    60  
    61  // Cap is the structure of a peer capability.
    62  type Cap struct {
    63  	Name    string
    64  	Version uint
    65  }
    66  
    67  func (cap Cap) RlpData() interface{} {
    68  	return []interface{}{cap.Name, cap.Version}
    69  }
    70  
    71  func (cap Cap) String() string {
    72  	return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
    73  }
    74  
    75  type capsByNameAndVersion []Cap
    76  
    77  func (cs capsByNameAndVersion) Len() int      { return len(cs) }
    78  func (cs capsByNameAndVersion) Swap(i, j int) { cs[i], cs[j] = cs[j], cs[i] }
    79  func (cs capsByNameAndVersion) Less(i, j int) bool {
    80  	return cs[i].Name < cs[j].Name || (cs[i].Name == cs[j].Name && cs[i].Version < cs[j].Version)
    81  }