Skip to content

Commit

Permalink
Send version info to rendezvous server.
Browse files Browse the repository at this point in the history
Include agent string and version when binding to the rendezvous
server. There's default values that can be overridden using
WithVersion() when instantiating the rendezvous client.

Fixes: #2
  • Loading branch information
psanford committed Sep 11, 2019
1 parent 6689c61 commit db5cc47
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 4 deletions.
34 changes: 30 additions & 4 deletions rendezvous/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/gorilla/websocket"
"github.com/psanford/wormhole-william/internal/crypto"
"github.com/psanford/wormhole-william/rendezvous/internal/msgs"
"github.com/psanford/wormhole-william/version"
)

// NewClient returns a Rendezvous client. URL is the websocket
Expand All @@ -21,8 +22,8 @@ import (
// AppID is the application identity string of the client.
//
// Two clients can only communicate if they have the same AppID.
func NewClient(url, sideID, appID string) *Client {
return &Client{
func NewClient(url, sideID, appID string, opts ...ClientOption) *Client {
c := &Client{
url: url,
sideID: sideID,
appID: appID,
Expand All @@ -33,6 +34,12 @@ func NewClient(url, sideID, appID string) *Client {

pendingMsgWaiters: make(map[uint32]chan uint32),
}

for _, opt := range opts {
opt.setValue(c)
}

return c
}

type pendingMsg struct {
Expand All @@ -52,6 +59,9 @@ type Client struct {

nameplate string

agentString string
agentVersion string

wsClient *websocket.Conn

mailboxMsgs []MailboxEvent
Expand Down Expand Up @@ -511,10 +521,26 @@ func (c *Client) prepareMsg(msg interface{}) (string, error) {
return id, nil
}

func (c *Client) agentID() (string, string) {
agent := c.agentString
if agent == "" {
agent = version.AgentString
}
v := c.agentVersion
if v == "" {
v = version.AgentVersion
}

return agent, v
}

func (c *Client) bind(ctx context.Context, side, appID string) error {
agent, version := c.agentID()

bind := msgs.Bind{
Side: side,
AppID: appID,
Side: side,
AppID: appID,
ClientVersion: []string{agent, version},
}

_, err := c.sendAndWait(ctx, &bind)
Expand Down
40 changes: 40 additions & 0 deletions rendezvous/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/psanford/wormhole-william/internal/crypto"
"github.com/psanford/wormhole-william/rendezvous/rendezvousservertest"
"github.com/psanford/wormhole-william/version"
)

func TestBasicClient(t *testing.T) {
Expand All @@ -30,6 +31,15 @@ func TestBasicClient(t *testing.T) {
t.Fatalf("MOTD got=%s expected=%s", info.MOTD, rendezvousservertest.TestMotd)
}

gotAgents := ts.Agents()
expectAgents := [][]string{
[]string{version.AgentString, version.AgentVersion},
}

if !reflect.DeepEqual(gotAgents, expectAgents) {
t.Fatalf("got agents=%v expected=%v", gotAgents, expectAgents)
}

nameplate, err := c0.CreateMailbox(ctx)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -100,3 +110,33 @@ func TestBasicClient(t *testing.T) {
default:
}
}

func TestCustomUserAgent(t *testing.T) {
ts := rendezvousservertest.NewServer()
defer ts.Close()

side0 := crypto.RandSideID()
appID := "nightclubs-reasonableness"

agentString := "deafening-buttermilk"
agentVersion := "v1.2.3"
c0 := NewClient(ts.WebSocketURL(), side0, appID, WithVersion(agentString, agentVersion))

ctx := context.Background()

_, err := c0.Connect(ctx)
if err != nil {
t.Fatal(err)
}

gotAgents := ts.Agents()
expectAgents := [][]string{
[]string{agentString, agentVersion},
}

if !reflect.DeepEqual(gotAgents, expectAgents) {
t.Fatalf("got agents=%v expected=%v", gotAgents, expectAgents)
}

c0.Close(ctx, "")
}
3 changes: 3 additions & 0 deletions rendezvous/internal/msgs/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type Bind struct {
ID string `json:"id"`
Side string `json:"side"`
AppID string `json:"appid"`
// ClientVersion is by convention a two value array
// of [client_id, version]
ClientVersion []string `json:"client_version"`
}

// Client sent aollocate message
Expand Down
24 changes: 24 additions & 0 deletions rendezvous/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package rendezvous

type ClientOption interface {
setValue(*Client)
}

type versionOption struct {
agentString string
agentVersion string
}

func (o *versionOption) setValue(c *Client) {
c.agentString = o.agentString
c.agentVersion = o.agentVersion
}

// WithVersion returns a ClientOption to override the default client
// identifier and version reported to the rendezvous server.
func WithVersion(agentID string, version string) ClientOption {
return &versionOption{
agentString: agentID,
agentVersion: version,
}
}
9 changes: 9 additions & 0 deletions rendezvous/rendezvousservertest/rendezvousservertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type TestServer struct {
mu sync.Mutex
mailboxes map[string]*mailbox
nameplates map[int16]string
agents [][]string
}

func NewServer() *TestServer {
Expand All @@ -40,6 +41,10 @@ func NewServer() *TestServer {
return ts
}

func (ts *TestServer) Agents() [][]string {
return ts.agents
}

func (ts *TestServer) WebSocketURL() string {
u, err := url.Parse(ts.URL)
if err != nil {
Expand Down Expand Up @@ -236,6 +241,10 @@ func (ts *TestServer) handleWS(w http.ResponseWriter, r *http.Request) {
continue
}

ts.mu.Lock()
ts.agents = append(ts.agents, m.ClientVersion)
ts.mu.Unlock()

sideID = m.Side
case *msgs.Allocate:
ackMsg(m.ID)
Expand Down
8 changes: 8 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package version

var (
// Default Agent identifier sent to rendezvous server
AgentString = "go-william"
// Default Agent version sent to rendezvous server
AgentVersion = "v1.0.0"
)

0 comments on commit db5cc47

Please sign in to comment.