Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 1 | package impl |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | |
Matt Rosencrantz | 5180d16 | 2014-12-03 13:48:40 -0800 | [diff] [blame] | 7 | "veyron.io/veyron/veyron2" |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 8 | "veyron.io/veyron/veyron2/ipc" |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 9 | "veyron.io/veyron/veyron2/naming" |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 10 | "veyron.io/veyron/veyron2/services/security/access" |
Todd Wang | 67fc8d7 | 2014-12-03 14:36:33 -0800 | [diff] [blame] | 11 | "veyron.io/veyron/veyron2/vdl/vdlroot/src/signature" |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | // proxyInvoker is an ipc.Invoker implementation that proxies all requests |
| 15 | // to a remote object, i.e. requests to <suffix> are forwarded to |
| 16 | // <remote> transparently. |
| 17 | // |
| 18 | // remote is the name of the remote object. |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 19 | // access is the access tag require to access the object. |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 20 | // sigStub is used to get the signature of the remote object. |
| 21 | type proxyInvoker struct { |
| 22 | remote string |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 23 | access access.Tag |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 24 | sigStub signatureStub |
| 25 | } |
| 26 | |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 27 | var _ ipc.Invoker = (*proxyInvoker)(nil) |
| 28 | |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 29 | type signatureStub interface { |
Todd Wang | 1fe7cdd | 2014-11-12 12:51:49 -0800 | [diff] [blame] | 30 | Signature(ipc.ServerContext) (ipc.ServiceSignature, error) |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Asim Shankar | 214f89c | 2014-11-03 16:35:47 -0800 | [diff] [blame] | 33 | func (p *proxyInvoker) Prepare(method string, numArgs int) (argptrs, tags []interface{}, err error) { |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 34 | argptrs = make([]interface{}, numArgs) |
| 35 | for i, _ := range argptrs { |
| 36 | var x interface{} |
| 37 | argptrs[i] = &x |
| 38 | } |
Asim Shankar | 6888519 | 2014-11-26 12:48:35 -0800 | [diff] [blame] | 39 | tags = []interface{}{p.access} |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 40 | return |
| 41 | } |
| 42 | |
| 43 | func (p *proxyInvoker) Invoke(method string, inCall ipc.ServerCall, argptrs []interface{}) (results []interface{}, err error) { |
| 44 | // We accept any values as argument and pass them through to the remote |
| 45 | // server. |
| 46 | args := make([]interface{}, len(argptrs)) |
| 47 | for i, ap := range argptrs { |
| 48 | args[i] = ap |
| 49 | } |
Matt Rosencrantz | 5180d16 | 2014-12-03 13:48:40 -0800 | [diff] [blame] | 50 | runtime := veyron2.RuntimeFromContext(inCall) |
| 51 | outCall, err := runtime.Client().StartCall(inCall, p.remote, method, args) |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | // Each RPC has a bi-directional stream, and there is no way to know in |
| 57 | // advance how much data will be sent in either direction, if any. |
| 58 | // |
| 59 | // This method (Invoke) must return when the remote server is done with |
| 60 | // the RPC, which is when outCall.Recv() returns EOF. When that happens, |
| 61 | // we need to call outCall.Finish() to get the return values, and then |
| 62 | // return these values to the client. |
| 63 | // |
| 64 | // While we are forwarding data from the server to the client, we must |
| 65 | // also forward data from the client to the server. This happens in a |
| 66 | // separate goroutine. This goroutine may return after Invoke has |
| 67 | // returned if the client doesn't call CloseSend() explicitly. |
| 68 | // |
| 69 | // Any error, other than EOF, will be returned to the client, if |
| 70 | // possible. The only situation where it is not possible to send an |
| 71 | // error to the client is when the error comes from forwarding data from |
| 72 | // the client to the server and Invoke has already returned or is about |
| 73 | // to return. In this case, the error is lost. So, it is possible that |
| 74 | // the client could successfully Send() data that the server doesn't |
| 75 | // actually receive if the server terminates the RPC while the data is |
| 76 | // in the proxy. |
| 77 | fwd := func(src, dst ipc.Stream, errors chan<- error) { |
| 78 | for { |
| 79 | var obj interface{} |
| 80 | switch err := src.Recv(&obj); err { |
| 81 | case io.EOF: |
| 82 | if call, ok := src.(ipc.Call); ok { |
| 83 | if err := call.CloseSend(); err != nil { |
| 84 | errors <- err |
| 85 | } |
| 86 | } |
| 87 | return |
| 88 | case nil: |
| 89 | break |
| 90 | default: |
| 91 | errors <- err |
| 92 | return |
| 93 | } |
| 94 | if err := dst.Send(obj); err != nil { |
| 95 | errors <- err |
| 96 | return |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | errors := make(chan error, 2) |
| 101 | go fwd(inCall, outCall, errors) |
| 102 | fwd(outCall, inCall, errors) |
| 103 | select { |
| 104 | case err := <-errors: |
| 105 | return nil, err |
| 106 | default: |
| 107 | } |
| 108 | |
| 109 | nResults, err := p.numResults(method) |
| 110 | if err != nil { |
| 111 | return nil, err |
| 112 | } |
| 113 | |
| 114 | // We accept any return values, without type checking, and return them |
| 115 | // to the client. |
| 116 | res := make([]interface{}, nResults) |
| 117 | for i := 0; i < len(res); i++ { |
| 118 | var foo interface{} |
| 119 | res[i] = &foo |
| 120 | } |
| 121 | err = outCall.Finish(res...) |
| 122 | results = make([]interface{}, len(res)) |
| 123 | for i, r := range res { |
| 124 | results[i] = *r.(*interface{}) |
| 125 | } |
| 126 | return results, err |
| 127 | } |
| 128 | |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 129 | // TODO(toddw): Expose a helper function that performs all error checking based |
| 130 | // on reflection, to simplify the repeated logic processing results. |
Todd Wang | 67fc8d7 | 2014-12-03 14:36:33 -0800 | [diff] [blame] | 131 | func (p *proxyInvoker) Signature(ctx ipc.ServerContext) ([]signature.Interface, error) { |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 132 | call, ok := ctx.(ipc.ServerCall) |
| 133 | if !ok { |
| 134 | return nil, fmt.Errorf("couldn't upgrade ipc.ServerContext to ipc.ServerCall") |
| 135 | } |
| 136 | results, err := p.Invoke(ipc.ReservedSignature, call, nil) |
| 137 | if err != nil { |
| 138 | return nil, err |
| 139 | } |
| 140 | if len(results) != 2 { |
| 141 | return nil, fmt.Errorf("unexpected number of result values. Got %d, want 2.", len(results)) |
| 142 | } |
| 143 | if results[1] != nil { |
| 144 | err, ok := results[1].(error) |
| 145 | if !ok { |
| 146 | return nil, fmt.Errorf("unexpected error type. Got %T, want error.", err) |
| 147 | } |
| 148 | return nil, err |
| 149 | } |
Todd Wang | 67fc8d7 | 2014-12-03 14:36:33 -0800 | [diff] [blame] | 150 | var res []signature.Interface |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 151 | if results[0] != nil { |
Todd Wang | 67fc8d7 | 2014-12-03 14:36:33 -0800 | [diff] [blame] | 152 | sig, ok := results[0].([]signature.Interface) |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 153 | if !ok { |
Todd Wang | 67fc8d7 | 2014-12-03 14:36:33 -0800 | [diff] [blame] | 154 | return nil, fmt.Errorf("unexpected result value type. Got %T, want []signature.Interface.", sig) |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | return res, nil |
| 158 | } |
| 159 | |
Todd Wang | 67fc8d7 | 2014-12-03 14:36:33 -0800 | [diff] [blame] | 160 | func (p *proxyInvoker) MethodSignature(ctx ipc.ServerContext, method string) (signature.Method, error) { |
| 161 | empty := signature.Method{} |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 162 | call, ok := ctx.(ipc.ServerCall) |
| 163 | if !ok { |
| 164 | return empty, fmt.Errorf("couldn't upgrade ipc.ServerContext to ipc.ServerCall") |
| 165 | } |
| 166 | results, err := p.Invoke(ipc.ReservedMethodSignature, call, []interface{}{&method}) |
| 167 | if err != nil { |
| 168 | return empty, err |
| 169 | } |
| 170 | if len(results) != 2 { |
| 171 | return empty, fmt.Errorf("unexpected number of result values. Got %d, want 2.", len(results)) |
| 172 | } |
| 173 | if results[1] != nil { |
| 174 | err, ok := results[1].(error) |
| 175 | if !ok { |
| 176 | return empty, fmt.Errorf("unexpected error type. Got %T, want error.", err) |
| 177 | } |
| 178 | return empty, err |
| 179 | } |
Todd Wang | 67fc8d7 | 2014-12-03 14:36:33 -0800 | [diff] [blame] | 180 | var res signature.Method |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 181 | if results[0] != nil { |
Todd Wang | 67fc8d7 | 2014-12-03 14:36:33 -0800 | [diff] [blame] | 182 | sig, ok := results[0].(signature.Method) |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 183 | if !ok { |
Todd Wang | 67fc8d7 | 2014-12-03 14:36:33 -0800 | [diff] [blame] | 184 | return empty, fmt.Errorf("unexpected result value type. Got %T, want signature.Method.", sig) |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | return res, nil |
| 188 | } |
| 189 | |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 190 | func (p *proxyInvoker) Globber() *ipc.GlobState { |
Robin Thellend | 8e9cc24 | 2014-11-26 09:43:10 -0800 | [diff] [blame] | 191 | return &ipc.GlobState{AllGlobber: p} |
Robin Thellend | b16d716 | 2014-11-07 13:47:26 -0800 | [diff] [blame] | 192 | } |
| 193 | |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 194 | type call struct { |
| 195 | ipc.ServerContext |
| 196 | ch chan<- naming.VDLMountEntry |
| 197 | } |
| 198 | |
| 199 | func (c *call) Recv(v interface{}) error { |
| 200 | return io.EOF |
| 201 | } |
| 202 | |
| 203 | func (c *call) Send(v interface{}) error { |
| 204 | c.ch <- v.(naming.VDLMountEntry) |
| 205 | return nil |
| 206 | } |
| 207 | |
| 208 | func (p *proxyInvoker) Glob__(ctx ipc.ServerContext, pattern string) (<-chan naming.VDLMountEntry, error) { |
| 209 | ch := make(chan naming.VDLMountEntry) |
| 210 | go func() { |
| 211 | p.Invoke(ipc.GlobMethod, &call{ctx, ch}, []interface{}{&pattern}) |
| 212 | close(ch) |
| 213 | }() |
| 214 | return ch, nil |
Robin Thellend | 94bc464 | 2014-11-05 18:05:08 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 217 | // numResults returns the number of result values for the given method. |
| 218 | func (p *proxyInvoker) numResults(method string) (int, error) { |
Todd Wang | 5739dda | 2014-11-16 22:44:02 -0800 | [diff] [blame] | 219 | // TODO(toddw): Replace this mechanism when the new signature mechanism is |
| 220 | // complete. |
| 221 | switch method { |
| 222 | case ipc.GlobMethod: |
| 223 | return 1, nil |
| 224 | case ipc.ReservedSignature, ipc.ReservedMethodSignature: |
| 225 | return 2, nil |
| 226 | } |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 227 | sig, err := p.sigStub.Signature(nil) |
| 228 | if err != nil { |
| 229 | return 0, err |
| 230 | } |
Robin Thellend | b9dd9bb | 2014-10-29 13:54:08 -0700 | [diff] [blame] | 231 | m, ok := sig.Methods[method] |
| 232 | if !ok { |
| 233 | return 0, fmt.Errorf("unknown method %q", method) |
| 234 | } |
| 235 | return len(m.OutArgs), nil |
| 236 | } |