-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
72 lines (64 loc) · 1.61 KB
/
main.go
File metadata and controls
72 lines (64 loc) · 1.61 KB
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
package main
import (
"bufio"
"fmt"
"os"
"time"
"github.com/AsynkronIT/protoactor-go/actor"
)
type PingPongMsg string
type StartPing struct {
Pid *actor.PID
}
type PingPongActor struct {
Name string
Msg PingPongMsg
delayInS time.Duration
}
func main() {
// Create actor system
system := actor.NewActorSystem()
// Create "Ping" actor
propsPing := actor.PropsFromProducer(func() actor.Actor {
return &PingPongActor{
Name: "Ping Actor",
Msg: "PING!",
delayInS: 1,
}
})
pidPing := system.Root.Spawn(propsPing)
// create "Pong" actor
propsPong := actor.PropsFromProducer(func() actor.Actor {
return &PingPongActor{
Name: "Pong Actor",
Msg: "PONG!",
delayInS: 1,
}
})
pidPong := system.Root.Spawn(propsPong)
// Kick off the ping-pong match!
system.Root.Send(pidPing, &StartPing{Pid: pidPong})
// keep program running till enter is hit
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
fmt.Println("> Exiting program...")
}
// Handler whenever a message is received/ fetched from mailbox
func (state *PingPongActor) Receive(ctx actor.Context) {
switch msg := ctx.Message().(type) {
case *actor.Started:
fmt.Println("Started PING actor.")
case *StartPing:
fmt.Printf("%v: Starting PING-PONG messaging...\n", state.Name)
ctx.Request(msg.Pid, state.Msg)
case PingPongMsg:
fmt.Printf("%v: %v\n", state.Name, msg)
pid := ctx.Sender()
time.AfterFunc(state.delayInS*time.Second, func() {
// IMPORTANT! Use Request() otherwise sender will not be send properly
ctx.Request(pid, state.Msg)
})
default:
fmt.Println("Received unknown message.")
}
}