-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (64 loc) · 3.22 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) 2018 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"
"github.com/gorilla/websocket"
sdk "github.com/ioFog/iofog-go-sdk"
"github.com/ioFog/core-networking/cn"
"log"
"os"
"time"
)
var (
logger = log.New(os.Stderr, "", log.LstdFlags)
ioFogClient, clientError = sdk.NewDefaultIoFogClient()
)
type ConnectionManager struct {
connection *websocket.Conn
lastUpdated time.Time
}
func main() {
if clientError != nil {
logger.Fatalln(clientError.Error())
}
config := new(cn.CNConfig)
if err := updateConfig(config); err != nil {
logger.Fatalln(err.Error())
}
if config.Cert == "" {
config.Cert = "MIIEsTCCA5mgAwIBAgIQCKWiRs1LXIyD1wK0u6tTSTANBgkqhkiG9w0BAQsFADBhMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBDQTAeFw0xNzExMDYxMjIzMzNaFw0yNzExMDYxMjIzMzNaMF4xCzAJBgNVBAYTAlVTMRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5jb20xHTAbBgNVBAMTFFJhcGlkU1NMIFJTQSBDQSAyMDE4MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5S2oihEo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QasR7CNfIr+4KScc6ABlQ5RDF86PGeE6kdwSQkFiB/cQysNyq0jEDQTkfa2pjmuWtMCNbBnhFXBYejfubIhaUbEv2FOQB3dCav+FPg5eEveXTVyMnGo="
}
cn.WriteCertToFile(config.Cert)
c, err := cn.NewCN(config, ioFogClient)
if err != nil {
logger.Fatalln(err.Error())
}
c.Start()
confChannel := ioFogClient.EstablishControlWsConnection(0)
for {
select {
case <-confChannel:
logger.Println("[ Main ] Config update is not supported yet")
//updateConfig(config)
}
}
}
func updateConfig(config interface{}) error {
attemptLimit := 5
var err error
for err = ioFogClient.GetConfigIntoStruct(config); err != nil && attemptLimit > 0; attemptLimit-- {
logger.Println(err.Error())
}
if attemptLimit == 0 {
return errors.New("Update config failed")
}
return nil
}