blob: 57b7f2c21466dd581f2d4a62144f712d00b26257 [file] [log] [blame]
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -08001package static
2
3import (
4 "flag"
Suharsh Sivakumard5049b72015-01-21 14:11:35 -08005 "os"
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -08006
7 "v.io/core/veyron2"
8 "v.io/core/veyron2/context"
9 "v.io/core/veyron2/ipc"
Nicolas LaCasse0a22b452015-01-22 09:57:15 -080010 "v.io/core/veyron2/ipc/stream"
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080011 "v.io/core/veyron2/vlog"
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080012
13 "v.io/core/veyron/lib/appcycle"
14 "v.io/core/veyron/lib/flags"
15 "v.io/core/veyron/lib/netstate"
Nicolas LaCasse0a22b452015-01-22 09:57:15 -080016 "v.io/core/veyron/lib/websocket"
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080017 "v.io/core/veyron/profiles/internal"
18 _ "v.io/core/veyron/runtimes/google/ipc/protocols/tcp"
19 _ "v.io/core/veyron/runtimes/google/ipc/protocols/ws"
20 _ "v.io/core/veyron/runtimes/google/ipc/protocols/wsh"
21 grt "v.io/core/veyron/runtimes/google/rt"
22 "v.io/core/veyron/services/mgmt/debug"
23
24 // TODO(cnicolaou,ashankar): move this into flags.
25 sflag "v.io/core/veyron/security/flag"
26)
27
28var (
29 commonFlags *flags.Flags
30)
31
32func init() {
33 commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Listen)
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080034 veyron2.RegisterProfileInit(Init)
Nicolas LaCasse0a22b452015-01-22 09:57:15 -080035 stream.RegisterUnknownProtocol("wsh", websocket.HybridDial, websocket.HybridListener)
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080036}
37
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080038func Init(ctx *context.T) (veyron2.RuntimeX, *context.T, veyron2.Shutdown, error) {
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080039 log := vlog.Log
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080040
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080041 reservedDispatcher := debug.NewDispatcher(log.LogDir(), sflag.NewAuthorizerOrDie())
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080042
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080043 commonFlags.Parse(os.Args[1:], nil)
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080044 lf := commonFlags.ListenFlags()
45 listenSpec := ipc.ListenSpec{
46 Addrs: ipc.ListenAddrs(lf.Addrs),
47 Proxy: lf.ListenProxy,
48 }
49
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
53 // configuration. GCEPublicAddress returns a non-nil addr if we are running on GCE.
54 if !internal.HasPublicIP(log) {
55 if addr := internal.GCEPublicAddress(log); addr != nil {
56 listenSpec.AddressChooser = func(string, []ipc.Address) ([]ipc.Address, error) {
57 return []ipc.Address{&netstate.AddrIfc{addr, "nat", nil}}, nil
58 }
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080059 runtime, ctx, shutdown, err := grt.Init(ctx, ac, nil, &listenSpec, reservedDispatcher)
60 if err != nil {
61 return nil, nil, nil, err
62 }
Matt Rosencrantzfa3082c2015-01-22 21:39:04 -080063 profileShutdown := func() {
64 ac.Shutdown()
65 shutdown()
66 }
67 return runtime, ctx, profileShutdown, nil
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080068 }
69 }
70 listenSpec.AddressChooser = internal.IPAddressChooser
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080071
72 runtime, ctx, shutdown, err := grt.Init(ctx, ac, nil, &listenSpec, reservedDispatcher)
73 if err != nil {
74 return nil, nil, shutdown, err
75 }
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080076
77 profileShutdown := func() {
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080078 ac.Shutdown()
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080079 shutdown()
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080080 }
81 return runtime, ctx, profileShutdown, nil
Suharsh Sivakumar033a30e2015-01-07 13:54:43 -080082}