| package main |
| |
| import ( |
| "log" |
| |
| "v.io/v23" |
| idiscovery "v.io/x/ref/lib/discovery" |
| "v.io/x/ref/lib/discovery/plugins/ble" |
| "v.io/x/ref/lib/discovery/plugins/mdns" |
| |
| _ "v.io/x/ref/runtime/factories/generic" |
| |
| mojom "mojom/vanadium/discovery" |
| "vanadium/discovery/internal" |
| |
| "mojo/public/go/application" |
| "mojo/public/go/bindings" |
| "mojo/public/go/system" |
| ) |
| |
| //#include "mojo/public/c/system/types.h" |
| import "C" |
| |
| type discoveryDelegate struct { |
| stubs []*bindings.Stub |
| impl *internal.DiscoveryService |
| shutdown v23.Shutdown |
| } |
| |
| func (d *discoveryDelegate) Initialize(c application.Context) { |
| ctx, shutdown := v23.Init() |
| host := c.Args()[1] |
| log.Println("Starting discovery with name", host) |
| mplugin, err := mdns.New(host) |
| if err != nil { |
| log.Println("Failed to start mplugin", err) |
| } |
| |
| bplugin, err := ble.NewPlugin(host) |
| if err != nil { |
| log.Println("Failed to start bplugin", err) |
| } |
| |
| d.impl = internal.NewDiscoveryService(ctx, idiscovery.New([]idiscovery.Plugin{mplugin, bplugin})) |
| d.shutdown = shutdown |
| } |
| |
| func (d *discoveryDelegate) Create(request mojom.Discoverer_Request) { |
| stub := mojom.NewDiscovererStub(request, d.impl, bindings.GetAsyncWaiter()) |
| d.stubs = append(d.stubs, stub) |
| go func() { |
| for { |
| if err := stub.ServeRequest(); err != nil { |
| connectionErr, ok := err.(*bindings.ConnectionError) |
| if !ok || !connectionErr.Closed() { |
| log.Println(err) |
| } |
| break |
| } |
| } |
| }() |
| } |
| |
| func (d *discoveryDelegate) AcceptConnection(connection *application.Connection) { |
| connection.ProvideServices(&mojom.Discoverer_ServiceFactory{d}) |
| } |
| |
| func (d *discoveryDelegate) Quit() { |
| d.impl.Stop() |
| d.shutdown() |
| for _, stub := range d.stubs { |
| stub.Close() |
| } |
| } |
| |
| //export MojoMain |
| func MojoMain(handle C.MojoHandle) C.MojoResult { |
| application.Run(&discoveryDelegate{}, system.MojoHandle(handle)) |
| return C.MOJO_RESULT_OK |
| } |
| |
| func main() { |
| } |