Commit 67e04f0d authored by Henry's avatar Henry

changed message from SemVer to Handshake1

parent 6f8a89cd
......@@ -9,47 +9,42 @@ It is generated from these files:
semver.proto
It has these top-level messages:
SemVer
Handshake1
*/
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"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = math.Inf
type SemVer struct {
Major *int64 `protobuf:"varint,1,opt,name=major" json:"major,omitempty"`
Minor *int64 `protobuf:"varint,2,opt,name=minor" json:"minor,omitempty"`
Patch *int64 `protobuf:"varint,3,opt,name=patch" json:"patch,omitempty"`
XXX_unrecognized []byte `json:"-"`
type Handshake1 struct {
// protocolVersion determines compatibility between peers
ProtocolVersion *string `protobuf:"bytes,1,opt,name=protocolVersion" json:"protocolVersion,omitempty"`
// 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"
AgentVersion *string `protobuf:"bytes,2,opt,name=agentVersion" json:"agentVersion,omitempty"`
XXX_unrecognized []byte `json:"-"`
}
func (m *SemVer) Reset() { *m = SemVer{} }
func (m *SemVer) String() string { return proto.CompactTextString(m) }
func (*SemVer) ProtoMessage() {}
func (m *Handshake1) Reset() { *m = Handshake1{} }
func (m *Handshake1) String() string { return proto.CompactTextString(m) }
func (*Handshake1) ProtoMessage() {}
func (m *SemVer) GetMajor() int64 {
if m != nil && m.Major != nil {
return *m.Major
func (m *Handshake1) GetProtocolVersion() string {
if m != nil && m.ProtocolVersion != nil {
return *m.ProtocolVersion
}
return 0
return ""
}
func (m *SemVer) GetMinor() int64 {
if m != nil && m.Minor != nil {
return *m.Minor
func (m *Handshake1) GetAgentVersion() string {
if m != nil && m.AgentVersion != nil {
return *m.AgentVersion
}
return 0
}
func (m *SemVer) GetPatch() int64 {
if m != nil && m.Patch != nil {
return *m.Patch
}
return 0
return ""
}
func init() {
......
package handshake;
message SemVer {
optional int64 major = 1;
optional int64 minor = 2;
optional int64 patch = 3;
// BUG(cryptix): do we need PreRelease and Metadata too?
message Handshake1 {
// protocolVersion determines compatibility between peers
optional string protocolVersion = 1; // semver
// 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
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
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
func Current() *SemVer {
func Current() *semver.Version {
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
func Compatible(a, b *SemVer) bool {
return *a.Major == *b.Major // protobuf fields are pointers
// returns nil if they are fine
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
func NewSemVer(major, minor, patch int64) *SemVer {
s := new(SemVer)
s.Major = &major
s.Minor = &minor
s.Patch = &patch
return s
// NewHandshake1 creates a new Handshake1 from the two strings
func NewHandshake1(protoVer, agentVer string) *Handshake1 {
return &Handshake1{
ProtocolVersion: &protoVer,
AgentVersion: &agentVer,
}
}
......@@ -4,17 +4,19 @@ import "testing"
func TestCompatible(t *testing.T) {
tcases := []struct {
a, b *SemVer
expected bool
a, b string
expected error
}{
{NewSemVer(0, 0, 0), NewSemVer(0, 0, 0), true},
{NewSemVer(0, 0, 0), NewSemVer(1, 0, 0), false},
{NewSemVer(1, 0, 0), NewSemVer(0, 0, 0), false},
{NewSemVer(1, 0, 0), NewSemVer(1, 0, 0), true},
{"0.0.0", "0.0.0", nil},
{"1.0.0", "1.1.0", nil},
{"1.0.0", "1.0.1", nil},
{"0.0.0", "1.0.0", ErrVersionMismatch},
{"1.0.0", "0.0.0", ErrVersionMismatch},
}
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)
}
}
......
......@@ -217,7 +217,7 @@ func (s *Swarm) connVersionExchange(remote *conn.Conn) error {
if !handshake.Compatible(myVersion, remoteVersion) {
remote.Close()
return errors.New("protocol missmatch")
return handshake.ErrVersionMismatch
}
log.Debug("[peer: %s] Version compatible", remote.Peer)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment