From 414d0dd6d15e2378ff7bfb930d20bfc571f93f2c Mon Sep 17 00:00:00 2001 From: sfw Date: Tue, 14 Aug 2018 13:58:52 +0800 Subject: [PATCH] add ch4.4-3 exameple --- vendor/ch4.4-3/clientpub/main.go | 29 +++ vendor/ch4.4-3/clientsub/main.go | 38 ++++ .../ch4.4-3/pubsubservice/pubsubservice.pb.go | 214 ++++++++++++++++++ .../ch4.4-3/pubsubservice/pubsubservice.proto | 14 ++ vendor/ch4.4-3/server/main.go | 70 ++++++ 5 files changed, 365 insertions(+) create mode 100644 vendor/ch4.4-3/clientpub/main.go create mode 100644 vendor/ch4.4-3/clientsub/main.go create mode 100644 vendor/ch4.4-3/pubsubservice/pubsubservice.pb.go create mode 100644 vendor/ch4.4-3/pubsubservice/pubsubservice.proto create mode 100644 vendor/ch4.4-3/server/main.go diff --git a/vendor/ch4.4-3/clientpub/main.go b/vendor/ch4.4-3/clientpub/main.go new file mode 100644 index 0000000..2c9315c --- /dev/null +++ b/vendor/ch4.4-3/clientpub/main.go @@ -0,0 +1,29 @@ +package main + +import ( + "context" + "log" + + "google.golang.org/grpc" + + pb "ch4.4-3/pubsubservice" +) + +func main() { + conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure()) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + client := pb.NewPubsubServiceClient(conn) + + _, err = client.Publish(context.Background(), &pb.String{Value: "golang: hello Go"}) + if err != nil { + log.Fatal(err) + } + _, err = client.Publish(context.Background(), &pb.String{Value: "docker: hello Docker"}) + if err != nil { + log.Fatal(err) + } +} diff --git a/vendor/ch4.4-3/clientsub/main.go b/vendor/ch4.4-3/clientsub/main.go new file mode 100644 index 0000000..78ec2ad --- /dev/null +++ b/vendor/ch4.4-3/clientsub/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "context" + "fmt" + "io" + "log" + + "google.golang.org/grpc" + + pb "ch4.4-3/pubsubservice" +) + +func main() { + conn, err := grpc.Dial("localhost:1234", grpc.WithInsecure()) + if err != nil { + log.Fatal(err) + } + defer conn.Close() + + client := pb.NewPubsubServiceClient(conn) + stream, err := client.Subscribe(context.Background(), &pb.String{Value: "golang:"}) + if err != nil { + log.Fatal(err) + } + + for { + reply, err := stream.Recv() + if err != nil { + if err == io.EOF { + break + } + log.Fatal(err) + } + + fmt.Println(reply.GetValue()) + } +} diff --git a/vendor/ch4.4-3/pubsubservice/pubsubservice.pb.go b/vendor/ch4.4-3/pubsubservice/pubsubservice.pb.go new file mode 100644 index 0000000..777b192 --- /dev/null +++ b/vendor/ch4.4-3/pubsubservice/pubsubservice.pb.go @@ -0,0 +1,214 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// source: pubsubservice.proto + +package pubsubservice + +import proto "github.com/golang/protobuf/proto" +import fmt "fmt" +import math "math" + +import ( + context "golang.org/x/net/context" + grpc "google.golang.org/grpc" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package + +type String struct { + Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` + XXX_NoUnkeyedLiteral struct{} `json:"-"` + XXX_unrecognized []byte `json:"-"` + XXX_sizecache int32 `json:"-"` +} + +func (m *String) Reset() { *m = String{} } +func (m *String) String() string { return proto.CompactTextString(m) } +func (*String) ProtoMessage() {} +func (*String) Descriptor() ([]byte, []int) { + return fileDescriptor_pubsubservice_f5055945d20b225a, []int{0} +} +func (m *String) XXX_Unmarshal(b []byte) error { + return xxx_messageInfo_String.Unmarshal(m, b) +} +func (m *String) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + return xxx_messageInfo_String.Marshal(b, m, deterministic) +} +func (dst *String) XXX_Merge(src proto.Message) { + xxx_messageInfo_String.Merge(dst, src) +} +func (m *String) XXX_Size() int { + return xxx_messageInfo_String.Size(m) +} +func (m *String) XXX_DiscardUnknown() { + xxx_messageInfo_String.DiscardUnknown(m) +} + +var xxx_messageInfo_String proto.InternalMessageInfo + +func (m *String) GetValue() string { + if m != nil { + return m.Value + } + return "" +} + +func init() { + proto.RegisterType((*String)(nil), "pubsubservice.String") +} + +// Reference imports to suppress errors if they are not otherwise used. +var _ context.Context +var _ grpc.ClientConn + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the grpc package it is being compiled against. +const _ = grpc.SupportPackageIsVersion4 + +// PubsubServiceClient is the client API for PubsubService service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type PubsubServiceClient interface { + Publish(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error) + Subscribe(ctx context.Context, in *String, opts ...grpc.CallOption) (PubsubService_SubscribeClient, error) +} + +type pubsubServiceClient struct { + cc *grpc.ClientConn +} + +func NewPubsubServiceClient(cc *grpc.ClientConn) PubsubServiceClient { + return &pubsubServiceClient{cc} +} + +func (c *pubsubServiceClient) Publish(ctx context.Context, in *String, opts ...grpc.CallOption) (*String, error) { + out := new(String) + err := c.cc.Invoke(ctx, "/pubsubservice.PubsubService/Publish", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *pubsubServiceClient) Subscribe(ctx context.Context, in *String, opts ...grpc.CallOption) (PubsubService_SubscribeClient, error) { + stream, err := c.cc.NewStream(ctx, &_PubsubService_serviceDesc.Streams[0], "/pubsubservice.PubsubService/Subscribe", opts...) + if err != nil { + return nil, err + } + x := &pubsubServiceSubscribeClient{stream} + if err := x.ClientStream.SendMsg(in); err != nil { + return nil, err + } + if err := x.ClientStream.CloseSend(); err != nil { + return nil, err + } + return x, nil +} + +type PubsubService_SubscribeClient interface { + Recv() (*String, error) + grpc.ClientStream +} + +type pubsubServiceSubscribeClient struct { + grpc.ClientStream +} + +func (x *pubsubServiceSubscribeClient) Recv() (*String, error) { + m := new(String) + if err := x.ClientStream.RecvMsg(m); err != nil { + return nil, err + } + return m, nil +} + +// PubsubServiceServer is the server API for PubsubService service. +type PubsubServiceServer interface { + Publish(context.Context, *String) (*String, error) + Subscribe(*String, PubsubService_SubscribeServer) error +} + +func RegisterPubsubServiceServer(s *grpc.Server, srv PubsubServiceServer) { + s.RegisterService(&_PubsubService_serviceDesc, srv) +} + +func _PubsubService_Publish_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(String) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(PubsubServiceServer).Publish(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/pubsubservice.PubsubService/Publish", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(PubsubServiceServer).Publish(ctx, req.(*String)) + } + return interceptor(ctx, in, info, handler) +} + +func _PubsubService_Subscribe_Handler(srv interface{}, stream grpc.ServerStream) error { + m := new(String) + if err := stream.RecvMsg(m); err != nil { + return err + } + return srv.(PubsubServiceServer).Subscribe(m, &pubsubServiceSubscribeServer{stream}) +} + +type PubsubService_SubscribeServer interface { + Send(*String) error + grpc.ServerStream +} + +type pubsubServiceSubscribeServer struct { + grpc.ServerStream +} + +func (x *pubsubServiceSubscribeServer) Send(m *String) error { + return x.ServerStream.SendMsg(m) +} + +var _PubsubService_serviceDesc = grpc.ServiceDesc{ + ServiceName: "pubsubservice.PubsubService", + HandlerType: (*PubsubServiceServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "Publish", + Handler: _PubsubService_Publish_Handler, + }, + }, + Streams: []grpc.StreamDesc{ + { + StreamName: "Subscribe", + Handler: _PubsubService_Subscribe_Handler, + ServerStreams: true, + }, + }, + Metadata: "pubsubservice.proto", +} + +func init() { proto.RegisterFile("pubsubservice.proto", fileDescriptor_pubsubservice_f5055945d20b225a) } + +var fileDescriptor_pubsubservice_f5055945d20b225a = []byte{ + // 132 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x2e, 0x28, 0x4d, 0x2a, + 0x2e, 0x4d, 0x2a, 0x4e, 0x2d, 0x2a, 0xcb, 0x4c, 0x4e, 0xd5, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, + 0xe2, 0x45, 0x11, 0x54, 0x92, 0xe3, 0x62, 0x0b, 0x2e, 0x29, 0xca, 0xcc, 0x4b, 0x17, 0x12, 0xe1, + 0x62, 0x2d, 0x4b, 0xcc, 0x29, 0x4d, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x0c, 0x82, 0x70, 0x8c, + 0x5a, 0x19, 0xb9, 0x78, 0x03, 0xc0, 0x3a, 0x82, 0x21, 0x3a, 0x84, 0xcc, 0xb9, 0xd8, 0x03, 0x4a, + 0x93, 0x72, 0x32, 0x8b, 0x33, 0x84, 0x44, 0xf5, 0x50, 0x6d, 0x80, 0x98, 0x24, 0x85, 0x5d, 0x58, + 0xc8, 0x9a, 0x8b, 0x33, 0xb8, 0x34, 0xa9, 0x38, 0xb9, 0x28, 0x33, 0x29, 0x95, 0x34, 0xad, 0x06, + 0x8c, 0x49, 0x6c, 0x60, 0xd7, 0x1b, 0x03, 0x02, 0x00, 0x00, 0xff, 0xff, 0x25, 0x3b, 0x39, 0x24, + 0xd4, 0x00, 0x00, 0x00, +} diff --git a/vendor/ch4.4-3/pubsubservice/pubsubservice.proto b/vendor/ch4.4-3/pubsubservice/pubsubservice.proto new file mode 100644 index 0000000..3d8e2f5 --- /dev/null +++ b/vendor/ch4.4-3/pubsubservice/pubsubservice.proto @@ -0,0 +1,14 @@ +syntax = "proto3"; + +package pubsubservice; + +message String { + string value = 1; +} + +service PubsubService { + rpc Publish (String) returns (String); + rpc Subscribe (String) returns (stream String); +} + +//protoc --go_out=plugins=grpc:. pubsubservice.proto diff --git a/vendor/ch4.4-3/server/main.go b/vendor/ch4.4-3/server/main.go new file mode 100644 index 0000000..11291fb --- /dev/null +++ b/vendor/ch4.4-3/server/main.go @@ -0,0 +1,70 @@ +package main + +import ( + "context" + "log" + "net" + "strings" + "time" + + "github.com/docker/docker/pkg/pubsub" + "google.golang.org/grpc" + + pb "ch4.4-3/pubsubservice" +) + +type PubsubService struct { + pub *pubsub.Publisher +} + +func NewPubsubService() *PubsubService { + return &PubsubService{ + pub: pubsub.NewPublisher(100*time.Millisecond, 10), + } +} + +func (p *PubsubService) Publish( + ctx context.Context, arg *pb.String, +) (*pb.String, error) { + p.pub.Publish(arg.GetValue()) + //debug + //reply := &String{Value: " " + arg.GetValue()} + //fmt.Println(reply.GetValue()) + return &pb.String{}, nil +} + +func (p *PubsubService) Subscribe( + arg *pb.String, stream pb.PubsubService_SubscribeServer, +) error { + ch := p.pub.SubscribeTopic(func(v interface{}) bool { + if key, ok := v.(string); ok { + //debug + //fmt.Printf(" %t %s %s %t\n", + // ok,arg.GetValue(),key,strings.HasPrefix(key,arg.GetValue())) + if strings.HasPrefix(key, arg.GetValue()) { + return true + } + } + return false + }) + + for v := range ch { + if err := stream.Send(&pb.String{Value: v.(string)}); err != nil { + return err + } + } + + return nil +} + +func main() { + grpcServer := grpc.NewServer() + pb.RegisterPubsubServiceServer(grpcServer, NewPubsubService()) + + lis, err := net.Listen("tcp", ":1234") + if err != nil { + log.Fatal(err) + } + + grpcServer.Serve(lis) +}