~juju-qa/ubuntu/yakkety/juju/2.0-rc3-again

« back to all changes in this revision

Viewing changes to src/launchpad.net/juju-core/rpc/rpc.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-04-24 22:34:47 UTC
  • Revision ID: package-import@ubuntu.com-20130424223447-f0qdji7ubnyo0s71
Tags: upstream-1.10.0.1
ImportĀ upstreamĀ versionĀ 1.10.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package rpc
 
2
 
 
3
import (
 
4
        "errors"
 
5
        "fmt"
 
6
        "launchpad.net/juju-core/log"
 
7
        "reflect"
 
8
)
 
9
 
 
10
var (
 
11
        errorType     = reflect.TypeOf((*error)(nil)).Elem()
 
12
        interfaceType = reflect.TypeOf((*interface{})(nil)).Elem()
 
13
        stringType    = reflect.TypeOf("")
 
14
)
 
15
 
 
16
var errNilDereference = errors.New("field retrieval from nil reference")
 
17
 
 
18
// Server represents an RPC server.
 
19
type Server struct {
 
20
        // root holds the default root value.
 
21
        root reflect.Value
 
22
 
 
23
        // obtain maps from root-object method name to
 
24
        // information about that method. The term "obtain"
 
25
        // is because these methods obtain an object to
 
26
        // call an action on.
 
27
        obtain map[string]*obtainer
 
28
 
 
29
        // action maps from an object type (as returned by
 
30
        // an obtainer method) to the set of methods on an
 
31
        // object of that type, with information about each
 
32
        // method.
 
33
        action map[reflect.Type]map[string]*action
 
34
 
 
35
        // transformErrors is used to process all errors sent to
 
36
        // the client.
 
37
        transformErrors func(error) error
 
38
}
 
39
 
 
40
// NewServer returns a new server that will serve requests
 
41
// by invoking methods on values of the same type as rootValue.
 
42
// Actual values of rootValue may be provided for
 
43
// each client connection (see ServeCodec and Accept),
 
44
// or rootValue may be used directly if no such values are
 
45
// provided.
 
46
//
 
47
// The server executes each client request by calling a "type" method on
 
48
// a root value to obtain an object to act on; then it invokes an
 
49
// action method on that object with the request parameters, possibly
 
50
// returning some result.
 
51
//
 
52
// Type methods on the root value are of the form:
 
53
//
 
54
//      M(id string) (O, error)
 
55
//
 
56
// where M is an exported name, conventionally naming the object type,
 
57
// id is some identifier for the object and O is the type of the
 
58
// returned object.
 
59
//
 
60
// Request methods defined on O may defined in one of the following
 
61
// forms, where T and R must be struct types.
 
62
//
 
63
//      Method()
 
64
//      Method() R
 
65
//      Method() (R, error)
 
66
//      Method() error
 
67
//      Method(T)
 
68
//      Method(T) R
 
69
//      Method(T) (R, error)
 
70
//      Method(T) error
 
71
//
 
72
// If transformErrors is non-nil, it will be called on all returned
 
73
// non-nil errors, for example to transform the errors into ServerErrors
 
74
// with specified codes.  There will be a panic if transformErrors
 
75
// returns nil.
 
76
func NewServer(rootValue interface{}, transformErrors func(error) error) (*Server, error) {
 
77
        if transformErrors == nil {
 
78
                transformErrors = func(e error) error { return e }
 
79
        }
 
80
        srv := &Server{
 
81
                root:            reflect.ValueOf(rootValue),
 
82
                obtain:          make(map[string]*obtainer),
 
83
                action:          make(map[reflect.Type]map[string]*action),
 
84
                transformErrors: transformErrors,
 
85
        }
 
86
        rt := srv.root.Type()
 
87
        for i := 0; i < rt.NumMethod(); i++ {
 
88
                m := rt.Method(i)
 
89
                o := srv.methodToObtainer(m)
 
90
                if o == nil {
 
91
                        log.Infof("rpc: discarding obtainer method %#v", m)
 
92
                        continue
 
93
                }
 
94
                actions := make(map[string]*action)
 
95
                for i := 0; i < o.ret.NumMethod(); i++ {
 
96
                        m := o.ret.Method(i)
 
97
                        if a := srv.methodToAction(m); a != nil {
 
98
                                actions[m.Name] = a
 
99
                        } else {
 
100
                                log.Infof("rpc: discarding action method %#v", m)
 
101
                        }
 
102
                }
 
103
                if len(actions) > 0 {
 
104
                        srv.action[o.ret] = actions
 
105
                        srv.obtain[m.Name] = o
 
106
                } else {
 
107
                        log.Infof("rpc: discarding obtainer %v because its result has no methods", m.Name)
 
108
                }
 
109
        }
 
110
        if len(srv.obtain) == 0 {
 
111
                return nil, fmt.Errorf("no RPC methods found on %s", rt)
 
112
        }
 
113
        return srv, nil
 
114
}
 
115
 
 
116
// obtainer holds information on a root-level method.
 
