-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
370 lines (331 loc) · 8.83 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package main
import (
"bufio"
"context"
"fmt"
stdlog "log"
"net"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
time "time"
"github.com/go-logr/logr"
"github.com/go-logr/stdr"
_ "github.com/jackc/pgx/v4/stdlib"
"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/plugins/discovery/static"
psql_quicksetup "github.com/jaym/go-orleans/quicksetup/psql"
"github.com/jaym/go-orleans/silo"
)
func main() {
var membershipPort int
var rpcPort int
var servicePort int
switch os.Args[1] {
case "node1":
membershipPort = 9991
rpcPort = 8991
servicePort = 9501
case "node2":
membershipPort = 9992
rpcPort = 8992
servicePort = 9502
case "node3":
membershipPort = 9993
rpcPort = 8993
servicePort = 9503
default:
panic("wrong")
}
stdr.SetVerbosity(10)
log := stdr.NewWithOptions(stdlog.New(os.Stderr, "", stdlog.LstdFlags), stdr.Options{LogCaller: stdr.All})
nodeName := os.Args[1]
s, err := psql_quicksetup.Setup(
context.Background(),
psql_quicksetup.WithLogr(log),
psql_quicksetup.WithNodeName(nodeName),
psql_quicksetup.WithPGEnvironment(),
psql_quicksetup.WithRPCAddr("127.0.0.1:"+strconv.Itoa(rpcPort)),
psql_quicksetup.WithMembershipAddr("127.0.0.1:"+strconv.Itoa(membershipPort)),
psql_quicksetup.WithSiloOptions(
silo.WithDiscovery(static.New([]string{"127.0.0.1:9991", "127.0.0.1:9992"})),
silo.WithMaxGrains(10),
),
)
if err != nil {
panic(err)
}
// gen.RegisterChatRoomGrainActivator(s, &ChatRoomGrainActivator{})
gengo.RegisterChatRoomGrainActivator(s, &ChatRoomGrainActivatorImpl{})
if err := s.Start(context.Background()); err != nil {
panic(err)
}
stop := make(chan os.Signal, 1)
// Register the signals we want to be notified, these 3 indicate exit
// signals, similar to CTRL+C
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
listener, err := net.Listen("tcp", fmt.Sprintf(":%d", servicePort))
if err != nil {
panic(err)
}
go listen(listener, servicePort, s, log)
<-stop
listener.Close()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
if err := s.Stop(ctx); err != nil {
panic(err)
}
}
func listen(listener net.Listener, servicePort int, s *silo.Silo, log logr.Logger) {
client := s.Client()
for {
conn, err := listener.Accept()
if err != nil {
log.V(0).Error(err, "failed to accept connection")
return
}
ctx := context.Background()
go func() {
defer conn.Close()
scanner := bufio.NewScanner(conn)
observerGrain, err := s.CreateGrain()
if err != nil {
log.V(0).Error(err, "failed to create generic grain")
}
defer s.DestroyGrain(observerGrain)
outChan := make(chan string, 10)
defer close(outChan)
go func() {
for line := range outChan {
if line != "" {
conn.Write([]byte(line))
}
}
}()
msgObserver, err := gengo.NewGenericMessageObserverBuilder().
OnReceive(func(ctx context.Context, sender grain.Identity, cm *gen.ChatMessage) {
outChan <- fmt.Sprintf("[%s@%s]: %s\n", cm.From,
sender.String(), cm.Msg)
}).Build(observerGrain)
if err != nil {
panic(err)
}
outChan <- "Enter username:\n"
var username string
if !scanner.Scan() {
outChan <- "[error] must enter username"
return
} else {
username = scanner.Text()
}
if scanner.Err() != nil {
log.Error(err, "an error occurred")
return
}
outChan <- "Enter Commands:\n"
for scanner.Scan() {
data := scanner.Text()
parts := strings.Split(data, " ")
if len(parts) == 0 {
continue
}
action := strings.TrimSpace(parts[0])
args := parts[1:]
line := "[ok] success\n"
switch action {
case "/join":
if len(args) != 1 {
line = "[error] incorrect args"
break
}
g := gengo.ChatRoomGrainRef(client, args[0])
_, err := g.Join(ctx, username, true)
if err != nil {
line = err.Error() + "\n"
break
}
token, err := g.RegisterMessageObserver(ctx, msgObserver, &gen.ListenRequest{})
if err != nil {
line = err.Error() + "\n"
break
}
line = token.String() + "\n"
case "/ping":
if len(args) != 1 {
line = "[error] incorrect args\n"
break
}
g := gengo.ChatRoomGrainRef(client, args[0])
g.Ping(ctx)
case "/msg":
if len(args) < 2 {
line = "[error] incorrect args\n"
break
}
g := gengo.ChatRoomGrainRef(client, args[0])
resp, ok, err := g.SendMessage(ctx, &gen.ChatMessage{
Msg: strings.Join(args[1:], " "),
From: username,
})
if err != nil {
line = fmt.Sprintf("[error] %s\n", err.Error())
} else {
line = fmt.Sprintf("[ok] count=%d ok=%v\n", resp.Count, ok)
}
case "/users":
if len(args) != 1 {
line = "[error] incorrect args\n"
break
}
g := gengo.ChatRoomGrainRef(client, args[0])
users, err := g.ListUsers(ctx)
if err != nil {
line = fmt.Sprintf("[error] %s\n", err.Error())
} else {
line = fmt.Sprintf("[ok] =>\n%s\n", strings.Join(users, "\n"))
}
default:
line = "[error] unknown command\n"
}
outChan <- line
}
}()
}
}
// func listen(listener net.Listener, servicePort int, s *silo.Silo, log logr.Logger) {
// client := s.Client()
// userNum := 0
// for {
// conn, err := listener.Accept()
// if err != nil {
// log.V(0).Error(err, "failed to accept connection")
// return
// }
// userName := fmt.Sprintf("user%04d%04d", servicePort, userNum)
// userNum++
// go func() {
// defer conn.Close()
// scanner := bufio.NewScanner(conn)
// subscriber, err := s.CreateGrain()
// if err != nil {
// log.V(0).Error(err, "failed to create subscriber grain")
// return
// }
// defer s.DestroyGrain(subscriber)
// stream, err := gen.CreateChatRoomGrainListenStream(subscriber)
// if err != nil {
// log.V(0).Error(err, "failed to create stream")
// return
// }
// ctx, cancel := context.WithCancel(context.Background())
// defer cancel()
// input := make(chan connInMsg, 10)
// go func() {
// streamC := stream.C()
// rooms := map[string]gen.ChatRoomGrainRef{}
// defer func() {
// for _, room := range rooms {
// _, err := room.Leave(context.Background(), &gen.LeaveRequest{
// UserName: userName,
// })
// if err != nil {
// log.V(0).Error(err, "leave failed")
// }
// }
// }()
// for {
// line := ""
// SEL:
// select {
// case <-ctx.Done():
// return
// case chatRoomMessage := <-streamC:
// line = fmt.Sprintf("[%s@%s]: %s\n", chatRoomMessage.Value.From,
// chatRoomMessage.Sender.ID, chatRoomMessage.Value.Msg)
// case inMsg := <-input:
// if inMsg.action == "/join" {
// if len(inMsg.args) != 1 {
// continue
// }
// ref := gen.GetChatRoomGrain(client, grain.Identity{
// GrainType: "ChatRoomGrain",
// ID: inMsg.args[0],
// })
// _, err = ref.Join(ctx, &gen.JoinRequest{
// UserName: userName,
// Listen: true,
// })
// if err != nil {
// log.V(0).Error(err, "join failed")
// line = "error\n"
// break SEL
// }
// err = stream.Observe(
// ctx,
// grain.Identity{
// GrainType: "ChatRoomGrain",
// ID: inMsg.args[0],
// },
// &gen.ListenRequest{},
// )
// if err != nil {
// log.V(0).Error(err, "stream observe failed")
// line = "error\n"
// break SEL
// }
// rooms[inMsg.args[0]] = ref
// } else if inMsg.action == "/msg" {
// if len(inMsg.args) < 2 {
// continue
// }
// if room, ok := rooms[inMsg.args[0]]; ok {
// _, err := room.Publish(ctx, &gen.ChatMessage{
// From: userName,
// Msg: strings.Join(inMsg.args[1:], " "),
// })
// if err != nil {
// log.V(0).Error(err, "stream observe failed")
// line = "error\n"
// } else {
// line = "messaged\n"
// }
// } else {
// line = "not a member of the room\n"
// }
// } else if inMsg.action == "/leave" {
// line = "left\n"
// }
// }
// if line != "" {
// conn.Write([]byte(line))
// }
// }
// }()
// for scanner.Scan() {
// data := scanner.Text()
// parts := strings.Split(data, " ")
// if len(parts) == 0 {
// continue
// }
// action := strings.TrimSpace(parts[0])
// args := parts[1:]
// input <- connInMsg{
// action: action,
// args: args,
// }
// }
// }()
// }
// }
type connInMsg struct {
action string
args []string
}
type connOutMsg struct {
line string
}