Przeglądaj źródła

Expanding echo.proto and Go sample to include client streaming and server streaming calls.

Tim Burks 9 lat temu
rodzic
commit
923b8e96e7

+ 54 - 10
Examples/Echo/Go/go/src/client/client.go

@@ -33,8 +33,11 @@ const (
 )
 
 func main() {
-
-	var stream = flag.Int("s", 0, "send multiple messages by streaming")
+	var get = flag.Bool("get", false, "call the Get method")
+	var update = flag.Bool("update", false, "call the Update method")
+	var collect = flag.Bool("collect", false, "call the Collect method")
+	var expand = flag.Bool("expand", false, "call the Expand method")
+	var count = flag.Int("n", 10, "number of message to send (update and collect only)")
 	var message = flag.String("m", defaultMessage, "the message to send")
 	var address = flag.String("a", "", "address of the echo server to use")
 	var useTLS = flag.Bool("tls", false, "Use tls for connections.")
@@ -63,17 +66,24 @@ func main() {
 	if err != nil {
 		log.Fatalf("did not connect: %v", err)
 	}
+
 	defer conn.Close()
 	c := pb.NewEchoClient(conn)
-
-	if *stream > 0 {
-		send_many_messages(c, *message, *stream)
-	} else {
-		send_one_message(c, *message)
+	if *get {
+		call_get(c, *message)
+	}
+	if *update {
+		call_update(c, *message, *count)
+	}
+	if *collect {
+		call_collect(c, *message, *count)
+	}
+	if *expand {
+		call_expand(c, *message)
 	}
 }
 
-func send_one_message(c pb.EchoClient, message string) {
+func call_get(c pb.EchoClient, message string) {
 	// Contact the server and print out its response.
 	response, err := c.Get(context.Background(), &pb.EchoRequest{Text: message})
 	if err != nil {
@@ -82,7 +92,7 @@ func send_one_message(c pb.EchoClient, message string) {
 	log.Printf("Received: %s", response.Text)
 }
 
-func send_many_messages(c pb.EchoClient, message string, count int) {
+func call_update(c pb.EchoClient, message string, count int) {
 	stream, err := c.Update(context.Background())
 	if err != nil {
 		panic(err)
@@ -99,7 +109,6 @@ func send_many_messages(c pb.EchoClient, message string, count int) {
 			if err != nil {
 				log.Fatalf("Failed to receive an echo : %v", err)
 			}
-			count = count + 1
 			log.Printf("Received: %s", in.Text)
 		}
 	}()
@@ -113,3 +122,38 @@ func send_many_messages(c pb.EchoClient, message string, count int) {
 	stream.CloseSend()
 	<-waitc
 }
+
+func call_collect(c pb.EchoClient, message string, count int) {
+	stream, err := c.Collect(context.Background())
+	if err != nil {
+		panic(err)
+	}
+	for i := 1; i <= count; i++ {
+		var note pb.EchoRequest
+		note.Text = fmt.Sprintf("%s %d", message, i)
+		if err := stream.Send(&note); err != nil {
+			log.Fatalf("Failed to send a message: %v", err)
+		}
+	}
+	response, err := stream.CloseAndRecv()
+	log.Printf("Received: %s", response.Text)
+}
+
+func call_expand(c pb.EchoClient, message string) {
+	stream, err := c.Expand(context.Background(), &pb.EchoRequest{Text: message})
+	if err != nil {
+		panic(err)
+	}
+	for {
+		in, err := stream.Recv()
+		if err == io.EOF {
+			// read done.
+			close(waitc)
+			return
+		}
+		if err != nil {
+			log.Fatalf("Failed to receive an echo : %v", err)
+		}
+		log.Printf("Received: %s", in.Text)
+	}
+}

+ 36 - 0
Examples/Echo/Go/go/src/server/server.go

@@ -20,6 +20,7 @@ import (
 	"io"
 	"log"
 	"net"
+	"strings"
 
 	pb "echo"
 	"golang.org/x/net/context"
@@ -66,6 +67,41 @@ func (s *EchoServer) Update(stream pb.Echo_UpdateServer) error {
 	return nil
 }
 
+func (s *EchoServer) Collect(stream pb.Echo_CollectServer) error {
+	parts := []string{}
+	for {
+		in, err := stream.Recv()
+		if err == io.EOF {
+			break
+		}
+		if err != nil {
+			return err
+		}
+		parts = append(parts, in.Text)
+	}
+	response := &pb.EchoResponse{}
+	response.Text = strings.Join(parts, " ")
+	if err := stream.SendAndClose(response); err != nil {
+		return err
+	}
+	return nil
+}
+
+func (s *EchoServer) Expand(request *pb.EchoRequest, stream pb.Echo_ExpandServer) error {
+	fmt.Printf("Expand received: %s\n", request.Text)
+	parts := strings.Split(request.Text, " ")
+
+	for _, part := range parts {
+		response := &pb.EchoResponse{}
+		response.Text = part
+		if err := stream.Send(response); err != nil {
+			return err
+		}
+	}
+
+	return nil
+}
+
 // [START main]
 func main() {
 	var useTLS = flag.Bool("tls", false, "Use tls for connections.")

+ 2 - 0
Examples/Echo/echo.proto

@@ -18,6 +18,8 @@ package echo;
 service Echo {
   rpc Get(EchoRequest) returns (EchoResponse) {}
   rpc Update(stream EchoRequest) returns (stream EchoResponse) {}
+  rpc Collect(stream EchoRequest) returns (EchoResponse) {}
+  rpc Expand(EchoRequest) returns (stream EchoResponse) {}  
 }
 
 message EchoRequest {