rpc/registry.go
crusader 8e43174376 ing
2017-10-31 18:25:44 +09:00

103 lines
2.6 KiB
Go

package rpc
import (
"reflect"
"git.loafle.net/commons_go/rpc/protocol"
)
/**
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
Invoke(codec protocol.RegistryCodec) (result interface{}, err error)
}
// 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.
// - The method has two arguments: *args, *reply.
// - All two arguments are pointers.
// - The first and second arguments are exported or local.
// - The method has return type error.
//
// All other methods are ignored.
func (rr *rpcRegistry) RegisterService(receiver interface{}, name string) error {
return rr.services.register(receiver, name)
}
// HasMethod returns true if the given method is registered.
//
// The method uses a dotted notation as in "Service.Method".
func (rr *rpcRegistry) HasMethod(method string) bool {
if _, _, err := rr.services.get(method); err == nil {
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.
func (rr *rpcRegistry) Invoke(codec protocol.RegistryCodec) (result interface{}, err error) {
serviceSpec, methodSpec, errGet := rr.services.get(codec.Method())
if errGet != nil {
return nil, errGet
}
// Decode the args.
args := reflect.New(methodSpec.argsType)
if errRead := codec.ReadParams(args.Interface()); errRead != nil {
return nil, errRead
}
// 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)
}
if errResult != nil {
return nil, errResult
}
return reply.Interface(), nil
}