forked from anthdm/hollywood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (64 loc) · 1.65 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
package main
import (
"fmt"
"log"
"time"
"github.com/anthdm/hollywood/actor"
)
type (
nameRequest struct{}
nameResponse struct {
name string
}
)
type nameResponder struct {
name string
}
func newNameResponder() actor.Receiver {
return &nameResponder{name: "noname"}
}
func newCustomNameResponder(name string) actor.Producer {
return func() actor.Receiver {
return &nameResponder{name}
}
}
func (r *nameResponder) Receive(ctx *actor.Context) {
switch ctx.Message().(type) {
case *nameRequest:
ctx.Respond(&nameResponse{r.name})
}
}
// main is the entry point of the application.
// it creates an actor engine and spawns two new actors.
// the first actor is spawned with a default name and the second
// actor is spawned with a custom name, showing you how to pass custom
// arguments to your actors.
func main() {
e, err := actor.NewEngine(actor.NewEngineConfig())
if err != nil {
log.Fatal(err)
}
pid := e.Spawn(newNameResponder, "responder")
// Request a name and block till we got a response or the request
// timed out.
resp := e.Request(pid, &nameRequest{}, time.Millisecond)
// calling Result() will block till we received a response or
// the request timed out.
res, err := resp.Result()
if err != nil {
log.Fatal(err)
}
if name, ok := res.(*nameResponse); ok {
fmt.Println("received name:", name.name)
}
// Spawn a new responder with a custom name.
pid = e.Spawn(newCustomNameResponder("anthdm"), "custom_responder")
resp = e.Request(pid, &nameRequest{}, time.Millisecond)
res, err = resp.Result()
if err != nil {
log.Fatal(err)
}
if name, ok := res.(*nameResponse); ok {
fmt.Println("received name:", name.name)
}
}