forked from ergo-services/ergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (58 loc) · 1.67 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"
"time"
"github.com/ergo-services/ergo"
"github.com/ergo-services/ergo/etf"
"github.com/ergo-services/ergo/gen"
"github.com/ergo-services/ergo/node"
)
type MyDemo struct {
Demo
}
func (md *MyDemo) InitDemo(process *DemoProcess, args ...etf.Term) error {
fmt.Printf("Started instance of MyDemo with PID %s and args %v\n", process.Self(), args)
return nil
}
func (md *MyDemo) HandleHello(process *DemoProcess) DemoStatus {
fmt.Println("got Hello")
return DemoStatusOK
}
func (md *MyDemo) HandleDemoDirect(process *DemoProcess, message interface{}) (interface{}, error) {
fmt.Println("Say hi to increase counter twice")
process.Hi()
return nil, nil
}
func main() {
// Initialize new node with given name, cookie, listening port range and epmd port
node, e := ergo.StartNode("mydemo@127.0.0.1", "cookie", node.Options{})
if e != nil {
fmt.Println("error", e)
return
}
demo := &MyDemo{}
// Spawn a new process with arguments
process, e := node.Spawn("demo", gen.ProcessOptions{}, demo, 1, 2, 3)
if e != nil {
fmt.Println("error", e)
return
}
fmt.Println("call Hello method 3 times")
demo.Hello(process)
demo.Hello(process)
demo.Hello(process)
fmt.Println("How many times Hello was called: ", demo.Stat(process))
fmt.Println("make direct request")
process.Direct(nil)
fmt.Println("How big is the counter now: ", demo.Stat(process))
fmt.Println("send a simple message (no handler) and call Exit")
process.Send(process.Self(), "simple message")
// add some delay to see "unhandled message" warning
time.Sleep(100 * time.Millisecond)
fmt.Println("Exiting")
process.Exit("normal")
process.Wait()
node.Stop()
node.Wait()
return
}