-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
134 lines (111 loc) · 2.94 KB
/
main.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
package main
import (
"fmt"
"github.com/cromega/clogger"
"hub.jazz.net/git/bluemixgarage/lrsc-bridge/bridge"
"hub.jazz.net/git/bluemixgarage/lrsc-bridge/iotf"
"hub.jazz.net/git/bluemixgarage/lrsc-bridge/reporter"
"hub.jazz.net/git/bluemixgarage/lrsc-bridge/utils"
"os"
"time"
)
var logger clogger.Logger
var lrscClient lrscConnection
func init() {
logger = utils.CreateLogger()
}
func main() {
logger.Info("================ LRSC <-> IoTF bridge launched ==================")
reporters, err := startBridge()
if err != nil {
logger.Fatal(err.Error())
}
setupHttp(reporters)
err = startHttp()
if err != nil {
logger.Error(err.Error())
panic(err)
}
}
func startBridge() (map[string]reporter.StatusReporter, error) {
appReporter := reporter.New()
commands := make(chan bridge.Command)
events := make(chan iotf.Event)
deviceType := "LRSC"
iotfManager, err := iotf.NewIoTFManager(os.Getenv("VCAP_SERVICES"), commands, events, deviceType)
if err != nil {
appReporter.Report("IoTF Manager:", err.Error())
return nil, err
}
if err := setupLrscClient(); err != nil {
return nil, err
}
reporters := make(map[string]reporter.StatusReporter)
reporters["app"] = appReporter
reporters["lrsc"] = lrscClient.StatusReporter
reporters["iotf"] = iotfManager.StatusReporter()
go runConnectionLoop("LRSC client", &lrscClient)
go runConnectionLoop("IoTF client", iotfManager)
go func() {
for command := range commands {
lrscClient.sendCommand(command)
logger.Debug("Received command message: %v", command)
}
}()
go func() {
for {
message := <-lrscClient.inbound
event := iotf.Event{Device: message.DeviceGuid, Payload: message.Payload}
events <- event
}
}()
return reporters, nil
}
func setupLrscClient() error {
lrscClient.StatusReporter = reporter.New()
dialerConfig := dialerConfig{
host: os.Getenv("LRSC_HOST"),
port: os.Getenv("LRSC_PORT"),
cert: os.Getenv("LRSC_CLIENT_CERT"),
key: os.Getenv("LRSC_CLIENT_KEY"),
}
dialer, err := createTlsDialer(dialerConfig, &lrscClient.StatusReporter)
if err != nil {
logger.Error("failed to create dialer: %v", err)
lrscClient.Report("CONNECTION", err.Error())
return err
}
lrscClient.dialer = dialer
lrscClient.err = make(chan error)
lrscClient.inbound = make(chan lrscMessage, 100)
return nil
}
type connection interface {
Connect() error
Error() <-chan error
Loop()
}
func retryWithBackoff(name string, body func() error) {
for timeout := time.Second; ; timeout *= 2 {
logger.Debug("starting " + name)
if timeout > time.Minute*5 {
timeout = time.Minute * 5
}
err := body()
if err == nil {
return
} else {
time.Sleep(timeout)
logger.Debug("connection failed, retrying " + name)
}
}
}
func runConnectionLoop(name string, conn connection) {
logger.Debug("starting connection loop " + name)
for {
retryWithBackoff(name, conn.Connect)
go conn.Loop()
err := <-conn.Error()
logger.Error(fmt.Sprintf("%v went tits up: %v", name, err))
}
}