提交 67e04f0d 作者: Henry

changed message from SemVer to Handshake1

上级 6f8a89cd
...@@ -9,47 +9,42 @@ It is generated from these files: ...@@ -9,47 +9,42 @@ It is generated from these files:
semver.proto semver.proto
It has these top-level messages: It has these top-level messages:
SemVer Handshake1
*/ */
package handshake package handshake
import proto "code.google.com/p/gogoprotobuf/proto" import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/proto"
import math "math" import math "math"
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal var _ = proto.Marshal
var _ = math.Inf var _ = math.Inf
type SemVer struct { type Handshake1 struct {
Major *int64 `protobuf:"varint,1,opt,name=major" json:"major,omitempty"` // protocolVersion determines compatibility between peers
Minor *int64 `protobuf:"varint,2,opt,name=minor" json:"minor,omitempty"` ProtocolVersion *string `protobuf:"bytes,1,opt,name=protocolVersion" json:"protocolVersion,omitempty"`
Patch *int64 `protobuf:"varint,3,opt,name=patch" json:"patch,omitempty"` // agentVersion is like a UserAgent string in browsers, or client version in bittorrent
XXX_unrecognized []byte `json:"-"` // includes the client name and client. e.g. "go-ipfs/0.1.0"
AgentVersion *string `protobuf:"bytes,2,opt,name=agentVersion" json:"agentVersion,omitempty"`
XXX_unrecognized []byte `json:"-"`
} }
func (m *SemVer) Reset() { *m = SemVer{} } func (m *Handshake1) Reset() { *m = Handshake1{} }
func (m *SemVer) String() string { return proto.CompactTextString(m) } func (m *Handshake1) String() string { return proto.CompactTextString(m) }
func (*SemVer) ProtoMessage() {} func (*Handshake1) ProtoMessage() {}
func (m *SemVer) GetMajor() int64 { func (m *Handshake1) GetProtocolVersion() string {
if m != nil && m.Major != nil { if m != nil && m.ProtocolVersion != nil {
return *m.Major return *m.ProtocolVersion
} }
return 0 return ""
} }
func (m *SemVer) GetMinor() int64 { func (m *Handshake1) GetAgentVersion() string {
if m != nil && m.Minor != nil { if m != nil && m.AgentVersion != nil {
return *m.Minor return *m.AgentVersion
} }
return 0 return ""
}
func (m *SemVer) GetPatch() int64 {
if m != nil && m.Patch != nil {
return *m.Patch
}
return 0
} }
func init() { func init() {
......
package handshake; package handshake;
message SemVer { message Handshake1 {
optional int64 major = 1; // protocolVersion determines compatibility between peers
optional int64 minor = 2; optional string protocolVersion = 1; // semver
optional int64 patch = 3;
// BUG(cryptix): do we need PreRelease and Metadata too? // agentVersion is like a UserAgent string in browsers, or client version in bittorrent
// includes the client name and client. e.g. "go-ipfs/0.1.0"
optional string agentVersion = 2; // semver
// we'll have more fields here later.
} }
package handshake package handshake
import (
"errors"
"fmt"
"github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
)
// currentVersion holds the current protocol version for a client running this code // currentVersion holds the current protocol version for a client running this code
var currentVersion = NewSemVer(0, 0, 1) var currentVersion *semver.Version
func init() {
var err error
currentVersion, err = semver.NewVersion("0.0.1")
if err != nil {
panic(fmt.Errorf("invalid protocol version: %v", err))
}
}
// Current returns the current protocol version as a protobuf message // Current returns the current protocol version as a protobuf message
func Current() *SemVer { func Current() *semver.Version {
return currentVersion return currentVersion
} }
// ErrVersionMismatch is returned when two clients don't share a protocol version
var ErrVersionMismatch = errors.New("protocol missmatch")
// Compatible checks wether two versions are compatible // Compatible checks wether two versions are compatible
func Compatible(a, b *SemVer) bool { // returns nil if they are fine
return *a.Major == *b.Major // protobuf fields are pointers func Compatible(handshakeA, handshakeB *Handshake1) error {
a, err := semver.NewVersion(*handshakeA.ProtocolVersion)
if err != nil {
return err
}
b, err := semver.NewVersion(*handshakeB.ProtocolVersion)
if err != nil {
return err
}
if a.Major != b.Major {
return ErrVersionMismatch
}
return nil
} }
// NewSemVer constructs a new protobuf SemVer // NewHandshake1 creates a new Handshake1 from the two strings
func NewSemVer(major, minor, patch int64) *SemVer { func NewHandshake1(protoVer, agentVer string) *Handshake1 {
s := new(SemVer) return &Handshake1{
s.Major = &major ProtocolVersion: &protoVer,
s.Minor = &minor AgentVersion: &agentVer,
s.Patch = &patch }
return s
} }
...@@ -4,17 +4,19 @@ import "testing" ...@@ -4,17 +4,19 @@ import "testing"
func TestCompatible(t *testing.T) { func TestCompatible(t *testing.T) {
tcases := []struct { tcases := []struct {
a, b *SemVer a, b string
expected bool expected error
}{ }{
{NewSemVer(0, 0, 0), NewSemVer(0, 0, 0), true}, {"0.0.0", "0.0.0", nil},
{NewSemVer(0, 0, 0), NewSemVer(1, 0, 0), false}, {"1.0.0", "1.1.0", nil},
{NewSemVer(1, 0, 0), NewSemVer(0, 0, 0), false}, {"1.0.0", "1.0.1", nil},
{NewSemVer(1, 0, 0), NewSemVer(1, 0, 0), true}, {"0.0.0", "1.0.0", ErrVersionMismatch},
{"1.0.0", "0.0.0", ErrVersionMismatch},
} }
for i, tcase := range tcases { for i, tcase := range tcases {
if Compatible(tcase.a, tcase.b) != tcase.expected {
if Compatible(NewHandshake1(tcase.a, ""), NewHandshake1(tcase.b, "")) != tcase.expected {
t.Fatalf("case[%d] failed", i) t.Fatalf("case[%d] failed", i)
} }
} }
......
...@@ -217,7 +217,7 @@ func (s *Swarm) connVersionExchange(remote *conn.Conn) error { ...@@ -217,7 +217,7 @@ func (s *Swarm) connVersionExchange(remote *conn.Conn) error {
if !handshake.Compatible(myVersion, remoteVersion) { if !handshake.Compatible(myVersion, remoteVersion) {
remote.Close() remote.Close()
return errors.New("protocol missmatch") return handshake.ErrVersionMismatch
} }
log.Debug("[peer: %s] Version compatible", remote.Peer) log.Debug("[peer: %s] Version compatible", remote.Peer)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论