compare versions for compatibility

This commit is contained in:
Henry 2014-10-16 12:09:43 +02:00
parent 0075a352da
commit 2a5b3eaa71
2 changed files with 18 additions and 4 deletions

View File

@ -164,6 +164,7 @@ func (s *Swarm) connVersionExchange(remote *conn.Conn) error {
var remoteVersion, myVersion *version.SemVer
myVersion = version.Current()
// BUG(cryptix): do we need to use a NetMessage here?
myVersionMsg, err := msg.FromObject(s.local, myVersion)
if err != nil {
return fmt.Errorf("connVersionExchange: could not prepare local version: %q", err)
@ -215,7 +216,12 @@ func (s *Swarm) connVersionExchange(remote *conn.Conn) error {
}
}
return errors.New("not yet")
if !version.Compatible(myVersion, remoteVersion) {
remote.Close()
return errors.New("protocol missmatch")
}
return nil
}
// Handles the unwrapping + sending of messages to the right connection.

View File

@ -2,7 +2,7 @@ package version
import semver "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/coreos/go-semver/semver"
var currentVersion = semver.Version{
var currentVersion = &semver.Version{
Major: 0,
Minor: 1,
Patch: 0,
@ -13,8 +13,16 @@ func Current() *SemVer {
return toPBSemVer(currentVersion)
}
// Compatible checks wether two versions are compatible
func Compatible(a, b *SemVer) bool {
aConv := fromPBSemVer(a)
bConv := fromPBSemVer(b)
return aConv.LessThan(*bConv)
}
// toPBSemVar converts a coreos/semver to our protobuf SemVer
func toPBSemVer(in semver.Version) (out *SemVer) {
func toPBSemVer(in *semver.Version) (out *SemVer) {
return &SemVer{
Major: &in.Major,
Minor: &in.Minor,
@ -23,7 +31,7 @@ func toPBSemVer(in semver.Version) (out *SemVer) {
}
// toPBSemVar converts our protobuf SemVer to a coreos/semver
func fromPBSemVer(in SemVer) *semver.Version {
func fromPBSemVer(in *SemVer) *semver.Version {
return &semver.Version{
Major: *in.Major,
Minor: *in.Minor,