github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/p2p/protocol.go (about)

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