-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.go
86 lines (73 loc) · 2.45 KB
/
chat.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
package main
import (
"context"
"fmt"
"time"
"github.com/jaym/go-orleans-chat-example/gen"
gengo "github.com/jaym/go-orleans-chat-example/gen-go"
"github.com/jaym/go-orleans/grain"
"github.com/jaym/go-orleans/grain/services"
)
type ChatRoomGrainImpl struct {
grain.Identity
client grain.SiloClient
observers []gengo.MessageObserver
msgCount int
users map[string]struct{}
}
func (c *ChatRoomGrainImpl) Join(ctx context.Context, username string, listen bool) (*gen.JoinResponse, error) {
c.users[username] = struct{}{}
fmt.Printf("Join %s %v\n", username, listen)
return &gen.JoinResponse{}, nil
}
func (s *ChatRoomGrainImpl) RegisterMessageObserver(ctx context.Context, observer gengo.MessageObserver, req *gen.ListenRequest) (grain.ObserverRegistrationToken, error) {
s.observers = append(s.observers, observer)
return grain.ObserverRegistrationToken{
Observer: observer.GetIdentity(),
ObservableName: "message-observer",
ID: "123",
Expires: time.Now().Add(30 * time.Second),
}, nil
}
func (s *ChatRoomGrainImpl) Ping(ctx context.Context) {
gengo.MessageObserverReceiveInvokeBatch(ctx, s.client, s.observers, &gen.ChatMessage{
From: s.GetIdentity().ID,
Msg: "PING",
})
}
func (*ChatRoomGrainImpl) UnregisterObserver(context.Context, grain.ObserverRegistrationToken) {
}
func (*ChatRoomGrainImpl) RefreshObserver(ctx context.Context, t grain.ObserverRegistrationToken) (grain.ObserverRegistrationToken, error) {
return grain.ObserverRegistrationToken{
Observer: t.Observer,
ObservableName: "message-observer",
ID: "123",
Expires: time.Now().Add(30 * time.Second),
}, nil
}
func (c *ChatRoomGrainImpl) SendMessage(ctx context.Context, msg *gen.ChatMessage) (*gen.SendMessageResponse, bool, error) {
c.msgCount++
gengo.MessageObserverReceiveInvokeBatch(ctx, c.client, c.observers, msg)
return &gen.SendMessageResponse{
Count: int64(c.msgCount),
}, true, nil
}
func (c *ChatRoomGrainImpl) ListUsers(ctx context.Context) ([]string, error) {
users := make([]string, len(c.users))
i := 0
for u := range c.users {
users[i] = u
i++
}
return users, nil
}
type ChatRoomGrainActivatorImpl struct {
}
func (*ChatRoomGrainActivatorImpl) Activate(ctx context.Context, identity grain.Identity, services services.CoreGrainServices) (gengo.ChatRoomGrain, error) {
g := &ChatRoomGrainImpl{
Identity: identity,
client: services.SiloClient(),
users: map[string]struct{}{},
}
return g, nil
}