-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #180 from cryptix/versionHandshake
Version handshake
- Loading branch information
Showing
6 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
all: semver.pb.go | ||
|
||
semver.pb.go: semver.proto | ||
protoc --gogo_out=. --proto_path=../../../../../:/usr/local/opt/protobuf/include:. $< | ||
|
||
clean: | ||
rm semver.pb.go |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package handshake; | ||
|
||
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. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package handshake | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
updates "github.com/jbenet/go-ipfs/updates" | ||
|
||
semver "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 *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)) | ||
} | ||
} | ||
|
||
// CurrentHandshake returns the current protocol version as a protobuf message | ||
func CurrentHandshake() *Handshake1 { | ||
return NewHandshake1(currentVersion.String(), "go-ipfs/"+updates.Version) | ||
} | ||
|
||
// 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 | ||
// 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 | ||
} | ||
|
||
// NewHandshake1 creates a new Handshake1 from the two strings | ||
func NewHandshake1(protoVer, agentVer string) *Handshake1 { | ||
return &Handshake1{ | ||
ProtocolVersion: &protoVer, | ||
AgentVersion: &agentVer, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package handshake | ||
|
||
import "testing" | ||
|
||
func TestCompatible(t *testing.T) { | ||
tcases := []struct { | ||
a, b string | ||
expected error | ||
}{ | ||
{"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(NewHandshake1(tcase.a, ""), NewHandshake1(tcase.b, "")) != tcase.expected { | ||
t.Fatalf("case[%d] failed", i) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters