diff options
author | obscuren <geffobscura@gmail.com> | 2014-07-04 06:12:28 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2014-07-04 06:12:28 +0800 |
commit | db60ebbbd72c5cfb852b37c8b9a37a7d81d79fb0 (patch) | |
tree | fea61eddff425f3d48c0e7bfdc6e1a1a2e0ad86e /ethwire | |
parent | cb7ebdf821adb4b022adcaea0973c8c7da2e2923 (diff) | |
parent | f02602d02d12204a10c2aa9d1d43332b1be2fe0a (diff) | |
download | dexon-db60ebbbd72c5cfb852b37c8b9a37a7d81d79fb0.tar.gz dexon-db60ebbbd72c5cfb852b37c8b9a37a7d81d79fb0.tar.zst dexon-db60ebbbd72c5cfb852b37c8b9a37a7d81d79fb0.zip |
Merge branch 'feature/clientid' of https://github.com/ethersphere/eth-go into ethersphere-feature/clientid
Diffstat (limited to 'ethwire')
-rw-r--r-- | ethwire/client_identity.go | 54 | ||||
-rw-r--r-- | ethwire/client_identity_test.go | 30 |
2 files changed, 84 insertions, 0 deletions
diff --git a/ethwire/client_identity.go b/ethwire/client_identity.go new file mode 100644 index 000000000..e803406d8 --- /dev/null +++ b/ethwire/client_identity.go @@ -0,0 +1,54 @@ +package ethwire + +import ( + "fmt" + "runtime" +) + +// should be used in Peer handleHandshake, incorporate Caps, ProtocolVersion, Pubkey etc. +type ClientIdentity interface { + String() string +} + +type SimpleClientIdentity struct { + clientString string + clientIdentifier string + version string + customIdentifier string + os string + implementation string +} + +func NewSimpleClientIdentity(clientIdentifier string, version string, customIdentifier string) *SimpleClientIdentity { + clientIdentity := &SimpleClientIdentity{ + clientIdentifier: clientIdentifier, + version: version, + customIdentifier: customIdentifier, + os: runtime.GOOS, + implementation: "Go", + } + clientIdentity.init() + return clientIdentity +} + +func (c *SimpleClientIdentity) init() { + c.clientString = fmt.Sprintf("%s/v%s/%s/%s/%s", + c.clientIdentifier, + c.version, + c.customIdentifier, + c.os, + c.implementation) +} + +func (c *SimpleClientIdentity) String() string { + return c.clientString +} + +func (c *SimpleClientIdentity) SetCustomIdentifier(customIdentifier string) { + c.customIdentifier = customIdentifier + c.init() +} + +func (c *SimpleClientIdentity) GetCustomIdentifier() string { + return c.customIdentifier +} diff --git a/ethwire/client_identity_test.go b/ethwire/client_identity_test.go new file mode 100644 index 000000000..f3c8bfd50 --- /dev/null +++ b/ethwire/client_identity_test.go @@ -0,0 +1,30 @@ +package ethwire + +import ( + "fmt" + "runtime" + "testing" +) + +func TestClientIdentity(t *testing.T) { + clientIdentity := NewSimpleClientIdentity("Ethereum(G)", "0.5.16", "test") + clientString := clientIdentity.String() + expected := fmt.Sprintf("Ethereum(G)/v0.5.16/test/%s/Go", runtime.GOOS) + if clientString != expected { + t.Error("Expected clientIdentity to be %v, got %v", expected, clientString) + } + customIdentifier := clientIdentity.GetCustomIdentifier() + if customIdentifier != "test" { + t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test', got %v", customIdentifier) + } + clientIdentity.SetCustomIdentifier("test2") + customIdentifier = clientIdentity.GetCustomIdentifier() + if customIdentifier != "test2" { + t.Error("Expected clientIdentity.GetCustomIdentifier() to be 'test2', got %v", customIdentifier) + } + clientString = clientIdentity.String() + expected = fmt.Sprintf("Ethereum(G)/v0.5.16/test2/%s/Go", runtime.GOOS) + if clientString != expected { + t.Error("Expected clientIdentity to be %v, got %v", expected, clientString) + } +} |