-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
188 lines (158 loc) · 5.6 KB
/
proxy.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package proxy
import (
"context"
"sync"
"github.com/opentracing/opentracing-go"
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"github.com/uber/jaeger-client-go"
"github.com/hsmade/comfoconnectbridge/pkg/comfoconnect"
)
type Proxy struct {
client *Client
uuid []byte
listener *Listener
toGateway chan comfoconnect.Message
fromGateway chan comfoconnect.Message
quit chan bool
exited chan bool
}
var (
proxyMessagetoGateway = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "comfoconnect_proxy_proxy_message_toGateway_total",
Help: "Number of messages sent to the gateway.",
},
[]string{"message_type"},
)
proxyMessagefromGateway = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "comfoconnect_proxy_proxy_message_fromGateway_total",
Help: "Number of messages received from the gateway.",
},
[]string{"message_type"},
)
metricsGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "comfoconnect_pdo_value",
Help: "Value for the different PDOs, as they're seen by the proxy",
},
[]string{"ID", "description"},
)
)
func NewProxy(gatewayIP string, myMacAddress []byte) *Proxy {
log := logrus.WithFields(logrus.Fields{
"module": "proxy",
"method": "NewProxy",
})
uuid := []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x10, 0x10, 0x80, 0x01} // uuid header
uuid = append(uuid, myMacAddress...)
prometheus.MustRegister(proxyMessagefromGateway)
prometheus.MustRegister(proxyMessagetoGateway)
prometheus.MustRegister(metricsGauge)
listenerToGateway := make(chan comfoconnect.Message, 500)
prometheus.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "comfoconnect_proxy_listener_toGateway_queue_length",
Help: "The current number of items on listenerToGateway queue.",
}, func() float64 {
return float64(len(listenerToGateway))
}))
clientToGateway := make(chan comfoconnect.Message, 500)
prometheus.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "comfoconnect_proxy_client_toGateway_queue_length",
Help: "The current number of items on clientToGateway queue.",
}, func() float64 {
return float64(len(clientToGateway))
}))
clientFromGateway := make(chan comfoconnect.Message, 500)
prometheus.MustRegister(prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Name: "comfoconnect_proxy_proxy_fromGateway_queue_length",
Help: "The current number of items on clientFromGateway queue.",
}, func() float64 {
return float64(len(clientFromGateway))
}))
log.Info("creating new listener")
l := NewListener(listenerToGateway)
log.Info("creating new client")
c := NewClient(gatewayIP, myMacAddress, clientToGateway, clientFromGateway)
p := Proxy{
client: c,
listener: l,
uuid: uuid,
toGateway: listenerToGateway,
fromGateway: clientFromGateway,
}
return &p
}
func (p Proxy) Run(ctx context.Context, wg *sync.WaitGroup) {
log := logrus.WithFields(logrus.Fields{
"module": "proxy",
"object": "Proxy",
"method": "Run",
})
log.Info("starting new client")
wg.Add(1)
go func(){
err := p.client.Run(ctx, wg)
if err != nil {
log.Errorf("client exited: %v", err)
}
}()
log.Info("starting new listener")
wg.Add(1)
go p.listener.Run(ctx, wg)
for {
select {
case <-ctx.Done():
log.Info("Shutting down proxy server")
wg.Wait()
return
case message := <-p.toGateway:
proxyMessagetoGateway.WithLabelValues(message.Operation.Type.String()).Inc()
log.Debugf("received a message for the gateway: %v", message)
span := opentracing.GlobalTracer().StartSpan("proxy.Proxy.Run.ReceivedForGateway", opentracing.ChildOf(message.Span.Context()))
comfoconnect.SpanSetMessage(span, message)
message.Span = span
generateMetrics(message)
log.WithField("span", span.Context().(jaeger.SpanContext).String()).Debugf("forwarding message to gateway: %v", message)
p.client.toGateway <- message
span.Finish()
case message := <-p.fromGateway:
proxyMessagefromGateway.WithLabelValues(message.Operation.Type.String()).Inc()
span := opentracing.GlobalTracer().StartSpan("proxy.Proxy.Run.ReceivedFromGateway", opentracing.ChildOf(message.Span.Context()))
comfoconnect.SpanSetMessage(span, message)
message.Span = span
log.WithField("span", span.Context().(jaeger.SpanContext).String()).Debugf("received a message from gateway: %v", message)
generateMetrics(message)
log.WithField("span", span.Context().(jaeger.SpanContext).String()).Debugf("going to copy to %d apps", len(p.listener.apps))
for _, app := range p.listener.apps {
message.Src = p.uuid // masquerade
message.Dst = app.uuid // masquerade
log.Debugf("copying message from gateway to app(%s/%x):%v", app.conn.RemoteAddr().String(), app.uuid, message)
err := app.Write(message)
if err != nil {
log.Errorf("error while copying message from gateway to app(%s/%x):%v", app.conn.RemoteAddr().String(), app.uuid, err)
}
}
span.Finish()
}
}
}
func generateMetrics(message comfoconnect.Message) {
span := opentracing.GlobalTracer().StartSpan("proxy.generateMetrics", opentracing.ChildOf(message.Span.Context()))
comfoconnect.SpanSetMessage(span, message)
defer span.Finish()
log := logrus.WithFields(logrus.Fields{
"module": "proxy",
"method": "generateMetrics",
"span": span.Context().(jaeger.SpanContext).String(),
})
switch message.Operation.Type.String() {
case "CnRpdoNotificationType":
conv := message.DecodePDO()
metricsGauge.WithLabelValues(conv.GetID(), conv.GetDescription()).Set(conv.Tofloat64())
case "CnAlarmNotificationType":
log.Warnf("Got alarm notification: %v", message)
}
log.Debugf("called for %v", message)
}