github.com/zignig/go-ipfs@v0.0.0-20141111235910-c9e5fdf55a52/net/handshake/handshake1.go (about)

     1  package handshake
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	config "github.com/jbenet/go-ipfs/config"
     8  	pb "github.com/jbenet/go-ipfs/net/handshake/pb"
     9  
    10  	semver "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
    11  )
    12  
    13  // ipfsVersion holds the current protocol version for a client running this code
    14  var ipfsVersion *semver.Version
    15  var clientVersion = "go-ipfs/" + config.CurrentVersionNumber
    16  
    17  func init() {
    18  	var err error
    19  	ipfsVersion, err = semver.NewVersion("0.0.1")
    20  	if err != nil {
    21  		panic(fmt.Errorf("invalid protocol version: %v", err))
    22  	}
    23  }
    24  
    25  // Handshake1Msg returns the current protocol version as a protobuf message
    26  func Handshake1Msg() *pb.Handshake1 {
    27  	return NewHandshake1(ipfsVersion.String(), clientVersion)
    28  }
    29  
    30  // ErrVersionMismatch is returned when two clients don't share a protocol version
    31  var ErrVersionMismatch = errors.New("protocol missmatch")
    32  
    33  // Handshake1Compatible checks whether two versions are compatible
    34  // returns nil if they are fine
    35  func Handshake1Compatible(handshakeA, handshakeB *pb.Handshake1) error {
    36  	a, err := semver.NewVersion(*handshakeA.ProtocolVersion)
    37  	if err != nil {
    38  		return err
    39  	}
    40  	b, err := semver.NewVersion(*handshakeB.ProtocolVersion)
    41  	if err != nil {
    42  		return err
    43  	}
    44  
    45  	if a.Major != b.Major {
    46  		return ErrVersionMismatch
    47  	}
    48  
    49  	return nil
    50  }
    51  
    52  // NewHandshake1 creates a new Handshake1 from the two strings
    53  func NewHandshake1(protoVer, agentVer string) *pb.Handshake1 {
    54  	return &pb.Handshake1{
    55  		ProtocolVersion: &protoVer,
    56  		AgentVersion:    &agentVer,
    57  	}
    58  }