Commit 78eb3493 authored by Henry's avatar Henry

fixed Compatible and added a small test case

parent 2a5b3eaa
......@@ -194,7 +194,7 @@ func (s *Swarm) connVersionExchange(remote *conn.Conn) error {
remote.Secure.Out <- our
sendMine = true
close(outBuf) // only send local version once
log.Debug("[peer: %s] Send my version(%s) to %s", s.local, myVersion, remote.Peer)
log.Debug("Send my version(%s) [to = %s]", myVersion, remote.Peer)
}
case data, ok := <-remote.Secure.In:
......@@ -202,8 +202,6 @@ func (s *Swarm) connVersionExchange(remote *conn.Conn) error {
return fmt.Errorf("Error retrieving from conn: %v", remote.Peer)
}
log.Debug("[peer: %s] Received message [from = %s]", s.local, remote.Peer)
remoteVersion = new(version.SemVer)
err = proto.Unmarshal(data, remoteVersion)
if err != nil {
......@@ -211,16 +209,18 @@ func (s *Swarm) connVersionExchange(remote *conn.Conn) error {
return fmt.Errorf("connSetup: could not decode remote version: %q", err)
}
gotTheirs = true
log.Debug("Received remote version(%s) [from = %s]", remoteVersion, remote.Peer)
// BUG(cryptix): could add another case here to trigger resending our version
}
}
if !version.Compatible(myVersion, remoteVersion) {
if !version.Compatible(myVersion.Convert(), remoteVersion.Convert()) {
remote.Close()
return errors.New("protocol missmatch")
}
log.Debug("[peer: %s] Version compatible", remote.Peer)
return nil
}
......
......@@ -14,10 +14,8 @@ func Current() *SemVer {
}
// Compatible checks wether two versions are compatible
func Compatible(a, b *SemVer) bool {
aConv := fromPBSemVer(a)
bConv := fromPBSemVer(b)
return aConv.LessThan(*bConv)
func Compatible(a, b semver.Version) bool {
return !a.LessThan(b)
}
// toPBSemVar converts a coreos/semver to our protobuf SemVer
......@@ -30,9 +28,9 @@ func toPBSemVer(in *semver.Version) (out *SemVer) {
}
}
// toPBSemVar converts our protobuf SemVer to a coreos/semver
func fromPBSemVer(in *SemVer) *semver.Version {
return &semver.Version{
// Convert our protobuf SemVer to a coreos/semver
func (in SemVer) Convert() semver.Version {
return semver.Version{
Major: *in.Major,
Minor: *in.Minor,
Patch: *in.Patch,
......
package version
import (
"testing"
semver "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
)
func TestCompatible(t *testing.T) {
tcases := []struct {
a, b semver.Version
expected bool
}{
{semver.Version{Major: 0}, semver.Version{Major: 0}, true},
{semver.Version{Major: 1}, semver.Version{Major: 0}, true},
{semver.Version{Major: 1}, semver.Version{Major: 1}, true},
{semver.Version{Major: 0}, semver.Version{Major: 1}, false},
}
for i, tcase := range tcases {
if Compatible(tcase.a, tcase.b) != tcase.expected {
t.Fatalf("case[%d] failed", i)
}
}
}
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