Matt Rosencrantz | e4a49f4 | 2015-09-03 17:08:51 -0700 | [diff] [blame] | 1 | // 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 | |
| 5 | package dispatcher |
| 6 | |
| 7 | import ( |
| 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. |
| 19 | type 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. |
| 26 | func (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. |
| 32 | func (w *DispatcherWrapper) SetDispatcher(d rpc.Dispatcher) { |
| 33 | w.wrapped = d |
| 34 | close(w.wrappedIsSet) |
| 35 | } |
| 36 | |
| 37 | // NewDispatcherWrapper creates a new DispatcherWrapper. |
| 38 | func NewDispatcherWrapper() *DispatcherWrapper { |
| 39 | return &DispatcherWrapper{ |
| 40 | wrappedIsSet: make(chan struct{}), |
| 41 | } |
| 42 | } |