Browse Source

codec: expose MsgpackSpecRpc and GoRpc as variables, to reduce API space.

Ugorji Nwoke 12 years ago
parent
commit
14ad4867c0
5 changed files with 21 additions and 15 deletions
  1. 2 2
      codec/0doc.go
  2. 2 2
      codec/README.md
  3. 3 3
      codec/codecs_test.go
  4. 7 4
      codec/msgpack.go
  5. 7 4
      codec/rpc.go

+ 2 - 2
codec/0doc.go

@@ -116,11 +116,11 @@ Typical usage model:
     err = enc.Encode(v)
     err = enc.Encode(v)
     
     
     //RPC Server
     //RPC Server
-    var rpcH codec.GoRpc // or codec.MsgpackSpecRpc  
     go func() {
     go func() {
         for {
         for {
             conn, err := listener.Accept()
             conn, err := listener.Accept()
-            rpcCodec := rpcH.ServerCodec(conn, h)
+            rpcCodec := codec.GoRpc.ServerCodec(conn, h)
+            //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
             rpc.ServeCodec(rpcCodec)
             rpc.ServeCodec(rpcCodec)
         }
         }
     }()
     }()

+ 2 - 2
codec/README.md

@@ -115,11 +115,11 @@ Typical usage model:
     err = enc.Encode(v)
     err = enc.Encode(v)
     
     
     //RPC Server
     //RPC Server
-    var rpcH codec.GoRpc // or codec.MsgpackSpecRpc  
     go func() {
     go func() {
         for {
         for {
             conn, err := listener.Accept()
             conn, err := listener.Accept()
-            rpcCodec := rpcH.ServerCodec(conn, h)
+            rpcCodec := codec.GoRpc.ServerCodec(conn, h)
+            //OR rpcCodec := codec.MsgpackSpecRpc.ServerCodec(conn, h)
             rpc.ServeCodec(rpcCodec)
             rpc.ServeCodec(rpcCodec)
         }
         }
     }()
     }()

+ 3 - 3
codec/codecs_test.go

@@ -718,8 +718,8 @@ func TestCodecs(t *testing.T) {
 }
 }
 
 
 func TestRpcs(t *testing.T) {
 func TestRpcs(t *testing.T) {
-	doTestRpcOne(t, MsgpackSpecRpc{}, testMsgpackH, true, true, true)
-	doTestRpcOne(t, GoRpc{}, testMsgpackH, true, true, true)
-	doTestRpcOne(t, GoRpc{}, testBincH, true, true, true)
+	doTestRpcOne(t, MsgpackSpecRpc, testMsgpackH, true, true, true)
+	doTestRpcOne(t, GoRpc, testMsgpackH, true, true, true)
+	doTestRpcOne(t, GoRpc, testBincH, true, true, true)
 }
 }
 
 

+ 7 - 4
codec/msgpack.go

@@ -72,6 +72,9 @@ const (
 	mpXv4Ext32 = 0xd9
 	mpXv4Ext32 = 0xd9
 )	
 )	
 
 
+// MsgpackSpecRpc implements Rpc using the communication protocol defined in 
+// the msgpack spec at http://wiki.msgpack.org/display/MSGPACK/RPC+specification
+var MsgpackSpecRpc msgpackSpecRpc
 
 
 // A MsgpackContainer type specifies the different types of msgpackContainers.
 // A MsgpackContainer type specifies the different types of msgpackContainers.
 type msgpackContainerType struct {
 type msgpackContainerType struct {
@@ -85,9 +88,9 @@ var (
 	msgpackContainerMap = msgpackContainerType{16, mpFixMapMin, mpMap16, mpMap32}
 	msgpackContainerMap = msgpackContainerType{16, mpFixMapMin, mpMap16, mpMap32}
 )
 )
 
 
-// MsgpackSpecRpc is the implementation of Rpc that uses custom communication protocol 
+// msgpackSpecRpc is the implementation of Rpc that uses custom communication protocol 
 // as defined in the msgpack spec at http://wiki.msgpack.org/display/MSGPACK/RPC+specification
 // as defined in the msgpack spec at http://wiki.msgpack.org/display/MSGPACK/RPC+specification
-type MsgpackSpecRpc struct{}
+type msgpackSpecRpc struct{}
 
 
 type msgpackSpecRpcCodec struct {
 type msgpackSpecRpcCodec struct {
 	rpcCodec
 	rpcCodec
@@ -612,11 +615,11 @@ func (d *msgpackDecoder) decodeExt(tag byte) (xbs []byte) {
 
 
 //--------------------------------------------------
 //--------------------------------------------------
 
 
-func (MsgpackSpecRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) (rpc.ServerCodec) {
+func (msgpackSpecRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) (rpc.ServerCodec) {
 	return &msgpackSpecRpcCodec{ newRPCCodec(conn, h) }
 	return &msgpackSpecRpcCodec{ newRPCCodec(conn, h) }
 }
 }
 
 
-func (MsgpackSpecRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) (rpc.ClientCodec) {
+func (msgpackSpecRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) (rpc.ClientCodec) {
 	return &msgpackSpecRpcCodec{ newRPCCodec(conn, h) }
 	return &msgpackSpecRpcCodec{ newRPCCodec(conn, h) }
 }
 }
 
 

+ 7 - 4
codec/rpc.go

@@ -14,6 +14,9 @@ import (
 	"io"
 	"io"
 )
 )
 
 
+// GoRpc implements Rpc using the communication protocol defined in net/rpc package.
+var GoRpc goRpc
+
 // Rpc interface provides a rpc Server or Client Codec for rpc communication.
 // Rpc interface provides a rpc Server or Client Codec for rpc communication.
 type Rpc interface {
 type Rpc interface {
 	ServerCodec(conn io.ReadWriteCloser, h Handle) (rpc.ServerCodec) 
 	ServerCodec(conn io.ReadWriteCloser, h Handle) (rpc.ServerCodec) 
@@ -30,15 +33,15 @@ type goRpcCodec struct {
 	rpcCodec
 	rpcCodec
 }
 }
 
 
-// GoRpc is the implementation of Rpc that uses the communication protocol 
+// goRpc is the implementation of Rpc that uses the communication protocol 
 // as defined in net/rpc package.
 // as defined in net/rpc package.
-type GoRpc struct {}
+type goRpc struct {}
 
 
-func (x GoRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) (rpc.ServerCodec) {
+func (x goRpc) ServerCodec(conn io.ReadWriteCloser, h Handle) (rpc.ServerCodec) {
 	return goRpcCodec { newRPCCodec(conn, h) }
 	return goRpcCodec { newRPCCodec(conn, h) }
 }
 }
 
 
-func (x GoRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) (rpc.ClientCodec) {
+func (x goRpc) ClientCodec(conn io.ReadWriteCloser, h Handle) (rpc.ClientCodec) {
 	return goRpcCodec { newRPCCodec(conn, h) }
 	return goRpcCodec { newRPCCodec(conn, h) }
 }
 }