This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
184 lines (165 loc) 路 3.7 KB
/
handler.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
package trip
import (
"context"
"log"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/google/uuid"
"github.com/baturalpk/ridesharing-services/client-app/common"
pb "github.com/baturalpk/ridesharing-services/client-app/genproto"
)
var (
riderID = uuid.NewString()
qMain = []string{
"Rider 馃毝",
"Driver 馃殩",
}
qRider = []string{
"Request new trip",
"Cancel the trip",
}
qDriver = []string{
"Find someone who requests a trip near here",
}
)
func Handler(c pb.TripServiceClient) {
var ans string
err := survey.AskOne(&survey.Select{
Message: "Do you want to be a rider or a driver?",
Options: qMain,
Default: qMain[0],
}, &ans)
if err != nil {
common.EvalError(err)
}
switch ans {
case qMain[0]:
riderHandler(c)
case qMain[1]:
driverHandler(c)
}
}
func riderHandler(c pb.TripServiceClient) {
for {
var ans string
err := survey.AskOne(&survey.Select{
Message: "Please choose an action:",
Options: qRider,
Default: qRider[0],
}, &ans)
if err != nil {
common.EvalError(err)
continue
}
switch ans {
case qRider[0]:
sl, err := common.AskLocation("Start location:")
if err != nil {
common.EvalError(err)
continue
}
dl, err := common.AskLocation("Destination location:")
if err != nil {
common.EvalError(err)
continue
}
typ, err := common.AskRideType("Which service would you like to use in your trip?")
if err != nil {
common.EvalError(err)
continue
}
req := &pb.RequestTripRequest{
RiderId: riderID,
Start: sl,
Destination: dl,
RideType: typ,
}
res, err := c.RequestTrip(context.Background(), req)
if err != nil {
common.EvalError(err)
continue
}
common.PrintResult(res)
case qRider[1]:
var ans string
err := survey.AskOne(&survey.Input{Message: "Which trip would you like to cancel? (type its id)"}, &ans)
if err != nil {
common.EvalError(err)
continue
}
_, err = c.CancelTrip(context.Background(), &pb.CancelTripRequest{TripId: ans})
if err != nil {
common.EvalError(err)
continue
}
common.PrintResult("success")
}
}
}
type driverChanResponse struct {
res interface{}
err error
}
func driverHandler(c pb.TripServiceClient) {
for {
var ans string
err := survey.AskOne(&survey.Select{
Message: "Please choose an action:",
Options: qDriver,
Default: qDriver[0],
}, &ans)
if err != nil {
common.EvalError(err)
continue
}
switch ans {
case qDriver[0]:
loc, err := common.AskLocation("Your current location:")
if err != nil {
common.EvalError(err)
continue
}
stream, err := c.SeekForTripAsDriver(context.Background())
if err != nil {
common.EvalError(err)
continue
}
ch := make(chan driverChanResponse, 1)
done := make(chan struct{})
go driverStreamSender(done, stream, loc)
go driverStreamReceiver(ch, done, stream)
log.Println(common.Info("Searching..."))
ret := <-ch
if ret.err != nil {
common.EvalError(ret.err)
continue
}
common.PrintResult(ret.res)
}
}
}
func driverStreamSender(done chan struct{}, stream pb.TripService_SeekForTripAsDriverClient, initialLoc *pb.Location) {
for {
select {
case <-done:
return
default:
if err := stream.Send(&pb.SeekForTripAsDriverRequest{DriverLocation: initialLoc}); err != nil {
common.EvalError(err)
return
}
}
time.Sleep(500 * time.Millisecond)
}
}
func driverStreamReceiver(ch chan driverChanResponse, done chan struct{}, stream pb.TripService_SeekForTripAsDriverClient) {
res, err := stream.Recv()
_ = stream.CloseSend()
if err != nil {
ch <- driverChanResponse{err: err}
done <- struct{}{}
return
}
ch <- driverChanResponse{res: res}
done <- struct{}{}
}