blob: b8aaf19be024a78e2b6d6ffc862fd719f7c18665 [file] [log] [blame]
Matt Rosencrantze4a49f42015-09-03 17:08:51 -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
5package dispatcher
6
7import (
8 "v.io/v23/context"
9 "v.io/v23/rpc"
10 "v.io/v23/security"
11)
12
13// DispatcherWrapper is used when a dispatcher can't be constructed at server
14// creation time. The most common use for this is when the dispatcher needs
15// to know some information about the server to be constructed. For example
16// it is sometimes helpful to know the server's endpoints.
17// In such cases you can construct a DispatcherWrapper which will simply block
18// all lookups until the real dispatcher is set with SetDispatcher.
19type DispatcherWrapper struct {
20 wrapped rpc.Dispatcher
21 wrappedIsSet chan struct{}
22}
23
24// Lookup will wait until SetDispatcher is called and then simply forward requests
25// to the underlying dispatcher.
26func (w *DispatcherWrapper) Lookup(ctx *context.T, suffix string) (interface{}, security.Authorizer, error) {
27 <-w.wrappedIsSet
28 return w.wrapped.Lookup(ctx, suffix)
29}
30
31// SetDispatcher sets the underlying dispatcher and allows Lookups to proceed.
32func (w *DispatcherWrapper) SetDispatcher(d rpc.Dispatcher) {
33 w.wrapped = d
34 close(w.wrappedIsSet)
35}
36
37// NewDispatcherWrapper creates a new DispatcherWrapper.
38func NewDispatcherWrapper() *DispatcherWrapper {
39 return &DispatcherWrapper{
40 wrappedIsSet: make(chan struct{}),
41 }
42}