/
main.go
91 lines (78 loc) · 2.53 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
/* YaNFD - Yet another NDN Forwarding Daemon
*
* Copyright (C) 2020-2022 Eric Newberry.
*
* This file is licensed under the terms of the MIT License, as found in LICENSE.md.
*/
//go:generate goversioninfo -icon=static/ndn_app.ico
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
server "github.com/named-data/YaNFD/cmd/yanfdui/server"
"github.com/named-data/YaNFD/core"
"github.com/named-data/YaNFD/executor"
)
// Version of YaNFD.
var Version string
// Addr of HTTP server.
var Addr string = "localhost:5010"
// HttpBaseDir is the base directory of server files
var HttpBaseDir string = "."
func main() {
// Parse command line options
var shouldPrintVersion bool
flag.BoolVar(&shouldPrintVersion, "version", false, "Print version and exit")
var configFileName string
flag.StringVar(&configFileName, "config", "/usr/local/etc/ndn/yanfd.toml", "Configuration file location")
var memoryBallastSize int
flag.IntVar(&memoryBallastSize, "memory-ballast", 0,
"Enable memory ballast of specified size (in GB) to avoid frequent garbage collection")
flag.Parse()
if shouldPrintVersion {
fmt.Println("YaNFD: Yet another NDN Forwarding Daemon")
fmt.Println("Version " + Version)
fmt.Println("Copyright (C) 2020-2021 Eric Newberry")
fmt.Println("Released under the terms of the MIT License")
return
}
if runtime.GOOS == "windows" && configFileName[0] == '/' {
configFileName = os.ExpandEnv("${APPDATA}\\ndn\\yanfd.toml")
}
config := executor.YaNFDConfig{
Version: Version,
ConfigFileName: configFileName,
DisableEthernet: false,
DisableUnix: false,
LogFile: "",
CpuProfile: "",
MemProfile: "",
BlockProfile: "",
MemoryBallastSize: memoryBallastSize,
}
yanfd := executor.NewYaNFD(&config)
yanfd.Start()
// Start HTTP server
httpServerExitDone := &sync.WaitGroup{}
httpServerExitDone.Add(1)
srv := server.StartHttpServer(httpServerExitDone, Addr, HttpBaseDir, configFileName)
server.OpenBrowser("http://" + Addr)
// Set up signal handler channel and wait for interrupt
core.LogInfo("Main", "HTTP server started, serving at http://", Addr)
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, os.Interrupt, syscall.SIGTERM)
receivedSig := <-sigChannel
core.LogInfo("Main", "Received signal ", receivedSig, " - exiting")
// Stop HTTP server
if err := srv.Shutdown(context.Background()); err != nil {
core.LogInfo("Main", "Error in shutting down HTTP server: ", err)
}
yanfd.Stop()
httpServerExitDone.Wait()
}