This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdiscovery.go
47 lines (42 loc) · 1.49 KB
/
discovery.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package ttnsdk
import (
"context"
"github.com/TheThingsNetwork/api/discovery"
"google.golang.org/grpc"
)
func (c *client) discover() (err error) {
logger := c.Logger.WithField("Address", c.DiscoveryServerAddress)
logger.Debug("ttn-sdk: Connecting to discovery...")
if c.DiscoveryServerInsecure {
c.discovery.conn, err = grpc.Dial(c.DiscoveryServerAddress, append(DialOptions, grpc.WithInsecure())...)
} else {
c.discovery.conn, err = grpc.Dial(c.DiscoveryServerAddress, append(DialOptions, grpc.WithTransportCredentials(c.transportCredentials))...)
}
if err != nil {
logger.WithError(err).Debug("ttn-sdk: Could not connect to discovery")
return err
}
logger.Debug("ttn-sdk: Connected to discovery")
discoveryClient := discovery.NewDiscoveryClient(c.discovery.conn)
ctx, cancel := context.WithTimeout(c.getContext(context.Background()), c.RequestTimeout)
defer cancel()
c.Logger.Debug("ttn-sdk: Finding handler...")
handler, err := discoveryClient.GetByAppID(ctx, &discovery.GetByAppIDRequest{AppID: c.appID})
if err != nil {
c.Logger.WithError(err).Debug("ttn-sdk: Could not find handler for application")
return err
}
c.handler.announcement = handler
return nil
}
func (c *client) closeDiscovery() error {
c.discovery.Lock()
defer c.discovery.Unlock()
if c.discovery.conn != nil {
c.discovery.conn.Close()
}
c.discovery.conn = nil
return nil
}