-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
77 lines (63 loc) · 1.62 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
/*
* *******************************************************************************
* * Copyright (c) 2020 Edgeworx, Inc.
* *
* * This program and the accompanying materials are made available under the
* * terms of the Eclipse Public License v. 2.0 which is available at
* * http://www.eclipse.org/legal/epl-2.0
* *
* * SPDX-License-Identifier: EPL-2.0
* *******************************************************************************
*
*/
package main
import (
"errors"
rt "github.com/eclipse-iofog/router/internal/router"
"log"
"os"
sdk "github.com/eclipse-iofog/iofog-go-sdk/pkg/microservices"
)
var (
router *rt.Router
)
func init() {
router = new(rt.Router)
router.Config = new(rt.Config)
}
func main() {
ioFogClient, clientError := sdk.NewDefaultIoFogClient()
if clientError != nil {
log.Fatalln(clientError.Error())
}
if err := updateConfig(ioFogClient, router.Config); err != nil {
log.Fatalln(err.Error())
}
confChannel := ioFogClient.EstablishControlWsConnection(0)
exitChannel := make(chan error)
go router.StartRouter(exitChannel)
for {
select {
case <-exitChannel:
os.Exit(0)
case <-confChannel:
newConfig := new(rt.Config)
if err := updateConfig(ioFogClient, newConfig); err != nil {
log.Fatal(err)
} else {
router.UpdateRouter(newConfig)
}
}
}
}
func updateConfig(ioFogClient *sdk.IoFogClient, config interface{}) error {
attemptLimit := 5
var err error
for err = ioFogClient.GetConfigIntoStruct(config); err != nil && attemptLimit > 0; attemptLimit-- {
return err
}
if attemptLimit == 0 {
return errors.New("Update config failed")
}
return nil
}