/
handshake.go
117 lines (101 loc) · 3.16 KB
/
handshake.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package command
import (
"context"
"runtime"
"github.com/mongodb/mongo-go-driver/core/address"
"github.com/mongodb/mongo-go-driver/core/description"
"github.com/mongodb/mongo-go-driver/core/result"
"github.com/mongodb/mongo-go-driver/core/version"
"github.com/mongodb/mongo-go-driver/core/wiremessage"
"github.com/mongodb/mongo-go-driver/x/bsonx"
)
// Handshake represents a generic MongoDB Handshake. It calls isMaster and
// buildInfo.
//
// The isMaster and buildInfo commands are used to build a server description.
type Handshake struct {
Client bsonx.Doc
Compressors []string
SaslSupportedMechs string
ismstr result.IsMaster
err error
}
// Encode will encode the handshake commands into a wire message containing isMaster
func (h *Handshake) Encode() (wiremessage.WireMessage, error) {
var wm wiremessage.WireMessage
ismstr, err := (&IsMaster{
Client: h.Client,
Compressors: h.Compressors,
SaslSupportedMechs: h.SaslSupportedMechs,
}).Encode()
if err != nil {
return wm, err
}
wm = ismstr
return wm, nil
}
// Decode will decode the wire messages.
// Errors during decoding are deferred until either the Result or Err methods
// are called.
func (h *Handshake) Decode(wm wiremessage.WireMessage) *Handshake {
h.ismstr, h.err = (&IsMaster{}).Decode(wm).Result()
if h.err != nil {
return h
}
return h
}
// Result returns the result of decoded wire messages.
func (h *Handshake) Result(addr address.Address) (description.Server, error) {
if h.err != nil {
return description.Server{}, h.err
}
return description.NewServer(addr, h.ismstr), nil
}
// Err returns the error set on this Handshake.
func (h *Handshake) Err() error { return h.err }
// Handshake implements the connection.Handshaker interface. It is identical
// to the RoundTrip methods on other types in this package. It will execute
// the isMaster command.
func (h *Handshake) Handshake(ctx context.Context, addr address.Address, rw wiremessage.ReadWriter) (description.Server, error) {
wm, err := h.Encode()
if err != nil {
return description.Server{}, err
}
err = rw.WriteWireMessage(ctx, wm)
if err != nil {
return description.Server{}, err
}
wm, err = rw.ReadWireMessage(ctx)
if err != nil {
return description.Server{}, err
}
return h.Decode(wm).Result(addr)
}
// ClientDoc creates a client information document for use in an isMaster
// command.
func ClientDoc(app string) bsonx.Doc {
doc := bsonx.Doc{
{"driver",
bsonx.Document(bsonx.Doc{
{"name", bsonx.String("mongo-go-driver")},
{"version", bsonx.String(version.Driver)},
}),
},
{"os",
bsonx.Document(bsonx.Doc{
{"type", bsonx.String(runtime.GOOS)},
{"architecture", bsonx.String(runtime.GOARCH)},
}),
},
{"platform", bsonx.String(runtime.Version())},
}
if app != "" {
doc = append(doc, bsonx.Elem{"application", bsonx.Document(bsonx.Doc{{"name", bsonx.String(app)}})})
}
return doc
}