From 3fa6dcaca490b098ea64f55c5a6ada20ca865597 Mon Sep 17 00:00:00 2001 From: Andrew Gerrand Date: Thu, 18 Nov 2010 14:14:42 +1100 Subject: [PATCH] rpc: add RegisterName to allow override of default type name R=r, r2 CC=golang-dev https://golang.org/cl/2890041 --- src/pkg/rpc/server.go | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/pkg/rpc/server.go b/src/pkg/rpc/server.go index dbb68dde848..48b67914d5d 100644 --- a/src/pkg/rpc/server.go +++ b/src/pkg/rpc/server.go @@ -199,7 +199,19 @@ func isExported(name string) bool { // - one return value, of type os.Error // It returns an error if the receiver is not an exported type or has no // suitable methods. +// The client accesses each method using a string of the form "Type.Method", +// where Type is the receiver's concrete type. func (server *Server) Register(rcvr interface{}) os.Error { + return server.register(rcvr, "", false) +} + +// RegisterName is like Register but uses the provided name for the type +// instead of the receiver's concrete type. +func (server *Server) RegisterName(name string, rcvr interface{}) os.Error { + return server.register(rcvr, name, true) +} + +func (server *Server) register(rcvr interface{}, name string, useName bool) os.Error { server.Lock() defer server.Unlock() if server.serviceMap == nil { @@ -209,10 +221,13 @@ func (server *Server) Register(rcvr interface{}) os.Error { s.typ = reflect.Typeof(rcvr) s.rcvr = reflect.NewValue(rcvr) sname := reflect.Indirect(s.rcvr).Type().Name() + if useName { + sname = name + } if sname == "" { log.Exit("rpc: no service name for type", s.typ.String()) } - if s.typ.PkgPath() != "" && !isExported(sname) { + if s.typ.PkgPath() != "" && !isExported(sname) && !useName { s := "rpc Register: type " + sname + " is not exported" log.Print(s) return os.ErrorString(s) @@ -429,15 +444,15 @@ func (server *Server) Accept(lis net.Listener) { } } -// Register publishes in the DefaultServer the set of methods -// of the receiver value that satisfy the following conditions: -// - exported method -// - two arguments, both pointers to exported structs -// - one return value, of type os.Error -// It returns an error if the receiver is not an exported type or has no -// suitable methods. +// Register publishes the receiver's methods in the DefaultServer. func Register(rcvr interface{}) os.Error { return DefaultServer.Register(rcvr) } +// RegisterName is like Register but uses the provided name for the type +// instead of the receiver's concrete type. +func RegisterName(name string, rcvr interface{}) os.Error { + return DefaultServer.RegisterName(name, rcvr) +} + // A ServerCodec implements reading of RPC requests and writing of // RPC responses for the server side of an RPC session. // The server calls ReadRequestHeader and ReadRequestBody in pairs