blob: b04189ecb413665b729d39bd3bc76122fb7ea824 [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
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -08005package profiles
6
7import (
Suharsh Sivakumard68949c2015-01-26 10:32:23 -08008 "flag"
9
Jiri Simsa6ac95222015-02-23 16:11:49 -080010 "v.io/v23"
11 "v.io/v23/context"
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070012 "v.io/v23/rpc"
Jiri Simsa337af232015-02-27 14:36:46 -080013 "v.io/x/lib/vlog"
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -080014
Jiri Simsaffceefa2015-02-28 11:03:34 -080015 "v.io/x/ref/lib/flags"
Jiri Simsaffceefa2015-02-28 11:03:34 -080016 "v.io/x/ref/profiles/internal"
Matt Rosencrantz86ba1a12015-03-09 13:19:02 -070017 "v.io/x/ref/profiles/internal/lib/appcycle"
18 "v.io/x/ref/profiles/internal/lib/websocket"
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070019 _ "v.io/x/ref/profiles/internal/rpc/protocols/tcp"
20 _ "v.io/x/ref/profiles/internal/rpc/protocols/ws"
21 _ "v.io/x/ref/profiles/internal/rpc/protocols/wsh"
Matt Rosencrantzdbc1be22015-02-28 15:15:49 -080022 grt "v.io/x/ref/profiles/internal/rt"
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -080023)
24
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080025var commonFlags *flags.Flags
26
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -080027func init() {
Suharsh Sivakumarc6f38e92015-03-31 13:04:06 -070028 v23.RegisterProfile(Init)
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070029 rpc.RegisterUnknownProtocol("wsh", websocket.HybridDial, websocket.HybridListener)
Cosmos Nicolaouf016f4a2015-03-24 12:10:26 -070030 flags.SetDefaultHostPort(":0")
Suharsh Sivakumar9d17e4a2015-02-02 22:42:16 -080031 commonFlags = flags.CreateAndRegister(flag.CommandLine, flags.Runtime, flags.Listen)
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -080032}
33
Jiri Simsa6ac95222015-02-23 16:11:49 -080034func Init(ctx *context.T) (v23.Runtime, *context.T, v23.Shutdown, error) {
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080035 if err := internal.ParseFlags(commonFlags); err != nil {
36 return nil, nil, nil, err
37 }
38
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080039 ac := appcycle.New()
40
Suharsh Sivakumar9d17e4a2015-02-02 22:42:16 -080041 lf := commonFlags.ListenFlags()
Matt Rosencrantz94502cf2015-03-18 09:43:44 -070042 listenSpec := rpc.ListenSpec{
43 Addrs: rpc.ListenAddrs(lf.Addrs),
Suharsh Sivakumar9d17e4a2015-02-02 22:42:16 -080044 AddressChooser: internal.IPAddressChooser,
45 Proxy: lf.ListenProxy,
46 }
47
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080048 runtime, ctx, shutdown, err := grt.Init(ctx,
49 ac,
50 nil,
Suharsh Sivakumar9d17e4a2015-02-02 22:42:16 -080051 &listenSpec,
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080052 commonFlags.RuntimeFlags(),
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080053 nil)
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -080054 if err != nil {
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080055 return nil, nil, nil, err
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -080056 }
Suharsh Sivakumard68949c2015-01-26 10:32:23 -080057 vlog.Log.VI(1).Infof("Initializing generic profile.")
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -080058
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080059 profileShutdown := func() {
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080060 ac.Shutdown()
Suharsh Sivakumard5049b72015-01-21 14:11:35 -080061 shutdown()
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -080062 }
Matt Rosencrantzaeed5d52015-01-14 15:18:34 -080063 return runtime, ctx, profileShutdown, nil
Suharsh Sivakumar628a8ee2015-01-14 11:38:56 -080064}