117
type obtainer struct {
 
118
        // ret holds the type of the object returned by the method.
 
119
        ret reflect.Type
 
120
 
 
121
        // call invokes the obtainer method. The rcvr parameter must be
 
122
        // the same type as the root object. The given id is passed
 
123
        // as an argument to the method.
 
124
        call func(rcvr reflect.Value, id string) (reflect.Value, error)
 
125
}
 
126
 
 
127
func (o *obtainer) String() string {
 
128
        return fmt.Sprintf("{id -> %s}", o.ret)
 
129
}
 
130
 
 
131
func (srv *Server) methodToObtainer(m reflect.Method) *obtainer {
 
132
        if m.PkgPath != "" {
 
133
                return nil
 
134
        }
 
135
        t := m.Type
 
136
        if t.NumIn() != 2 ||
 
137
                t.NumOut() != 2 ||
 
138
                t.In(1) != stringType ||
 
139
                t.Out(1) != errorType {
 
140
                return nil
 
141
        }
 
142
        f := func(rcvr reflect.Value, id string) (r reflect.Value, err error) {
 
143
                out := rcvr.Method(m.Index).Call([]reflect.Value{reflect.ValueOf(id)})
 
144
                if !out[1].IsNil() {
 
145
                        err = out[1].Interface().(error)
 
146
                }
 
147
                r = out[0]
 
148
                return
 
149
        }
 
150
        return &obtainer{
 
151
                t.Out(0),
 
152
                f,
 
153
        }
 
154
}
 
155
 
 
156
// action holds information about a method on an object
 
157
// that can be obtained from the root object.
 
158
type action struct {
 
159
        // arg holds the argument type of the method, or nil
 
160
        // if there is no argument.
 
161
        arg reflect.Type
 
162
 
 
163
        // ret holds the return type of the method, or nil
 
164
        // if the method returns no value.
 
165
        ret reflect.Type
 
166
 
 
167
        // call calls the action method with the given argument
 
168
        // on the given receiver value. If the method does
 
169
        // not return a value, the returned value will not be valid.
 
170
        call func(rcvr, arg reflect.Value) (reflect.Value, error)
 
171
}
 
172
 
 
173
func (p *action) String() string {
 
174
        return fmt.Sprintf("{%v -> %v}", p.arg, p.ret)
 
175
}
 
176
 
 
177
func (srv *Server) methodToAction(m reflect.Method) *action {
 
178
        if m.PkgPath != "" {
 
179
                return nil
 
180
        }
 
181
        var p action
 
182
        var assemble func(arg reflect.Value) []reflect.Value
 
183
        // N.B. The method type has the receiver as its first argument.
 
184
        t := m.Type
 
185
        switch {
 
186
        case t.NumIn() == 1:
 
187
                // Method() ...
 
188
                assemble = func(arg reflect.Value) []reflect.Value {
 
189
                        return nil
 
190
                }
 
191
        case t.NumIn() == 2:
 
192
                // Method(T) ...
 
193
                p.arg = t.In(1)
 
194
                assemble = func(arg reflect.Value) []reflect.Value {
 
195
                        return []reflect.Value{arg}
 
196
                }
 
197
        default:
 
198
                return nil
 
199
        }
 
200
 
 
201
        switch {
 
202
        case t.NumOut() == 0:
 
203
                // Method(...)
 
204
                p.call = func(rcvr, arg reflect.Value) (r reflect.Value, err error) {
 
205
                        rcvr.Method(m.Index).Call(assemble(arg))
 
206
                        return
 
207
                }
 
208
        case t.NumOut() == 1 && t.Out(0) == errorType:
 
209
                // Method(...) error
 
210
                p.call = func(rcvr, arg reflect.Value) (r reflect.Value, err error) {
 
211
                        out := rcvr.Method(m.Index).Call(assemble(arg))
 
212
                        if !out[0].IsNil() {
 
213
                                err = out[0].Interface().(error)
 
214
                        }
 
215
                        return
 
216
                }
 
217
        case t.NumOut() == 1:
 
218
                // Method(...) R
 
219
                p.ret = t.Out(0)
 
220
                p.call = func(rcvr, arg reflect.Value) (reflect.Value, error) {
 
221
                        out := rcvr.Method(m.Index).Call(assemble(arg))
 
222
                        return out[0], nil
 
223
                }
 
224
        case t.NumOut() == 2 && t.Out(1) == errorType:
 
225
                // Method(...) (R, error)
 
226
                p.ret = t.Out(0)
 
227
                p.call = func(rcvr, arg reflect.Value) (r reflect.Value, err error) {
 
228
                        out := rcvr.Method(m.Index).Call(assemble(arg))
 
229
                        r = out[0]
 
230
                        if !out[1].IsNil() {
 
231
                                err = out[1].Interface().(error)
 
232
                        }
 
233
                        return
 
234
                }
 
235
        default:
 
236
                return nil
 
237
        }
 
238
        if p.arg != nil && p.arg.Kind() != reflect.Struct {
 
239
                return nil
 
240
        }
 
241
        if p.ret != nil && p.ret.Kind() != reflect.Struct {
 
242
                return nil
 
243
        }
 
244
        return &p
 
245
}