-
Notifications
You must be signed in to change notification settings - Fork 1
/
discovery.go
134 lines (109 loc) · 3.08 KB
/
discovery.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
131
132
133
134
// 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.
// +build mojo
package main
import (
"flag"
"sync"
"mojo/public/go/application"
"mojo/public/go/bindings"
"mojo/public/go/system"
mojom "mojom/v.io/discovery"
"v.io/v23"
"v.io/v23/context"
"v.io/v23/rpc"
idiscovery "v.io/x/ref/lib/discovery"
fdiscovery "v.io/x/ref/lib/discovery/factory"
"v.io/x/ref/lib/discovery/plugins/mock"
"v.io/x/ref/runtime/factories/roaming"
"v.io/x/ref/services/mounttable/mounttablelib"
"v.io/mojo/discovery/internal"
)
//#include "mojo/public/c/system/handle.h"
import "C"
var (
flagTestMode = flag.Bool("test-mode", false, "should only be true for apptests.")
)
type delegate struct {
ctx *context.T
shutdown v23.Shutdown
mu sync.Mutex
stubs map[*bindings.Stub]struct{} // GUARDED_BY(mu)
}
func (d *delegate) Initialize(mctx application.Context) {
// TODO(bjornick): Calling init multiple times in the same process
// will be bad. For now, this is ok because this is the only
// vanadium service that will be used in the demos and each go library
// will be in its own process.
roaming.SetArgs(mctx)
d.ctx, d.shutdown = v23.Init()
if *flagTestMode {
// Inject a mock plugin.
df, _ := idiscovery.NewFactory(d.ctx, mock.New())
fdiscovery.InjectFactory(df)
// Start a mounttable and set the namespace roots.
//
// Note that we need to listen on a local IP address in order to
// accept connections within a GCE instance.
d.ctx = v23.WithListenSpec(d.ctx, rpc.ListenSpec{Addrs: rpc.ListenAddrs{{Protocol: "tcp", Address: "127.0.0.1:0"}}})
name, _, err := mounttablelib.StartServers(d.ctx, v23.GetListenSpec(d.ctx), "", "", "", "", "mounttable")
if err != nil {
panic(err)
}
ns := v23.GetNamespace(d.ctx)
ns.SetRoots(name)
}
}
func (d *delegate) AcceptConnection(connection *application.Connection) {
f := &factory{d, connection.ConnectionURL()}
connection.ProvideServices(&mojom.Discovery_ServiceFactory{f})
}
func (d *delegate) run(stub *bindings.Stub, done func()) {
d.mu.Lock()
d.stubs[stub] = struct{}{}
d.mu.Unlock()
go func() {
defer done()
for {
if err := stub.ServeRequest(); err != nil {
connErr, ok := err.(*bindings.ConnectionError)
if !ok || !connErr.Closed() {
d.ctx.Error(err)
}
break
}
}
d.mu.Lock()
delete(d.stubs, stub)
d.mu.Unlock()
}()
}
func (d *delegate) Quit() {
d.mu.Lock()
for stub := range d.stubs {
stub.Close()
}
d.mu.Unlock()
d.shutdown()
}
type factory struct {
d *delegate
url string
}
func (f *factory) Create(request mojom.Discovery_Request) {
discovery, err := internal.NewDiscovery(f.d.ctx, f.url)
if err != nil {
f.d.ctx.Error(err)
request.Close()
return
}
stub := mojom.NewDiscoveryStub(request, discovery, bindings.GetAsyncWaiter())
f.d.run(stub, discovery.Close)
}
//export MojoMain
func MojoMain(handle C.MojoHandle) C.MojoResult {
application.Run(&delegate{stubs: map[*bindings.Stub]struct{}{}}, system.MojoHandle(handle))
return C.MOJO_RESULT_OK
}
func main() {}