diff --git a/ch4-rpc/ch4-01-rpc-intro.md b/ch4-rpc/ch4-01-rpc-intro.md index a03704a..5a877a0 100644 --- a/ch4-rpc/ch4-01-rpc-intro.md +++ b/ch4-rpc/ch4-01-rpc-intro.md @@ -78,7 +78,7 @@ type HelloServiceInterface = interface { } func RegisterHelloService(svc HelloServiceInterface) error { - rpc.RegisterName(HelloServiceName, svc) + return rpc.RegisterName(HelloServiceName, svc) } ``` @@ -117,11 +117,11 @@ func DialHelloService(network, address string) (*HelloServiceClient, error) { if err != nil { return nil, err } - return &HelloServiceClient{Client: c} + return &HelloServiceClient{Client: c}, nil } func (p *HelloServiceClient) Hello(request string, reply *string) error { - return client.Call(HelloServiceName+".Hello", request, reply) + return p.Client.Call(HelloServiceName+".Hello", request, reply) } ``` diff --git a/examples/ch4-01-rpc-inro/hello-service-v2/api/hello.go b/examples/ch4-01-rpc-inro/hello-service-v2/api/hello.go new file mode 100644 index 0000000..5795f8f --- /dev/null +++ b/examples/ch4-01-rpc-inro/hello-service-v2/api/hello.go @@ -0,0 +1,31 @@ +package api + +import "net/rpc" + +const HelloServiceName = "path/to/pkg.HelloService" + +type HelloServiceInterface = interface { + Hello(request string, reply *string) error +} + +func RegisterHelloService(svc HelloServiceInterface) error { + return rpc.RegisterName(HelloServiceName, svc) +} + +type HelloServiceClient struct { + *rpc.Client +} + +var _ HelloServiceInterface = (*HelloServiceClient)(nil) + +func DialHelloService(network, address string) (*HelloServiceClient, error) { + c, err := rpc.Dial(network, address) + if err != nil { + return nil, err + } + return &HelloServiceClient{Client: c}, nil +} + +func (p *HelloServiceClient) Hello(request string, reply *string) error { + return p.Client.Call(HelloServiceName+".Hello", request, reply) +} diff --git a/examples/ch4-01-rpc-inro/hello-service-v2/client/main.go b/examples/ch4-01-rpc-inro/hello-service-v2/client/main.go new file mode 100644 index 0000000..6362fd2 --- /dev/null +++ b/examples/ch4-01-rpc-inro/hello-service-v2/client/main.go @@ -0,0 +1,31 @@ +package main + +import ( + "fmt" + "log" + + "github.com/chai2010/advanced-go-programming-book/examples/ch4-01-rpc-inro/hello-service-v2/api" +) + +type HelloService struct{} + +func (p *HelloService) Hello(request string, reply *string) error { + *reply = "hello:" + request + return nil +} + +func main() { + + client, err := api.DialHelloService("tcp", "localhost:1234") + if err != nil { + log.Fatal("dialing:", err) + } + + var reply string + err = client.Hello("hello", &reply) + if err != nil { + log.Fatal(err) + } + + fmt.Println(reply) +} diff --git a/examples/ch4-01-rpc-inro/hello-service-v2/server/main.go b/examples/ch4-01-rpc-inro/hello-service-v2/server/main.go new file mode 100644 index 0000000..f6df3dc --- /dev/null +++ b/examples/ch4-01-rpc-inro/hello-service-v2/server/main.go @@ -0,0 +1,34 @@ +package main + +import ( + "log" + "net" + "net/rpc" + + "github.com/chai2010/advanced-go-programming-book/examples/ch4-01-rpc-inro/hello-service-v2/api" +) + +type HelloService struct{} + +func (p *HelloService) Hello(request string, reply *string) error { + *reply = "hello:" + request + return nil +} + +func main() { + api.RegisterHelloService(new(HelloService)) + + listener, err := net.Listen("tcp", ":1234") + if err != nil { + log.Fatal("ListenTCP error:", err) + } + + for { + conn, err := listener.Accept() + if err != nil { + log.Fatal("Accept error:", err) + } + + go rpc.ServeConn(conn) + } +}