blob: 339e32a7f9d4c1287cebb8404a147f6c1f291c34 [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Todd Wang8c4e5cc2015-04-09 11:30:52 -07005// Package static implements a profile for static network configurations.
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -08006package static
7
8import (
9 "flag"
Cosmos Nicolaouaa87e292015-04-21 22:15:50 -070010 "net"
11
12 "v.io/x/lib/vlog"
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080013
Jiri Simsa6ac95222015-02-23 16:11:49 -080014 "v.io/v23"
15 "v.io/v23/context"
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070016 "v.io/v23/rpc"
Cosmos Nicolaouaa87e292015-04-21 22:15:50 -070017
Jiri Simsaffceefa2015-02-28 11:03:34 -080018 "v.io/x/ref/lib/flags"
Todd Wangb3511492015-04-07 23:32:34 -070019 "v.io/x/ref/lib/security/securityflag"
Jiri Simsaffceefa2015-02-28 11:03:34 -080020 "v.io/x/ref/profiles/internal"
Matt Rosencrantz86ba1a12015-03-09 13:19:02 -070021 "v.io/x/ref/profiles/internal/lib/appcycle"
22 "v.io/x/ref/profiles/internal/lib/websocket"
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070023 _ "v.io/x/ref/profiles/internal/rpc/protocols/tcp"
24 _ "v.io/x/ref/profiles/internal/rpc/protocols/ws"
25 _ "v.io/x/ref/profiles/internal/rpc/protocols/wsh"
Todd Wangb3511492015-04-07 23:32:34 -070026 "v.io/x/ref/profiles/internal/rt"
Todd Wang1ea8f192015-04-03 17:31:51 -070027 "v.io/x/ref/services/debug/debuglib"
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080028)
29
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080030var commonFlags *flags.Flags
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080031
32func init() {
Suharsh Sivakumarc6f38e92015-03-31 13:04:06 -070033 v23.RegisterProfile(Init)
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070034 rpc.RegisterUnknownProtocol("wsh", websocket.HybridDial, websocket.HybridListener)
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080035 commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Runtime, flags.Listen)
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080036}
37
Jiri Simsa6ac95222015-02-23 16:11:49 -080038func Init(ctx *context.T) (v23.Runtime, *context.T, v23.Shutdown, error) {
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080039 if err := internal.ParseFlags(commonFlags); err != nil {
Matt Rosencrantz1b793912015-01-23 13:32:53 -080040 return nil, nil, nil, err
41 }
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080042
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080043 lf := commonFlags.ListenFlags()
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070044 listenSpec := rpc.ListenSpec{
45 Addrs: rpc.ListenAddrs(lf.Addrs),
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080046 Proxy: lf.ListenProxy,
47 }
Todd Wangb3511492015-04-07 23:32:34 -070048 reservedDispatcher := debuglib.NewDispatcher(vlog.Log.LogDir, securityflag.NewAuthorizerOrDie())
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080049
50 ac := appcycle.New()
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080051
52 // Our address is private, so we test for running on GCE and for its 1:1 NAT
Cosmos Nicolaouaa87e292015-04-21 22:15:50 -070053 // configuration. GCEPublicAddress returns a non-nil addr if we are
54 // running on GCE.
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080055 if !internal.HasPublicIP(vlog.Log) {
56 if addr := internal.GCEPublicAddress(vlog.Log); addr != nil {
Cosmos Nicolaouaa87e292015-04-21 22:15:50 -070057 listenSpec.AddressChooser = func(string, []net.Addr) ([]net.Addr, error) {
58 return []net.Addr{addr}, nil
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080059 }
Todd Wangb3511492015-04-07 23:32:34 -070060 runtime, ctx, shutdown, err := rt.Init(ctx, ac, nil, &listenSpec, commonFlags.RuntimeFlags(), reservedDispatcher)
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080061 if err != nil {
62 return nil, nil, nil, err
63 }
Matt Rosencrantzfa3082c2015-01-22 21:39:04 -080064 profileShutdown := func() {
65 ac.Shutdown()
66 shutdown()
67 }
68 return runtime, ctx, profileShutdown, nil
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080069 }
70 }
71 listenSpec.AddressChooser = internal.IPAddressChooser
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080072
Todd Wangb3511492015-04-07 23:32:34 -070073 runtime, ctx, shutdown, err := rt.Init(ctx, ac, nil, &listenSpec, commonFlags.RuntimeFlags(), reservedDispatcher)
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080074 if err != nil {
75 return nil, nil, shutdown, err
76 }
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080077
78 profileShutdown := func() {
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080079 ac.Shutdown()
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080080 shutdown()
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080081 }
82 return runtime, ctx, profileShutdown, nil
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080083}