-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
76 lines (62 loc) · 1.46 KB
/
server.go
File metadata and controls
76 lines (62 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"flag"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
"log"
"net"
"time"
pb "github.com/bioothod/grpc_test/grtest"
)
type server struct {
}
func (ts *server) PingRequest(ctx context.Context, req *pb.Ping) (*pb.Pong, error) {
reply := &pb.Pong {
Pong: "reply",
Aux: req.Aux,
}
fmt.Printf("ping: %s -> %s\n", req.Ping, reply.Pong)
return reply, nil
}
func (ts *server) Stream(req *pb.Ping, stream pb.TestService_StreamServer) error {
fmt.Printf("stream: %s:%s\n", req.Ping, req.Aux)
var prev_time time.Time
counter := 0
for {
reply := &pb.Pong {
Pong: "reply",
Aux: fmt.Sprintf("%s:%d", req.Aux, counter),
}
err := stream.Send(reply)
if err != nil {
err = fmt.Errorf("stream: send error: %v", err)
fmt.Println(err.Error())
return err
}
counter++
t := time.Now()
if t.After(prev_time.Add(10 * time.Second)) {
fmt.Printf("%s: sending reply %s:%s\n", t.String(), reply.Pong, reply.Aux)
prev_time = t
}
}
return nil
}
func main() {
listen := flag.String("listen", "localhost:12345", "Address to listen at")
flag.Parse()
lis, err := net.Listen("tcp", *listen)
if err != nil {
log.Fatalf("Could not start listening at %s: %v\n", *listen, err)
}
ts := &server {
}
grpc.EnableTracing = true
srv := grpc.NewServer()
pb.RegisterTestServiceServer(srv, ts)
reflection.Register(srv)
log.Printf("Starting to listen at %s\n", *listen)
srv.Serve(lis)
}