@cryptotaxi247 / kubo / commits / 67e04f0d2

changed message from SemVer to Handshake1

Henry committed Oct 16, 2014 at 13:57 UTC 67e04f0d2950fe0498ee1447735e839a2e33b1d8
5 files changed +81 -49
net/handshake/semver.pb.go
+20 -25
@@ -9,47 +9,42 @@ It is generated from these files:
9 semver.proto
10
11 It has these top-level messages:
12 - SemVer
12 + Handshake1
13 */
14 package handshake
15
16 -import proto "code.google.com/p/gogoprotobuf/proto"
16 +import proto "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/gogoprotobuf/proto"
17 import math "math"
18
19 // Reference imports to suppress errors if they are not otherwise used.
20 var _ = proto.Marshal
21 var _ = math.Inf
22
23 -type SemVer struct {
24 - Major *int64 `protobuf:"varint,1,opt,name=major" json:"major,omitempty"`
25 - Minor *int64 `protobuf:"varint,2,opt,name=minor" json:"minor,omitempty"`
26 - Patch *int64 `protobuf:"varint,3,opt,name=patch" json:"patch,omitempty"`
27 - XXX_unrecognized []byte `json:"-"`
23 +type Handshake1 struct {
24 + // protocolVersion determines compatibility between peers
25 + ProtocolVersion *string `protobuf:"bytes,1,opt,name=protocolVersion" json:"protocolVersion,omitempty"`
26 + // agentVersion is like a UserAgent string in browsers, or client version in bittorrent
27 + // includes the client name and client. e.g. "go-ipfs/0.1.0"
28 + AgentVersion *string `protobuf:"bytes,2,opt,name=agentVersion" json:"agentVersion,omitempty"`
29 + XXX_unrecognized []byte `json:"-"`
30 }
31
30 -func (m *SemVer) Reset() { *m = SemVer{} }
31 -func (m *SemVer) String() string { return proto.CompactTextString(m) }
32 -func (*SemVer) ProtoMessage() {}
32 +func (m *Handshake1) Reset() { *m = Handshake1{} }
33 +func (m *Handshake1) String() string { return proto.CompactTextString(m) }
34 +func (*Handshake1) ProtoMessage() {}
35
34 -func (m *SemVer) GetMajor() int64 {
35 - if m != nil && m.Major != nil {
36 - return *m.Major
36 +func (m *Handshake1) GetProtocolVersion() string {
37 + if m != nil && m.ProtocolVersion != nil {
38 + return *m.ProtocolVersion
39 }
38 - return 0
40 + return ""
41 }
42
41 -func (m *SemVer) GetMinor() int64 {
42 - if m != nil && m.Minor != nil {
43 - return *m.Minor
43 +func (m *Handshake1) GetAgentVersion() string {
44 + if m != nil && m.AgentVersion != nil {
45 + return *m.AgentVersion
46 }
45 - return 0
46 -}
47 -
48 -func (m *SemVer) GetPatch() int64 {
49 - if m != nil && m.Patch != nil {
50 - return *m.Patch
51 - }
52 - return 0
47 + return ""
48 }
49
50 func init() {
net/handshake/semver.proto
+9 -5
@@ -1,8 +1,12 @@
1 package handshake;
2
3 -message SemVer {
4 - optional int64 major = 1;
5 - optional int64 minor = 2;
6 - optional int64 patch = 3;
7 - // BUG(cryptix): do we need PreRelease and Metadata too?
3 +message Handshake1 {
4 + // protocolVersion determines compatibility between peers
5 + optional string protocolVersion = 1; // semver
6 +
7 + // agentVersion is like a UserAgent string in browsers, or client version in bittorrent
8 + // includes the client name and client. e.g. "go-ipfs/0.1.0"
9 + optional string agentVersion = 2; // semver
10 +
11 + // we'll have more fields here later.
12 }
net/handshake/version.go
+42 -11
@@ -1,23 +1,54 @@
1 package handshake
2
3 +import (
4 + "errors"
5 + "fmt"
6 +
7 + "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
8 +)
9 +
10 // currentVersion holds the current protocol version for a client running this code
4 -var currentVersion = NewSemVer(0, 0, 1)
11 +var currentVersion *semver.Version
12 +
13 +func init() {
14 + var err error
15 + currentVersion, err = semver.NewVersion("0.0.1")
16 + if err != nil {
17 + panic(fmt.Errorf("invalid protocol version: %v", err))
18 + }
19 +}
20
21 // Current returns the current protocol version as a protobuf message
7 -func Current() *SemVer {
22 +func Current() *semver.Version {
23 return currentVersion
24 }
25
26 +// ErrVersionMismatch is returned when two clients don't share a protocol version
27 +var ErrVersionMismatch = errors.New("protocol missmatch")
28 +
29 // Compatible checks wether two versions are compatible
12 -func Compatible(a, b *SemVer) bool {
13 - return *a.Major == *b.Major // protobuf fields are pointers
30 +// returns nil if they are fine
31 +func Compatible(handshakeA, handshakeB *Handshake1) error {
32 + a, err := semver.NewVersion(*handshakeA.ProtocolVersion)
33 + if err != nil {
34 + return err
35 + }
36 + b, err := semver.NewVersion(*handshakeB.ProtocolVersion)
37 + if err != nil {
38 + return err
39 + }
40 +
41 + if a.Major != b.Major {
42 + return ErrVersionMismatch
43 + }
44 +
45 + return nil
46 }
47
16 -// NewSemVer constructs a new protobuf SemVer
17 -func NewSemVer(major, minor, patch int64) *SemVer {
18 - s := new(SemVer)
19 - s.Major = &major
20 - s.Minor = &minor
21 - s.Patch = &patch
22 - return s
48 +// NewHandshake1 creates a new Handshake1 from the two strings
49 +func NewHandshake1(protoVer, agentVer string) *Handshake1 {
50 + return &Handshake1{
51 + ProtocolVersion: &protoVer,
52 + AgentVersion: &agentVer,
53 + }
54 }
net/handshake/version_test.go
+9 -7
@@ -4,17 +4,19 @@ import "testing"
4
5 func TestCompatible(t *testing.T) {
6 tcases := []struct {
7 - a, b *SemVer
8 - expected bool
7 + a, b string
8 + expected error
9 }{
10 - {NewSemVer(0, 0, 0), NewSemVer(0, 0, 0), true},
11 - {NewSemVer(0, 0, 0), NewSemVer(1, 0, 0), false},
12 - {NewSemVer(1, 0, 0), NewSemVer(0, 0, 0), false},
13 - {NewSemVer(1, 0, 0), NewSemVer(1, 0, 0), true},
10 + {"0.0.0", "0.0.0", nil},
11 + {"1.0.0", "1.1.0", nil},
12 + {"1.0.0", "1.0.1", nil},
13 + {"0.0.0", "1.0.0", ErrVersionMismatch},
14 + {"1.0.0", "0.0.0", ErrVersionMismatch},
15 }
16
17 for i, tcase := range tcases {
17 - if Compatible(tcase.a, tcase.b) != tcase.expected {
18 +
19 + if Compatible(NewHandshake1(tcase.a, ""), NewHandshake1(tcase.b, "")) != tcase.expected {
20 t.Fatalf("case[%d] failed", i)
21 }
22 }
net/swarm/conn.go
+1 -1
@@ -217,7 +217,7 @@ func (s *Swarm) connVersionExchange(remote *conn.Conn) error {
217
218 if !handshake.Compatible(myVersion, remoteVersion) {
219 remote.Close()
220 - return errors.New("protocol missmatch")
220 + return handshake.ErrVersionMismatch
221 }
222
223 log.Debug("[peer: %s] Version compatible", remote.Peer)