-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
connector.go
147 lines (121 loc) · 2.82 KB
/
connector.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package engine
import (
"context"
"crypto/tls"
"crypto/x509"
"github.com/pion/ion/proto/rtc"
"io/ioutil"
log "github.com/pion/ion-log"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type ConnectorConfig struct {
SSL bool
Cafile string
Token string
}
type ServiceEvent struct {
Name string
ErrStatus *status.Status
}
type Service interface {
Name() string
Connect()
Close()
Connected() bool
}
type Connector struct {
services map[string]Service
Metadata metadata.MD
config *ConnectorConfig
grpcConn *grpc.ClientConn
OnOpen func(Service)
OnClose func(Service, ServiceEvent)
ctx context.Context
}
// NewConnector create a ion connector
func NewConnector(addr string, config ...ConnectorConfig) *Connector {
c := &Connector{
services: make(map[string]Service),
Metadata: make(metadata.MD),
ctx: context.Background(),
}
if len(config) > 0 {
c.config = &config[0]
}
if addr == "" {
log.Errorf("error: %v", errInvalidAddr)
return nil
}
if c.config != nil && c.config.Token != "" {
c.Metadata.Append("authorization", c.config.Token)
}
var err error
if c.config != nil && c.config.SSL {
var config *tls.Config
if c.config.Cafile != "" {
b, _ := ioutil.ReadFile(c.config.Cafile)
cp := x509.NewCertPool()
if !cp.AppendCertsFromPEM(b) {
log.Errorf("credentials: failed to append certificates")
return nil
}
config = &tls.Config{
InsecureSkipVerify: false,
RootCAs: cp,
}
} else {
config = &tls.Config{
InsecureSkipVerify: false,
}
}
c.grpcConn, err = grpc.Dial(addr, grpc.WithTransportCredentials(credentials.NewTLS(config)), grpc.WithBlock())
if err != nil {
log.Errorf("did not connect: %v", err)
return nil
}
} else {
c.grpcConn, err = grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock())
}
if err != nil {
log.Errorf("did not connect: %v", err)
return nil
}
log.Infof("gRPC connected: %s", addr)
return c
}
func (c *Connector) Signal(r *RTC) (Signaller, error) {
c.RegisterService(r)
client := rtc.NewRTCClient(c.grpcConn)
r.ctx = metadata.NewOutgoingContext(r.ctx, c.Metadata)
return client.Signal(r.ctx)
}
func (c *Connector) Close() {
for _, s := range c.services {
if s.Connected() {
s.Close()
}
}
}
func (c *Connector) OnHeaders(service Service, headers metadata.MD) {
for k, v := range headers {
c.Metadata.Append(k, v[0])
}
if c.OnOpen != nil {
c.OnOpen(service)
}
}
func (c *Connector) OnEnd(service Service, headers metadata.MD) {
if c.OnClose != nil {
c.OnClose(service, ServiceEvent{
Name: service.Name(),
ErrStatus: status.New(codes.OK, "close"),
})
}
}
func (c *Connector) RegisterService(service Service) {
c.services[service.Name()] = service
}