-
Notifications
You must be signed in to change notification settings - Fork 0
/
inventory.go
80 lines (69 loc) · 2.03 KB
/
inventory.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
package vim
import (
"context"
"net"
"net/rpc/jsonrpc"
commonrpc "github.com/ovandriyanov/dlvim/proxy/rpc"
"github.com/ovandriyanov/dlvim/proxy/rpc/proxy"
"github.com/ovandriyanov/dlvim/proxy/upstream"
"github.com/ovandriyanov/dlvim/proxy/vimevent"
"golang.org/x/xerrors"
)
type inventory struct {
upstreamProcess *upstream.Upstream
upstreamClient commonrpc.Client
proxyServer *proxy.Server
stack *Stack
}
func (i *inventory) Stop() {
i.proxyServer.Stop()
i.upstreamClient.Close()
if i.upstreamProcess != nil {
i.upstreamProcess.Stop()
}
}
func (i *inventory) ProxyListenAddress() net.Addr {
return i.proxyServer.ListenAddress()
}
func NewInventory(ctx context.Context, upstreamStartOption upstream.StartOption, events chan<- vimevent.Event, debugRPC bool) (*inventory, error) {
var upstreamProcess *upstream.Upstream
var err error
var listenAddress string
switch startOption := upstreamStartOption.(type) {
case upstream.StartDlvProcess:
upstreamProcess, err = upstream.New(ctx, startOption)
if err != nil {
return nil, xerrors.Errorf("cannot create upstream: %w", err)
}
listenAddress = upstream.ListenAddress
case *upstream.Connect:
listenAddress = startOption.Address()
}
stopUpstream := func() {
if upstreamProcess != nil {
upstreamProcess.Stop()
}
}
upstreamConnection, err := net.Dial("tcp", listenAddress)
if err != nil {
stopUpstream()
return nil, xerrors.Errorf("cannot dial upstream at %s: %w", listenAddress, err)
}
stack := NewStack()
proxyServer, err := proxy.NewServer(listenAddress, events, stack.SetStackTrace, debugRPC)
if err != nil {
_ = upstreamConnection.Close()
stopUpstream()
return nil, xerrors.Errorf("cannot create proxy server: %w", err)
}
var upstreamClient commonrpc.Client = jsonrpc.NewClient(upstreamConnection)
if debugRPC {
upstreamClient = commonrpc.NewLoggingClient("dlv", upstreamClient)
}
return &inventory{
upstreamProcess: upstreamProcess,
upstreamClient: upstreamClient,
proxyServer: proxyServer,
stack: stack,
}, nil
}