rpc/registry.go

103 lines
2.6 KiB
Go
Raw Normal View History

2017-10-25 14:52:47 +00:00
package rpc
import (
"reflect"
2017-10-26 07:21:35 +00:00
"git.loafle.net/commons_go/rpc/protocol"
2017-10-25 14:52:47 +00:00
)
/**
Network connection
Handshake(1..n)
Send request
HTTP
*/
// NewRPCRegistry returns a new RPC registry.
func NewRegistry() Registry {
return &rpcRegistry{
services: new(serviceMap),
}
}
type Registry interface {
RegisterService(receiver interface{}, name string) error
HasMethod(method string) bool
2017-10-31 09:25:44 +00:00
Invoke(codec protocol.RegistryCodec) (result interface{}, err error)
2017-10-25 14:52:47 +00:00
}
// RPCRegistry serves registered RPC services using registered codecs.
type rpcRegistry struct {
services *serviceMap
}
// RegisterService adds a new service to the server.
//
// The name parameter is optional: if empty it will be inferred from
// the receiver type name.
//
// Methods from the receiver will be extracted if these rules are satisfied:
//
// - The receiver is exported (begins with an upper case letter) or local
// (defined in the package registering the service).
// - The method name is exported.
2017-10-27 04:00:30 +00:00
// - The method has two arguments: *args, *reply.
// - All two arguments are pointers.
// - The first and second arguments are exported or local.
2017-10-25 14:52:47 +00:00
// - The method has return type error.
//
// All other methods are ignored.
2017-10-26 07:21:35 +00:00
func (rr *rpcRegistry) RegisterService(receiver interface{}, name string) error {
return rr.services.register(receiver, name)
2017-10-25 14:52:47 +00:00
}
// HasMethod returns true if the given method is registered.
//
// The method uses a dotted notation as in "Service.Method".
2017-10-26 07:21:35 +00:00
func (rr *rpcRegistry) HasMethod(method string) bool {
if _, _, err := rr.services.get(method); err == nil {
2017-10-25 14:52:47 +00:00
return true
}
return false
}
// Invoke execute a method.
//
// Codecs are defined to process a given serialization scheme, e.g., JSON or
// XML. A codec is chosen based on the "Content-Type" header from the request,
// excluding the charset definition.
2017-10-31 09:25:44 +00:00
func (rr *rpcRegistry) Invoke(codec protocol.RegistryCodec) (result interface{}, err error) {
serviceSpec, methodSpec, errGet := rr.services.get(codec.Method())
2017-10-25 14:52:47 +00:00
if errGet != nil {
2017-10-31 09:25:44 +00:00
return nil, errGet
2017-10-25 14:52:47 +00:00
}
// Decode the args.
args := reflect.New(methodSpec.argsType)
2017-10-31 09:25:44 +00:00
if errRead := codec.ReadParams(args.Interface()); errRead != nil {
return nil, errRead
2017-10-25 14:52:47 +00:00
}
// Call the service method.
reply := reflect.New(methodSpec.replyType)
errValue := methodSpec.method.Func.Call([]reflect.Value{
serviceSpec.rcvr,
args,
reply,
})
// Cast the result to error if needed.
var errResult error
errInter := errValue[0].Interface()
if errInter != nil {
errResult = errInter.(error)
}
2017-10-26 07:21:35 +00:00
if errResult != nil {
2017-10-31 09:25:44 +00:00
return nil, errResult
2017-10-25 14:52:47 +00:00
}
2017-10-31 09:25:44 +00:00
return reply.Interface(), nil
2017-10-25 14:52:47 +00:00
}