]> Cypherpunks repositories - gostls13.git/commitdiff
rpc: add RegisterName to allow override of default type name
authorAndrew Gerrand <adg@golang.org>
Thu, 18 Nov 2010 03:14:42 +0000 (14:14 +1100)
committerAndrew Gerrand <adg@golang.org>
Thu, 18 Nov 2010 03:14:42 +0000 (14:14 +1100)
R=r, r2
CC=golang-dev
https://golang.org/cl/2890041

src/pkg/rpc/server.go

index dbb68dde8486735feb565fc36aff6ed0096e673f..48b67914d5d8ae0b93ab35c20121d388d0f17da6 100644 (file)
@@ -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