| // 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 android |
| |
| // Package android implements a RuntimeFactory suitable for android. It is |
| // based on the roaming package. |
| // |
| // The pubsub.Publisher mechanism is used for communicating networking |
| // settings to the rpc.Server implementation of the runtime and publishes |
| // the Settings it expects. |
| package android |
| |
| import ( |
| "flag" |
| "os" |
| |
| "v.io/v23" |
| "v.io/v23/context" |
| "v.io/v23/discovery" |
| "v.io/v23/flow" |
| "v.io/v23/rpc" |
| |
| "v.io/x/ref/internal/logger" |
| idiscovery "v.io/x/ref/lib/discovery" |
| |
| dfactory "v.io/x/ref/lib/discovery/factory" |
| "v.io/x/ref/lib/flags" |
| "v.io/x/ref/lib/pubsub" |
| "v.io/x/ref/lib/security/securityflag" |
| "v.io/x/ref/runtime/internal" |
| _ "v.io/x/ref/runtime/internal/flow/protocols/tcp" |
| _ "v.io/x/ref/runtime/internal/flow/protocols/ws" |
| _ "v.io/x/ref/runtime/internal/flow/protocols/wsh" |
| "v.io/x/ref/runtime/internal/lib/appcycle" |
| "v.io/x/ref/runtime/internal/lib/roaming" |
| "v.io/x/ref/runtime/internal/lib/websocket" |
| "v.io/x/ref/runtime/internal/lib/xwebsocket" |
| "v.io/x/ref/runtime/internal/rt" |
| "v.io/x/ref/services/debug/debuglib" |
| |
| // TODO(suharshs): Remove these once we switch to the flow protocols. |
| _ "v.io/x/ref/runtime/internal/rpc/protocols/tcp" |
| _ "v.io/x/ref/runtime/internal/rpc/protocols/ws" |
| _ "v.io/x/ref/runtime/internal/rpc/protocols/wsh" |
| "v.io/x/ref/lib/discovery/plugins/mdns" |
| ) |
| |
| var commonFlags *flags.Flags |
| |
| var bleCreator func(string) (idiscovery.Plugin, error) |
| |
| func init() { |
| v23.RegisterRuntimeFactory(Init) |
| rpc.RegisterUnknownProtocol("wsh", websocket.HybridDial, websocket.HybridResolve, websocket.HybridListener) |
| flow.RegisterUnknownProtocol("wsh", xwebsocket.WSH{}) |
| commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Runtime, flags.Listen) |
| } |
| |
| func Init(ctx *context.T) (v23.Runtime, *context.T, v23.Shutdown, error) { |
| if err := internal.ParseFlagsAndConfigureGlobalLogger(commonFlags); err != nil { |
| return nil, nil, nil, err |
| } |
| |
| ac := appcycle.New() |
| discovery, err := createDiscovery() |
| if err != nil { |
| ac.Shutdown() |
| return nil, nil, nil, err |
| } |
| |
| lf := commonFlags.ListenFlags() |
| listenSpec := rpc.ListenSpec{ |
| Addrs: rpc.ListenAddrs(lf.Addrs), |
| Proxy: lf.Proxy, |
| AddressChooser: internal.NewAddressChooser(logger.Global()), |
| } |
| reservedDispatcher := debuglib.NewDispatcher(securityflag.NewAuthorizerOrDie()) |
| |
| ishutdown := func() { |
| ac.Shutdown() |
| discovery.Close() |
| } |
| |
| publisher := pubsub.NewPublisher() |
| |
| // TODO(suharshs): We can remove the SettingName argument after the transition to new RPC. |
| runtime, ctx, shutdown, err := rt.Init(ctx, ac, discovery, nil, &listenSpec, publisher, roaming.RoamingSetting, commonFlags.RuntimeFlags(), reservedDispatcher) |
| if err != nil { |
| ishutdown() |
| return nil, nil, nil, err |
| } |
| |
| stopRoaming, err := roaming.CreateRoamingStream(ctx, publisher, listenSpec) |
| if err != nil { |
| return nil, nil, nil, err |
| } |
| |
| runtimeFactoryShutdown := func() { |
| ishutdown() |
| shutdown() |
| stopRoaming() |
| } |
| return runtime, ctx, runtimeFactoryShutdown, nil |
| } |
| |
| func createDiscovery() (discovery.T, error) { |
| plugins := make([]idiscovery.Plugin, 2) |
| host, _ := os.Hostname() |
| if host == "" { |
| host = "v23" |
| } |
| var err error |
| plugins[0], err = mdns.New(host) |
| if err != nil { |
| return nil, err |
| } |
| |
| plugins[1], err = bleCreator(host) |
| if err != nil { |
| return nil, err |
| } |
| |
| return idiscovery.NewWithPlugins(plugins...), nil |
| } |
| |
| // SetBleCreator sets the function that will used to create the ble plugin. |
| func SetBleCreator(creator func(string) (idiscovery.Plugin, error)) { |
| bleCreator = creator |
| } |