-
Notifications
You must be signed in to change notification settings - Fork 28
/
servers.go
111 lines (100 loc) · 3.16 KB
/
servers.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mounttablelib
import (
"fmt"
"net"
v23 "v.io/v23"
"v.io/v23/context"
"v.io/v23/naming"
"v.io/v23/options"
"v.io/v23/rpc"
"v.io/x/ref/lib/signals"
"v.io/x/ref/lib/timekeeper"
)
func Main(opts Opts) error {
ctx, shutdown := v23.Init()
defer shutdown()
return MainWithCtx(ctx, opts)
}
func MainWithCtx(ctx *context.T, opts Opts) error {
name, stop, err := StartServers(ctx, v23.GetListenSpec(ctx), opts.MountName, opts.NhName, opts.AclFile, opts.PersistDir, "mounttable", opts.LogLevel)
if err != nil {
return fmt.Errorf("mounttablelib.StartServers failed: %v", err)
}
defer stop()
// Consumed by integration tests and the like.
fmt.Printf("NAME=%s\n", name)
ctx.Info("Received signal ", <-signals.ShutdownOnSignals(ctx))
return nil
}
func StartServers(ctx *context.T, listenSpec rpc.ListenSpec, mountName, nhName, permsFile, persistDir, debugPrefix string, logLevel int) (string, func(), error) {
var stopFuncs []func()
ctx, cancel := context.WithCancel(ctx)
stop := func() {
cancel()
for i := len(stopFuncs) - 1; i >= 0; i-- {
stopFuncs[i]()
}
}
mt, err := NewMountTableDispatcherWithClock(ctx, permsFile, persistDir, debugPrefix, timekeeper.RealTime(), logLevel)
if err != nil {
ctx.Errorf("NewMountTable failed: %v", err)
return "", nil, err
}
ctx = v23.WithListenSpec(ctx, listenSpec)
ctx, mtServer, err := v23.WithNewDispatchingServer(ctx, mountName, mt, options.ServesMountTable(true))
if err != nil {
ctx.Errorf("v23.WithNewServer failed: %v", err)
return "", nil, err
}
stopFuncs = append(stopFuncs, func() {
<-mtServer.Closed()
})
var mtName string
var mtEndpoints []naming.Endpoint
for {
status := mtServer.Status()
mtEndpoints = status.Endpoints
mtName = mtEndpoints[0].Name()
if mtEndpoints[0].Addr().Network() != "bidi" {
break
}
<-status.Dirty
}
ctx.Infof("Mount table service at: %q endpoint: %s", mountName, mtName)
if len(nhName) > 0 {
// The ListenSpec code ensures that we have a valid address here.
host, port, _ := net.SplitHostPort(listenSpec.Addrs[0].Address)
if port != "" {
neighborhoodListenSpec := listenSpec.Copy()
neighborhoodListenSpec.Addrs[0].Address = net.JoinHostPort(host, "0")
ctx = v23.WithListenSpec(ctx, neighborhoodListenSpec)
}
names := []string{}
for _, ep := range mtEndpoints {
names = append(names, ep.Name())
}
var nh rpc.Dispatcher
if host == "127.0.0.1" || host == "localhost" {
nh, err = NewLoopbackNeighborhoodDispatcher(nhName, logLevel, names...)
} else {
nh, err = NewNeighborhoodDispatcher(nhName, logLevel, names...)
}
if err != nil {
ctx.Errorf("NewLoopback or Neighborhood dispatcher (host %s) failed: %v", host, err)
return "", nil, err
}
ctx, nhServer, err := v23.WithNewDispatchingServer(ctx, naming.Join(mtName, "nh"), nh, options.ServesMountTable(true))
if err != nil {
ctx.Errorf("v23.WithNewServer failed: %v", err)
stop()
return "", nil, err
}
stopFuncs = append(stopFuncs, func() {
<-nhServer.Closed()
})
}
return mtName, stop, nil
}