-
Notifications
You must be signed in to change notification settings - Fork 33
/
client.go
185 lines (154 loc) · 4.21 KB
/
client.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"bufio"
"io"
"os"
"time"
"github.com/pkg/errors"
chat "github.com/rodaine/grpc-chat/protos"
"golang.org/x/net/context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
type client struct {
chat.ChatClient
Host, Password, Name, Token string
Shutdown bool
}
func Client(host, pass, name string) *client {
return &client{
Host: host,
Password: pass,
Name: name,
}
}
func (c *client) Run(ctx context.Context) error {
connCtx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
conn, err := grpc.DialContext(connCtx, c.Host, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
return errors.WithMessage(err, "unable to connect")
}
defer conn.Close()
c.ChatClient = chat.NewChatClient(conn)
if c.Token, err = c.login(ctx); err != nil {
return errors.WithMessage(err, "failed to login")
}
ClientLogf(time.Now(), "logged in successfully")
err = c.stream(ctx)
ClientLogf(time.Now(), "logging out")
if err := c.logout(ctx); err != nil {
ClientLogf(time.Now(), "failed to log out: %v", err)
}
return errors.WithMessage(err, "stream error")
}
func (c *client) stream(ctx context.Context) error {
md := metadata.New(map[string]string{tokenHeader: c.Token})
ctx = metadata.NewOutgoingContext(ctx, md)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
client, err := c.ChatClient.Stream(ctx)
if err != nil {
return err
}
defer client.CloseSend()
ClientLogf(time.Now(), "connected to stream")
go c.send(client)
return c.receive(client)
}
func (c *client) receive(sc chat.Chat_StreamClient) error {
for {
res, err := sc.Recv()
if s, ok := status.FromError(err); ok && s.Code() == codes.Canceled {
DebugLogf("stream canceled (usually indicates shutdown)")
return nil
} else if err == io.EOF {
DebugLogf("stream closed by server")
return nil
} else if err != nil {
return err
}
ts := res.Timestamp.AsTime().In(time.Local)
switch evt := res.Event.(type) {
case *chat.StreamResponse_ClientLogin:
ServerLogf(ts, "%s has logged in", evt.ClientLogin.Name)
case *chat.StreamResponse_ClientLogout:
ServerLogf(ts, "%s has logged out", evt.ClientLogout.Name)
case *chat.StreamResponse_ClientMessage:
MessageLog(ts, evt.ClientMessage.Name, evt.ClientMessage.Message)
case *chat.StreamResponse_ServerShutdown:
ServerLogf(ts, "the server is shutting down")
c.Shutdown = true
return nil
default:
ClientLogf(ts, "unexpected event from the server: %T", evt)
return nil
}
}
}
func (c *client) send(client chat.Chat_StreamClient) {
sc := bufio.NewScanner(os.Stdin)
sc.Split(bufio.ScanLines)
for {
select {
case <-client.Context().Done():
DebugLogf("client send loop disconnected")
default:
if sc.Scan() {
if err := client.Send(&chat.StreamRequest{Message: sc.Text()}); err != nil {
ClientLogf(time.Now(), "failed to send message: %v", err)
return
}
} else {
ClientLogf(time.Now(), "input scanner failure: %v", sc.Err())
return
}
}
}
}
//func (c *client) send(client chat.Chat_StreamClient, done <-chan struct{}) error {
// sc := bufio.NewScanner(os.Stdin)
// sc.Split(bufio.ScanLines)
//
// for {
// select {
// case <-done:
// return nil
// default:
// if sc.Scan() {
// if err := client.Send(&chat.StreamRequest{Message: sc.Text()}); err != nil {
// return err
// }
// }
// return sc.Err()
// }
// }
//}
func (c *client) login(ctx context.Context) (string, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
res, err := c.ChatClient.Login(ctx, &chat.LoginRequest{
Name: c.Name,
Password: c.Password,
})
if err != nil {
return "", err
}
return res.Token, nil
}
func (c *client) logout(ctx context.Context) error {
if c.Shutdown {
DebugLogf("unable to logout (server sent shutdown signal)")
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
_, err := c.ChatClient.Logout(ctx, &chat.LogoutRequest{Token: c.Token})
if s, ok := status.FromError(err); ok && s.Code() == codes.Unavailable {
DebugLogf("unable to logout (connection already closed)")
return nil
}
return err
}