Skip to content

Commit edfdc4f

Browse files
committed
example: multiplex
Show that a channel can be shared by two stubs and two servicers can share a server.
1 parent 8177081 commit edfdc4f

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

Diff for: examples/multiplex/client/main.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
*
3+
* Copyright 2018 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package main
20+
21+
import (
22+
"log"
23+
24+
"google.golang.org/grpc"
25+
hwpb "google.golang.org/grpc/examples/helloworld/helloworld"
26+
hwclient "google.golang.org/grpc/examples/helloworld/helloworld/client"
27+
rgpb "google.golang.org/grpc/examples/route_guide/routeguide"
28+
rgclient "google.golang.org/grpc/examples/route_guide/routeguide/client"
29+
)
30+
31+
const (
32+
address = "localhost:50051"
33+
)
34+
35+
func main() {
36+
// Set up a connection to the server.
37+
conn, err := grpc.Dial(address, grpc.WithInsecure())
38+
if err != nil {
39+
log.Fatalf("did not connect: %v", err)
40+
}
41+
defer conn.Close()
42+
43+
// Make a greeter client and send an RPC.
44+
hwc := hwpb.NewGreeterClient(conn)
45+
hwclient.CallSayHello(hwc, "multiplex")
46+
47+
// Make a routeguild client with the same ClientConn.
48+
rgc := rgpb.NewRouteGuideClient(conn)
49+
rgclient.CallPrintFeature(rgc, &rgpb.Point{Latitude: 409146138, Longitude: -746188906})
50+
}

Diff for: examples/multiplex/server/main.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
*
3+
* Copyright 2018 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package main
20+
21+
import (
22+
"log"
23+
"net"
24+
25+
"google.golang.org/grpc"
26+
hwpb "google.golang.org/grpc/examples/helloworld/helloworld"
27+
hwserver "google.golang.org/grpc/examples/helloworld/helloworld/server"
28+
rgpb "google.golang.org/grpc/examples/route_guide/routeguide"
29+
rgserver "google.golang.org/grpc/examples/route_guide/routeguide/server"
30+
"google.golang.org/grpc/examples/route_guide/testdata"
31+
)
32+
33+
const (
34+
port = ":50051"
35+
)
36+
37+
func main() {
38+
lis, err := net.Listen("tcp", port)
39+
if err != nil {
40+
log.Fatalf("failed to listen: %v", err)
41+
}
42+
s := grpc.NewServer()
43+
44+
// Register Greeter on the server.
45+
hwpb.RegisterGreeterServer(s, hwserver.New())
46+
47+
// Register RouteGuide on the same server.
48+
rgpb.RegisterRouteGuideServer(s, rgserver.New(testdata.Path("route_guide_db.json")))
49+
50+
if err := s.Serve(lis); err != nil {
51+
log.Fatalf("failed to serve: %v", err)
52+
}
53+
}

0 commit comments

Comments
 (0)