-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
107 lines (98 loc) · 2.82 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
package main
import (
"flag"
"fmt"
"gossip/src/core"
"gossip/src/parser/ini"
"log"
"os"
"path/filepath"
)
var gossipWorkspacePath string
// init is an initialization function for 'main' package, called by Go.
func init() {
gossipWorkspacePath, _ = os.Getwd()
}
func newCentralControllerFromConfigFile(configPath string) (*core.CentralController, error) {
config, err := ini.ReadConfigFile(configPath)
if err != nil {
return nil, err
}
// Check if GLOBAL configurations exist.
globalConfig, ok := config["GLOBAL"]
if !ok {
return nil, fmt.Errorf("GLOBAL section cannot be found in the config file: %s", configPath)
}
hostKeyPath, err := globalConfig.GetStringValue("hostkey")
if err != nil {
return nil, err
}
pubKeyPath, err := globalConfig.GetStringValue("pubkey")
if err != nil {
return nil, err
}
// Check if the configurations for gossip module exist
gossipConfig, ok := config["gossip"]
if !ok {
return nil, fmt.Errorf("Gossip section cannot be found in the config file: %s", configPath)
}
// Check if the trusted identities path exists
trustedIdentitiesPath, err := gossipConfig.GetStringValue("trusted_identities_path")
if err != nil {
return nil, err
}
// Check if the bootstrapper address exists
bootstrapper, err := gossipConfig.GetStringValue("bootstrapper")
if err != nil {
return nil, err
}
// Check if the API address exists
apiAddr, err := gossipConfig.GetStringValue("api_address")
if err != nil {
return nil, err
}
// Check if the P2P listening address exists
p2pAddr, err := gossipConfig.GetStringValue("listen_address")
if err != nil {
return nil, err
}
// Check if the "cache size" exists
cacheSize, err := gossipConfig.GetUint16Value("cache_size")
if err != nil {
return nil, err
}
// Check if the "degree" exists
degree, err := gossipConfig.GetUint8Value("degree")
if err != nil {
return nil, err
}
// Check if the "maxTTL" exists
maxTTL, err := gossipConfig.GetUint8Value("max_ttl")
if err != nil {
return nil, err
}
centralController, err := core.NewCentralController(
trustedIdentitiesPath, hostKeyPath, pubKeyPath, bootstrapper, apiAddr, p2pAddr, cacheSize, degree, maxTTL,
)
if err != nil {
return nil, err
}
return centralController, nil
}
func main() {
// Set global logging settings.
log.SetOutput(os.Stdout)
log.SetFlags(log.Ldate | log.Ltime | log.LUTC)
// Take config file path as a command line argument.
defaultConfigPath := filepath.Join(gossipWorkspacePath, "config", "config.ini")
configPath := flag.String("config_path", defaultConfigPath, "a file path string for the configuration")
flag.Parse()
// Create a central controller and run it.
centralController, err := newCentralControllerFromConfigFile(*configPath)
if err != nil {
// Log the error and exit.
log.Fatalln(err)
}
log.Println(centralController)
centralController.Run()
}