-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmain.go
130 lines (118 loc) · 2.84 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
// Command ndndpdk-godemo demonstrates NDNgo library features.
package main
import (
"context"
"io"
"log"
"os"
"os/signal"
"sort"
"syscall"
"github.com/urfave/cli/v2"
"github.com/usnistgov/ndn-dpdk/core/gqlclient"
"github.com/usnistgov/ndn-dpdk/core/version"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndn/l3"
"github.com/usnistgov/ndn-dpdk/ndn/memiftransport"
"github.com/usnistgov/ndn-dpdk/ndn/mgmt"
"github.com/usnistgov/ndn-dpdk/ndn/mgmt/gqlmgmt"
"github.com/usnistgov/ndn-dpdk/ndn/mgmt/nfdmgmt"
)
var (
gqlserver string
mtuFlag int
useNfd bool
enableLog bool
client mgmt.Client
face mgmt.Face
fwFace l3.FwFace
)
func openUplink(c *cli.Context) (e error) {
switch client := client.(type) {
case *gqlmgmt.Client:
var loc memiftransport.Locator
loc.Dataroom = mtuFlag
face, e = client.OpenMemif(loc)
default:
face, e = client.OpenFace()
}
if e != nil {
return e
}
l3face := face.Face()
fw := l3.GetDefaultForwarder()
if fwFace, e = fw.AddFace(l3face); e != nil {
return e
}
fwFace.AddRoute(ndn.Name{})
fw.AddReadvertiseDestination(face)
log.Printf("uplink opened, state is %s", l3face.State())
l3face.OnStateChange(func(st l3.TransportState) {
log.Printf("uplink state changes to %s", l3face.State())
})
return nil
}
var app = &cli.App{
Version: version.V.String(),
Usage: "NDNgo library demo.",
EnableBashCompletion: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "gqlserver",
Usage: "GraphQL `endpoint` of NDN-DPDK service",
Value: "http://127.0.0.1:3030/",
Destination: &gqlserver,
},
&cli.IntFlag{
Name: "mtu",
Usage: "application face `MTU`",
Destination: &mtuFlag,
},
&cli.BoolFlag{
Name: "nfd",
Usage: "connect to NFD or YaNFD instead of NDN-DPDK (set FaceUri in NDN_CLIENT_TRANSPORT environment variable)",
Destination: &useNfd,
},
&cli.BoolFlag{
Name: "logging",
Usage: "whether to enable logging",
Value: true,
Destination: &enableLog,
},
},
Before: func(c *cli.Context) (e error) {
if !enableLog {
log.SetOutput(io.Discard)
}
if useNfd {
client, e = nfdmgmt.New()
} else {
client, e = gqlmgmt.New(gqlclient.Config{HTTPUri: gqlserver})
}
return e
},
After: func(c *cli.Context) (e error) {
if face != nil {
log.Printf("uplink closed, error is %v", face.Close())
}
return client.Close()
},
}
func defineCommand(command *cli.Command) {
app.Commands = append(app.Commands, command)
}
func main() {
sort.Sort(cli.CommandsByName(app.Commands))
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, syscall.SIGINT)
go func() {
<-interrupt
cancel()
}()
e := app.RunContext(ctx, os.Args)
if e != nil {
log.Fatal(e)
}
}