-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (87 loc) · 2.18 KB
/
main.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
package main
import (
"context"
"log"
"github.com/golang/protobuf/proto"
"google.golang.org/grpc"
"murmur-nudge/MurmurRPC"
)
func main() {
log.Println("Starting...")
conn, err := grpc.Dial("127.0.0.1:50051", grpc.WithInsecure())
if err != nil {
log.Fatal(err)
}
defer conn.Close()
server := &MurmurRPC.Server{
Id: proto.Uint32(1),
}
go func() {
streamClient := MurmurRPC.NewV1Client(conn)
caClient := MurmurRPC.NewV1Client(conn)
stream, err := streamClient.ServerEvents(context.Background(), server)
if err != nil {
log.Fatal(err)
}
for {
event, err := stream.Recv()
if err != nil {
break
}
if event.GetType() != MurmurRPC.Server_Event_UserConnected {
continue
}
log.Println("Added context action to", event.GetUser().GetName())
caClient.ContextActionAdd(context.Background(), &MurmurRPC.ContextAction{
Server: server,
Context: proto.Uint32(uint32(MurmurRPC.ContextAction_User)),
Action: proto.String("nudge"),
Text: proto.String("Nudge!"),
User: event.GetUser(),
})
}
}()
msgClient := MurmurRPC.NewV1Client(conn)
caClient := MurmurRPC.NewV1Client(conn)
stream, err := caClient.ContextActionEvents(context.Background(), &MurmurRPC.ContextAction{
Server: server,
Action: proto.String("nudge"),
})
if err != nil {
log.Fatal(err)
}
for {
event, err := stream.Recv()
if err != nil {
break
}
if event.GetUser() == nil {
continue
}
actor, err := msgClient.UserGet(context.Background(), event.GetActor())
if err != nil {
continue
}
user, err := msgClient.UserGet(context.Background(), event.GetUser())
if err != nil {
continue
}
if actor.GetSession() == user.GetSession() {
msgClient.TextMessageSend(context.Background(), &MurmurRPC.TextMessage{
Server: server,
Users: []*MurmurRPC.User{actor},
Text: proto.String("Cannot nudge yourself!"),
})
continue
}
log.Printf("%s nudged %s\n", actor.GetName(), user.GetName())
msg := &MurmurRPC.TextMessage{
Server: server,
Users: []*MurmurRPC.User{user},
Text: proto.String(actor.GetName() + " nudged you!"),
}
for i := 1; i <= 3; i++ {
msgClient.TextMessageSend(context.Background(), msg)
}
}
}