forked from keybase/client
/
reachability.go
102 lines (84 loc) · 2.53 KB
/
reachability.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
// Copyright 2015 Keybase, Inc. All rights reserved. Use of
// this source code is governed by the included BSD license.
package service
import (
"sync"
"golang.org/x/net/context"
"github.com/keybase/client/go/libkb"
"github.com/keybase/client/go/protocol/keybase1"
"github.com/keybase/go-framed-msgpack-rpc/rpc"
)
type reachabilityHandler struct {
*BaseHandler
libkb.Contextified
reachability *reachability
}
func newReachabilityHandler(xp rpc.Transporter, g *libkb.GlobalContext, reachability *reachability) *reachabilityHandler {
return &reachabilityHandler{
BaseHandler: NewBaseHandler(g, xp),
Contextified: libkb.NewContextified(g),
reachability: reachability,
}
}
func (h *reachabilityHandler) ReachabilityChanged(_ context.Context, _ keybase1.Reachability) (err error) {
h.G().Trace("ReachabilityChanged", &err)()
return nil
}
func (h *reachabilityHandler) StartReachability(_ context.Context) (res keybase1.Reachability, err error) {
h.G().Trace("StartReachability", &err)()
return keybase1.Reachability{
Reachable: keybase1.Reachable_UNKNOWN,
}, nil
}
func (h *reachabilityHandler) CheckReachability(_ context.Context) (res keybase1.Reachability, err error) {
h.G().Trace("CheckReachability", &err)()
return h.reachability.check(), nil
}
type reachability struct {
libkb.Contextified
lastReachability keybase1.Reachability
setMutex sync.Mutex
gh *gregorHandler
}
func newReachability(g *libkb.GlobalContext, gh *gregorHandler) *reachability {
return &reachability{
Contextified: libkb.NewContextified(g),
gh: gh,
}
}
func (h *reachability) setReachability(r keybase1.Reachability) {
h.setMutex.Lock()
changed := h.lastReachability.Reachable != r.Reachable
h.lastReachability = r
h.setMutex.Unlock()
if changed {
h.G().Log.Debug("Reachability changed: %#v", r)
h.G().NotifyRouter.HandleReachability(r)
}
}
func (h *reachability) check() (k keybase1.Reachability) {
reachable := h.gh.isReachable()
if reachable {
k.Reachable = keybase1.Reachable_YES
} else {
k.Reachable = keybase1.Reachable_NO
}
h.setReachability(k)
return k
}
func (h *reachability) IsConnected(ctx context.Context) libkb.ConnectivityMonitorResult {
h.setMutex.Lock()
defer h.setMutex.Unlock()
switch h.lastReachability.Reachable {
case keybase1.Reachable_YES:
return libkb.ConnectivityMonitorYes
case keybase1.Reachable_NO:
return libkb.ConnectivityMonitorNo
default:
return libkb.ConnectivityMonitorUnknown
}
}
func (h *reachability) CheckReachability(ctx context.Context) error {
h.check()
return nil
}