/
chattercli.go
130 lines (125 loc) · 3.2 KB
/
chattercli.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package chatter
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"os/signal"
chattersvc "goa.design/examples/streaming/gen/chatter"
)
// InteractWithStreams performs the sample operations on the stream
func InteractWithStreams(data interface{}) {
if data != nil {
switch stream := data.(type) {
case chattersvc.EchoerClientStream:
// bidirectional streaming
trapCtrlC(stream)
fmt.Println("Press Ctrl+D to stop chatting.")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
p := scanner.Text()
if err := stream.Send(p); err != nil {
fmt.Println(fmt.Errorf("error sending into stream: %s", err))
os.Exit(1)
}
d, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
fmt.Println(fmt.Errorf("error reading from stream: %s", err))
}
prettyPrint(d)
}
if err := stream.Close(); err != nil {
fmt.Println(fmt.Errorf("error closing stream: %s", err))
}
case chattersvc.ListenerClientStream:
// payload streaming (no server response)
trapCtrlC(stream)
fmt.Println("Press Ctrl+D to stop chatting.")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
p := scanner.Text()
if err := stream.Send(p); err != nil {
fmt.Println(fmt.Errorf("error sending into stream: %s", err))
os.Exit(1)
}
}
if err := stream.Close(); err != nil {
fmt.Println(fmt.Errorf("error closing stream: %s", err))
}
case chattersvc.SummaryClientStream:
// payload streaming (server responds with a result type)
trapCtrlC(stream)
fmt.Println("Press Ctrl+D to stop chatting.")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
p := scanner.Text()
if err := stream.Send(p); err != nil {
fmt.Println(fmt.Errorf("error sending into stream: %s", err))
os.Exit(1)
}
}
if p, err := stream.CloseAndRecv(); err != nil {
fmt.Println(fmt.Errorf("error closing stream: %s", err))
} else {
prettyPrint(p)
}
case chattersvc.SubscribeClientStream:
// result streaming
for {
p, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
fmt.Println(fmt.Errorf("error reading from stream: %v", err))
break
}
prettyPrint(p)
}
case chattersvc.HistoryClientStream:
// result streaming with views
for {
p, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
fmt.Println(fmt.Errorf("error reading from stream: %v", err))
}
prettyPrint(p)
}
default:
prettyPrint(data)
}
}
if data != nil {
m, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(m))
}
}
func prettyPrint(s interface{}) {
m, _ := json.MarshalIndent(s, "", " ")
fmt.Println(string(m))
}
// Trap Ctrl+C to gracefully exit the client.
func trapCtrlC(stream interface{}) {
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt)
go func(stream interface{}) {
for range ch {
fmt.Println("\nexiting")
if s, ok := stream.(chattersvc.EchoerClientStream); ok {
s.Close()
} else if s, ok := stream.(chattersvc.ListenerClientStream); ok {
s.Close()
} else if s, ok := stream.(chattersvc.SummaryClientStream); ok {
s.CloseAndRecv()
}
os.Exit(0)
}
}(stream)
}