github.com/theQRL/go-zond@v0.1.1/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  	"strings"
    22  
    23  	"github.com/theQRL/go-zond/p2p/enode"
    24  	"github.com/theQRL/go-zond/p2p/enr"
    25  )
    26  
    27  // Protocol represents a P2P subprotocol implementation.
    28  type Protocol struct {
    29  	// Name should contain the official protocol name,
    30  	// often a three-letter word.
    31  	Name string
    32  
    33  	// Version should contain the version number of the protocol.
    34  	Version uint
    35  
    36  	// Length should contain the number of message codes used
    37  	// by the protocol.
    38  	Length uint64
    39  
    40  	// Run is called in a new goroutine when the protocol has been
    41  	// negotiated with a peer. It should read and write messages from
    42  	// rw. The Payload for each message must be fully consumed.
    43  	//
    44  	// The peer connection is closed when Start returns. It should return
    45  	// any protocol-level error (such as an I/O error) that is
    46  	// encountered.
    47  	Run func(peer *Peer, rw MsgReadWriter) error
    48  
    49  	// NodeInfo is an optional helper method to retrieve protocol specific metadata
    50  	// about the host node.
    51  	NodeInfo func() interface{}
    52  
    53  	// PeerInfo is an optional helper method to retrieve protocol specific metadata
    54  	// about a certain peer in the network. If an info retrieval function is set,
    55  	// but returns nil, it is assumed that the protocol handshake is still running.
    56  	PeerInfo func(id enode.ID) interface{}
    57  
    58  	// DialCandidates, if non-nil, is a way to tell Server about protocol-specific nodes
    59  	// that should be dialed. The server continuously reads nodes from the iterator and
    60  	// attempts to create connections to them.
    61  	DialCandidates enode.Iterator
    62  
    63  	// Attributes contains protocol specific information for the node record.
    64  	Attributes []enr.Entry
    65  }
    66  
    67  func (p Protocol) cap() Cap {
    68  	return Cap{p.Name, p.Version}
    69  }
    70  
    71  // Cap is the structure of a peer capability.
    72  type Cap struct {
    73  	Name    string
    74  	Version uint
    75  }
    76  
    77  func (cap Cap) String() string {
    78  	return fmt.Sprintf("%s/%d", cap.Name, cap.Version)
    79  }
    80  
    81  // Cmp defines the canonical sorting order of capabilities.
    82  func (cap Cap) Cmp(other Cap) int {
    83  	if cap.Name == other.Name {
    84  		if cap.Version < other.Version {
    85  			return -1
    86  		}
    87  		if cap.Version > other.Version {
    88  			return 1
    89  		}
    90  		return 0
    91  	}
    92  	return strings.Compare(cap.Name, other.Name)
    93  }