blob: 94771ea6f0c52e702c8403f840fe0a95cd85b1fe [file] [log] [blame]
Jiri Simsa5293dcb2014-05-10 09:56:38 -07001package rt
2
3import (
Asim Shankar6a65d282014-06-26 12:12:50 -07004 "fmt"
Cosmos Nicolaou9388ae42014-11-10 10:57:15 -08005 "time"
Asim Shankar6a65d282014-06-26 12:12:50 -07006
Jiri Simsa764efb72014-12-25 20:57:03 -08007 iipc "v.io/core/veyron/runtimes/google/ipc"
8 imanager "v.io/core/veyron/runtimes/google/ipc/stream/manager"
9 "v.io/core/veyron/runtimes/google/ipc/stream/vc"
10 ivtrace "v.io/core/veyron/runtimes/google/vtrace"
Cosmos Nicolaou416ca972014-05-28 22:03:06 -070011
Jiri Simsa764efb72014-12-25 20:57:03 -080012 "v.io/core/veyron2/context"
13 "v.io/core/veyron2/i18n"
14 "v.io/core/veyron2/ipc"
15 "v.io/core/veyron2/ipc/stream"
16 "v.io/core/veyron2/naming"
17 "v.io/core/veyron2/options"
18 "v.io/core/veyron2/verror2"
19 "v.io/core/veyron2/vtrace"
Jiri Simsa5293dcb2014-05-10 09:56:38 -070020)
21
22func (rt *vrt) NewClient(opts ...ipc.ClientOpt) (ipc.Client, error) {
Bogdan Capritafbcfe892014-09-30 16:50:45 -070023 rt.mu.Lock()
24 sm := rt.sm[0]
25 rt.mu.Unlock()
Cosmos Nicolaou4e029972014-06-13 14:53:08 -070026 ns := rt.ns
Jiri Simsa5293dcb2014-05-10 09:56:38 -070027 var otherOpts []ipc.ClientOpt
28 for _, opt := range opts {
29 switch topt := opt.(type) {
Asim Shankarcc044212014-10-15 23:25:26 -070030 case options.StreamManager:
Jiri Simsa5293dcb2014-05-10 09:56:38 -070031 sm = topt.Manager
Asim Shankarcc044212014-10-15 23:25:26 -070032 case options.Namespace:
Cosmos Nicolaou4e029972014-06-13 14:53:08 -070033 ns = topt.Namespace
Jiri Simsa5293dcb2014-05-10 09:56:38 -070034 default:
35 otherOpts = append(otherOpts, opt)
36 }
37 }
Cosmos Nicolaou4e8da642014-11-13 08:32:05 -080038 otherOpts = append(otherOpts, vc.LocalPrincipal{rt.principal}, &imanager.DialTimeout{5 * time.Minute}, rt.preferredProtocols)
Suharsh Sivakumar1b6683e2014-12-30 13:00:38 -080039 return iipc.InternalNewClient(sm, ns, otherOpts...)
Jiri Simsa5293dcb2014-05-10 09:56:38 -070040}
41
42func (rt *vrt) Client() ipc.Client {
43 return rt.client
44}
45
Matt Rosencrantz4f8ac602014-12-29 14:42:48 -080046func (rt *vrt) NewContext() *context.T {
Matt Rosencrantz3395b4e2015-01-13 09:41:18 -080047 var ctx *context.T
Mike Burrows110e22f2014-10-10 10:23:37 -070048 ctx = i18n.ContextWithLangID(ctx, rt.lang)
49 ctx = verror2.ContextWithComponentName(ctx, rt.program)
Matt Rosencrantz5f98d942015-01-08 13:48:30 -080050 ctx = ivtrace.DeprecatedInit(ctx, rt.traceStore)
51 ctx, _ = vtrace.SetNewTrace(ctx)
Matt Rosencrantz5845bf12015-01-06 13:05:21 -080052 return rt.initRuntimeXContext(ctx)
Matt Rosencrantz9fe60822014-09-12 10:09:53 -070053}
54
Jiri Simsa5293dcb2014-05-10 09:56:38 -070055func (rt *vrt) NewServer(opts ...ipc.ServerOpt) (ipc.Server, error) {
Asim Shankar6a65d282014-06-26 12:12:50 -070056 rt.mu.Lock()
Bogdan Capritafbcfe892014-09-30 16:50:45 -070057 // Create a new RoutingID (and StreamManager) for each server, except
58 // the first one. The reasoning is for the first server to share the
59 // RoutingID (and StreamManager) with Clients.
60 //
61 // TODO(ashankar/caprita): special-casing the first server is ugly, and
62 // has diminished practical benefits since the first server in practice
63 // is the app cycle manager server. If the goal of sharing connections
64 // between Clients and Servers in the same Runtime is still important,
65 // we need to think of other ways to achieve it.
66 sm := rt.sm[0]
Asim Shankar6a65d282014-06-26 12:12:50 -070067 rt.nServers++
Bogdan Capritafbcfe892014-09-30 16:50:45 -070068 nServers := rt.nServers
Asim Shankar6a65d282014-06-26 12:12:50 -070069 rt.mu.Unlock()
Bogdan Capritafbcfe892014-09-30 16:50:45 -070070 if nServers > 1 {
71 var err error
72 sm, err = rt.NewStreamManager()
73 if err != nil {
74 return nil, fmt.Errorf("failed to create ipc/stream/Manager: %v", err)
75 }
Asim Shankar6a65d282014-06-26 12:12:50 -070076 }
Cosmos Nicolaou4e029972014-06-13 14:53:08 -070077 ns := rt.ns
Jiri Simsa5293dcb2014-05-10 09:56:38 -070078 var otherOpts []ipc.ServerOpt
79 for _, opt := range opts {
80 switch topt := opt.(type) {
Asim Shankarcc044212014-10-15 23:25:26 -070081 case options.Namespace:
Cosmos Nicolaou4e029972014-06-13 14:53:08 -070082 ns = topt
Jiri Simsa5293dcb2014-05-10 09:56:38 -070083 default:
84 otherOpts = append(otherOpts, opt)
85 }
86 }
Asim Shankar220a0152014-10-30 21:21:09 -070087 // Add the option that provides the principal to the server.
88 otherOpts = append(otherOpts, vc.LocalPrincipal{rt.principal})
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070089 if rt.reservedDisp != nil {
Cosmos Nicolaou6413c992014-11-06 11:00:54 -080090 ropts := options.ReservedNameDispatcher{rt.reservedDisp}
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070091 otherOpts = append(otherOpts, ropts)
92 otherOpts = append(otherOpts, rt.reservedOpts...)
93 }
Ankure49a86a2014-11-11 18:52:43 -080094
Nicolas LaCasse55a10f32014-11-26 13:25:53 -080095 // Add the preferred protocols from the runtime if there are any.
96 if len(rt.preferredProtocols) > 0 {
97 otherOpts = append(otherOpts, iipc.PreferredServerResolveProtocols(rt.preferredProtocols))
98 }
Asim Shankarf4864f42014-11-25 18:53:05 -080099 ctx := rt.NewContext()
Matt Rosencrantz5f98d942015-01-08 13:48:30 -0800100 return iipc.InternalNewServer(ctx, sm, ns, otherOpts...)
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700101}
102
103func (rt *vrt) NewStreamManager(opts ...stream.ManagerOpt) (stream.Manager, error) {
104 rid, err := naming.NewRoutingID()
105 if err != nil {
106 return nil, err
107 }
Asim Shankar6a65d282014-06-26 12:12:50 -0700108 sm := imanager.InternalNew(rid)
Bogdan Capritafbcfe892014-09-30 16:50:45 -0700109 rt.mu.Lock()
110 defer rt.mu.Unlock()
111 if rt.cleaningUp {
112 sm.Shutdown() // For whatever it's worth.
Bogdan Capritafbcfe892014-09-30 16:50:45 -0700113 return nil, errCleaningUp
114 }
115 rt.sm = append(rt.sm, sm)
Asim Shankar6a65d282014-06-26 12:12:50 -0700116 return sm, nil
Jiri Simsa5293dcb2014-05-10 09:56:38 -0700117}